Browse Source

Merge branch 'master' of https://mihai.plasoianu@git.tk.informatik.tu-darmstadt.de/scm-ssi-student-hostage.git

Mihai Plasoianu 10 years ago
parent
commit
aececcb3c7

+ 12 - 5
AndroidManifest.xml

@@ -15,6 +15,9 @@
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
+    <uses-permission android:name="android.permission.BLUETOOTH" />
+    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
+    <uses-permission android:name="android.permission.NFC" />
     <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
 
     <uses-feature
@@ -42,9 +45,8 @@
             android:configChanges="keyboardHidden|orientation|screenSize"
             android:label="@string/app_name"
             android:screenOrientation="portrait" >
-            <intent-filter>
+           	<intent-filter>
                 <action android:name="android.intent.action.MAIN" />
-
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
@@ -52,6 +54,7 @@
             android:name="de.tudarmstadt.informatik.hostage.ui.MainActivity"
             android:configChanges="keyboardHidden|orientation|screenSize"
             android:label="@string/app_name" >
+
         </activity>
         <activity
             android:name="de.tudarmstadt.informatik.hostage.ui.ViewLog"
@@ -78,17 +81,17 @@
             android:label="@string/gui_playground" >
         </activity>
         <activity
-            android:name="de.tudarmstadt.informatik.hostage.sync.TracingSync"
+            android:name="de.tudarmstadt.informatik.hostage.sync.tracing.TracingSyncActivity"
             android:label="@string/gui_tracing"
             android:theme="@android:style/Theme.Dialog" >
         </activity>
         <activity
-            android:name="de.tudarmstadt.informatik.hostage.sync.BluetoothSync"
+            android:name="de.tudarmstadt.informatik.hostage.sync.bluetooth.BluetoothSync"
             android:label="@string/gui_bluetooth"
             android:theme="@android:style/Theme.Dialog" >
         </activity>
         <activity
-            android:name="de.tudarmstadt.informatik.hostage.sync.NFCSync"
+            android:name="de.tudarmstadt.informatik.hostage.sync.nfc.NFCSync"
             android:label="@string/gui_nfc"
             android:theme="@android:style/Theme.Dialog" >
             <intent-filter>
@@ -108,6 +111,10 @@
             android:name="de.tudarmstadt.informatik.hostage.logging.Logger"
             android:exported="false" >
         </service>
+        <service
+            android:name="de.tudarmstadt.informatik.hostage.sync.tracing.TracingSyncService"
+            android:exported="false" >
+        </service>
 
         <provider
             android:name="de.tudarmstadt.informatik.hostage.provider.HostageContentProvider"

+ 17 - 0
res/layout/activity_sync_central.xml

@@ -0,0 +1,17 @@
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="vertical"
+    android:paddingBottom="@dimen/activity_vertical_margin"
+    android:paddingLeft="@dimen/activity_horizontal_margin"
+    android:paddingRight="@dimen/activity_horizontal_margin"
+    android:paddingTop="@dimen/activity_vertical_margin" >
+
+    <TextView
+        android:id="@+id/sync_central_text_view"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="Uploading Records to" />
+
+</LinearLayout>

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

@@ -107,7 +107,7 @@ public class Handler implements Runnable {
 		} catch (Exception e) {
 			
 		} 
-		
+		//TODO kann ConcurrentModificationException auslösen, da über collection iteriert wird während elemente entfernt werden
 		listener.refreshHandlers();
 	}
 

+ 1 - 0
src/de/tudarmstadt/informatik/hostage/Listener.java

@@ -121,6 +121,7 @@ public class Listener implements Runnable {
 			addHandler();
 		}
 		for (Handler handler : handlers) {
+			//TODO kann ConcurrentModificationException auslösen, da über collection iteriert wird während elemente entfernt werden
 			handler.kill();
 		}
 	}

+ 8 - 0
src/de/tudarmstadt/informatik/hostage/logging/AttackRecord.java

@@ -159,5 +159,13 @@ public class AttackRecord implements Parcelable, Serializable{
 	public void setExternalIP(String externalIP) {
 		this.externalIP = externalIP;
 	}
+	
+	//TEMP ZU TEST
+	@Override
+	public String toString(){
+		return String
+				.format("{ \"sensor\":{\"type\": \"Honeypot\", \"name\": \"HosTaGe\"}, \"type\": \"%s server access\", \"src\":{\"ip\": \"%s\", \"port\": %d}, \"dst\":{\"ip\": \"%s\", \"port\": %d} }",
+						getProtocol(), getRemoteIP(), getRemotePort(), getExternalIP(), getLocalPort());
+	}
 
 }

+ 1 - 1
src/de/tudarmstadt/informatik/hostage/persistence/HostageDBOpenHelper.java

@@ -790,7 +790,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 		record.setLocalPort(Integer.parseInt(cursor.getString(4)));
 		record.setRemoteIP(cursor.getString(5));
 		record.setRemotePort(Integer.parseInt(cursor.getString(6)));
-		record.setBssid(cursor.getString(11));
+		record.setBssid(cursor.getString(7));
 
 		return record;
 	}

+ 18 - 11
src/de/tudarmstadt/informatik/hostage/sync/bluetooth/BluetoothSync.java

@@ -29,10 +29,11 @@ import de.tudarmstadt.informatik.hostage.sync.SyncMessage;
 
 public class BluetoothSync extends Activity{	
 	
-    public static final int CONNECTION_ESTABLISHED = 0x0;
-    public static final int CONNECTION_FAILED = 0x1;
-    public static final int SYNC_SUCCESSFUL = 0x2;
-    public static final int SYNC_FAILED = 0x3;
+	public static final int CONNECTING = 0x0;
+    public static final int CONNECTION_ESTABLISHED = 0x1;
+    public static final int CONNECTION_FAILED = 0x2;
+    public static final int SYNC_SUCCESSFUL = 0x3;
+    public static final int SYNC_FAILED = 0x4;
     
     public static UUID serviceUUID;
 	
@@ -111,14 +112,10 @@ public class BluetoothSync extends Activity{
 	}
 		
 
-	private void manageConnectedSocket(BluetoothSocket socket) {
+	protected void manageConnectedSocket(BluetoothSocket socket) {
 		mBluetoothAdapter.cancelDiscovery();
 		unregisterBroadcastReceiver();
 		
-		layout.removeView(listView);
-		String deviceName = socket.getRemoteDevice().getName();
-		mInfoText.setText("Synchronizing with " + deviceName + "...");	
-		
 		HostageDBOpenHelper dbh = new HostageDBOpenHelper(this);
 		commThread = new CommunicationThread(this, socket, mHandler);
 		commThread.start();
@@ -180,6 +177,8 @@ public class BluetoothSync extends Activity{
 			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 				String deviceInfo = arrayAdapter.getItem(position);
 				String mac = deviceInfo.substring(deviceInfo.indexOf("\n") + 1);
+				String name = deviceInfo.substring(0, deviceInfo.indexOf("\n"));
+				mHandler.obtainMessage(CONNECTING, name).sendToTarget();
 				clientThread = new ClientThread(mBluetoothAdapter.getRemoteDevice(mac), mHandler);
 				clientThread.start();
 			}
@@ -191,11 +190,19 @@ public class BluetoothSync extends Activity{
         @Override
         public void handleMessage(Message msg) {
         	switch(msg.what){
+        		case CONNECTING:       
+        			layout.removeView(listView);
+        			mInfoText.setText("Connecting to " + (String)msg.obj + "!");
+        			break;
         		case CONNECTION_ESTABLISHED: 
-        			manageConnectedSocket((BluetoothSocket) msg.obj);
+        			BluetoothSocket socket = (BluetoothSocket) msg.obj;
+        			layout.removeView(listView);
+        			String deviceName = socket.getRemoteDevice().getName();
+        			mInfoText.setText("Synchronizing with " + deviceName + "...");	
+        			manageConnectedSocket(socket);
         			break;
         		case CONNECTION_FAILED: 
-        			mInfoText.setText("Synchronization failed!");
+        			mInfoText.setText("Failed to connect to device!");
         			break;
         		case SYNC_SUCCESSFUL: 
         			mInfoText.setText("Synchronization successfull!");

+ 3 - 2
src/de/tudarmstadt/informatik/hostage/sync/bluetooth/CommunicationThread.java

@@ -98,11 +98,12 @@ public class CommunicationThread extends Thread {
 				if(devices_local.containsKey("TRACING"))
 					tracing_timestamp = devices_local.get("TRACING");				
 					
-				for(String key : devices_remote.keySet()){
+				for(Iterator<String> i = devices_remote.keySet().iterator(); i.hasNext(); ){
+					String key = i.next();
 					if((devices_local.containsKey(key) && devices_local.get(key) >= devices_remote.get(key)) 
 					    || (tracing_timestamp > devices_remote.get(key))){
 						Log.i("CommunicationThread", "Removed: " + key);
-						devices_remote.remove(key);
+						i.remove();
 					}
 				}
 				mdbh.updateSyncDevices(devices_remote);

+ 19 - 22
src/de/tudarmstadt/informatik/hostage/sync/nfc/NFCSync.java

@@ -49,9 +49,6 @@ public class NFCSync extends Activity implements CreateNdefMessageCallback, OnNd
 	TextView mInfoText;
 	private static final int MESSAGE_SENT = 1;
 
-
-
-	/** This handler receives a message from onNdefPushComplete */
 	private final Handler mHandler = new Handler() {
 		@Override
 		public void handleMessage(Message msg) {
@@ -63,6 +60,25 @@ public class NFCSync extends Activity implements CreateNdefMessageCallback, OnNd
 			}
 		}
 	};
+	
+	@Override
+	public void onCreate(Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		setContentView(R.layout.activity_nfc);
+
+		mInfoText = (TextView) findViewById(R.id.nfc_text_view);
+		// Check for available NFC Adapter
+		mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
+		if (mNfcAdapter == null) {
+			mInfoText.setText("NFC is not available on this device.");
+		} else {
+			mInfoText.setText("Hold phones together to synchronize.");
+			// Register callback to set NDEF message
+			mNfcAdapter.setNdefPushMessageCallback(this, this);
+			// Register callback to listen for message-sent success
+			mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
+		}
+	}
 
 	/**
 	 * Implementation for the CreateNdefMessageCallback interface
@@ -84,25 +100,6 @@ public class NFCSync extends Activity implements CreateNdefMessageCallback, OnNd
 		return msg;
 	}
 
-	@Override
-	public void onCreate(Bundle savedInstanceState) {
-		super.onCreate(savedInstanceState);
-		setContentView(R.layout.activity_nfc);
-
-		mInfoText = (TextView) findViewById(R.id.nfc_text_view);
-		// Check for available NFC Adapter
-		mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
-		if (mNfcAdapter == null) {
-			mInfoText.setText("NFC is not available on this device.");
-		} else {
-			mInfoText.setText("Hold phones together to synchronize.");
-			// Register callback to set NDEF message
-			mNfcAdapter.setNdefPushMessageCallback(this, this);
-			// Register callback to listen for message-sent success
-			mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
-		}
-	}
-
 	/**
 	 * Implementation for the OnNdefPushCompleteCallback interface
 	 */

+ 40 - 2
src/de/tudarmstadt/informatik/hostage/sync/tracing/TracingSyncActivity.java

@@ -1,5 +1,43 @@
 package de.tudarmstadt.informatik.hostage.sync.tracing;
 
-public class TracingSyncActivity {
-	//TODO
+import de.tudarmstadt.informatik.hostage.R;
+import android.app.Activity;
+import android.content.Intent;
+import android.os.Bundle;
+import android.os.Handler;
+import android.widget.TextView;
+
+public class TracingSyncActivity extends Activity implements TracingSyncResultReciever.Receiver{
+	
+	TextView mInfoText;
+
+	@Override
+	public void onCreate(Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		setContentView(R.layout.activity_nfc);
+		mInfoText = (TextView) findViewById(R.id.nfc_text_view);
+		mInfoText.setText("Uploading Records...");
+		
+		TracingSyncResultReciever mReceiver = new TracingSyncResultReciever(new Handler());
+        mReceiver.setReceiver(this);
+        
+		Intent intent = new Intent(this, TracingSyncService.class);
+		intent.setAction(TracingSyncService.ACTION_START_SYNC);
+		intent.putExtra(TracingSyncService.EXTRA_RECEIVER, mReceiver);
+		startService(intent);
+	}
+
+	@Override
+	public void onReceiveResult(int resultCode, Bundle resultData) {
+        switch (resultCode) {
+        case TracingSyncService.SYNC_COMPLETE:
+        	mInfoText.setText("Information is up to date!");
+            break;        
+        case TracingSyncService.RECORD_UPLOADED:      
+        	mInfoText.setText("Uploading Records...(" + resultData.getInt(TracingSyncService.UPLOAD_PROGRESS) + "/"+ resultData.getInt(TracingSyncService.UPLOAD_SIZE) + ")");
+        break;
+    }
+		
+	}	
+	
 }

+ 28 - 0
src/de/tudarmstadt/informatik/hostage/sync/tracing/TracingSyncResultReciever.java

@@ -0,0 +1,28 @@
+package de.tudarmstadt.informatik.hostage.sync.tracing;
+
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.ResultReceiver;
+ 
+public class TracingSyncResultReciever extends ResultReceiver {
+    private Receiver mReceiver;
+ 
+    public TracingSyncResultReciever(Handler handler) {
+        super(handler);
+    }
+ 
+    public void setReceiver(Receiver receiver) {
+        mReceiver = receiver;
+    }
+ 
+    public interface Receiver {
+        public void onReceiveResult(int resultCode, Bundle resultData);
+    }
+ 
+    @Override
+    protected void onReceiveResult(int resultCode, Bundle resultData) {
+        if (mReceiver != null) {
+            mReceiver.onReceiveResult(resultCode, resultData);
+        }
+    }
+}

+ 52 - 18
src/de/tudarmstadt/informatik/hostage/sync/tracing/TracingSyncService.java

@@ -3,6 +3,7 @@ package de.tudarmstadt.informatik.hostage.sync.tracing;
 import java.security.KeyStore;
 import java.util.ArrayList;
 
+import org.apache.http.HttpResponse;
 import org.apache.http.HttpVersion;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpPost;
@@ -23,30 +24,45 @@ import android.app.IntentService;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
-import android.os.Handler;
+import android.os.Bundle;
+import android.os.ResultReceiver;
 import android.preference.PreferenceManager;
+import android.util.Log;
 import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
-import de.tudarmstadt.informatik.hostage.logging.formatter.TraCINgFormatter;
 import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
 
 public class TracingSyncService extends IntentService{
 	
+	public static final String REMOTE_DEVICE = "de.tudarmstadt.informatik.hostage.REMOTE_DEVICE";
+	
+	public static final String ACTION_START_SYNC = "de.tudarmstadt.informatik.hostage.ACTION_START_SYNC";
+	public static final String EXTRA_RECEIVER = "de.tudarmstadt.informatik.hostage.EXTRA_HANDLER";
+	
+	public static final String UPLOAD_SIZE = "de.tudarmstadt.informatik.hostage.UPLOAD_SIZE";
+	public static final String UPLOAD_PROGRESS = "de.tudarmstadt.informatik.hostage.UPLOAD_PROGRESS";
+	
 	public static final int RECORD_UPLOADED = 0x00;
 	public static final int SYNC_COMPLETE = 0x01;
 	
+	
 	private HttpClient httpClient;
-	private Handler mHandler;
+	private ResultReceiver receiver;
 	
 	HostageDBOpenHelper dbh;
 	
 	SharedPreferences pref;
 	Editor editor;
 
-	public TracingSyncService(Handler handler) {
-		super("TracingSyncService");
-		mHandler = handler;
-		httpClient = createHttpClient();
+	public TracingSyncService() {
+		super(TracingSyncService.class.getName());
+
+	}
+	
+
+	@Override
+	public void onCreate() {
+		super.onCreate();
 		pref = PreferenceManager.getDefaultSharedPreferences(this);
 		editor = pref.edit();
 		dbh = new HostageDBOpenHelper(this);
@@ -59,10 +75,19 @@ public class TracingSyncService extends IntentService{
 	 */
 	@Override
 	protected void onHandleIntent(Intent intent) {
-		//TODO Fallunterscheidung Intents		
-		uploadNewRecords();
-		getRemoteData();
-		dbh.clearSyncInfos();
+		if (intent != null) {
+			final String action = intent.getAction();
+			if (ACTION_START_SYNC.equals(action)) {
+				receiver = intent.getParcelableExtra(EXTRA_RECEIVER);
+				uploadNewRecords();
+				getRemoteData();
+				//TODO add: dbh.clearSyncInfos();
+				if(receiver != null){
+					receiver.send(SYNC_COMPLETE, null);
+				}
+			}
+
+		}	
 	}	
 	
 	
@@ -77,9 +102,16 @@ public class TracingSyncService extends IntentService{
 		int offset = 1;
 		for(AttackRecord record: recordList){
 			editor.putInt("LAST_UPLOADED_ATTACK_ID", lastUploadedAttackId + offset);
-			uploadSingleRecord(record, serverAddress);
-			if(mHandler != null)
-				mHandler.obtainMessage(RECORD_UPLOADED, offset, size);
+			editor.commit();
+			
+			boolean success = uploadSingleRecord(record, serverAddress);
+			Log.i("Tracing upload", "Upload of record: " + offset +"/" + size + ((success) ? " successful.": " failed."));
+			if(receiver != null){
+				Bundle data = new Bundle();
+				data.putInt(UPLOAD_SIZE, size); 
+				data.putInt(UPLOAD_PROGRESS, offset);
+				receiver.send(RECORD_UPLOADED, data);
+			}
 			offset++;			
 		}
 	}	
@@ -95,15 +127,17 @@ public class TracingSyncService extends IntentService{
 		// Create a https client. Uses MySSLSocketFactory to accept all certificates
 		HttpPost httppost;
 		try {
+			httpClient = createHttpClient();
 			// Create HttpPost
-			httppost = new HttpPost();
+			httppost = new HttpPost(serverAddress);
 			// Create JSON String of Record
 			//TODO FIX ME
-			StringEntity se = new StringEntity( null/*record.toString(TraCINgFormatter
-					.getInstance()) */);
+			//StringEntity se = new StringEntity( record.toString(TraCINgFormatter.getInstance()) );
+			StringEntity se = new StringEntity(record.toString());
 			httppost.setEntity(se);
 			// Execute HttpPost
-			httpClient.execute(httppost);
+			HttpResponse response = httpClient.execute(httppost);
+			Log.i("TracingSyncService", "Status Code: " + response.getStatusLine().getStatusCode()); 
 		} catch (Exception e) {
 			e.printStackTrace();
 			return false;

+ 2 - 1
src/de/tudarmstadt/informatik/hostage/ui/PlayGroundActivity.java

@@ -21,6 +21,7 @@ import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract;
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
 import de.tudarmstadt.informatik.hostage.sync.bluetooth.BluetoothSync;
 import de.tudarmstadt.informatik.hostage.sync.nfc.NFCSync;
+import de.tudarmstadt.informatik.hostage.sync.tracing.TracingSyncActivity;
 
 public class PlayGroundActivity extends Activity {
 
@@ -50,7 +51,7 @@ public class PlayGroundActivity extends Activity {
 	}
 
 	public void startNFC(View view) {
-		startActivity(new Intent(this, NFCSync.class));
+		startActivity(new Intent(this, TracingSyncActivity.class));
 	}
 
 	public void syncData(View view) {