|
@@ -1,23 +1,17 @@
|
|
package de.tudarmstadt.informatik.hostage.persistence;
|
|
package de.tudarmstadt.informatik.hostage.persistence;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
-import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.LinkedList;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
-import java.util.Map;
|
|
|
|
-import java.util.Set;
|
|
|
|
-import java.util.UUID;
|
|
|
|
|
|
|
|
import android.content.ContentValues;
|
|
import android.content.ContentValues;
|
|
import android.content.Context;
|
|
import android.content.Context;
|
|
-import android.content.SharedPreferences;
|
|
|
|
import android.database.Cursor;
|
|
import android.database.Cursor;
|
|
|
|
+import android.database.CursorIndexOutOfBoundsException;
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
-import android.preference.PreferenceManager;
|
|
|
|
import android.util.Log;
|
|
import android.util.Log;
|
|
-import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
|
|
|
|
import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
|
|
import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
|
|
import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
|
|
import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
|
|
import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
|
|
import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
|
|
@@ -25,6 +19,7 @@ import de.tudarmstadt.informatik.hostage.logging.Record;
|
|
import de.tudarmstadt.informatik.hostage.logging.SyncDevice;
|
|
import de.tudarmstadt.informatik.hostage.logging.SyncDevice;
|
|
import de.tudarmstadt.informatik.hostage.logging.SyncInfoRecord;
|
|
import de.tudarmstadt.informatik.hostage.logging.SyncInfoRecord;
|
|
import de.tudarmstadt.informatik.hostage.logging.MessageRecord.TYPE;
|
|
import de.tudarmstadt.informatik.hostage.logging.MessageRecord.TYPE;
|
|
|
|
+import de.tudarmstadt.informatik.hostage.logging.SyncRecord;
|
|
import de.tudarmstadt.informatik.hostage.model.Profile;
|
|
import de.tudarmstadt.informatik.hostage.model.Profile;
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.AttackEntry;
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.AttackEntry;
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.NetworkEntry;
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.NetworkEntry;
|
|
@@ -32,7 +27,6 @@ import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEnt
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.ProfileEntry;
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.ProfileEntry;
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncDeviceEntry;
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncDeviceEntry;
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncInfoEntry;
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncInfoEntry;
|
|
-import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
|
|
|
|
import de.tudarmstadt.informatik.hostage.ui.helper.ColorSequenceGenerator;
|
|
import de.tudarmstadt.informatik.hostage.ui.helper.ColorSequenceGenerator;
|
|
import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
|
|
import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
|
|
import de.tudarmstadt.informatik.hostage.ui.model.PlotComparisonItem;
|
|
import de.tudarmstadt.informatik.hostage.ui.model.PlotComparisonItem;
|
|
@@ -176,18 +170,43 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
public void addMessageRecord(MessageRecord record) {
|
|
public void addMessageRecord(MessageRecord record) {
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
|
- 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
|
|
|
|
- recordValues.put(PacketEntry.COLUMN_NAME_TYPE, record.getType().name()); // Log Type
|
|
|
|
- recordValues.put(PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP, record.getTimestamp()); // Log Timestamp
|
|
|
|
- recordValues.put(PacketEntry.COLUMN_NAME_PACKET, record.getPacket()); // Log Packet
|
|
|
|
-
|
|
|
|
- // Inserting Rows
|
|
|
|
- db.insert(PacketEntry.TABLE_NAME, null, recordValues);
|
|
|
|
|
|
+ this.insertMessageRecordWithOnConflict(record, db);
|
|
db.close(); // Closing database connection
|
|
db.close(); // Closing database connection
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Adds a given {@link de.tudarmstadt.informatik.hostage.logging.MessageRecord}s to the database.
|
|
|
|
+ *
|
|
|
|
+ * @param records {@link List}<MessageRecord>
|
|
|
|
+ * The added {@link de.tudarmstadt.informatik.hostage.logging.MessageRecord}s .
|
|
|
|
+ */
|
|
|
|
+ public void insertMessageRecords(List<MessageRecord> records){
|
|
|
|
+ SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
+ db.beginTransaction();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ for (MessageRecord record : records){
|
|
|
|
+ this.insertMessageRecordWithOnConflict(record,db);
|
|
|
|
+ }
|
|
|
|
+ db.setTransactionSuccessful();
|
|
|
|
+ } finally {
|
|
|
|
+ db.endTransaction();
|
|
|
|
+ }
|
|
|
|
+ db.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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
|
|
|
|
+ recordValues.put(PacketEntry.COLUMN_NAME_TYPE, record.getType().name()); // Log Type
|
|
|
|
+ recordValues.put(PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP, record.getTimestamp()); // Log Timestamp
|
|
|
|
+ recordValues.put(PacketEntry.COLUMN_NAME_PACKET, record.getPacket()); // Log Packet
|
|
|
|
+
|
|
|
|
+ // Inserting Rows
|
|
|
|
+ db.insertWithOnConflict(PacketEntry.TABLE_NAME, null, recordValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Adds a given {@link AttackRecord} to the database.
|
|
* Adds a given {@link AttackRecord} to the database.
|
|
*
|
|
*
|
|
@@ -197,28 +216,14 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
public void addAttackRecord(AttackRecord record) {
|
|
public void addAttackRecord(AttackRecord record) {
|
|
//Log.i("DBHelper", "Add Attack Record with id: " + record.getAttack_id());
|
|
//Log.i("DBHelper", "Add Attack Record with id: " + record.getAttack_id());
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
-
|
|
|
|
- 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());
|
|
|
|
- attackValues.put(AttackEntry.COLUMN_NAME_EXTERNAL_IP, record.getExternalIP());
|
|
|
|
- attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_IP, record.getLocalIP()); // Log Local IP
|
|
|
|
- attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_PORT, record.getLocalPort());
|
|
|
|
- attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_IP, record.getRemoteIP()); // Log Remote IP
|
|
|
|
- attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
|
|
|
|
- attackValues.put(AttackEntry.COLUMN_NAME_INTERNAL_ATTACK, record.getWasInternalAttack());
|
|
|
|
- attackValues.put(AttackEntry.COLUMN_NAME_BSSID, record.getBssid());
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- // Inserting Rows
|
|
|
|
- db.insertWithOnConflict(AttackEntry.TABLE_NAME, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
|
|
|
+ this.insertAttackRecordWithOnConflict(record,db);
|
|
db.close(); // Closing database connection
|
|
db.close(); // Closing database connection
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* Adds a given {@link AttackRecord}s to the database.
|
|
* Adds a given {@link AttackRecord}s to the database.
|
|
*
|
|
*
|
|
- * @param {@link List}<AttackRecord>
|
|
|
|
|
|
+ * @param records {@link List}<AttackRecord>
|
|
* The added {@link AttackRecord}s .
|
|
* The added {@link AttackRecord}s .
|
|
*/
|
|
*/
|
|
public void insertAttackRecords(List<AttackRecord> records) {
|
|
public void insertAttackRecords(List<AttackRecord> records) {
|
|
@@ -230,19 +235,46 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
|
|
|
try {
|
|
try {
|
|
for (AttackRecord record : records){
|
|
for (AttackRecord record : records){
|
|
- 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());
|
|
|
|
- attackValues.put(AttackEntry.COLUMN_NAME_EXTERNAL_IP, record.getExternalIP());
|
|
|
|
- attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_IP, record.getLocalIP()); // Log Local IP
|
|
|
|
- attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_PORT, record.getLocalPort());
|
|
|
|
- attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_IP, record.getRemoteIP()); // Log Remote IP
|
|
|
|
- attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
|
|
|
|
- attackValues.put(AttackEntry.COLUMN_NAME_INTERNAL_ATTACK, record.getWasInternalAttack());
|
|
|
|
- attackValues.put(AttackEntry.COLUMN_NAME_BSSID, record.getBssid());
|
|
|
|
-
|
|
|
|
- // Inserting Rows
|
|
|
|
- db.insertWithOnConflict(AttackEntry.TABLE_NAME, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
|
|
|
+ this.insertAttackRecordWithOnConflict(record,db);
|
|
|
|
+ }
|
|
|
|
+ db.setTransactionSuccessful();
|
|
|
|
+ } finally {
|
|
|
|
+ db.endTransaction();
|
|
|
|
+ }
|
|
|
|
+ db.close(); // Closing database connection
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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());
|
|
|
|
+ attackValues.put(AttackEntry.COLUMN_NAME_EXTERNAL_IP, record.getExternalIP());
|
|
|
|
+ attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_IP, record.getLocalIP()); // Log Local IP
|
|
|
|
+ attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_PORT, record.getLocalPort());
|
|
|
|
+ attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_IP, record.getRemoteIP()); // Log Remote IP
|
|
|
|
+ attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
|
|
|
|
+ attackValues.put(AttackEntry.COLUMN_NAME_INTERNAL_ATTACK, record.getWasInternalAttack());
|
|
|
|
+ attackValues.put(AttackEntry.COLUMN_NAME_BSSID, record.getBssid());
|
|
|
|
+
|
|
|
|
+ // Inserting Rows
|
|
|
|
+ db.insertWithOnConflict(AttackEntry.TABLE_NAME, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Adds a given {@link SyncRecord}s to the database.
|
|
|
|
+ *
|
|
|
|
+ * @param records {@link List}<AttackRecord>
|
|
|
|
+ * The added {@link SyncRecord}s .
|
|
|
|
+ */
|
|
|
|
+ public void insertSyncRecords(List<SyncRecord> records) {
|
|
|
|
+ //Log.i("DBHelper", "Add Attack Record with id: " + record.getAttack_id());
|
|
|
|
+ SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
+ db.beginTransaction();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ for (SyncRecord record : records){
|
|
|
|
+ this.insertAttackRecordWithOnConflict(record.getAttackRecord(), db);
|
|
|
|
+ this.insertMessageRecordWithOnConflict(record.getMessageRecord(), db);
|
|
}
|
|
}
|
|
db.setTransactionSuccessful();
|
|
db.setTransactionSuccessful();
|
|
} finally {
|
|
} finally {
|
|
@@ -365,19 +397,18 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
/**
|
|
/**
|
|
* Returns a String array with all BSSIDs stored in the database.
|
|
* Returns a String array with all BSSIDs stored in the database.
|
|
*
|
|
*
|
|
- * @return String[] of all recorded BSSIDs.
|
|
|
|
|
|
+ * @return ArrayList<String> of all recorded BSSIDs.
|
|
*/
|
|
*/
|
|
- public synchronized String[] getAllBSSIDS() {
|
|
|
|
- String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME;
|
|
|
|
|
|
+ public synchronized ArrayList<String> getAllBSSIDS() {
|
|
|
|
+ String selectQuery = "SELECT "+NetworkEntry.COLUMN_NAME_BSSID+" FROM " + NetworkEntry.TABLE_NAME;
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
|
- String[] bssidList = new String[cursor.getCount()];
|
|
|
|
- int counter = 0;
|
|
|
|
|
|
+ ArrayList<String> bssidList = new ArrayList<String>();
|
|
// looping through all rows and adding to list
|
|
// looping through all rows and adding to list
|
|
if (cursor.moveToFirst()) {
|
|
if (cursor.moveToFirst()) {
|
|
do {
|
|
do {
|
|
- bssidList[counter] = cursor.getString(0);
|
|
|
|
- counter++;
|
|
|
|
|
|
+ String s = cursor.getString(0);
|
|
|
|
+ bssidList.add(s);
|
|
} while (cursor.moveToNext());
|
|
} while (cursor.moveToNext());
|
|
}
|
|
}
|
|
cursor.close();
|
|
cursor.close();
|
|
@@ -385,6 +416,28 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
return bssidList;
|
|
return bssidList;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Returns all missing network records.
|
|
|
|
+ *
|
|
|
|
+ * @return a list of missing network records.
|
|
|
|
+ */
|
|
|
|
+ public synchronized ArrayList<NetworkRecord> getMissingNetworkRecords(ArrayList<String> otherBSSIDs) {
|
|
|
|
+ String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " N WHERE " + "N."+NetworkEntry.COLUMN_NAME_BSSID + " NOT IN " + otherBSSIDs;
|
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
+ Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
|
+ ArrayList<NetworkRecord> networkInformation = new ArrayList<NetworkRecord>();
|
|
|
|
+ // looping through all rows and adding to list
|
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
|
+ do {
|
|
|
|
+ NetworkRecord record = this.createNetworkRecord(cursor);
|
|
|
|
+ networkInformation.add(record);
|
|
|
|
+ } while (cursor.moveToNext());
|
|
|
|
+ }
|
|
|
|
+ cursor.close();
|
|
|
|
+ db.close();
|
|
|
|
+ return networkInformation;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Determines the number of different attacks in the database.
|
|
* Determines the number of different attacks in the database.
|
|
*
|
|
*
|
|
@@ -705,13 +758,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
// looping through all rows and adding to list
|
|
// looping through all rows and adding to list
|
|
if (cursor.moveToFirst()) {
|
|
if (cursor.moveToFirst()) {
|
|
do {
|
|
do {
|
|
- NetworkRecord record = new NetworkRecord();
|
|
|
|
- record.setBssid(cursor.getString(0));
|
|
|
|
- record.setSsid(cursor.getString(1));
|
|
|
|
- record.setLatitude(Double.parseDouble(cursor.getString(2)));
|
|
|
|
- record.setLongitude(Double.parseDouble(cursor.getString(3)));
|
|
|
|
- record.setAccuracy(Float.parseFloat(cursor.getString(4)));
|
|
|
|
- record.setTimestampLocation(cursor.getLong(5));
|
|
|
|
|
|
+ NetworkRecord record = this.createNetworkRecord(cursor);
|
|
networkInformation.add(record);
|
|
networkInformation.add(record);
|
|
} while (cursor.moveToNext());
|
|
} while (cursor.moveToNext());
|
|
}
|
|
}
|
|
@@ -721,6 +768,17 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
return networkInformation;
|
|
return networkInformation;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private NetworkRecord createNetworkRecord(Cursor cursor){
|
|
|
|
+ NetworkRecord record = new NetworkRecord();
|
|
|
|
+ record.setBssid(cursor.getString(0));
|
|
|
|
+ record.setSsid(cursor.getString(1));
|
|
|
|
+ record.setLatitude(Double.parseDouble(cursor.getString(2)));
|
|
|
|
+ record.setLongitude(Double.parseDouble(cursor.getString(3)));
|
|
|
|
+ record.setAccuracy(Float.parseFloat(cursor.getString(4)));
|
|
|
|
+ record.setTimestampLocation(cursor.getLong(5));
|
|
|
|
+ return record;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Updates the network table with the information contained in the parameter.
|
|
* Updates the network table with the information contained in the parameter.
|
|
* @param networkInformation ArrayList of {@link NetworkRecord NetworkRecords}
|
|
* @param networkInformation ArrayList of {@link NetworkRecord NetworkRecords}
|
|
@@ -818,12 +876,32 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
|
|
|
db.close();
|
|
db.close();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public void updateSyncDevices(ArrayList<SyncDevice> devices){
|
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
+ db.beginTransaction();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ for(SyncDevice device : devices){
|
|
|
|
+ ContentValues deviceValues = new ContentValues();
|
|
|
|
+ deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, device.getDeviceID());
|
|
|
|
+ deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, device.getLast_sync_timestamp());
|
|
|
|
+ deviceValues.put(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID, device.getHighest_attack_id());
|
|
|
|
+ db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
|
+ }
|
|
|
|
+ db.setTransactionSuccessful();
|
|
|
|
+ } finally {
|
|
|
|
+ db.endTransaction();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ db.close();
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
* Returns a HashMap of all devices that were previously synchronized with.
|
|
* Returns a HashMap of all devices that were previously synchronized with.
|
|
* @return HashMap containing device id's and the last synchronization timestamp.
|
|
* @return HashMap containing device id's and the last synchronization timestamp.
|
|
*/
|
|
*/
|
|
- public synchronized HashMap<String, Long> getSyncDevices(){
|
|
|
|
|
|
+ public synchronized HashMap<String, Long> getSyncDeviceHashMap(){
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
HashMap<String, Long> devices = new HashMap<String, Long>();
|
|
HashMap<String, Long> devices = new HashMap<String, Long>();
|
|
|
|
|
|
@@ -839,6 +917,28 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
db.close();
|
|
db.close();
|
|
return devices;
|
|
return devices;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Returns a HashMap of all devices that were previously synchronized with.
|
|
|
|
+ * @return HashMap containing device id's and the last synchronization timestamp.
|
|
|
|
+ */
|
|
|
|
+ public synchronized ArrayList<SyncDevice> getSyncDevices(){
|
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
+ ArrayList<SyncDevice> devices = new ArrayList<SyncDevice>();
|
|
|
|
+
|
|
|
|
+ String query = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
|
|
|
|
+ Cursor cursor = db.rawQuery(query, null);
|
|
|
|
+
|
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
|
+ do {
|
|
|
|
+ SyncDevice device = this.createSyncDevice(cursor);
|
|
|
|
+ devices.add(device);
|
|
|
|
+ } while (cursor.moveToNext());
|
|
|
|
+ }
|
|
|
|
+ cursor.close();
|
|
|
|
+ db.close();
|
|
|
|
+ return devices;
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
* Returns a ArrayList containing all information stored in the SyncInfo table.
|
|
* Returns a ArrayList containing all information stored in the SyncInfo table.
|
|
@@ -973,11 +1073,11 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Creates a {@link Record} from a Cursor. If the cursor does not show to a
|
|
|
|
|
|
+ * Creates a {@link MessageRecord} from a Cursor. If the cursor does not show to a
|
|
* valid data structure a runtime exception is thrown.
|
|
* valid data structure a runtime exception is thrown.
|
|
*
|
|
*
|
|
* @param cursor
|
|
* @param cursor
|
|
- * @return Returns the created {@link Record} .
|
|
|
|
|
|
+ * @return Returns the created {@link MessageRecord} .
|
|
*/
|
|
*/
|
|
private synchronized MessageRecord createMessageRecord(Cursor cursor) {
|
|
private synchronized MessageRecord createMessageRecord(Cursor cursor) {
|
|
MessageRecord record = new MessageRecord();
|
|
MessageRecord record = new MessageRecord();
|
|
@@ -995,7 +1095,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
* valid data structure a runtime exception is thrown.
|
|
* valid data structure a runtime exception is thrown.
|
|
*
|
|
*
|
|
* @param cursor
|
|
* @param cursor
|
|
- * @return Returns the created {@link Record} .
|
|
|
|
|
|
+ * @return Returns the created {@link AttackRecord} .
|
|
*/
|
|
*/
|
|
private synchronized AttackRecord createAttackRecord(Cursor cursor) {
|
|
private synchronized AttackRecord createAttackRecord(Cursor cursor) {
|
|
AttackRecord record = new AttackRecord();
|
|
AttackRecord record = new AttackRecord();
|
|
@@ -1012,6 +1112,36 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
record.setDevice(cursor.getString(10));
|
|
record.setDevice(cursor.getString(10));
|
|
return record;
|
|
return record;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Creates a {@link SyncRecord} from a Cursor. If the cursor does not show to a
|
|
|
|
+ * valid data structure a runtime exception is thrown.
|
|
|
|
+ *
|
|
|
|
+ * @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));
|
|
|
|
+
|
|
|
|
+ record.setId(Integer.parseInt(cursor.getString(11)));
|
|
|
|
+ //record.setAttack_id(cursor.getLong(12));
|
|
|
|
+ record.setType(MessageRecord.TYPE.valueOf(cursor.getString(13)));
|
|
|
|
+ record.setTimestamp(cursor.getLong(14));
|
|
|
|
+ record.setPacket(cursor.getString(15));
|
|
|
|
+
|
|
|
|
+ return record;
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
* Creates a {@link Record} from a Cursor. If the cursor does not show to a
|
|
* Creates a {@link Record} from a Cursor. If the cursor does not show to a
|
|
@@ -1562,25 +1692,74 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
return recordList;
|
|
return recordList;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Returns all device ids.
|
|
|
|
+ * @return list of all device ids.
|
|
|
|
+ */
|
|
|
|
+ public ArrayList<String> getAllDevicesIds(){
|
|
|
|
+
|
|
|
|
+ String selectQuery = "SELECT "+ SyncDeviceEntry.COLUMN_NAME_DEVICE_ID+" FROM " + SyncDeviceEntry.TABLE_NAME;
|
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
+ Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
|
+
|
|
|
|
+ ArrayList<String> ids = new ArrayList<String>();
|
|
|
|
+ // looping through all rows and adding to list
|
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
|
+ do {
|
|
|
|
+ String s = cursor.getString(0);
|
|
|
|
+ ids.add(s);
|
|
|
|
+ } while (cursor.moveToNext());
|
|
|
|
+ }
|
|
|
|
+ cursor.close();
|
|
|
|
+
|
|
|
|
+ // return record list
|
|
|
|
+ db.close();
|
|
|
|
+ return ids;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /***
|
|
|
|
+ * Returns all missing devices ids
|
|
|
|
+ * @param devices owned device ids
|
|
|
|
+ * @return list of missing devices ids
|
|
|
|
+ */
|
|
|
|
+ public ArrayList<String> getMissingDeviceIds(ArrayList<String> devices){
|
|
|
|
+ ArrayList<String> ids = new ArrayList<String>();
|
|
|
|
+ String selectQuery = "SELECT "+ SyncDeviceEntry.COLUMN_NAME_DEVICE_ID +" FROM " + SyncDeviceEntry.TABLE_NAME + " D WHERE D." + SyncDeviceEntry.COLUMN_NAME_DEVICE_ID + " NOT IN " + devices;
|
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
+ Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
|
+
|
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
|
+ do {
|
|
|
|
+ String deviceID = cursor.getString(0);
|
|
|
|
+ ids.add(deviceID);
|
|
|
|
+ } while (cursor.moveToNext());
|
|
|
|
+ }
|
|
|
|
+ cursor.close();
|
|
|
|
+
|
|
|
|
+ db.close();
|
|
|
|
+ return ids;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Returns all new {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s for the given devices (including all missing devices).
|
|
* Returns all new {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s for the given devices (including all missing devices).
|
|
* @param devices {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}
|
|
* @param devices {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}
|
|
* @param includeMissingDevices boolean
|
|
* @param includeMissingDevices boolean
|
|
* @return list of {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s
|
|
* @return list of {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s
|
|
*/
|
|
*/
|
|
- public ArrayList<AttackRecord> getUnsyncedAttacksFor(List<SyncDevice> devices, boolean includeMissingDevices){
|
|
|
|
|
|
+ public ArrayList<SyncRecord> getUnsyncedAttacksFor(List<SyncDevice> devices, boolean includeMissingDevices){
|
|
|
|
|
|
ArrayList<SyncDevice> updatedDevices = this.getUpdatedDevicesFor(devices, includeMissingDevices);
|
|
ArrayList<SyncDevice> updatedDevices = this.getUpdatedDevicesFor(devices, includeMissingDevices);
|
|
|
|
|
|
- ArrayList<AttackRecord> recordList = new ArrayList<AttackRecord>();
|
|
|
|
|
|
+ ArrayList<SyncRecord> recordList = new ArrayList<SyncRecord>();
|
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
|
for (SyncDevice sDevice : updatedDevices){
|
|
for (SyncDevice sDevice : updatedDevices){
|
|
- String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A "
|
|
|
|
|
|
+ String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A " + " NATURAL JOIN " + PacketEntry.TABLE_NAME + " P "
|
|
+ " WHERE "
|
|
+ " WHERE "
|
|
+" ( "
|
|
+" ( "
|
|
- + " A." + AttackEntry.COLUMN_NAME_DEVICE + " = " + "'" + sDevice.getDeviceID() + "'"
|
|
|
|
|
|
+ + "A." + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + " P."+ PacketEntry.COLUMN_NAME_ATTACK_ID
|
|
|
|
+ + " AND " + " A." + AttackEntry.COLUMN_NAME_DEVICE + " = " + "'" + sDevice.getDeviceID() + "'"
|
|
+ " AND " + " A." + AttackEntry.COLUMN_NAME_SYNC_ID + " > " + sDevice.getHighest_attack_id()
|
|
+ " AND " + " A." + AttackEntry.COLUMN_NAME_SYNC_ID + " > " + sDevice.getHighest_attack_id()
|
|
+ " ) "
|
|
+ " ) "
|
|
//+ " GROUP BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_DEVICE
|
|
//+ " GROUP BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_DEVICE
|
|
@@ -1591,7 +1770,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
if (cursor != null){
|
|
if (cursor != null){
|
|
if (cursor.moveToFirst()) {
|
|
if (cursor.moveToFirst()) {
|
|
do {
|
|
do {
|
|
- AttackRecord record = createAttackRecord(cursor);
|
|
|
|
|
|
+ SyncRecord record = createSyncRecord(cursor);
|
|
recordList.add(record);
|
|
recordList.add(record);
|
|
} while (cursor.moveToNext());
|
|
} while (cursor.moveToNext());
|
|
}
|
|
}
|