|
@@ -38,7 +38,7 @@ import de.tudarmstadt.informatik.hostage.ui.model.PlotComparisonItem;
|
|
public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
|
|
|
private static final String DATABASE_NAME = "hostage.db";
|
|
private static final String DATABASE_NAME = "hostage.db";
|
|
- private static final int DATABASE_VERSION = 2;
|
|
|
|
|
|
+ private static final int DATABASE_VERSION = 3;
|
|
private Context context;
|
|
private Context context;
|
|
|
|
|
|
static {
|
|
static {
|
|
@@ -64,8 +64,12 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
attackSQLBuilder.append(AttackEntry.COLUMN_NAME_REMOTE_PORT).append(" INTEGER,");
|
|
attackSQLBuilder.append(AttackEntry.COLUMN_NAME_REMOTE_PORT).append(" INTEGER,");
|
|
attackSQLBuilder.append(AttackEntry.COLUMN_NAME_INTERNAL_ATTACK).append(" INTEGER,");
|
|
attackSQLBuilder.append(AttackEntry.COLUMN_NAME_INTERNAL_ATTACK).append(" INTEGER,");
|
|
attackSQLBuilder.append(AttackEntry.COLUMN_NAME_BSSID).append(" TEXT,");
|
|
attackSQLBuilder.append(AttackEntry.COLUMN_NAME_BSSID).append(" TEXT,");
|
|
- attackSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", AttackEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME,
|
|
|
|
|
|
+ attackSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s) ON DELETE CASCADE ON UPDATE CASCADE,", AttackEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME,
|
|
NetworkEntry.COLUMN_NAME_BSSID));
|
|
NetworkEntry.COLUMN_NAME_BSSID));
|
|
|
|
+ attackSQLBuilder.append(AttackEntry.COLUMN_NAME_SYNC_ID).append(" INTEGER,");
|
|
|
|
+ attackSQLBuilder.append(AttackEntry.COLUMN_NAME_DEVICE).append(" TEXT,");
|
|
|
|
+ attackSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s) ON DELETE CASCADE ON UPDATE CASCADE", AttackEntry.COLUMN_NAME_DEVICE, SyncDeviceEntry.TABLE_NAME,
|
|
|
|
+ SyncDeviceEntry.COLUMN_NAME_DEVICE_ID));
|
|
attackSQLBuilder.append(")");
|
|
attackSQLBuilder.append(")");
|
|
SQL_CREATE_ATTACK_ENTRIES = attackSQLBuilder.toString();
|
|
SQL_CREATE_ATTACK_ENTRIES = attackSQLBuilder.toString();
|
|
|
|
|
|
@@ -86,7 +90,8 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
StringBuilder syncDevicesSQLBuilder = new StringBuilder("CREATE TABLE ").append(SyncDeviceEntry.TABLE_NAME).append("(");
|
|
StringBuilder syncDevicesSQLBuilder = new StringBuilder("CREATE TABLE ").append(SyncDeviceEntry.TABLE_NAME).append("(");
|
|
syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID).append(" TEXT PRIMARY KEY,");
|
|
syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID).append(" TEXT PRIMARY KEY,");
|
|
syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP).append(" INTEGER");
|
|
syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP).append(" INTEGER");
|
|
- syncDevicesSQLBuilder.append(")");
|
|
|
|
|
|
+ syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID).append(" INTEGER");
|
|
|
|
+ syncDevicesSQLBuilder.append(")");
|
|
SQL_CREATE_SYNC_DEVICES_ENTRIES = syncDevicesSQLBuilder.toString();
|
|
SQL_CREATE_SYNC_DEVICES_ENTRIES = syncDevicesSQLBuilder.toString();
|
|
|
|
|
|
// SyncInfoEntry
|
|
// SyncInfoEntry
|
|
@@ -177,7 +182,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
|
|
|
/**
|
|
/**
|
|
* Adds a given {@link AttackRecord} to the database.
|
|
* Adds a given {@link AttackRecord} to the database.
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @param record
|
|
* @param record
|
|
* The added {@link AttackRecord} .
|
|
* The added {@link AttackRecord} .
|
|
*/
|
|
*/
|
|
@@ -201,6 +206,42 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
db.insertWithOnConflict(AttackEntry.TABLE_NAME, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
db.insertWithOnConflict(AttackEntry.TABLE_NAME, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
db.close(); // Closing database connection
|
|
db.close(); // Closing database connection
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Adds a given {@link AttackRecord}s to the database.
|
|
|
|
+ *
|
|
|
|
+ * @param List<AttackRecord>
|
|
|
|
+ * The added {@link AttackRecord}s .
|
|
|
|
+ */
|
|
|
|
+ public void insertAttackRecords(List<AttackRecord> records) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //Log.i("DBHelper", "Add Attack Record with id: " + record.getAttack_id());
|
|
|
|
+ SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
+ db.beginTransaction();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ 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);
|
|
|
|
+ }
|
|
|
|
+ db.setTransactionSuccessful();
|
|
|
|
+ } finally {
|
|
|
|
+ db.endTransaction();
|
|
|
|
+ }
|
|
|
|
+ db.close(); // Closing database connection
|
|
|
|
+ }
|
|
|
|
|
|
public void updateSyncAttackCounter(AttackRecord record){
|
|
public void updateSyncAttackCounter(AttackRecord record){
|
|
|
|
|
|
@@ -677,9 +718,32 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
* @see {@link HostageDBOpenHelper#updateNetworkInformation(NetworkRecord record)}
|
|
* @see {@link HostageDBOpenHelper#updateNetworkInformation(NetworkRecord record)}
|
|
*/
|
|
*/
|
|
public void updateNetworkInformation(ArrayList<NetworkRecord> networkInformation) {
|
|
public void updateNetworkInformation(ArrayList<NetworkRecord> networkInformation) {
|
|
- for (NetworkRecord record : networkInformation) {
|
|
|
|
- updateNetworkInformation(record);
|
|
|
|
- }
|
|
|
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
+
|
|
|
|
+ db.beginTransaction();
|
|
|
|
+ try {
|
|
|
|
+ for (NetworkRecord record : networkInformation) {
|
|
|
|
+ String bssid = record.getBssid();
|
|
|
|
+ String bssidQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
|
|
|
|
+ Cursor cursor = db.rawQuery(bssidQuery, new String[] {bssid});
|
|
|
|
+ if (!cursor.moveToFirst() || cursor.getLong(5) < record.getTimestampLocation()){
|
|
|
|
+ ContentValues bssidValues = new ContentValues();
|
|
|
|
+ bssidValues.put(NetworkEntry.COLUMN_NAME_BSSID, bssid);
|
|
|
|
+ bssidValues.put(NetworkEntry.COLUMN_NAME_SSID, record.getSsid());
|
|
|
|
+ bssidValues.put(NetworkEntry.COLUMN_NAME_LATITUDE, record.getLatitude());
|
|
|
|
+ bssidValues.put(NetworkEntry.COLUMN_NAME_LONGITUDE, record.getLongitude());
|
|
|
|
+ bssidValues.put(NetworkEntry.COLUMN_NAME_ACCURACY, record.getAccuracy());
|
|
|
|
+ bssidValues.put(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP, record.getTimestampLocation());
|
|
|
|
+ db.insertWithOnConflict(NetworkEntry.TABLE_NAME, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
|
+ }
|
|
|
|
+ cursor.close();
|
|
|
|
+ }
|
|
|
|
+ db.setTransactionSuccessful();
|
|
|
|
+ } finally {
|
|
|
|
+ db.endTransaction();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ db.close();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -728,13 +792,21 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
* @param devices HashMap of device ids and their synchronization timestamps.
|
|
* @param devices HashMap of device ids and their synchronization timestamps.
|
|
*/
|
|
*/
|
|
public void updateSyncDevices(HashMap<String, Long> devices){
|
|
public void updateSyncDevices(HashMap<String, Long> devices){
|
|
- SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
- for(String key : devices.keySet()){
|
|
|
|
- ContentValues deviceValues = new ContentValues();
|
|
|
|
- deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, key);
|
|
|
|
- deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, devices.get(key));
|
|
|
|
- db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
|
- }
|
|
|
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
+ db.beginTransaction();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ for(String key : devices.keySet()){
|
|
|
|
+ ContentValues deviceValues = new ContentValues();
|
|
|
|
+ deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, key);
|
|
|
|
+ deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, devices.get(key));
|
|
|
|
+ db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
|
+ }
|
|
|
|
+ db.setTransactionSuccessful();
|
|
|
|
+ } finally {
|
|
|
|
+ db.endTransaction();
|
|
|
|
+ }
|
|
|
|
+
|
|
db.close();
|
|
db.close();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -792,10 +864,26 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
* @param syncInfo ArrayList of {@link SyncInfoRecord SyncInfoRecords}
|
|
* @param syncInfo ArrayList of {@link SyncInfoRecord SyncInfoRecords}
|
|
* @see {@link HostageDBOpenHelper#updateSyncInfo(SyncInfoRecord syncInfo)}
|
|
* @see {@link HostageDBOpenHelper#updateSyncInfo(SyncInfoRecord syncInfo)}
|
|
*/
|
|
*/
|
|
- public synchronized void updateSyncInfo(ArrayList<SyncInfoRecord> syncInfo){
|
|
|
|
- for(SyncInfoRecord info : syncInfo){
|
|
|
|
- updateSyncInfo(info);
|
|
|
|
- }
|
|
|
|
|
|
+ public synchronized void updateSyncInfo(ArrayList<SyncInfoRecord> syncInfos){
|
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
+ db.beginTransaction();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ for(SyncInfoRecord syncInfo : syncInfos){
|
|
|
|
+ ContentValues syncValues = new ContentValues();
|
|
|
|
+ syncValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, syncInfo.getBSSID());
|
|
|
|
+ syncValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, syncInfo.getDeviceID());
|
|
|
|
+ syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, syncInfo.getNumber_of_attacks());
|
|
|
|
+ syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, syncInfo.getNumber_of_portscans());
|
|
|
|
+ db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, syncValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
|
+ }
|
|
|
|
+ db.setTransactionSuccessful();
|
|
|
|
+ } finally {
|
|
|
|
+ db.endTransaction();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ db.close();
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -911,7 +999,8 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
record.setRemotePort(Integer.parseInt(cursor.getString(6)));
|
|
record.setRemotePort(Integer.parseInt(cursor.getString(6)));
|
|
record.setWasInternalAttack(cursor.getInt(7) == 1);
|
|
record.setWasInternalAttack(cursor.getInt(7) == 1);
|
|
record.setBssid(cursor.getString(8));
|
|
record.setBssid(cursor.getString(8));
|
|
-
|
|
|
|
|
|
+ record.setSync_id(cursor.getLong(9));
|
|
|
|
+ record.setDevice(cursor.getString(10));
|
|
return record;
|
|
return record;
|
|
}
|
|
}
|
|
|
|
|