Browse Source

Synchronization with Tracing works now

Alexander Brakowski 9 years ago
parent
commit
7ed6530f93

BIN
res/drawable-hdpi/ic_action_import_export.png


BIN
res/drawable-mdpi/ic_action_import_export.png


BIN
res/drawable-xhdpi/ic_action_import_export.png


BIN
res/drawable-xxhdpi/ic_action_import_export.png


BIN
res/drawable-xxxhdpi/ic_action_import_export.png


+ 1 - 1
res/xml/syncadapter.xml

@@ -16,7 +16,7 @@ limitations under the License.
 -->
 
 <sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
-              android:contentAuthority="de.tudarmstadt.informatik.hostage.androidsync"
+              android:contentAuthority="de.tudarmstadt.informatik.hostage"
               android:accountType="de.tudarmstadt.informatik.hostage"
               android:userVisible="true"
               android:supportsUploading="true"

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

@@ -257,6 +257,8 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
      *            The added {@link de.tudarmstadt.informatik.hostage.logging.MessageRecord}s .
      */
     synchronized public void insertMessageRecords(List<MessageRecord> records){
+        if(records == null) return;
+
         SQLiteDatabase db = this.getWritableDatabase();
         db.beginTransaction();
 
@@ -271,6 +273,8 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
         db.close();
     }
     public void insertMessageRecords(List<MessageRecord> records, SQLiteDatabase db){
+        if(records == null) return;
+
         for (MessageRecord record : records){
             this.insertMessageRecordWithOnConflict(record,db);
         }
@@ -362,8 +366,20 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 
         try {
             for (SyncRecord record : records){
-                this.insertAttackRecordWithOnConflict(record.getAttackRecord(), db);
-                this.insertMessageRecords(record.getMessageRecords(), db);
+                AttackRecord attackRecord = record.getAttackRecord();
+                this.insertAttackRecordWithOnConflict(attackRecord, db);
+
+                if(record.getMessageRecords() == null){
+                    MessageRecord msg = new MessageRecord(true);
+                    msg.setAttack_id(attackRecord.getAttack_id());
+                    msg.setType(MessageRecord.TYPE.RECEIVE);
+                    msg.setPacket("");
+                    msg.setTimestamp(System.currentTimeMillis());
+
+                    this.insertMessageRecordWithOnConflict(msg, db);
+                } else {
+                    this.insertMessageRecords(record.getMessageRecords(), db);
+                }
             }
             db.setTransactionSuccessful();
         } finally {
@@ -1366,9 +1382,10 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 		record.setWasInternalAttack(Integer.parseInt(cursor.getString(11)) == 1);
 
 		record.setBssid(cursor.getString(12));
-        record.setDevice(cursor.getString(13));
-        record.setSync_ID(cursor.getLong(14));
-		record.setSsid(cursor.getString(15));
+        record.setDevice(cursor.getString(cursor.getColumnIndex(AttackEntry.COLUMN_NAME_DEVICE)));
+		record.setSync_ID(cursor.getLong(cursor.getColumnIndex(AttackEntry.COLUMN_NAME_SYNC_ID)));
+
+        record.setSsid(cursor.getString(15));
 		record.setLatitude(Double.parseDouble(cursor.getString(16)));
 		record.setLongitude(Double.parseDouble(cursor.getString(17)));
 		record.setAccuracy(Float.parseFloat(cursor.getString(18)));

+ 1 - 1
src/de/tudarmstadt/informatik/hostage/sync/Synchronizer.java

@@ -112,7 +112,7 @@ public class Synchronizer {
      * Updates new inserted devices.
      * @param otherDeviceIds ArrayList<String> other device ids
      */
-    private void updateNewDevices(ArrayList<String> otherDeviceIds){
+    public void updateNewDevices(ArrayList<String> otherDeviceIds){
 
         if (otherDeviceIds != null){
             ArrayList<SyncDevice> otherDevices = new ArrayList<SyncDevice>();

+ 1 - 1
src/de/tudarmstadt/informatik/hostage/sync/android/SyncAdapter.java

@@ -84,7 +84,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
 
             if(currOffset == 100 || offset == size){
                 boolean success = SyncUtils.uploadRecordsToServer(writer.toString(), serverAddress);
-                if(!success){
+                if(success){
                     syncResult.stats.numIoExceptions++;
                     error =  true;
                     break;

+ 138 - 14
src/de/tudarmstadt/informatik/hostage/sync/android/SyncUtils.java

@@ -49,9 +49,14 @@ import java.util.List;
 import java.util.Map;
 
 import de.tudarmstadt.informatik.hostage.location.MyLocationManager;
+import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
 import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
 import de.tudarmstadt.informatik.hostage.logging.Record;
+import de.tudarmstadt.informatik.hostage.logging.SyncData;
+import de.tudarmstadt.informatik.hostage.logging.SyncInfo;
+import de.tudarmstadt.informatik.hostage.logging.SyncRecord;
 import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
+import de.tudarmstadt.informatik.hostage.sync.Synchronizer;
 import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
 
 /**
@@ -59,22 +64,22 @@ import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
  */
 public class SyncUtils {
     private static final long SYNC_FREQUENCY = 60 * 60;  // 1 hour (in seconds)
-    public static final String CONTENT_AUTHORITY = "de.tudarmstadt.informatik.hostage.androidsync";
+    public static final String CONTENT_AUTHORITY = "de.tudarmstadt.informatik.hostage";
     private static final String PREF_SETUP_COMPLETE = "sync_setup_complete";
 
     private static final Map<String, Integer> protocolsTypeMap;
 
     static {
         protocolsTypeMap = new HashMap<String, Integer>();
-        protocolsTypeMap.put("ECHO", 10);
-        protocolsTypeMap.put("FTP", 0);
-        protocolsTypeMap.put("GHOST", 0);
-        protocolsTypeMap.put("HTTP", 0);
-        protocolsTypeMap.put("HTTPS", 0);
+        protocolsTypeMap.put("ECHO", 1);
+        protocolsTypeMap.put("GHOST", 2);
         protocolsTypeMap.put("MySQL", 31);
-        protocolsTypeMap.put("SIP", 50);
         protocolsTypeMap.put("SMB", 40);
-        protocolsTypeMap.put("TELNET", 0);
+        protocolsTypeMap.put("SIP", 50);
+        protocolsTypeMap.put("FTP", 60);
+        protocolsTypeMap.put("HTTP", 70);
+        protocolsTypeMap.put("HTTPS", 71);
+        protocolsTypeMap.put("TELNET", 80);
     }
 
     /**
@@ -123,6 +128,15 @@ public class SyncUtils {
                 b);                                      // Extras
     }
 
+
+    public static String getProtocolFromInt(int p){
+        for(Map.Entry<String, Integer> entry: protocolsTypeMap.entrySet()){
+            if(entry.getValue() == p) return entry.getKey();
+        }
+
+        return null;
+    }
+
     public static void appendRecordToStringWriter(Record record, Writer stream){
         try {
             stream.append(
@@ -142,7 +156,11 @@ public class SyncUtils {
                 "\"type\":" + (protocolsTypeMap.containsKey(record.getProtocol()) ? protocolsTypeMap.get(record.getProtocol()) : 0) + "," +
                 "\"log\":\"" + record.getProtocol() + "\"," +
                 "\"md5sum\":\"\"," +
-                "\"date\":" + (int)(record.getTimestamp() / 1000) +
+                "\"date\":" + (int)(record.getTimestamp() / 1000) + "," +
+                "\"bssid\":\"" + record.getBssid() + "\"," +
+                "\"ssid\":\"" + record.getSsid() + "\"," +
+                "\"device\":\"" + record.getDevice() + "\"," +
+                "\"sync_id\":\"" + record.getSync_id() + "\"" +
              "}\n"
             );
         } catch (IOException e) {
@@ -191,14 +209,120 @@ public class SyncUtils {
                 return klass.newInstance();
             }
 
+            return klass.getConstructor(klass).newInstance(readResponseToString(response));
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    public static String readResponseToString(HttpResponse response){
+        StringBuilder builder = new StringBuilder();
+
+        try {
             BufferedReader bReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
             String line;
-            StringBuilder builder = new StringBuilder();
+
             while ((line = bReader.readLine()) != null) {
                 builder.append(line);
             }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+        return builder.toString();
+    }
+
+    public static SyncData getSyncDataFromTracing(Context context, Synchronizer synchronizer){
+        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
+        String serverAddress = pref.getString("pref_upload_server", "https://ssi.cased.de");
+
+        HttpPost httppost;
+        try {
+            HttpClient httpClient = createHttpClient();
+            // Create HttpPost
+            httppost = new HttpPost(serverAddress + "/sync");
+
+            SyncInfo info = synchronizer.getSyncInfo();
+
+            JSONArray deviceMap = new JSONArray();
+            for(Map.Entry<String, Long> entry: info.deviceMap.entrySet()){
+                JSONObject m = new JSONObject();
+                m.put("sync_id", entry.getValue());
+                m.put("device", entry.getKey());
+                deviceMap.put(m);
+            }
+
+            JSONObject req = new JSONObject();
+            req.put("condition", "");
+            req.put("info", deviceMap);
+
+            StringEntity se = new StringEntity(req.toString());
+            httppost.addHeader("content-type", "application/json");
+            httppost.setEntity(se);
+
+            // Execute HttpPost
+            HttpResponse response = httpClient.execute(httppost);
+            Log.i("TracingSyncService", "Status Code: " + response.getStatusLine().getStatusCode());
+            if(response.getStatusLine().getStatusCode() >= 400 && response.getStatusLine().getStatusCode() < 600){
+                return null;
+            }
+
+            String responseBody = readResponseToString(response);
+
+            JSONArray syncData = new JSONArray(responseBody);
+
+            ArrayList<SyncRecord> syncRecords = new ArrayList<SyncRecord>();
+            Map<String, NetworkRecord> networkRecordMap = new HashMap<String, NetworkRecord>();
+
+            SyncData result = new SyncData();
+
+            for(int i=0; i<syncData.length(); i++){
+                try {
+                    JSONObject item = syncData.getJSONObject(i);
+                    JSONObject src = item.getJSONObject("src");
+                    JSONArray src_ll = src.getJSONArray("ll");
+
+                    JSONObject dst = item.getJSONObject("dst");
+                    JSONArray dst_ll = dst.getJSONArray("ll");
+
+                    Calendar date = toCalendar(item.getString("date"));
+
+                    if(!networkRecordMap.containsKey(item.getString("bssid"))){
+                        NetworkRecord networkRecord = new NetworkRecord();
+                        networkRecord.setAccuracy(0);
+                        networkRecord.setBssid(item.getString("bssid"));
+                        networkRecord.setSsid(item.getString("ssid"));
+                        networkRecord.setLatitude(dst_ll.getDouble(1));
+                        networkRecord.setLatitude(dst_ll.getDouble(0));
+                        networkRecord.setTimestampLocation(date.getTimeInMillis());
+                        networkRecordMap.put(item.getString("bssid"), networkRecord);
+                    }
+
+                    SyncRecord record = new SyncRecord();
+                    record.setBssid(item.getString("bssid"));
+                    record.setAttack_id(i);
+                    record.setDevice(item.getString("device"));
+                    record.setSync_id(item.getLong("sync_id"));
+                    record.setProtocol(getProtocolFromInt(item.getInt("type")));
+                    record.setLocalIP(dst.getString("ip"));
+                    record.setLocalPort(dst.getInt("port"));
+                    record.setRemoteIP(src.getString("ip"));
+                    record.setRemotePort(src.getInt("port"));
+                    record.setExternalIP("0.0.0.0");
+                    record.setWasInternalAttack(false);
+
+                    syncRecords.add(record);
+                } catch(Exception e){
+                    e.printStackTrace();
+                    continue;
+                }
+            }
+
+            result.networkRecords = new ArrayList<NetworkRecord>(networkRecordMap.values());
+            result.syncRecords = syncRecords;
 
-            return klass.getConstructor(klass).newInstance(builder.toString());
+            return result;
         } catch (Exception e) {
             e.printStackTrace();
             return null;
@@ -279,7 +403,7 @@ public class SyncUtils {
 
     public static String fromCalendar(final Calendar calendar) {
         Date date = calendar.getTime();
-        String formatted = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
+        String formatted = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
                 .format(date);
         return formatted.substring(0, 22) + ":" + formatted.substring(22);
     }
@@ -287,7 +411,7 @@ public class SyncUtils {
     public static Calendar toCalendar(final String iso8601string)
             throws ParseException {
         Calendar calendar = GregorianCalendar.getInstance();
-        String s = iso8601string.replace("Z", "+00:00");
+        String s = iso8601string.replace("Z", "0+0000");
 
         try {
             s = s.substring(0, 22) + s.substring(23);  // to get rid of the ":"
@@ -295,7 +419,7 @@ public class SyncUtils {
             throw new ParseException("Invalid length", 0);
         }
 
-        Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(s);
+        Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse(s);
         calendar.setTime(date);
         return calendar;
     }

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

@@ -20,7 +20,7 @@ public class TracingSyncActivity extends Activity implements TracingSyncResultRe
 		super.onCreate(savedInstanceState);
 		setContentView(R.layout.activity_nfc);
 		mInfoText = (TextView) findViewById(R.id.nfc_text_view);
-		mInfoText.setText("Uploading Records...");
+		mInfoText.setText("Synchronizing...");
 		
 		TracingSyncResultReciever mReceiver = new TracingSyncResultReciever(new Handler());
         mReceiver.setReceiver(this);

+ 28 - 8
src/de/tudarmstadt/informatik/hostage/sync/tracing/TracingSyncService.java

@@ -12,7 +12,9 @@ import java.net.URL;
 import java.security.KeyStore;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpVersion;
@@ -43,9 +45,12 @@ import android.preference.PreferenceManager;
 import android.util.Log;
 import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
 import de.tudarmstadt.informatik.hostage.logging.Record;
+import de.tudarmstadt.informatik.hostage.logging.SyncData;
 import de.tudarmstadt.informatik.hostage.logging.SyncInfoRecord;
+import de.tudarmstadt.informatik.hostage.logging.SyncRecord;
 import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
+import de.tudarmstadt.informatik.hostage.sync.Synchronizer;
 import de.tudarmstadt.informatik.hostage.sync.android.SyncUtils;
 import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
 
@@ -72,6 +77,7 @@ public class TracingSyncService extends IntentService {
 	private ResultReceiver receiver;
 
 	HostageDBOpenHelper dbh;
+    Synchronizer synchronizer;
 
 	SharedPreferences pref;
 	Editor editor;
@@ -87,6 +93,7 @@ public class TracingSyncService extends IntentService {
 		pref = PreferenceManager.getDefaultSharedPreferences(this);
 		editor = pref.edit();
 		dbh = new HostageDBOpenHelper(this);
+        synchronizer = new Synchronizer(dbh);
 	}
 
 	/**
@@ -101,7 +108,7 @@ public class TracingSyncService extends IntentService {
 			if (ACTION_START_SYNC.equals(action)) {
 				receiver = intent.getParcelableExtra(EXTRA_RECEIVER);
 				syncNewRecords();
-				dbh.clearSyncInfos();
+
 				if (receiver != null) {
 					receiver.send(SYNC_COMPLETE, null);
 				}
@@ -116,7 +123,9 @@ public class TracingSyncService extends IntentService {
 	private void syncNewRecords() {
         long lastSyncTime = pref.getLong("LAST_SYNC_TIME", 0);
 
-		String serverAddress = pref.getString("pref_upload_server", "https://ssi.cased.de"); //"https://192.168.1.118:9999"
+
+        // Then upload to tracing
+        String serverAddress = pref.getString("pref_upload_server", "https://ssi.cased.de"); //"https://192.168.1.118:9999"
 
         LogFilter filter = new LogFilter();
         filter.setAboveTimestamp(lastSyncTime);
@@ -124,15 +133,15 @@ public class TracingSyncService extends IntentService {
         ArrayList<Record> records = dbh.getRecordsForFilter(filter);
         StringWriter writer = new StringWriter();
 
-		int size = records.size();
-		int offset = 1;
+        int size = records.size();
+        int offset = 1;
         int currOffset = 1;
 
         boolean error = false;
-		for (Record record : records) {
+        for (Record record : records) {
             SyncUtils.appendRecordToStringWriter(record, writer);
 
-            if(currOffset == 5 || offset == size){
+            if(currOffset == 100 || offset == size){
                 boolean success = SyncUtils.uploadRecordsToServer(writer.toString(), serverAddress);
                 Log.i("Tracing upload", "Upload of record: " + offset + "/" + size + ((success) ? " successful." : " failed."));
 
@@ -157,10 +166,21 @@ public class TracingSyncService extends IntentService {
 
             offset++;
             currOffset++;
-		}
+        }
+
+        // First download from tracing
+        SyncData syncDataFromTracing = SyncUtils.getSyncDataFromTracing(this, synchronizer);
+
+        HashSet<String> devices = new HashSet<String>();
+        for(SyncRecord s: syncDataFromTracing.syncRecords){
+            devices.add(s.getDevice());
+        }
+
+        synchronizer.updateNewDevices(new ArrayList<String>(devices));
+        synchronizer.updateFromSyncData(syncDataFromTracing);
 
         if(!error) pref.edit().putLong("LAST_SYNC_TIME", System.currentTimeMillis()).apply();
-	}
+    }
 
 
 	/**