1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291 |
- package de.tudarmstadt.informatik.hostage.persistence;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.List;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
- import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
- 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.SyncInfoRecord;
- import de.tudarmstadt.informatik.hostage.logging.MessageRecord.TYPE;
- import de.tudarmstadt.informatik.hostage.model.Profile;
- import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.AttackEntry;
- import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.NetworkEntry;
- import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEntry;
- 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.LogFilter;
- public class HostageDBOpenHelper extends SQLiteOpenHelper {
- private static final String DATABASE_NAME = "hostage.db";
- private static final int DATABASE_VERSION = 1;
- private Context context;
- static {
- 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,");
- networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_LATITUDE).append(" INTEGER,");
- networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_LONGITUDE).append(" INTEGER,");
- networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_ACCURACY).append(" INTEGER,");
- networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP).append(" INTEGER");
- networkSQLBuilder.append(")");
- SQL_CREATE_NETWORK_ENTRIES = networkSQLBuilder.toString();
- 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,");
- attackSQLBuilder.append(AttackEntry.COLUMN_NAME_EXTERNAL_IP).append(" TEXT,");
- attackSQLBuilder.append(AttackEntry.COLUMN_NAME_LOCAL_IP).append(" BLOB,");
- attackSQLBuilder.append(AttackEntry.COLUMN_NAME_LOCAL_PORT).append(" INTEGER,");
- attackSQLBuilder.append(AttackEntry.COLUMN_NAME_REMOTE_IP).append(" BLOB,");
- attackSQLBuilder.append(AttackEntry.COLUMN_NAME_REMOTE_PORT).append(" INTEGER,");
- 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,
- NetworkEntry.COLUMN_NAME_BSSID));
- attackSQLBuilder.append(")");
- SQL_CREATE_ATTACK_ENTRIES = attackSQLBuilder.toString();
- 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,");
- packetSQLBuilder.append(PacketEntry.COLUMN_NAME_TYPE).append(" TEXT,");
- packetSQLBuilder.append(PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP).append(" INTEGER,");
- packetSQLBuilder.append(PacketEntry.COLUMN_NAME_PACKET).append(" TEXT,");
- packetSQLBuilder.append(String.format("PRIMARY KEY(%s,%s)", PacketEntry.COLUMN_NAME_ID, PacketEntry.COLUMN_NAME_ATTACK_ID));
- packetSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", PacketEntry.COLUMN_NAME_ATTACK_ID, AttackEntry.TABLE_NAME,
- AttackEntry.COLUMN_NAME_ATTACK_ID));
- packetSQLBuilder.append(")");
- SQL_CREATE_PACKET_ENTRIES = packetSQLBuilder.toString();
-
- 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();
-
- 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,");
- syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS).append(" INTEGER,");
- syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS).append(" INTEGER,");
- syncInfoSQLBuilder.append(String.format("PRIMARY KEY(%s,%s)", SyncInfoEntry.COLUMN_NAME_DEVICE_ID, SyncInfoEntry.COLUMN_NAME_BSSID));
- syncInfoSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", SyncInfoEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME,
- NetworkEntry.COLUMN_NAME_BSSID));
- syncInfoSQLBuilder.append(")");
- SQL_CREATE_SYNC_INFO_ENTRIES = syncInfoSQLBuilder.toString();
-
- 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,");
- profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_DESCRIPTION ).append(" TEXT,");
- profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ICON).append(" TEXT,");
- profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ICON_NAME).append(" TEXT,");
- profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_EDITABLE).append(" INTEGER,");
- profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ACTIVE).append(" INTEGER");
- profilSQLBuilder.append(")");
- SQL_CREATE_PROFIL_ENTRIES = profilSQLBuilder.toString();
- }
- private static final String SQL_CREATE_NETWORK_ENTRIES;
- private static final String SQL_CREATE_ATTACK_ENTRIES;
- private static final String SQL_CREATE_PACKET_ENTRIES;
- private static final String SQL_CREATE_PROFIL_ENTRIES;
- private static final String SQL_CREATE_SYNC_DEVICES_ENTRIES;
- private static final String SQL_CREATE_SYNC_INFO_ENTRIES;
- private static final String SQL_DELETE_PACKET_ENTRIES = "DROP TABLE IF EXISTS " + PacketEntry.TABLE_NAME;
- private static final String SQL_DELETE_ATTACK_ENTRIES = "DROP TABLE IF EXISTS " + AttackEntry.TABLE_NAME;
- private static final String SQL_DELETE_NETWORK_ENTRIES = "DROP TABLE IF EXISTS " + NetworkEntry.TABLE_NAME;
- private static final String SQL_DELETE_PROFIL_ENTRIES = "DROP TABLE IF EXISTS " + ProfileEntry.TABLE_NAME;
- private static final String SQL_DELETE_SYNC_DEVICES_ENTRIES = "DROP TABLE IF EXISTS " + SyncDeviceEntry.TABLE_NAME;
- private static final String SQL_DELETE_SYNC_INFO_ENTRIES = "DROP TABLE IF EXISTS " + SyncInfoEntry.TABLE_NAME;
- public HostageDBOpenHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- this.context = context;
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(SQL_CREATE_NETWORK_ENTRIES);
- db.execSQL(SQL_CREATE_ATTACK_ENTRIES);
- db.execSQL(SQL_CREATE_PACKET_ENTRIES);
- db.execSQL(SQL_CREATE_PROFIL_ENTRIES);
- db.execSQL(SQL_CREATE_SYNC_DEVICES_ENTRIES);
- db.execSQL(SQL_CREATE_SYNC_INFO_ENTRIES);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL(SQL_DELETE_SYNC_INFO_ENTRIES);
- db.execSQL(SQL_DELETE_PACKET_ENTRIES);
- db.execSQL(SQL_DELETE_ATTACK_ENTRIES);
- db.execSQL(SQL_DELETE_PROFIL_ENTRIES);
- db.execSQL(SQL_DELETE_NETWORK_ENTRIES);
- db.execSQL(SQL_DELETE_SYNC_DEVICES_ENTRIES);
- onCreate(db);
- }
- /**
- * Adds a given {@link MessageRecord} to the database.
- *
- * @param record
- * The added {@link MessageRecord} .
- */
- public void addMessageRecord(MessageRecord record) {
- 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);
- db.close(); // Closing database connection
- }
-
- /**
- * Adds a given {@link AttackRecord} to the database.
- *
- * @param record
- * The added {@link AttackRecord} .
- */
- public void addAttackRecord(AttackRecord record) {
- 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_BSSID, record.getBssid());
-
- // Inserting Rows
- db.insertWithOnConflict(AttackEntry.TABLE_NAME, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
- db.close(); // Closing database connection
- }
-
- public void updateSyncAttackCounter(AttackRecord record){
-
- SQLiteDatabase db = this.getWritableDatabase();
- String mac = HelperUtils.getMacAdress(context);
- ContentValues syncDeviceValues = new ContentValues();
- syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, mac);
- syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, System.currentTimeMillis());
-
- String query = "SELECT * FROM " + SyncInfoEntry.TABLE_NAME +
- " WHERE " + SyncInfoEntry.COLUMN_NAME_DEVICE_ID + " = ? " +
- "AND " + SyncInfoEntry.COLUMN_NAME_BSSID + " = ?";
- Cursor cursor = db.rawQuery(query, new String[] {mac, record.getBssid()});
- long attackCount = 0;
- long portscanCount = 0;
- if (cursor.moveToFirst()){
- attackCount = cursor.getLong(2);
- portscanCount = cursor.getLong(3);
- }
-
- if("PORTSCAN".equals(record.getProtocol())){
- portscanCount++;
- }else { attackCount++; }
-
-
- ContentValues synInfoValues = new ContentValues();
- synInfoValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, record.getBssid());
- synInfoValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, mac);
- synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, attackCount);
- synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, portscanCount);
- // Inserting Rows
- db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, synInfoValues, SQLiteDatabase.CONFLICT_REPLACE);
- db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, syncDeviceValues, SQLiteDatabase.CONFLICT_REPLACE);
- db.close(); // Closing database connection
- }
-
- /**
- * Determines if a network with given BSSID has already been recorded as malicious.
- *
- * @param BSSID
- * The BSSID of the network.
- * @return True if an attack has been recorded in a network with the given
- * BSSID, else false.
- */
- public boolean bssidSeen(String BSSID) {
- String countQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(countQuery, new String[] {BSSID});
- int result = cursor.getCount();
- cursor.close();
- db.close();
- return result > 0;
- }
- /**
- * Determines if an attack has been recorded on a specific protocol in a
- * network with a given BSSID.
- *
- * @param protocol
- * The
- * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
- * Protocol} to inspect.
- * @param BSSID
- * The BSSID of the network.
- * @return True if an attack on the given protocol has been recorded in a
- * network with the given BSSID, else false.
- */
- public boolean bssidSeen(String protocol, String BSSID) {
- if(BSSID == null || protocol == null){
- return false;
- }
- String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " NATURAL JOIN " + NetworkEntry.TABLE_NAME + " WHERE "
- + AttackEntry.COLUMN_NAME_PROTOCOL + " = ? AND " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, BSSID});
- int result = cursor.getCount();
- cursor.close();
- db.close();
- return result > 0;
- }
-
- public int numBssidSeen(String BSSID) {
- String countQuery = "SELECT COUNT(*) FROM " + AttackEntry.TABLE_NAME + " WHERE "
- + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_BSSID + " = " + "'" + BSSID + "'";
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(countQuery, null);
- cursor.moveToFirst();
- int result = cursor.getInt(0);
- cursor.close();
- db.close();
- return result;
- }
- 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 + "'";
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(countQuery, null);
- cursor.moveToFirst();
- int result = cursor.getInt(0);
- cursor.close();
- db.close();
- return result;
- }
- /**
- * Returns a String array with all BSSIDs stored in the database.
- *
- * @return String[] of all recorded BSSIDs.
- */
- public String[] getAllBSSIDS() {
- String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- String[] bssidList = new String[cursor.getCount()];
- int counter = 0;
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- bssidList[counter] = cursor.getString(0);
- counter++;
- } while (cursor.moveToNext());
- }
- cursor.close();
- db.close();
- return bssidList;
- }
- /**
- * Determines the number of different attacks in the database.
- *
- * @return The number of different attacks in the database.
- */
- public int getAttackCount() {
- SQLiteDatabase db = this.getReadableDatabase();
- String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
- " WHERE " + AttackEntry.COLUMN_NAME_PROTOCOL + " <> ?";
- Cursor cursor = db.rawQuery(countQuery, new String[]{"PORTSCAN"});
- int result = cursor.getCount();
- cursor.close();
- // return count
- db.close();
- return result;
- }
-
- /**
- * Determines the number of different recorded attacks in a specific access point since the given attack_id.
- * The given attack_id is not included.
- * @param attack_id The attack id to match the query against.
- * @param bssid The BSSID of the access point.
- * @return The number of different attacks in the database since the given attack_id.
- */
- public int getAttackCount(int attack_id, String bssid) {
- SQLiteDatabase db = this.getReadableDatabase();
- String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
- " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " <> ? " +
- "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? " +
- "AND " + AttackEntry.COLUMN_NAME_BSSID + " = ?";
- String[] selectArgs = new String[]{"PORTSCAN", attack_id + "", bssid};
- Cursor cursor = db.rawQuery(countQuery, selectArgs);
- int result = cursor.getCount();
- cursor.close();
- // return count
- db.close();
- return result;
- }
-
- /**
- * Determines the number of different attacks for a specific protocol in
- * the database.
- *
- * @param protocol
- * The String representation of the
- * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
- * Protocol}
- * @return The number of different attacks in the database.
- */
- public int getAttackPerProtocolCount(String protocol) {
- SQLiteDatabase db = this.getReadableDatabase();
- String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
- " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? ";
- Cursor cursor = db.rawQuery(countQuery, new String[]{protocol});
- int result = cursor.getCount();
- cursor.close();
- // return count
- db.close();
- return result;
- }
-
- /**
- * Determines the number of attacks for a specific protocol in
- * the database since the given attack_id.
- *
- * @param protocol
- * The String representation of the
- * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
- * Protocol}
- * @param attack_id The attack id to match the query against.
- * @return The number of different attacks in the database since the given attack_id.
- */
- public int getAttackPerProtocolCount(String protocol, int attack_id) {
- SQLiteDatabase db = this.getReadableDatabase();
- String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
- " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? " +
- "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? ";
- Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, attack_id + ""});
- int result = cursor.getCount();
- cursor.close();
- // return count
- db.close();
- return result;
- }
-
- /**
- * Determines the number of recorded attacks for a specific protocol and accesss point since the given attack_id.
- *
- * @param protocol
- * The String representation of the
- * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
- * Protocol}
- * @param attack_id The attack id to match the query against.
- * @param bssid The BSSID of the access point.
- * @return The number of different attacks in the database since the given attack_id.
- */
- public int getAttackPerProtocolCount(String protocol, int attack_id, String bssid) {
- SQLiteDatabase db = this.getReadableDatabase();
- String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
- " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? " +
- "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? " +
- "AND " + AttackEntry.COLUMN_NAME_BSSID + " = ?";
- Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, attack_id + "", bssid});
- int result = cursor.getCount();
- cursor.close();
- // return count
- db.close();
- return result;
- }
-
- /**
- * Determines the number of portscans stored in the database.
- *
- * @return The number of portscans stored in the database.
- */
- public int getPortscanCount() {
- return getAttackPerProtocolCount("PORTSCAN");
- }
-
- /**
- * Determines the number of recorded portscans since the given attack_id.
- * @param attack_id The attack id to match the query against.
- * @return The number of portscans stored in the database since the given attack_id.
- */
- public int getPortscanCount(int attack_id) {
- return getAttackPerProtocolCount("PORTSCAN", attack_id);
- }
-
- /**
- * Determines the number of recorded portscans in a specific access point since the given attack_id.
- * @param attack_id The attack id to match the query against.
- * @param bssid The BSSID of the access point.
- * @return The number of portscans stored in the database since the given attack_id.
- */
- public int getPortscanCount(int attack_id, String bssid) {
- return getAttackPerProtocolCount("PORTSCAN", attack_id, bssid);
- }
- /**
- * Determines the number of {@link Record Records} in the database.
- *
- * @return The number of {@link Record Records} in the database.
- */
- public int getRecordCount() {
- String countQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(countQuery, null);
- int result = cursor.getCount();
- cursor.close();
- // return count
- db.close();
- return result;
- }
- //TODO ADD AGAIN ?
- /**
- * Returns the {@link AttackRecord} with the given attack id from the database.
- *
- * @param attack_id
- * The attack id of the {@link Record};
- * @return The {@link Record}.
- */
- /*
- public AttackRecord getRecordOfAttackId(long attack_id) {
- String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " WHERE " + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- AttackRecord record = null;
- if (cursor.moveToFirst()) {
- record = createAttackRecord(cursor);
- }
- cursor.close();
- // return record list
- db.close();
- return record;
- } */
- /**
- * Gets a {@link AttackRecord} for every attack identified by its attack id.
- *
- * @return A ArrayList with a {@link AttackRecord} for each attack id in the Database.
- */
- public ArrayList<AttackRecord> getRecordOfEachAttack() {
- ArrayList<AttackRecord> recordList = new ArrayList<AttackRecord>();
- String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- AttackRecord record = createAttackRecord(cursor);
- // Adding record to list
- recordList.add(record);
- } while (cursor.moveToNext());
- }
- cursor.close();
- // return record list
- db.close();
- return recordList;
- }
- /**
- * Gets a AttackRecord for every attack with a higher attack id than the specified.
- *
- * @param attack_id
- * The attack id to match the query against.
- * @return A ArrayList with one {@link AttackRecord} for each attack id
- * higher than the given.
- */
- public ArrayList<AttackRecord> getRecordOfEachAttack(long attack_id) {
- ArrayList<AttackRecord> recordList = new ArrayList<AttackRecord>();
- String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " WHERE " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > " + attack_id;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- AttackRecord record = createAttackRecord(cursor);
- // Adding record to list
- recordList.add(record);
- } while (cursor.moveToNext());
- }
- cursor.close();
- // return count
- db.close();
- return recordList;
- }
-
- /**
- * Determines the highest attack id stored in the database.
- *
- * @return The highest attack id stored in the database.
- */
- public long getHighestAttackId() {
- String selectQuery = "SELECT MAX(" + AttackEntry.COLUMN_NAME_ATTACK_ID + ") FROM " + AttackEntry.TABLE_NAME;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- int result;
- if (cursor.moveToFirst()) {
- result = cursor.getInt(0);
- } else {
- result = -1;
- }
- cursor.close();
- db.close();
- return result;
- }
- /**
- * Determines the smallest attack id stored in the database.
- *
- * @return The smallest attack id stored in the database.
- */
- public long getSmallestAttackId() {
- String selectQuery = "SELECT MIN(" + AttackEntry.COLUMN_NAME_ATTACK_ID + ") FROM " + AttackEntry.TABLE_NAME;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- int result;
- if (cursor.moveToFirst()) {
- result = cursor.getInt(0);
- } else {
- result = -1;
- }
- cursor.close();
- db.close();
- return result;
- }
- /**
- * Gets the last recorded SSID to a given BSSID.
- *
- * @param bssid
- * The BSSID to match against.
- * @return A String of the last SSID or null if the BSSID is not in the
- * database.
- */
- public String getSSID(String bssid) {
- String selectQuery = "SELECT " + NetworkEntry.COLUMN_NAME_SSID + " FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID
- + " = " + "'" + bssid + "'";
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- String ssid = null;
- if (cursor.moveToFirst()) {
- ssid = cursor.getString(0);
- }
- cursor.close();
- db.close();
- return ssid;
- }
-
- public ArrayList<NetworkRecord> getNetworkInformation() {
- String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME;
- 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 = 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));
- networkInformation.add(record);
- } while (cursor.moveToNext());
- }
- cursor.close();
- db.close();
- return networkInformation;
- }
- public void updateNetworkInformation(ArrayList<NetworkRecord> networkInformation) {;
- for (NetworkRecord record : networkInformation) {
- updateNetworkInformation(record);
- }
- }
- public void updateNetworkInformation(NetworkRecord record) {
- SQLiteDatabase db = this.getReadableDatabase();
- 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.close();
- }
-
-
- /**
- * Updates the the timestamp of a single device id
- * @param devices The Device id
- * @param timestamp The synchronization timestamp
- */
- public void updateSyncDevice(String devices, long timestamp){
- SQLiteDatabase db = this.getReadableDatabase();
- ContentValues deviceValues = new ContentValues();
- deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, devices);
- deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, timestamp);
- db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
- db.close();
- }
-
- /**
- * Updates the Timestamps of synchronization devices from a HashMap.
- * @param devices HashMap of device ids and their synchronization timestamps.
- */
- 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);
- }
- db.close();
- }
-
- /**
- * Returns a HashMap of all devices that were previously synchronized with.
- * @return HashMap containing device id's and the last synchronization timestamp.
- */
- public HashMap<String, Long> getSyncDevices(){
- SQLiteDatabase db = this.getReadableDatabase();
- HashMap<String, Long> devices = new HashMap<String, Long>();
-
- String query = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
- Cursor cursor = db.rawQuery(query, null);
-
- if (cursor.moveToFirst()) {
- do {
- devices.put(cursor.getString(0), cursor.getLong(1));
- } while (cursor.moveToNext());
- }
- cursor.close();
- db.close();
- return devices;
- }
-
- /**
- * Returns a ArrayList containing all information stored in the SyncInfo table.
- * @return ArrayList<SyncInfo>
- */
- public ArrayList<SyncInfoRecord> getSyncInfo(){
- SQLiteDatabase db = this.getReadableDatabase();
- ArrayList<SyncInfoRecord> syncInfo = new ArrayList<SyncInfoRecord>();
-
- String query = "SELECT * FROM " + SyncInfoEntry.TABLE_NAME;
- Cursor cursor = db.rawQuery(query, null);
-
- if (cursor.moveToFirst()) {
- do {
- SyncInfoRecord info = new SyncInfoRecord();
- info.setDeviceID(cursor.getString(0));
- info.setBSSID(cursor.getString(1));
- info.setNumber_of_attacks(cursor.getLong(2));
- info.setNumber_of_portscans(cursor.getLong(3));
- syncInfo.add(info);
- } while (cursor.moveToNext());
- }
- cursor.close();
- db.close();
- return syncInfo;
- }
-
-
- public void updateSyncInfo(ArrayList<SyncInfoRecord> syncInfo){
- for(SyncInfoRecord info : syncInfo){
- updateSyncInfo(info);
- }
- }
-
- public void updateSyncInfo(SyncInfoRecord syncInfo){
- SQLiteDatabase db = this.getReadableDatabase();
- 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.close();
- }
-
- /**
- * 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();
- db.delete(SyncDeviceEntry.TABLE_NAME, null, null);
- db.delete(SyncInfoEntry.TABLE_NAME, null, null);
- db.close();
- }
-
-
- /**
- * Deletes all records from {@link #PacketEntry.TABLE_NAME}.
- */
- public void clearData() {
- SQLiteDatabase db = this.getReadableDatabase();
- db.delete(PacketEntry.TABLE_NAME, null, null);
- db.delete(AttackEntry.TABLE_NAME, null, null);
- db.close();
- }
- /**
- * Deletes all records from {@link #PacketEntry.TABLE_NAME} with a specific BSSID.
- *
- * @param bssid
- * The BSSID to match against.
- */
- public void deleteByBSSID(String bssid) {
- SQLiteDatabase db = this.getReadableDatabase();
- 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.close();
- }
-
- /**
- * Deletes all records from {@link #PacketEntry.TABLE_NAME} with a time stamp smaller
- * then the given
- *
- * @param date
- * A Date represented in milliseconds.
- */
- public void deleteByDate(long date) {
- SQLiteDatabase db = this.getReadableDatabase();
- String deleteQuery = "DELETE FROM " + PacketEntry.TABLE_NAME + " WHERE " + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP + " < " + date;
- db.execSQL(deleteQuery);
- db.close();
- }
- /**
- * Creates a {@link Record} 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 Record} .
- */
- private MessageRecord createMessageRecord(Cursor cursor) {
- MessageRecord record = new MessageRecord();
- record.setId(Integer.parseInt(cursor.getString(0)));
- record.setAttack_id(cursor.getLong(1));
- record.setType(MessageRecord.TYPE.valueOf(cursor.getString(2)));
- record.setTimestamp(cursor.getLong(3));
- record.setPacket(cursor.getString(4));
- return record;
- }
-
- /**
- * Creates a {@link AttackRecord} 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 Record} .
- */
- private AttackRecord createAttackRecord(Cursor cursor) {
- AttackRecord record = new AttackRecord();
- 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.setBssid(cursor.getString(7));
- return record;
- }
-
- /**
- * Creates a {@link Record} 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 Record} .
- */
- private Record createRecord(Cursor cursor) {
- Record record = new Record();
- record.setId(Integer.parseInt(cursor.getString(0)));
- record.setAttack_id(cursor.getLong(1));
- record.setType(TYPE.valueOf(cursor.getString(2)));
- record.setTimestamp(cursor.getLong(3));
- record.setPacket(cursor.getString(4));
- record.setProtocol(cursor.getString(5));
- record.setExternalIP(cursor.getString(6));
- record.setLocalIP(cursor.getString(7));
- record.setLocalPort(Integer.parseInt(cursor.getString(8)));
- record.setRemoteIP(cursor.getString(9));
- record.setRemotePort(Integer.parseInt(cursor.getString(10)));
- record.setBssid(cursor.getString(11));
- record.setSsid(cursor.getString(12));
- record.setLatitude(Double.parseDouble(cursor.getString(13)));
- record.setLongitude(Double.parseDouble(cursor.getString(14)));
- record.setAccuracy(Float.parseFloat(cursor.getString(15)));
- record.setTimestampLocation(cursor.getLong(16));
- return record;
- }
-
- /**
- * Gets all received {@link Record Records} for the specified information in
- * the LogFilter ordered by date.
- *
- * @return A ArrayList with all received {@link Record Records} for the
- * LogFilter.
- */
- public 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();
- 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);
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- Record record = createRecord(cursor);
- // Adding record to list
- recordList.add(record);
- } while (cursor.moveToNext());
- }
- cursor.close();
- // return record list
- db.close();
- return recordList;
- }
-
- /*
- * Returns the Conversation of a specific attack id
- *
- * @param attack_id Tha attack id to match the query against.
- *
- * @return A arraylist with all {@link Record Records}s for an attack id.
- */
- public ArrayList<Record> getConversationForAttackID(long attack_id) {
- 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
- + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- if (cursor.moveToFirst()) {
- do {
- Record record = createRecord(cursor);
- recordList.add(record);
- } while (cursor.moveToNext());
- }
- cursor.close();
- db.close();
- return recordList;
- }
-
- /**
- * Gets a single {@link Record} with the given attack id from the database.
- *
- * @param attack_id
- * The attack id of the {@link Record};
- * @return The {@link Record}.
- */
- public Record getRecordOfAttackId(long attack_id) {
- String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
- + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id + " GROUP BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ID;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- Record record = null;
- if (cursor.moveToFirst()) {
- record = createRecord(cursor);
- }
- cursor.close();
- // return record list
- db.close();
- return record;
- }
-
- /**
- * Gets a single {@link Record} with the given ID from the database.
- *
- * @param id
- * The ID of the {@link Record};
- * @return The {@link Record}.
- */
- public Record getRecord(int id) {
- String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + PacketEntry.COLUMN_NAME_ATTACK_ID
- + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ID + " = " + id;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- Record record = null;
- if (cursor.moveToFirst()) {
- record = createRecord(cursor);
- }
- cursor.close();
- db.close();
- // return contact
- return record;
- }
-
- /**
- * Gets all {@link Record Records} saved in the database.
- *
- * @return A ArrayList of all the {@link Record Records} in the Database.
- */
- public ArrayList<Record> getAllRecords() {
- ArrayList<Record> recordList = new ArrayList<Record>();
- // Select All Query
- String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
- + ")";
- SQLiteDatabase db = this.getWritableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- Record record = createRecord(cursor);
- // Adding record to list
- recordList.add(record);
- } while (cursor.moveToNext());
- }
- cursor.close();
- db.close();
- // return record list
- return recordList;
- }
-
- /**
- * Gets all non duplicate Records For the key BSSID.
- *
- * @return A ArrayList with received Records.
- */
- public ArrayList<String> getUniqueBSSIDRecords() {
- return this.getUniqueDataEntryForKeyType(NetworkEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME);
- }
- /**
- * Gets all non duplicate Records For the key ESSID.
- *
- * @return A ArrayList with received Records.
- */
- public ArrayList<String> getUniqueESSIDRecords() {
- return this.getUniqueDataEntryForKeyType(NetworkEntry.COLUMN_NAME_SSID, NetworkEntry.TABLE_NAME);
- }
- public ArrayList<String> getUniqueESSIDRecordsForProtocol(String protocol) {
- return this.getUniqueIDForProtocol(NetworkEntry.COLUMN_NAME_SSID, protocol);
- }
- public ArrayList<String> getUniqueBSSIDRecordsForProtocol(String protocol) {
- return this.getUniqueIDForProtocol(NetworkEntry.COLUMN_NAME_BSSID, protocol);
- }
- private ArrayList<String> getUniqueIDForProtocol(String id, String protocol) {
- ArrayList<String> recordList = new ArrayList<String>();
- String selectQuery = "SELECT DISTINCT " + id + " FROM " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID + ") " + " WHERE "
- + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_PROTOCOL + " = " + "'" + protocol + "'" + " ORDER BY " + id; // " NATURAL JOIN "
- // +
- // TABLE_ATTACK_INFO
- // +
- // " NATURAL JOIN "
- // +
- // TABLE_BSSIDS
- // +
- // " NATURAL JOIN "
- // +
- // TABLE_PORTS
- // +
- // ORDERED BY TIME
- System.out.println(selectQuery);
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- String record = cursor.getString(0);
- recordList.add(record);
- } while (cursor.moveToNext());
- }
- cursor.close();
- // return record list
- db.close();
- return recordList;
- }
- /**
- * Gets all non duplicate Data Entry For a specific KeyType ( e.g. BSSIDs).
- *
- * @return A ArrayList with received Records.
- */
- public ArrayList<String> getUniqueDataEntryForKeyType(String keyType, String table) {
- ArrayList<String> recordList = new ArrayList<String>();
- // String selectQuery = "SELECT * FROM " + TABLE_RECORDS +
- // " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " +
- // TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS;
- String selectQuery = "SELECT DISTINCT " + keyType + " FROM " + table + " ORDER BY " + keyType; // " NATURAL JOIN "
- // +
- // TABLE_ATTACK_INFO
- // +
- // " NATURAL JOIN "
- // +
- // TABLE_BSSIDS
- // +
- // " NATURAL JOIN "
- // +
- // TABLE_PORTS
- // +
- // ORDERED BY TIME
- System.out.println(selectQuery);
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- String record = cursor.getString(0);
- recordList.add(record);
- } while (cursor.moveToNext());
- }
- cursor.close();
- // return record list
- db.close();
- return recordList;
- }
-
-
- //TODO PROFILE DATABASE QUERIES - STILL NEEDED?
-
- /**
- * Retrieves all the profiles from the database
- *
- * @return list of profiles
- */
- public List<Profile> getAllProfiles() {
- List<Profile> profiles = new LinkedList<Profile>();
- // Select All Query
- String selectQuery = "SELECT * FROM " + ProfileEntry.TABLE_NAME;
- SQLiteDatabase db = this.getWritableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- Profile profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
- if (cursor.getInt(6) == 1) {
- profile.mActivated = true;
- }
- profile.mIconName = cursor.getString(4);
- // Adding record to list
- profiles.add(profile);
- } while (cursor.moveToNext());
- }
- cursor.close();
- db.close();
- // return record list
- return profiles;
- }
-
- /**
- * Persists the given profile into the database
- *
- * @param profile
- * the profile which should be persisted
- *
- * @return
- */
- public long persistProfile(Profile profile) {
- SQLiteDatabase db = this.getReadableDatabase();
- ContentValues values = new ContentValues();
- if (profile.mId != -1) {
- values.put(ProfileEntry.COLUMN_NAME_PROFILE_ID, profile.mId);
- }
- values.put(ProfileEntry.COLUMN_NAME_PROFILE_NAME, profile.mLabel);
- values.put(ProfileEntry.COLUMN_NAME_PROFILE_DESCRIPTION, profile.mText);
- values.put(ProfileEntry.COLUMN_NAME_PROFILE_ICON, profile.mIconPath);
- values.put(ProfileEntry.COLUMN_NAME_PROFILE_ICON_NAME, profile.mIconName);
- values.put(ProfileEntry.COLUMN_NAME_PROFILE_ACTIVE, profile.mActivated);
- values.put(ProfileEntry.COLUMN_NAME_PROFILE_EDITABLE, profile.mEditable);
- return db.replace(ProfileEntry.TABLE_NAME, null, values);
- }
-
- /**
- * private static final String CREATE_PROFILE_TABLE = "CREATE TABLE " +
- * TABLE_PROFILES + "(" + KEY_PROFILE_ID +
- * " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_PROFILE_NAME + " TEXT," +
- * KEY_PROFILE_DESCRIPTION + " TEXT," + KEY_PROFILE_ICON + " TEXT," +
- * KEY_PROFILE_ICON_ID + " INTEGER," + KEY_PROFILE_EDITABLE + " INTEGER," +
- * KEY_PROFILE_ACTIVE + " INTEGER" + ")";
- */
- public Profile getProfile(int id) {
- String selectQuery = "SELECT * FROM " + ProfileEntry.TABLE_NAME + " WHERE " + ProfileEntry.TABLE_NAME + "." + ProfileEntry.COLUMN_NAME_PROFILE_ID + " = " + id;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- Profile profile = null;
- if (cursor.moveToFirst()) {
- profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
- if (cursor.getInt(6) == 1) {
- profile.mActivated = true;
- }
- profile.mIconName = cursor.getString(5);
- }
- cursor.close();
- db.close();
- // return contact
- return profile;
- }
-
- public void deleteProfile(int id) {
- SQLiteDatabase db = this.getReadableDatabase();
- db.delete(ProfileEntry.TABLE_NAME, ProfileEntry.COLUMN_NAME_PROFILE_ID + "=?", new String[] { String.valueOf(id) });
- }
-
-
- /**
- * Gets all received {@link Record Records} for every attack identified by
- * its attack id and ordered by date.
- *
- * @return A ArrayList with all received {@link Record Records} for each
- * attack id in the Database.
- */
- public ArrayList<Record> getAllReceivedRecordsOfEachAttack() {
- 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
- + ")" + " WHERE " + PacketEntry.COLUMN_NAME_TYPE + "='RECEIVE'" + " ORDER BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- Record record = createRecord(cursor);
- // Adding record to list
- recordList.add(record);
- } while (cursor.moveToNext());
- }
- cursor.close();
- // return record list
- db.close();
- return recordList;
- }
- }
|