|
@@ -1,5 +1,7 @@
|
|
|
package de.tudarmstadt.informatik.hostage.persistence;
|
|
|
|
|
|
+import java.lang.reflect.Array;
|
|
|
+import java.sql.SQLInput;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.LinkedList;
|
|
@@ -170,6 +172,11 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
|
|
|
private static SyncDevice thisDevice = null;
|
|
|
public static SyncDevice currentDevice(){
|
|
|
+ if (thisDevice != null){
|
|
|
+ SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.getContext());
|
|
|
+ int attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
|
|
|
+ thisDevice.setHighest_attack_id(attack_id);
|
|
|
+ }
|
|
|
return thisDevice;
|
|
|
}
|
|
|
/**
|
|
@@ -199,6 +206,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
record.setLast_sync_timestamp(cursor.getLong(1));
|
|
|
record.setHighest_attack_id(cursor.getLong(2));
|
|
|
thisDevice = record;
|
|
|
+ thisDevice.setHighest_attack_id(attack_id-1);
|
|
|
cursor.close();
|
|
|
db.close();
|
|
|
// return record list
|
|
@@ -215,7 +223,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
editor.putString("CURRENT_DEVICE_IDENTIFIER", thisDevice.getDeviceID());
|
|
|
editor.commit();
|
|
|
thisDevice.setLast_sync_timestamp(0);
|
|
|
- thisDevice.setHighest_attack_id(-1);
|
|
|
+ thisDevice.setHighest_attack_id(attack_id-1);
|
|
|
ArrayList<SyncDevice> devices = new ArrayList<SyncDevice>();
|
|
|
devices.add(thisDevice);
|
|
|
this.insertSyncDevices(devices);
|
|
@@ -235,7 +243,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
* @param record
|
|
|
* The added {@link MessageRecord} .
|
|
|
*/
|
|
|
- public void addMessageRecord(MessageRecord record) {
|
|
|
+ synchronized public void addMessageRecord(MessageRecord record) {
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
|
this.insertMessageRecordWithOnConflict(record, db);
|
|
@@ -248,7 +256,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
* @param records {@link List}<MessageRecord>
|
|
|
* The added {@link de.tudarmstadt.informatik.hostage.logging.MessageRecord}s .
|
|
|
*/
|
|
|
- public void insertMessageRecords(List<MessageRecord> records){
|
|
|
+ synchronized public void insertMessageRecords(List<MessageRecord> records){
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
db.beginTransaction();
|
|
|
|
|
@@ -262,8 +270,13 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
}
|
|
|
db.close();
|
|
|
}
|
|
|
+ public void insertMessageRecords(List<MessageRecord> records, SQLiteDatabase db){
|
|
|
+ for (MessageRecord record : records){
|
|
|
+ this.insertMessageRecordWithOnConflict(record,db);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- private void insertMessageRecordWithOnConflict(MessageRecord record, SQLiteDatabase db){
|
|
|
+ synchronized private void insertMessageRecordWithOnConflict(MessageRecord record, SQLiteDatabase db){
|
|
|
ContentValues recordValues = new ContentValues();
|
|
|
recordValues.put(PacketEntry.COLUMN_NAME_ID, record.getId()); // Log Message Number
|
|
|
recordValues.put(PacketEntry.COLUMN_NAME_ATTACK_ID, record.getAttack_id()); // Log Attack ID
|
|
@@ -281,7 +294,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
* @param record
|
|
|
* The added {@link AttackRecord} .
|
|
|
*/
|
|
|
- public void addAttackRecord(AttackRecord record) {
|
|
|
+ synchronized public void addAttackRecord(AttackRecord record) {
|
|
|
//Log.i("DBHelper", "Add Attack Record with id: " + record.getAttack_id());
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
this.insertAttackRecordWithOnConflict(record,db);
|
|
@@ -297,7 +310,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
* @param records {@link List}<AttackRecord>
|
|
|
* The added {@link AttackRecord}s .
|
|
|
*/
|
|
|
- public void insertAttackRecords(List<AttackRecord> records) {
|
|
|
+ synchronized public void insertAttackRecords(List<AttackRecord> records) {
|
|
|
|
|
|
|
|
|
//Log.i("DBHelper", "Add Attack Record with id: " + record.getAttack_id());
|
|
@@ -318,7 +331,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
this.updateSyncDevices(devices);
|
|
|
}
|
|
|
|
|
|
- private void insertAttackRecordWithOnConflict(AttackRecord record, SQLiteDatabase db){
|
|
|
+ synchronized private void insertAttackRecordWithOnConflict(AttackRecord record, SQLiteDatabase db){
|
|
|
ContentValues attackValues = new ContentValues();
|
|
|
attackValues.put(AttackEntry.COLUMN_NAME_ATTACK_ID, record.getAttack_id()); // Log Attack ID
|
|
|
attackValues.put(AttackEntry.COLUMN_NAME_PROTOCOL, record.getProtocol().toString());
|
|
@@ -342,7 +355,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
* @param records {@link List}<AttackRecord>
|
|
|
* The added {@link SyncRecord}s .
|
|
|
*/
|
|
|
- public void insertSyncRecords(List<SyncRecord> records) {
|
|
|
+ synchronized public void insertSyncRecords(List<SyncRecord> records) {
|
|
|
//Log.i("DBHelper", "Add Attack Record with id: " + record.getAttack_id());
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
db.beginTransaction();
|
|
@@ -350,7 +363,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
try {
|
|
|
for (SyncRecord record : records){
|
|
|
this.insertAttackRecordWithOnConflict(record.getAttackRecord(), db);
|
|
|
- this.insertMessageRecordWithOnConflict(record.getMessageRecord(), db);
|
|
|
+ this.insertMessageRecords(record.getMessageRecords(), db);
|
|
|
}
|
|
|
db.setTransactionSuccessful();
|
|
|
} finally {
|
|
@@ -366,10 +379,10 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
/**
|
|
|
* Updates the sync devices max sync id.
|
|
|
*/
|
|
|
- public void updateSyncDevicesMaxID(SQLiteDatabase db){
|
|
|
+ synchronized public void updateSyncDevicesMaxID(SQLiteDatabase db){
|
|
|
HashMap<String, Long> deviceIDmap = new HashMap<String, Long>();
|
|
|
|
|
|
- String selectQuery = "SELECT "+AttackEntry.COLUMN_NAME_DEVICE+ ","+ AttackEntry.COLUMN_NAME_SYNC_ID+" FROM " + AttackEntry.TABLE_NAME + " A " + " GROUP BY " + AttackEntry.COLUMN_NAME_DEVICE + " HAVING " + AttackEntry.COLUMN_NAME_SYNC_ID + " = MAX( " + AttackEntry.COLUMN_NAME_SYNC_ID + " )";
|
|
|
+ String selectQuery = "SELECT "+AttackEntry.COLUMN_NAME_DEVICE+ ","+ AttackEntry.COLUMN_NAME_SYNC_ID+" FROM " + AttackEntry.TABLE_NAME + " A " + " WHERE " + AttackEntry.COLUMN_NAME_SYNC_ID + " NOT NULL " + " GROUP BY " + AttackEntry.COLUMN_NAME_DEVICE + " HAVING " + AttackEntry.COLUMN_NAME_SYNC_ID + " = MAX( " + AttackEntry.COLUMN_NAME_SYNC_ID + " )";
|
|
|
|
|
|
{
|
|
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
|
@@ -388,15 +401,17 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
{
|
|
|
ArrayList<SyncDevice> allDevices = this.getSyncDevices(db);
|
|
|
for (SyncDevice device : allDevices){
|
|
|
- long sync_id = deviceIDmap.get(device.getDeviceID());
|
|
|
- device.setHighest_attack_id(sync_id);
|
|
|
+ Long sync_id = deviceIDmap.get(device.getDeviceID());
|
|
|
+ long highestID = device.getHighest_attack_id();
|
|
|
+ if (sync_id != null) highestID = sync_id.longValue();
|
|
|
+ device.setHighest_attack_id(highestID);
|
|
|
}
|
|
|
this.updateSyncDevices(allDevices, db);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-
|
|
|
- public void updateSyncAttackCounter(AttackRecord record){
|
|
|
+
|
|
|
+ synchronized public void updateSyncAttackCounter(AttackRecord record){
|
|
|
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
@@ -493,7 +508,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- public int numBssidSeen(String protocol, String BSSID) {
|
|
|
+ synchronized public int numBssidSeen(String protocol, String BSSID) {
|
|
|
String countQuery = "SELECT COUNT(*) FROM " + AttackEntry.TABLE_NAME
|
|
|
+ " WHERE " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_PROTOCOL + " = " + "'" + protocol + "'"
|
|
|
+ " AND " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_BSSID + " = " + "'" + BSSID + "'";
|
|
@@ -1012,12 +1027,21 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
db.close();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Updates sync devices
|
|
|
+ * @param devices array list of sync devices
|
|
|
+ */
|
|
|
public synchronized void updateSyncDevices(ArrayList<SyncDevice> devices){
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
this.updateSyncDevices(devices, db);
|
|
|
db.close();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Updates sync devices.
|
|
|
+ * @param devices array list of sync devices
|
|
|
+ * @param db sqlite database
|
|
|
+ */
|
|
|
public synchronized void updateSyncDevices(ArrayList<SyncDevice> devices, SQLiteDatabase db){
|
|
|
db.beginTransaction();
|
|
|
|
|
@@ -1046,6 +1070,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
for (SyncDevice device : devices){
|
|
|
deviceMap.put(device.getDeviceID(), device.getHighest_attack_id());
|
|
|
}
|
|
|
+ deviceMap.put(SyncDevice.currentDevice().getDeviceID(), SyncDevice.currentDevice().getHighest_attack_id());
|
|
|
SyncInfo syncInfo = new SyncInfo();
|
|
|
syncInfo.deviceMap = deviceMap;
|
|
|
syncInfo.bssids = this.getAllBSSIDS();
|
|
@@ -1201,7 +1226,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
public synchronized void deleteByBSSID(String bssid) {
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
db.delete(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID + " = ?", new String[] { bssid });
|
|
|
- db.delete(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_BSSID + " = ?", new String[] { bssid });
|
|
|
+ db.delete(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_BSSID + " = ?", new String[]{bssid});
|
|
|
db.close();
|
|
|
}
|
|
|
|
|
@@ -1227,11 +1252,36 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
*/
|
|
|
public synchronized void deleteByAttackID(long attackID) {
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
- db.delete(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_ATTACK_ID + " = ?", new String[] { String.valueOf(attackID) });
|
|
|
- db.delete(PacketEntry.TABLE_NAME, PacketEntry.COLUMN_NAME_ATTACK_ID + " = ?", new String[] { String.valueOf(attackID) });
|
|
|
+ db.delete(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_ATTACK_ID + " = ?", new String[]{String.valueOf(attackID)});
|
|
|
+ db.delete(PacketEntry.TABLE_NAME, PacketEntry.COLUMN_NAME_ATTACK_ID + " = ?", new String[]{String.valueOf(attackID)});
|
|
|
db.close();
|
|
|
}
|
|
|
|
|
|
+ public ArrayList<MessageRecord> getMessageRecords(AttackRecord attackRecord , SQLiteDatabase db){
|
|
|
+ ArrayList<MessageRecord> mr = new ArrayList<MessageRecord>();
|
|
|
+
|
|
|
+ String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " WHERE " + PacketEntry.COLUMN_NAME_ATTACK_ID + " = " + attackRecord.getAttack_id();
|
|
|
+
|
|
|
+ boolean createdDB = false;
|
|
|
+ if (db == null){
|
|
|
+ db = this.getReadableDatabase();
|
|
|
+ createdDB = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
+
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
+ do {
|
|
|
+ MessageRecord record = createMessageRecord(cursor);
|
|
|
+ mr.add(record);
|
|
|
+ } while (cursor.moveToNext());
|
|
|
+ }
|
|
|
+ cursor.close();
|
|
|
+
|
|
|
+ if (createdDB) db.close();
|
|
|
+ return mr;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Creates a {@link MessageRecord} from a Cursor. If the cursor does not show to a
|
|
|
* valid data structure a runtime exception is thrown.
|
|
@@ -1280,31 +1330,12 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
* @param cursor
|
|
|
* @return Returns the created {@link SyncRecord} .
|
|
|
*/
|
|
|
- private synchronized SyncRecord createSyncRecord(Cursor cursor){
|
|
|
- SyncRecord record = new SyncRecord();
|
|
|
- record.setAttack_id(cursor.getLong(0));
|
|
|
- record.setProtocol(cursor.getString(1));
|
|
|
- record.setExternalIP(cursor.getString(2));
|
|
|
- record.setLocalIP(cursor.getString(3));
|
|
|
- record.setLocalPort(Integer.parseInt(cursor.getString(4)));
|
|
|
- record.setRemoteIP(cursor.getString(5));
|
|
|
- record.setRemotePort(Integer.parseInt(cursor.getString(6)));
|
|
|
- record.setWasInternalAttack(cursor.getInt(7) == 1);
|
|
|
- record.setBssid(cursor.getString(8));
|
|
|
- record.setSync_id(cursor.getLong(9));
|
|
|
- record.setDevice(cursor.getString(10));
|
|
|
+ private synchronized SyncRecord createSyncRecord(Cursor cursor, SQLiteDatabase db){
|
|
|
+ AttackRecord attackRecord = this.createAttackRecord(cursor);
|
|
|
+ SyncRecord record = new SyncRecord(attackRecord);
|
|
|
|
|
|
- record.setId(Integer.parseInt(cursor.getString(11)));
|
|
|
- //record.setAttack_id(cursor.getLong(12));
|
|
|
- String v = cursor.getString(13);
|
|
|
- if (v != null && !v.equals("RECEIVE")) {
|
|
|
- record.setType(TYPE.SEND);
|
|
|
- //record.setType(MessageRecord.TYPE.valueOf(v));
|
|
|
- } else {
|
|
|
- record.setType(TYPE.RECEIVE);
|
|
|
- }
|
|
|
- record.setTimestamp(cursor.getLong(14));
|
|
|
- record.setPacket(cursor.getString(15));
|
|
|
+ ArrayList<MessageRecord> mr = this.getMessageRecords(attackRecord,db);
|
|
|
+ record.setMessageRecords(mr);
|
|
|
|
|
|
return record;
|
|
|
}
|
|
@@ -1825,6 +1856,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
* @param includeMissing boolean
|
|
|
* @return array of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
|
|
|
*/
|
|
|
+ /*
|
|
|
public ArrayList<SyncDevice> getUpdatedDevicesFor(List<SyncDevice> oldDevices, boolean includeMissing){
|
|
|
|
|
|
HashMap<String, Long> oldDeviceMap = new HashMap<String, Long>();
|
|
@@ -1859,7 +1891,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
// return record list
|
|
|
db.close();
|
|
|
return recordList;
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -1875,11 +1907,20 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
|
|
|
+ boolean actualiseOwnDevice = false;
|
|
|
+ if (oldDeviceMap.keySet().contains(currentDevice().getDeviceID()) || includeMissing){
|
|
|
+ actualiseOwnDevice = true;
|
|
|
+ }
|
|
|
+
|
|
|
// looping through all rows and adding to list
|
|
|
if (cursor.moveToFirst()) {
|
|
|
do {
|
|
|
SyncDevice record = createSyncDevice(cursor);
|
|
|
// Adding record to list
|
|
|
+
|
|
|
+ if (currentDevice().getDeviceID().equals(record.getDeviceID()) && actualiseOwnDevice)
|
|
|
+ record.setHighest_attack_id(currentDevice().getHighest_attack_id());
|
|
|
+
|
|
|
if (oldDeviceMap.containsKey(record.getDeviceID())){
|
|
|
Long oldSyncId = oldDeviceMap.get(record.getDeviceID());
|
|
|
if (oldSyncId < record.getHighest_attack_id()){
|
|
@@ -1889,6 +1930,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
if (includeMissing)
|
|
|
recordList.add(record);
|
|
|
}
|
|
|
+
|
|
|
} while (cursor.moveToNext());
|
|
|
}
|
|
|
cursor.close();
|
|
@@ -1965,12 +2007,13 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
for (SyncDevice sDevice : updatedDevices){
|
|
|
String deviceID = sDevice.getDeviceID();
|
|
|
Long maxID = deviceMap.get(deviceID);
|
|
|
- String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A " + " , " + PacketEntry.TABLE_NAME + " P "
|
|
|
+ long checkId = -1;
|
|
|
+ if (maxID != null) checkId = maxID.longValue();
|
|
|
+ String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A "
|
|
|
+ " WHERE "
|
|
|
+" ( "
|
|
|
- + "A." + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + " P."+ PacketEntry.COLUMN_NAME_ATTACK_ID
|
|
|
- + " AND " + " A." + AttackEntry.COLUMN_NAME_DEVICE + " = " + "'" + deviceID + "'"
|
|
|
- + " AND " + " A." + AttackEntry.COLUMN_NAME_SYNC_ID + " > " + maxID
|
|
|
+ + " A." + AttackEntry.COLUMN_NAME_DEVICE + " = " + "'" + deviceID + "'"
|
|
|
+ + " AND " + " A." + AttackEntry.COLUMN_NAME_SYNC_ID + " > " + checkId
|
|
|
+ " ) "
|
|
|
//+ " GROUP BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_DEVICE
|
|
|
+ " ORDER BY " + " A" + "." + AttackEntry.COLUMN_NAME_SYNC_ID + " DESC ";
|
|
@@ -1980,7 +2023,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
if (cursor != null){
|
|
|
if (cursor.moveToFirst()) {
|
|
|
do {
|
|
|
- SyncRecord record = createSyncRecord(cursor);
|
|
|
+ SyncRecord record = createSyncRecord(cursor , db);
|
|
|
recordList.add(record);
|
|
|
} while (cursor.moveToNext());
|
|
|
}
|
|
@@ -2002,7 +2045,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
|
|
|
String filterQuery = this.selectionQueryFromFilter(filter, AttackEntry.COLUMN_NAME_ATTACK_ID);
|
|
|
|
|
|
- String attackPerBSSID_Query = "SELECT " + NetworkEntry.COLUMN_NAME_BSSID + " , " + "COUNT( " + AttackEntry.COLUMN_NAME_ATTACK_ID + " ) " + " "
|
|
|
+ String attackPerBSSID_Query = "SELECT " + NetworkEntry.TABLE_NAME + "." + NetworkEntry.COLUMN_NAME_BSSID + " , " + "COUNT( " + AttackEntry.COLUMN_NAME_ATTACK_ID + " ) " + " "
|
|
|
+ " FROM " + AttackEntry.TABLE_NAME + " a " + " , " + NetworkEntry.TABLE_NAME
|
|
|
+ " WHERE " + " a." + AttackEntry.COLUMN_NAME_ATTACK_ID + " IN " + " ( " + filterQuery + " ) "
|
|
|
+ " GROUP BY " + NetworkEntry.TABLE_NAME + "." + NetworkEntry.COLUMN_NAME_BSSID;
|