|
@@ -25,7 +25,9 @@ import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEnt
|
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.ProfileEntry;
|
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncDeviceEntry;
|
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncInfoEntry;
|
|
|
+import de.tudarmstadt.informatik.hostage.ui.helper.ColorSequenceGenerator;
|
|
|
import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
|
|
|
+import de.tudarmstadt.informatik.hostage.ui.model.PlotComparisonItem;
|
|
|
|
|
|
/**
|
|
|
* Database Helper class to create, read and write the database.
|
|
@@ -40,6 +42,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
private Context context;
|
|
|
|
|
|
static {
|
|
|
+ // NETWORK
|
|
|
StringBuilder networkSQLBuilder = new StringBuilder("CREATE TABLE ").append(NetworkEntry.TABLE_NAME).append("(");
|
|
|
networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_BSSID).append(" TEXT PRIMARY KEY,");
|
|
|
networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_SSID).append(" TEXT,");
|
|
@@ -50,6 +53,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
networkSQLBuilder.append(")");
|
|
|
SQL_CREATE_NETWORK_ENTRIES = networkSQLBuilder.toString();
|
|
|
|
|
|
+ // ATTACK
|
|
|
StringBuilder attackSQLBuilder = new StringBuilder("CREATE TABLE ").append(AttackEntry.TABLE_NAME).append("(");
|
|
|
attackSQLBuilder.append(AttackEntry.COLUMN_NAME_ATTACK_ID).append(" INTEGER PRIMARY KEY,");
|
|
|
attackSQLBuilder.append(AttackEntry.COLUMN_NAME_PROTOCOL).append(" TEXT,");
|
|
@@ -64,6 +68,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
attackSQLBuilder.append(")");
|
|
|
SQL_CREATE_ATTACK_ENTRIES = attackSQLBuilder.toString();
|
|
|
|
|
|
+ // PACKET
|
|
|
StringBuilder packetSQLBuilder = new StringBuilder("CREATE TABLE ").append(PacketEntry.TABLE_NAME).append("(");
|
|
|
packetSQLBuilder.append(PacketEntry.COLUMN_NAME_ID).append(" INTEGER NOT NULL,");
|
|
|
packetSQLBuilder.append(PacketEntry.COLUMN_NAME_ATTACK_ID).append(" INTEGER NOT NULL,");
|
|
@@ -75,13 +80,15 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
AttackEntry.COLUMN_NAME_ATTACK_ID));
|
|
|
packetSQLBuilder.append(")");
|
|
|
SQL_CREATE_PACKET_ENTRIES = packetSQLBuilder.toString();
|
|
|
-
|
|
|
+
|
|
|
+ // SyncDeviceEntry
|
|
|
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_TIMESTAMP).append(" INTEGER");
|
|
|
syncDevicesSQLBuilder.append(")");
|
|
|
SQL_CREATE_SYNC_DEVICES_ENTRIES = syncDevicesSQLBuilder.toString();
|
|
|
-
|
|
|
+
|
|
|
+ // SyncInfoEntry
|
|
|
StringBuilder syncInfoSQLBuilder = new StringBuilder("CREATE TABLE ").append(SyncInfoEntry.TABLE_NAME).append("(");
|
|
|
syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_DEVICE_ID).append(" TEXT,");
|
|
|
syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_BSSID).append(" TEXT,");
|
|
@@ -92,7 +99,8 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
NetworkEntry.COLUMN_NAME_BSSID));
|
|
|
syncInfoSQLBuilder.append(")");
|
|
|
SQL_CREATE_SYNC_INFO_ENTRIES = syncInfoSQLBuilder.toString();
|
|
|
-
|
|
|
+
|
|
|
+ // ProfileEntry
|
|
|
StringBuilder profilSQLBuilder = new StringBuilder("CREATE TABLE ").append(ProfileEntry.TABLE_NAME).append("(");
|
|
|
profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT,");
|
|
|
profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_NAME).append(" TEXT,");
|
|
@@ -779,7 +787,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
|
|
|
/**
|
|
|
* Updates the sync_info table with the information contained in the parameter.
|
|
|
- * @param networkInformation ArrayList of {@link SyncInfoRecord SyncInfoRecords}
|
|
|
+ * @param syncInfo ArrayList of {@link SyncInfoRecord SyncInfoRecords}
|
|
|
* @see {@link HostageDBOpenHelper#updateSyncInfo(SyncInfoRecord syncInfo)}
|
|
|
*/
|
|
|
public synchronized void updateSyncInfo(ArrayList<SyncInfoRecord> syncInfo){
|
|
@@ -806,7 +814,6 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
|
|
|
/**
|
|
|
* Deletes a device with given id from the device {@link SyncDeviceEntry.TABLE_NAME} and also all data captured by this device in {@link SyncInfoEntry.TABLE_NAME}
|
|
|
- * @param device_id The id of the device that is to be deleted.
|
|
|
*/
|
|
|
public void clearSyncInfos(){
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
@@ -947,38 +954,8 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
*/
|
|
|
public synchronized ArrayList<Record> getRecordsForFilter(LogFilter filter) {
|
|
|
ArrayList<Record> recordList = new ArrayList<Record>();
|
|
|
- String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
|
|
|
- + ")";
|
|
|
-
|
|
|
- // TIMESTAMPS
|
|
|
- selectQuery = selectQuery + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
|
|
|
- selectQuery = selectQuery + " < " + filter.getBelowTimestamp();
|
|
|
- selectQuery = selectQuery + " AND " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
|
|
|
- selectQuery = selectQuery + " > " + filter.getAboveTimestamp();
|
|
|
+ String selectQuery = this.selectionQueryFromFilter(filter, "*");
|
|
|
|
|
|
- if (filter.getBSSIDs() != null && filter.getBSSIDs().size() > 0) {
|
|
|
- selectQuery = selectQuery + " AND ";
|
|
|
- selectQuery = selectQuery + filter.getBSSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID);
|
|
|
- }
|
|
|
- if (filter.getESSIDs() != null && filter.getESSIDs().size() > 0) {
|
|
|
- selectQuery = selectQuery + " AND ";
|
|
|
- selectQuery = selectQuery + filter.getESSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_SSID);
|
|
|
- }
|
|
|
- if (filter.getProtocols() != null && filter.getProtocols().size() > 0) {
|
|
|
- selectQuery = selectQuery + " AND ";
|
|
|
- selectQuery = selectQuery + filter.getProtocolsQueryStatement(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_PROTOCOL);
|
|
|
- }
|
|
|
-
|
|
|
- selectQuery = selectQuery + " GROUP BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID;
|
|
|
-
|
|
|
- if (filter.getSorttype() == LogFilter.SortType.packet_timestamp) {
|
|
|
- // DESC
|
|
|
- selectQuery = selectQuery + " ORDER BY " + filter.getSorttype() + " DESC";
|
|
|
- } else {
|
|
|
- selectQuery = selectQuery + " ORDER BY " + filter.getSorttype();
|
|
|
- }
|
|
|
-
|
|
|
- System.out.println(selectQuery);
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
|
|
@@ -996,8 +973,53 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
db.close();
|
|
|
return recordList;
|
|
|
}
|
|
|
-
|
|
|
- /*
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns the query for the given filter.
|
|
|
+ * @param filter (LogFilter)
|
|
|
+ * @param selectionString (String) for everything: "*"
|
|
|
+ * @return (String) query string
|
|
|
+ */
|
|
|
+ public String selectionQueryFromFilter(LogFilter filter, String selectionString)
|
|
|
+ {
|
|
|
+
|
|
|
+ String selectQuery = "SELECT " + selectionString + " FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
|
|
|
+ + ")";
|
|
|
+ if (filter == null) return selectQuery;
|
|
|
+
|
|
|
+ // TIMESTAMPS
|
|
|
+ selectQuery = selectQuery + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
|
|
|
+ selectQuery = selectQuery + " < " + filter.getBelowTimestamp();
|
|
|
+ selectQuery = selectQuery + " AND " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
|
|
|
+ selectQuery = selectQuery + " > " + filter.getAboveTimestamp();
|
|
|
+
|
|
|
+ if (filter.getBSSIDs() != null && filter.getBSSIDs().size() > 0) {
|
|
|
+ selectQuery = selectQuery + " AND ";
|
|
|
+ selectQuery = selectQuery + filter.getBSSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID);
|
|
|
+ }
|
|
|
+ if (filter.getESSIDs() != null && filter.getESSIDs().size() > 0) {
|
|
|
+ selectQuery = selectQuery + " AND ";
|
|
|
+ selectQuery = selectQuery + filter.getESSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_SSID);
|
|
|
+ }
|
|
|
+ if (filter.getProtocols() != null && filter.getProtocols().size() > 0) {
|
|
|
+ selectQuery = selectQuery + " AND ";
|
|
|
+ selectQuery = selectQuery + filter.getProtocolsQueryStatement(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_PROTOCOL);
|
|
|
+ }
|
|
|
+
|
|
|
+ selectQuery = selectQuery + " GROUP BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID;
|
|
|
+
|
|
|
+ if (filter.getSorttype() == LogFilter.SortType.packet_timestamp) {
|
|
|
+ // DESC
|
|
|
+ selectQuery = selectQuery + " ORDER BY " + filter.getSorttype() + " DESC";
|
|
|
+ } else {
|
|
|
+ selectQuery = selectQuery + " ORDER BY " + filter.getSorttype();
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println(selectQuery);
|
|
|
+ return selectQuery;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* Returns the Conversation of a specific attack id
|
|
|
*
|
|
|
* @param attack_id Tha attack id to match the query against.
|
|
@@ -1339,4 +1361,90 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
return recordList;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns PlotComparisionItems for attacks per essid.
|
|
|
+ * @param filter (LogFilter) filter object
|
|
|
+ * @return ArrayList<PlotComparisonItem>
|
|
|
+ */
|
|
|
+ public synchronized ArrayList<PlotComparisonItem> attacksPerESSID(LogFilter filter) {
|
|
|
+
|
|
|
+ String filterQuery = this.selectionQueryFromFilter(filter, AttackEntry.COLUMN_NAME_ATTACK_ID);
|
|
|
+
|
|
|
+ String attackPerESSID_Query = "SELECT " + NetworkEntry.COLUMN_NAME_SSID + " , " + "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_SSID;
|
|
|
+
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
+ Cursor cursor = db.rawQuery(attackPerESSID_Query, null);
|
|
|
+ ArrayList<PlotComparisonItem> plots = new ArrayList<PlotComparisonItem>();
|
|
|
+
|
|
|
+ int counter = 0;
|
|
|
+
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
+ do {
|
|
|
+ String title = cursor.getString(0); // COLUMN_NAME_SSID
|
|
|
+ double value = cursor.getDouble(1); // COUNT
|
|
|
+ if (value == 0.) continue;
|
|
|
+ PlotComparisonItem plotItem = new PlotComparisonItem(title, this.getColor(counter), 0. , value);
|
|
|
+ plots.add(plotItem);
|
|
|
+
|
|
|
+ counter++;
|
|
|
+ } while (cursor.moveToNext());
|
|
|
+ }
|
|
|
+ cursor.close();
|
|
|
+ db.close();
|
|
|
+
|
|
|
+ return plots;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Attacks per BSSID
|
|
|
+ * @param filter (LogFilter) query filter
|
|
|
+ * @return ArrayList<PlotComparisonItem>
|
|
|
+ */
|
|
|
+ public synchronized ArrayList<PlotComparisonItem> attacksPerBSSID(LogFilter filter) {
|
|
|
+
|
|
|
+ String filterQuery = this.selectionQueryFromFilter(filter, AttackEntry.COLUMN_NAME_ATTACK_ID);
|
|
|
+
|
|
|
+ String attackPerBSSID_Query = "SELECT " + 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;
|
|
|
+
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
+ Cursor cursor = db.rawQuery(attackPerBSSID_Query, null);
|
|
|
+ ArrayList<PlotComparisonItem> plots = new ArrayList<PlotComparisonItem>();
|
|
|
+
|
|
|
+ int counter = 0;
|
|
|
+
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
+ do {
|
|
|
+ String title = cursor.getString(0); // COLUMN_NAME_BSSID
|
|
|
+ double value = cursor.getDouble(1); // COUNT
|
|
|
+
|
|
|
+ if (value == 0.) continue;
|
|
|
+ PlotComparisonItem plotItem = new PlotComparisonItem(title, this.getColor(counter), 0. , value);
|
|
|
+ plots.add(plotItem);
|
|
|
+
|
|
|
+ counter++;
|
|
|
+ } while (cursor.moveToNext());
|
|
|
+ }
|
|
|
+ cursor.close();
|
|
|
+ db.close();
|
|
|
+
|
|
|
+ return plots;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /** Returns the color for the given index
|
|
|
+ * @return int color*/
|
|
|
+ public Integer getColor(int index) {
|
|
|
+ return ColorSequenceGenerator.getColorForIndex(index);
|
|
|
+ }
|
|
|
+
|
|
|
}
|