Browse Source

Added externalIP to Record.
Updated DatabaseHandler
Tracing upload uses new externalIP field.
Updating of connection information in MainActivity now in a own Thread

lp-tu 10 years ago
parent
commit
67b4aa15f3

+ 1 - 1
res/values/arrays.xml

@@ -3,7 +3,7 @@
 
     <string-array name="format">
         <item>DEFAULT</item>
-        <item>JSON</item>
+        <item>TraCINg JSON</item>
     </string-array>
     
     <string-array name="delete_criteria">

+ 1 - 1
src/de/tudarmstadt/informatik/hostage/ConnectionRegister.java

@@ -45,7 +45,7 @@ public class ConnectionRegister {
 	 * @return true if a new connection is allowed, else false.
 	 */
 	public boolean isConnectionFree() {
-		return openConnections < getMaxConnections();
+		return getMaxConnections() == 0 || openConnections < getMaxConnections();
 	}
 	
 	/**

+ 1 - 1
src/de/tudarmstadt/informatik/hostage/commons/HelperUtils.java

@@ -121,7 +121,7 @@ public final class HelperUtils {
 	/**
 	 * Determines and returns the external IP address of the device.
 	 * @param context  Needs a context to get system recourses.
-	 * @return A String representation of the external IP of the device.
+	 * @return A String representation of the external IP of the device or null if no Internet connectivity exists.
 	 * @see GetExternalIPTask
 	 */
 	public static String getExternalIP(Context context) {

+ 3 - 0
src/de/tudarmstadt/informatik/hostage/handler/AbstractHandler.java

@@ -32,6 +32,7 @@ public abstract class AbstractHandler implements Runnable {
 	protected Thread thread;
 
 	private int attack_id;
+	private String externalIP;
 	private String BSSID;
 	private String SSID;
 
@@ -63,6 +64,7 @@ public abstract class AbstractHandler implements Runnable {
 		editor.commit();
 		BSSID = HelperUtils.getBSSID(service.getApplicationContext());
 		SSID = HelperUtils.getSSID(service.getApplicationContext());
+		externalIP = HelperUtils.getExternalIP(service.getApplicationContext());
 		setSoTimeout(client);
 		thread.start();
 	}
@@ -141,6 +143,7 @@ public abstract class AbstractHandler implements Runnable {
 		record.setProtocol(protocol.toString());
 		record.setType(type);
 		record.setTimestamp(System.currentTimeMillis());
+		record.setExternalIP(externalIP);
 		record.setLocalIP(client.getLocalAddress());
 		record.setLocalPort(protocol.getPort());
 		record.setRemoteIP(client.getInetAddress());

+ 11 - 8
src/de/tudarmstadt/informatik/hostage/logging/DatabaseHandler.java

@@ -37,6 +37,7 @@ public class DatabaseHandler extends SQLiteOpenHelper {
 	private static final String KEY_PROTOCOL = "protocol";
 	private static final String KEY_TYPE = "type";
 	private static final String KEY_TIME = "timestamp";
+	private static final String KEY_EXTERNAL_IP ="externalIP";
 	private static final String KEY_LOCAL_IP = "localIP";
 	private static final String KEY_LOCAL_HOSTNAME = "localHostName";
 	private static final String KEY_LOCAL_PORT = "localPort";
@@ -50,7 +51,7 @@ public class DatabaseHandler extends SQLiteOpenHelper {
 	// Database sql create statements
 	private static final String CREATE_RECORD_TABLE = "CREATE TABLE " + TABLE_RECORDS + "(" + KEY_ID
 			+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ATTACK_ID + " INTEGER," + KEY_PROTOCOL + " TEXT,"
-			+ KEY_TYPE + " TEXT," + KEY_TIME + " INTEGER," + KEY_LOCAL_IP
+			+ KEY_TYPE + " TEXT," + KEY_TIME + " INTEGER," + KEY_EXTERNAL_IP + " TEXT," + KEY_LOCAL_IP
 			+ " BLOB," + KEY_LOCAL_HOSTNAME + " TEXT," + KEY_LOCAL_PORT + " INTEGER," + KEY_REMOTE_IP
 			+ " BLOB," + KEY_REMOTE_HOSTNAME + " TEXT," + KEY_REMOTE_PORT + " INTEGER," 
 			+ KEY_BSSID + " TEXT," +  KEY_PACKET + " TEXT," 
@@ -97,6 +98,7 @@ public class DatabaseHandler extends SQLiteOpenHelper {
 		recordValues.put(KEY_PROTOCOL, record.getProtocol().toString());
 		recordValues.put(KEY_TYPE, record.getType().name()); // Log Type
 		recordValues.put(KEY_TIME, record.getTimestamp()); // Log Timestamp
+		recordValues.put(KEY_EXTERNAL_IP, record.getExternalIP());
 		recordValues.put(KEY_LOCAL_IP, record.getLocalIP().getAddress()); // Log Local IP
 		recordValues.put(KEY_LOCAL_HOSTNAME, record.getLocalIP().getHostName());
 		recordValues.put(KEY_LOCAL_PORT, record.getLocalPort()); // Log Local Port
@@ -125,13 +127,14 @@ public class DatabaseHandler extends SQLiteOpenHelper {
 		record.setProtocol(cursor.getString(2));
 		record.setType(cursor.getString(3).equals("SEND") ? TYPE.SEND : TYPE.RECEIVE);
 		record.setTimestamp(cursor.getLong(4));
-		record.setLocalIP(InetAddress.getByAddress(cursor.getString(6), cursor.getBlob(5)));
-		record.setLocalPort(Integer.parseInt(cursor.getString(7)));
-		record.setRemoteIP(InetAddress.getByAddress(cursor.getString(9), cursor.getBlob(8)));
-		record.setRemotePort(Integer.parseInt(cursor.getString(10)));
-		record.setBSSID(cursor.getString(11));
-		record.setPacket(cursor.getString(12));
-		record.setSSID(cursor.getString(13));
+		record.setExternalIP(cursor.getString(5));
+		record.setLocalIP(InetAddress.getByAddress(cursor.getString(7), cursor.getBlob(6)));
+		record.setLocalPort(Integer.parseInt(cursor.getString(8)));
+		record.setRemoteIP(InetAddress.getByAddress(cursor.getString(10), cursor.getBlob(9)));
+		record.setRemotePort(Integer.parseInt(cursor.getString(11)));
+		record.setBSSID(cursor.getString(12));
+		record.setPacket(cursor.getString(13));
+		record.setSSID(cursor.getString(14));
 		} catch (UnknownHostException e) {
 			e.printStackTrace();
 		}

+ 18 - 2
src/de/tudarmstadt/informatik/hostage/logging/Record.java

@@ -24,6 +24,7 @@ public class Record implements Serializable {
 	private String protocol;
 	private TYPE type;
 	private long timestamp;
+	private String externalIP;
 	private InetAddress localIP;
 	private int localPort;
 	private InetAddress remoteIP;
@@ -104,6 +105,20 @@ public class Record implements Serializable {
 		this.timestamp = timestamp;
 	}
 
+	/**
+	 * @return the externalIP
+	 */
+	public String getExternalIP() {
+		return externalIP;
+	}
+
+	/**
+	 * @param externalIP the externalIP to set
+	 */
+	public void setExternalIP(String externalIP) {
+		this.externalIP = externalIP;
+	}
+
 	/**
 	 * @return the localIP
 	 */
@@ -219,9 +234,10 @@ public class Record implements Serializable {
 	public String toString(int format){
 		// Choose String Format
 		switch (format){
+			// TraCINg Upload format, replaces internal ip's with external ip of network
 			case 1: 
-				return String.format("{ \"sensor\":{\"type\": \"Honeypot\", \"name\": \"HOsTaGe\"}, \"type\": 0, \"src\":{\"ip\": \"%s\", \"port\": %d}, \"dst\":{\"ip\": \"%s\", \"port\": %d} }", localIP.getHostAddress(), localPort, remoteIP.getHostAddress(),
-					remotePort);
+				return String.format("{ \"sensor\":{\"type\": \"Honeypot\", \"name\": \"HOsTaGe\"}, \"type\": 0, \"src\":{\"ip\": \"%s\", \"port\": %d}, \"dst\":{\"ip\": \"%s\", \"port\": %d} }", externalIP, localPort, externalIP, remotePort);
+			// ViewLogTable format: contains all important information about an attack.
 			case 2: 
 				return String.format("%d: %s in %s(%s) from [%s:%d] to [%s:%d]", attack_id, protocol, SSID, BSSID, remoteIP.getHostAddress(), remotePort, localIP.getHostAddress(), localPort);
 			default:

+ 44 - 35
src/de/tudarmstadt/informatik/hostage/ui/MainActivity.java

@@ -568,41 +568,50 @@ public class MainActivity extends Activity {
 	 * Gets Information about connection state and updates the GUI.
 	 */
 	private void updateConnectionInfo() {
-		//Get text fields
-		TextView ssidView = (TextView) findViewById(R.id.textViewSSIDValue);
-		TextView bssidView = (TextView) findViewById(R.id.textViewBSSIDValue);
-		TextView internalIPView = (TextView) findViewById(R.id.textViewInternalIPValue);
-		TextView externalIPView = (TextView) findViewById(R.id.textViewExternalIPValue);
-		
-		//Update the connection information
-		HelperUtils.updateConnectionInfo(this);
-		
-		//Get connection information
-		String ssid = pref.getString(SSID, "-");
-		String bssid = pref.getString(BSSID, "-");
-		String internalIP = pref.getString(INTERNAL_IP, "-");
-		String externalIP = pref.getString(EXTERNAL_IP, "-");
-
-		//Set text fields
-		if (ssid != null)
-			ssidView.setText(ssid);
-		else
-			ssidView.setText("-");
-		
-		if (bssid != null)
-			bssidView.setText(bssid);
-		else
-			bssidView.setText("-");
-		
-		if (internalIP != null)
-			internalIPView.setText(internalIP);
-		else
-			internalIPView.setText("-");
-		
-		if (externalIP != null)
-			externalIPView.setText(externalIP);
-		else
-			externalIPView.setText("-");		
+		final Context context = this;
+		new Thread(new Runnable() {
+			  public void run() { 
+				//Get text fields
+					TextView ssidView = (TextView) findViewById(R.id.textViewSSIDValue);
+					TextView bssidView = (TextView) findViewById(R.id.textViewBSSIDValue);
+					TextView internalIPView = (TextView) findViewById(R.id.textViewInternalIPValue);
+					TextView externalIPView = (TextView) findViewById(R.id.textViewExternalIPValue);
+					ssidView.setText("Loading...");
+					bssidView.setText("Loading...");
+					internalIPView.setText("Loading...");
+					externalIPView.setText("Loading...");
+					
+					//Update the connection information
+					HelperUtils.updateConnectionInfo(context);
+					
+					//Get connection information
+					String ssid = pref.getString(SSID, "-");
+					String bssid = pref.getString(BSSID, "-");
+					String internalIP = pref.getString(INTERNAL_IP, "-");
+					String externalIP = pref.getString(EXTERNAL_IP, "-");
+
+					//Set text fields
+					if (ssid != null)
+						ssidView.setText(ssid);
+					else
+						ssidView.setText("-");
+					
+					if (bssid != null)
+						bssidView.setText(bssid);
+					else
+						bssidView.setText("-");
+					
+					if (internalIP != null)
+						internalIPView.setText(internalIP);
+					else
+						internalIPView.setText("-");
+					
+					if (externalIP != null)
+						externalIPView.setText(externalIP);
+					else
+						externalIPView.setText("-");	
+				  }
+			  }).start();			
 	}
 
 

+ 30 - 33
src/de/tudarmstadt/informatik/hostage/ui/ViewLog.java

@@ -206,45 +206,42 @@ public class ViewLog extends Activity {
 					final int progressMax = recordList.size();		
 					Log.i("SQLLogger", "Logs to upload: " + progressMax);	
 							HttpPost httppost;
-							String externalIPString = HelperUtils.getExternalIP(getBaseContext());
 							int progressBarStatus = 0;
 							int retry_counter = 0;
 							while(progressBarStatus < progressMax){
-								Log.i("SQLLogger", "Uploading log: " + progressBarStatus);
 								Record record = recordList.get(progressBarStatus);
-								try {
-								// Set the IP to your external IP
-								InetAddress external = InetAddress.getByName(externalIPString);
-								record.setLocalIP(external);
-								record.setRemoteIP(external);
-								// Create HttpPost
-								httppost = new HttpPost(pref.getString("pref_upload", "https://ssi.cased.de"));
-								// Create JSON String of Record
-								StringEntity se = new StringEntity(record.toString(JSON));
-								httppost.setEntity(se);
-								// Execute HttpPost
-								HttpResponse response = httpclient.execute(httppost);
-								Log.i("SQLLogger", "Statuscode of log "  + progressBarStatus + ": " + " " + response.getStatusLine().getReasonPhrase());	
-								Log.i("SQLLogger", "Statuscode of log "  + progressBarStatus + ": " + " " + response.getStatusLine().getStatusCode());	
-								retry_counter = 0;
-								// Update Notification progress bar
-								progressBarStatus++;								
-								builder.setProgress(progressMax, progressBarStatus, false);
-			                     // Update the progress bar
-								mNotifyManager.notify(2, builder.build());
-								} catch (Exception e) {
+								if(record.getExternalIP() != null){
+									Log.i("SQLLogger", "Uploading log: " + progressBarStatus);
+									try {
+									// Create HttpPost
+									httppost = new HttpPost(pref.getString("pref_upload", "https://ssi.cased.de"));
+									// Create JSON String of Record
+									StringEntity se = new StringEntity(record.toString(JSON));
+									httppost.setEntity(se);
+									// Execute HttpPost
+									HttpResponse response = httpclient.execute(httppost);
+									Log.i("SQLLogger", "Statuscode of log "  + progressBarStatus + ": " + " " + response.getStatusLine().getReasonPhrase());	
+									Log.i("SQLLogger", "Statuscode of log "  + progressBarStatus + ": " + " " + response.getStatusLine().getStatusCode());	
+									retry_counter = 0;
+									// Update Notification progress bar
+									progressBarStatus++;								
+									builder.setProgress(progressMax, progressBarStatus, false);
+				                     // Update the progress bar
+									mNotifyManager.notify(2, builder.build());
+									} catch (Exception e) {
 
-									retry_counter++;
-									if(retry_counter == 3){
-										retry_counter = 0;
-										progressBarStatus++;
-										Log.i("SQLLogger", "Upload of log " + progressBarStatus + " failed. Continue with next log");
-									} else {
-										Log.i("SQLLogger", "Upload of log " + progressBarStatus + " failed...retry");
-									}
+										retry_counter++;
+										if(retry_counter == 3){
+											retry_counter = 0;
+											progressBarStatus++;
+											Log.i("SQLLogger", "Upload of log " + progressBarStatus + " failed. Continue with next log");
+										} else {
+											Log.i("SQLLogger", "Upload of log " + progressBarStatus + " failed...retry");
+										}
 
-									e.printStackTrace();
-								}
+										e.printStackTrace();
+									}
+								}								
 							}
 							
 					if(progressBarStatus == progressMax){