Browse Source

Implemented SQL Logging, Deleted old Logging Mechanism

Lars 10 years ago
parent
commit
1e50305219

BIN
bin/classes.dex


BIN
bin/classes/de/tudarmstadt/informatik/hostage/HoneyService$LocalBinder.class


BIN
bin/classes/de/tudarmstadt/informatik/hostage/HoneyService.class


BIN
bin/classes/de/tudarmstadt/informatik/hostage/logging/FileLogger.class


BIN
bin/classes/de/tudarmstadt/informatik/hostage/logging/Logger.class


BIN
bin/classes/de/tudarmstadt/informatik/hostage/logging/Record$TYPE.class


BIN
bin/classes/de/tudarmstadt/informatik/hostage/logging/Record.class


BIN
bin/hostage.apk


+ 4 - 6
src/de/tudarmstadt/informatik/hostage/HoneyService.java

@@ -14,8 +14,7 @@ import android.support.v4.app.NotificationCompat;
 import android.support.v4.app.TaskStackBuilder;
 import android.support.v4.content.LocalBroadcastManager;
 import android.widget.Toast;
-import de.tudarmstadt.informatik.hostage.logging.FileLogger;
-import de.tudarmstadt.informatik.hostage.logging.Logger;
+import de.tudarmstadt.informatik.hostage.logging.SQLLogger;
 import de.tudarmstadt.informatik.hostage.protocol.Protocol;
 import de.tudarmstadt.informatik.hostage.ui.MainActivity;
 
@@ -27,9 +26,9 @@ public class HoneyService extends Service {
 		return listeners;
 	}
 
-	private Logger log;
+	private SQLLogger log;
 
-	public Logger getLog() {
+	public SQLLogger getLog() {
 		return log;
 	}
 
@@ -49,7 +48,7 @@ public class HoneyService extends Service {
 	@Override
 	public void onCreate() {
 		super.onCreate();
-		log = new FileLogger(getApplicationContext());
+		log = new SQLLogger(getApplicationContext());
 		createNotification();
 		for (Protocol protocol : getProtocolArray()) {
 			listeners.add(new HoneyListener(this, protocol));
@@ -67,7 +66,6 @@ public class HoneyService extends Service {
 	@Override
 	public void onDestroy() {
 		cancelNotification();
-		log.close();
 		super.onDestroy();
 	}
 

+ 12 - 9
src/de/tudarmstadt/informatik/hostage/handler/AbstractHandler.java

@@ -7,9 +7,8 @@ import java.net.Socket;
 
 import de.tudarmstadt.informatik.hostage.HoneyListener;
 import de.tudarmstadt.informatik.hostage.HoneyService;
-import de.tudarmstadt.informatik.hostage.logging.Logger;
-import de.tudarmstadt.informatik.hostage.logging.Record;
-import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
+import de.tudarmstadt.informatik.hostage.logging.SQLLogger;
+import de.tudarmstadt.informatik.hostage.logging.SQLRecord;
 import de.tudarmstadt.informatik.hostage.protocol.Protocol;
 
 public abstract class AbstractHandler implements Runnable {
@@ -20,8 +19,10 @@ public abstract class AbstractHandler implements Runnable {
 	private Socket client;
 	protected Thread thread;
 
+	private int attack_id;
+
 	private HoneyListener listener;
-	protected Logger log;
+	protected SQLLogger log;
 
 	public AbstractHandler(HoneyService service, HoneyListener listener,
 			Protocol protocol, Socket client) {
@@ -30,6 +31,7 @@ public abstract class AbstractHandler implements Runnable {
 		this.protocol = protocol;
 		this.client = client;
 		this.thread = new Thread(this);
+		attack_id = 0; 	//TODO Attack ID zuordnen
 		setSoTimeout(client);
 		thread.start();
 	}
@@ -73,13 +75,14 @@ public abstract class AbstractHandler implements Runnable {
 	abstract protected void talkToClient(InputStream in, OutputStream out)
 			throws IOException;
 
-	protected Record createRecord(TYPE type, String packet) {
-		Record record = new Record();
+	protected SQLRecord createRecord(String type, String packet) {
+		SQLRecord record = new SQLRecord();
+		record.setAttack_id(attack_id);
 		record.setType(type);
-		record.setTimestamp(System.currentTimeMillis());
-		record.setLocalIP(client.getLocalAddress());
+		record.setTimestamp(String.valueOf(System.currentTimeMillis()));
+		record.setLocalIP(client.getLocalAddress().getHostAddress());
 		record.setLocalPort(protocol.getPort());
-		record.setRemoteIP(client.getInetAddress());
+		record.setRemoteIP(client.getInetAddress().getHostAddress());
 		record.setRemotePort(client.getPort());
 		record.setPacket(packet);
 		return record;

+ 3 - 4
src/de/tudarmstadt/informatik/hostage/handler/ByteArrayHandlerImpl.java

@@ -10,7 +10,6 @@ import de.tudarmstadt.informatik.hostage.HoneyListener;
 import de.tudarmstadt.informatik.hostage.HoneyService;
 import de.tudarmstadt.informatik.hostage.io.ByteArrayReaderWriter;
 import de.tudarmstadt.informatik.hostage.io.ReaderWriter;
-import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
 import de.tudarmstadt.informatik.hostage.protocol.Protocol;
 import de.tudarmstadt.informatik.hostage.protocol.Protocol.TALK_FIRST;
 import de.tudarmstadt.informatik.hostage.wrapper.ByteArray;
@@ -34,17 +33,17 @@ public class ByteArrayHandlerImpl extends AbstractHandler {
 			outputLine = protocol.processMessage(null);
 			stream.write(outputLine);
 			for (ByteArray s : outputLine) {
-				log.write(createRecord(TYPE.SEND, s.toString()));
+				log.addRecord(createRecord("SEND", s.toString()));
 			}
 		}
 
 		while (!thread.isInterrupted() && (inputLine = stream.read()) != null) {
-			log.write(createRecord(TYPE.RECEIVE, inputLine.toString()));
+			log.addRecord(createRecord("RECEIVE", inputLine.toString()));
 			outputLine = protocol.processMessage(inputLine);
 			if (outputLine != null) {
 				stream.write(outputLine);
 				for (ByteArray s : outputLine) {
-					log.write(createRecord(TYPE.SEND, s.toString()));
+					log.addRecord(createRecord("SEND", s.toString()));
 				}
 			}
 			if (protocol.isClosed()) {

+ 3 - 4
src/de/tudarmstadt/informatik/hostage/handler/StringHandlerImpl.java

@@ -10,7 +10,6 @@ import de.tudarmstadt.informatik.hostage.HoneyListener;
 import de.tudarmstadt.informatik.hostage.HoneyService;
 import de.tudarmstadt.informatik.hostage.io.ReaderWriter;
 import de.tudarmstadt.informatik.hostage.io.StringReaderWriter;
-import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
 import de.tudarmstadt.informatik.hostage.protocol.Protocol;
 import de.tudarmstadt.informatik.hostage.protocol.Protocol.TALK_FIRST;
 
@@ -33,17 +32,17 @@ public class StringHandlerImpl extends AbstractHandler {
 			outputLine = protocol.processMessage(null);
 			stream.write(outputLine);
 			for (String s : outputLine) {
-				log.write(createRecord(TYPE.SEND, s));
+				log.addRecord(createRecord("SEND", s));
 			}
 		}
 
 		while (!thread.isInterrupted() && (inputLine = stream.read()) != null) {
-			log.write(createRecord(TYPE.RECEIVE, inputLine));
+			log.addRecord(createRecord("RECEIVE", inputLine));
 			outputLine = protocol.processMessage(inputLine);
 			if (outputLine != null) {
 				stream.write(outputLine);
 				for (String s : outputLine) {
-					log.write(createRecord(TYPE.SEND, s));
+					log.addRecord(createRecord("SEND", s));
 				}
 			}
 			if (protocol.isClosed()) {

+ 143 - 0
src/de/tudarmstadt/informatik/hostage/logging/DatabaseHandler.java

@@ -0,0 +1,143 @@
+package de.tudarmstadt.informatik.hostage.logging;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+
+public class DatabaseHandler extends SQLiteOpenHelper {
+
+	// All Static variables
+	// Database Version
+	private static final int DATABASE_VERSION = 1;
+
+	// Database Name
+	private static final String DATABASE_NAME = "recordManager";
+
+	// Contacts table name
+	private static final String TABLE_RECORDS = "records";
+
+	// Contacts Table Columns names
+	private static final String KEY_ID = "id";
+	private static final String KEY_ATTACK_ID = "attack_id";
+	private static final String KEY_TYPE = "type";
+	private static final String KEY_TIME = "timestamp";
+	private static final String KEY_LOCAL_IP = "localIP";
+	private static final String KEY_LOCAL_PORT = "localPort";
+	private static final String KEY_REMOTE_IP = "remoteIP";
+	private static final String KEY_REMOTE_PORT = "remotePort";
+	private static final String KEY_PACKET = "packet";
+
+	public DatabaseHandler(Context context) {
+		super(context, DATABASE_NAME, null, DATABASE_VERSION);
+	}
+
+	// Creating Tables
+	@Override
+	public void onCreate(SQLiteDatabase db) {
+		String CREATE_RECORD_TABLE = "CREATE TABLE " + TABLE_RECORDS + "(" + KEY_ID
+				+ " INTEGER PRIMARY KEY," + KEY_ATTACK_ID + " INTEGER,"
+				+ KEY_TYPE + " TEXT," + KEY_TIME + " TEXT," + KEY_LOCAL_IP
+				+ " Text," + KEY_LOCAL_PORT + " INTEGER," + KEY_REMOTE_IP
+				+ " TEXT," + KEY_REMOTE_PORT + " INTEGER," + KEY_PACKET
+				+ " TEXT" + ")";
+		db.execSQL(CREATE_RECORD_TABLE);
+	}
+
+	// Upgrading database
+	@Override
+	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+		// Drop older table if existed
+		db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS);
+
+		// Create tables again
+		onCreate(db);
+	}
+
+	// Adding new record
+	public void addRecord(SQLRecord record) {
+		SQLiteDatabase db = this.getWritableDatabase();
+
+		ContentValues values = new ContentValues();
+		values.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
+		values.put(KEY_TYPE, record.getType()); // Log Type
+		values.put(KEY_TIME, record.getTimestamp()); // Log Timestamp
+		values.put(KEY_LOCAL_IP, record.getLocalIP()); // Log Local IP
+		values.put(KEY_LOCAL_PORT, record.getLocalPort()); // Log Local Port
+		values.put(KEY_REMOTE_IP, record.getRemoteIP()); // Log Remote IP
+		values.put(KEY_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
+		values.put(KEY_PACKET, record.getPacket()); // Log Packet
+
+		// Inserting Row
+		db.insert(TABLE_RECORDS, null, values);
+		db.close(); // Closing database connection
+	}
+
+	// Getting single record
+	public SQLRecord getRecord(int id) {
+		SQLiteDatabase db = this.getReadableDatabase();
+
+		Cursor cursor = db.query(TABLE_RECORDS, new String[] { KEY_ID,
+				KEY_ATTACK_ID, KEY_TYPE, KEY_TIME, KEY_LOCAL_IP,
+				KEY_LOCAL_PORT, KEY_REMOTE_IP, KEY_REMOTE_PORT, KEY_PACKET },
+				KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null,
+				null, null);
+		if (cursor != null)
+			cursor.moveToFirst();
+
+		SQLRecord record = new SQLRecord(Integer.parseInt(cursor.getString(0)),
+				Integer.parseInt(cursor.getString(1)), cursor.getString(2),
+				cursor.getString(3), cursor.getString(4),
+				Integer.parseInt(cursor.getString(5)), cursor.getString(6),
+				Integer.parseInt(cursor.getString(7)), cursor.getString(8));
+		// return contact
+		return record;
+	}
+
+	// Getting All Records
+	public ArrayList<SQLRecord> getAllRecords() {
+		ArrayList<SQLRecord> contactList = new ArrayList<SQLRecord>();
+		// Select All Query
+		String selectQuery = "SELECT  * FROM " + TABLE_RECORDS;
+
+		SQLiteDatabase db = this.getWritableDatabase();
+		Cursor cursor = db.rawQuery(selectQuery, null);
+
+		// looping through all rows and adding to list
+		if (cursor.moveToFirst()) {
+			do {
+				SQLRecord record = new SQLRecord();
+				record.setID(Integer.parseInt(cursor.getString(0)));
+				record.setAttack_id(Integer.parseInt(cursor.getString(1)));
+				record.setType(cursor.getString(2));
+				record.setTimestamp(cursor.getString(3));
+				record.setLocalIP(cursor.getString(4));
+				record.setLocalPort(Integer.parseInt(cursor.getString(5)));
+				record.setRemoteIP(cursor.getString(6));
+				record.setRemotePort(Integer.parseInt(cursor.getString(7)));
+				record.setPacket(cursor.getString(8));
+				
+				// Adding record to list
+				contactList.add(record);
+			} while (cursor.moveToNext());
+		}
+
+		// return record list
+		return contactList;
+	}
+	
+	// Getting record Count
+    public int getRecordCount() {
+        String countQuery = "SELECT  * FROM " + TABLE_RECORDS;
+        SQLiteDatabase db = this.getReadableDatabase();
+        Cursor cursor = db.rawQuery(countQuery, null);
+        cursor.close();
+ 
+        // return count
+        return cursor.getCount();
+    }
+}

+ 0 - 42
src/de/tudarmstadt/informatik/hostage/logging/FileLogger.java

@@ -1,42 +0,0 @@
-package de.tudarmstadt.informatik.hostage.logging;
-
-import java.io.FileOutputStream;
-
-import android.content.Context;
-
-public class FileLogger implements Logger {
-
-	private FileOutputStream log = null;
-
-	public FileLogger(Context context) {
-		try {
-			log = context.openFileOutput("hostage.log", Context.MODE_APPEND);
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-	}
-
-	@Override
-	public synchronized void write(Record record) {
-		if (log != null) {
-			try {
-				log.write((record.toString() + "\n").getBytes());
-			} catch (Exception e) {
-				e.printStackTrace();
-			}
-		}
-	}
-
-	@Override
-	public void close() {
-		if (log != null) {
-			try {
-				log.flush();
-				log.close();
-			} catch (Exception e) {
-				e.printStackTrace();
-			}
-		}
-	}
-
-}

+ 0 - 9
src/de/tudarmstadt/informatik/hostage/logging/Logger.java

@@ -1,9 +0,0 @@
-package de.tudarmstadt.informatik.hostage.logging;
-
-public interface Logger {
-
-	void write(Record record);
-
-	void close();
-
-}

+ 0 - 86
src/de/tudarmstadt/informatik/hostage/logging/Record.java

@@ -1,86 +0,0 @@
-package de.tudarmstadt.informatik.hostage.logging;
-
-import java.io.Serializable;
-import java.net.InetAddress;
-
-public class Record implements Serializable {
-
-	private static final long serialVersionUID = 1L;
-
-	public static enum TYPE {
-		SEND, RECEIVE
-	};
-
-	private TYPE type;
-	private long timestamp;
-	private InetAddress localIP;
-	private int localPort;
-	private InetAddress remoteIP;
-	private int remotePort;
-	private String packet;
-
-	public TYPE getType() {
-		return type;
-	}
-
-	public void setType(TYPE type) {
-		this.type = type;
-	}
-
-	public long getTimestamp() {
-		return timestamp;
-	}
-
-	public void setTimestamp(long timestamp) {
-		this.timestamp = timestamp;
-	}
-
-	public InetAddress getLocalIP() {
-		return localIP;
-	}
-
-	public void setLocalIP(InetAddress localIP) {
-		this.localIP = localIP;
-	}
-
-	public int getLocalPort() {
-		return localPort;
-	}
-
-	public void setLocalPort(int localPort) {
-		this.localPort = localPort;
-	}
-
-	public InetAddress getRemoteIP() {
-		return remoteIP;
-	}
-
-	public void setRemoteIP(InetAddress remoteIP) {
-		this.remoteIP = remoteIP;
-	}
-
-	public int getRemotePort() {
-		return remotePort;
-	}
-
-	public void setRemotePort(int remotePort) {
-		this.remotePort = remotePort;
-	}
-
-	public String getPacket() {
-		return packet;
-	}
-
-	public void setPacket(String packet) {
-		this.packet = packet;
-	}
-
-	@Override
-	public String toString() {
-		return String.format("%s [%d,%s:%d,%s:%d,%s]",
-				((type == TYPE.SEND) ? "SEND" : "RECEIVE"), timestamp,
-				localIP.getHostAddress(), localPort, remoteIP.getHostAddress(),
-				remotePort, packet);
-	}
-	
-}

+ 49 - 0
src/de/tudarmstadt/informatik/hostage/logging/SQLLogger.java

@@ -0,0 +1,49 @@
+package de.tudarmstadt.informatik.hostage.logging;
+
+import java.io.FileOutputStream;
+import java.util.ArrayList;
+
+import android.content.Context;
+
+public class SQLLogger{
+	Context context;
+	DatabaseHandler db;
+	private FileOutputStream log = null;
+	
+	public SQLLogger(Context context){
+		this.context = context;
+		db = new DatabaseHandler(context);		
+	}
+
+	public void addRecord(SQLRecord record) {
+		db.addRecord(record);
+	}
+	
+	public void exportDatabase(){
+		try {
+			log = context.openFileOutput("hostage.log", Context.MODE_APPEND);
+		} catch (Exception e) {
+			e.printStackTrace();
+		}		
+		
+		ArrayList<SQLRecord> records = db.getAllRecords();
+		for(SQLRecord record : records){
+			writeLine(record);
+		}
+	}
+	
+	private void writeLine(SQLRecord record){
+		if (log != null) {
+			try {
+				log.write((record.toString() + "\n").getBytes());
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}		
+	}
+	
+	public void exportDatabaseJson(){
+		//TODO JSON implementieren, beachten dass nur jeder Angriff nicht jeder Record geloggt wird
+	}
+
+}

+ 196 - 0
src/de/tudarmstadt/informatik/hostage/logging/SQLRecord.java

@@ -0,0 +1,196 @@
+package de.tudarmstadt.informatik.hostage.logging;
+
+import java.net.InetAddress;
+
+public class SQLRecord {
+	// private variables
+	private int id;
+	private int attack_id;
+	private String type;
+	private String timestamp;
+	private String localIP;
+	private int localPort;
+	private String remoteIP;
+	private int remotePort;
+	private String packet;
+
+	// Empty constructor
+	public SQLRecord() {
+
+	}
+
+	// constructor
+	public SQLRecord(int id, int attack_id, String type, String timestamp,
+			String localIP, int localPort, String remoteIP, int remotePort,
+			String packet) {
+		this.id = id;
+		this.attack_id = attack_id;
+		this.type = type;
+		this.timestamp = timestamp;
+		this.localIP = localIP;
+		this.localPort = localPort;
+		this.remoteIP = remoteIP;
+		this.remotePort = remotePort;
+		this.packet = packet;
+	}
+
+	// constructor
+	public SQLRecord(int attack_id, String type, String timestamp, String localIP,
+			int localPort, String remoteIP, int remotePort, String packet) {
+		this.attack_id = attack_id;
+		this.type = type;
+		this.timestamp = timestamp;
+		this.localIP = localIP;
+		this.localPort = localPort;
+		this.remoteIP = remoteIP;
+		this.remotePort = remotePort;
+		this.packet = packet;
+	}
+
+	/**
+	 * @return the id
+	 */
+	public int getID() {
+		return id;
+	}
+
+	/**
+	 * @param id
+	 *            the id to set
+	 */
+	public void setID(int id) {
+		this.id = id;
+	}
+
+	/**
+	 * @return the attack_id
+	 */
+	public int getAttack_id() {
+		return attack_id;
+	}
+
+	/**
+	 * @param attack_id
+	 *            the attack_id to set
+	 */
+	public void setAttack_id(int attack_id) {
+		this.attack_id = attack_id;
+	}
+
+	/**
+	 * @return the type
+	 */
+	public String getType() {
+		return type;
+	}
+
+	/**
+	 * @param type
+	 *            the type to set
+	 */
+	public void setType(String type) {
+		this.type = type;
+	}
+
+	/**
+	 * @return the timestamp
+	 */
+	public String getTimestamp() {
+		return timestamp;
+	}
+
+	/**
+	 * @param timestamp
+	 *            the timestamp to set
+	 */
+	public void setTimestamp(String timestamp) {
+		this.timestamp = timestamp;
+	}
+
+	/**
+	 * @return the localIP
+	 */
+	public String getLocalIP() {
+		return localIP;
+	}
+
+	/**
+	 * @param localIP
+	 *            the localIP to set
+	 */
+	public void setLocalIP(String localIP) {
+		this.localIP = localIP;
+	}
+
+	/**
+	 * @return the localPort
+	 */
+	public int getLocalPort() {
+		return localPort;
+	}
+
+	/**
+	 * @param localPort
+	 *            the localPort to set
+	 */
+	public void setLocalPort(int localPort) {
+		this.localPort = localPort;
+	}
+
+	/**
+	 * @return the remoteIP
+	 */
+	public String getRemoteIP() {
+		return remoteIP;
+	}
+
+	/**
+	 * @param remoteIP
+	 *            the remoteIP to set
+	 */
+	public void setRemoteIP(String remoteIP) {
+		this.remoteIP = remoteIP;
+	}
+
+	/**
+	 * @return the remotePort
+	 */
+	public int getRemotePort() {
+		return remotePort;
+	}
+
+	/**
+	 * @param remotePort
+	 *            the remotePort to set
+	 */
+	public void setRemotePort(int remotePort) {
+		this.remotePort = remotePort;
+	}
+
+	/**
+	 * @return the packet
+	 */
+	public String getPacket() {
+		return packet;
+	}
+
+	/**
+	 * @param packet
+	 *            the packet to set
+	 */
+	public void setPacket(String packet) {
+		this.packet = packet;
+	}
+
+	public String toString() {
+		return String.format("%d %s [%d,%s:%d,%s:%d,%s]", id, type, timestamp,
+				localIP, localPort, remoteIP, remotePort, packet);
+	}
+
+	public String toJson() {
+		return String
+				.format("{ \"src\":{\"IP\": %s, \"Port\": %d} \"dst\": {\"IP\": %s, \"Port\": %d} \"type\": 0 \"name\": \"HOsTaGe\" }",
+						localIP, localPort, remoteIP, remotePort);
+	}
+
+}