123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028 |
- 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.CursorIndexOutOfBoundsException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- 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.SyncDevice;
- import de.tudarmstadt.informatik.hostage.logging.SyncInfo;
- import de.tudarmstadt.informatik.hostage.logging.SyncInfoRecord;
- 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.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.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.
- * @author Mihai Plasoianu
- * @author Lars Pandikow
- *
- */
- public class HostageDBOpenHelper extends SQLiteOpenHelper {
- private static final String DATABASE_NAME = "hostage.db";
- private static final int DATABASE_VERSION = 3;
- 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,");
- 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();
- // 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,");
- 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_INTERNAL_ATTACK).append(" INTEGER,");
- attackSQLBuilder.append(AttackEntry.COLUMN_NAME_BSSID).append(" TEXT,");
- 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_BSSID, NetworkEntry.TABLE_NAME,
- NetworkEntry.COLUMN_NAME_BSSID));
- 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(")");
- 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,");
- 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();
- // 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(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID).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,");
- 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();
- // 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,");
- 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_PROFILE_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_PROFILE_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_PROFILE_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_SYNC_DEVICES_ENTRIES);
- db.execSQL(SQL_CREATE_NETWORK_ENTRIES);
- db.execSQL(SQL_CREATE_ATTACK_ENTRIES);
- db.execSQL(SQL_CREATE_PACKET_ENTRIES);
- db.execSQL(SQL_CREATE_PROFILE_ENTRIES);
- db.execSQL(SQL_CREATE_SYNC_INFO_ENTRIES);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL(SQL_DELETE_SYNC_DEVICES_ENTRIES);
- db.execSQL(SQL_DELETE_SYNC_INFO_ENTRIES);
- db.execSQL(SQL_DELETE_PACKET_ENTRIES);
- db.execSQL(SQL_DELETE_ATTACK_ENTRIES);
- db.execSQL(SQL_DELETE_PROFILE_ENTRIES);
- db.execSQL(SQL_DELETE_NETWORK_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();
- this.insertMessageRecordWithOnConflict(record, db);
- 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.
- *
- * @param record
- * The added {@link AttackRecord} .
- */
- public void addAttackRecord(AttackRecord record) {
- //Log.i("DBHelper", "Add Attack Record with id: " + record.getAttack_id());
- SQLiteDatabase db = this.getWritableDatabase();
- this.insertAttackRecordWithOnConflict(record,db);
- db.close(); // Closing database connection
- }
- /**
- * Adds a given {@link AttackRecord}s to the database.
- *
- * @param records {@link 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){
- 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();
- } finally {
- db.endTransaction();
- }
- this.updateSyncDevicesMaxID(db);
- db.close(); // Closing database connection
- }
- /**
- * Updates the sync devices max sync id.
- */
- public void updateSyncDevicesMaxID(SQLiteDatabase db){
- HashMap<String, Long> deviceIDmap = new HashMap<String, Long>();
- String selectQuery = "SELECT "+AttackEntry.COLUMN_NAME_DEVICE+ ","+ AttackEntry.COLUMN_NAME_SYNC_ID+" FROM " + AttackEntry.TABLE_NAME + " A " + " GROUP BY " + AttackEntry.COLUMN_NAME_DEVICE + " HAVING " + AttackEntry.COLUMN_NAME_SYNC_ID + " = MAX( " + AttackEntry.COLUMN_NAME_SYNC_ID + " )";
- {
- Cursor cursor = db.rawQuery(selectQuery, null);
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- String device_id = cursor.getString(0);
- long sync_id = cursor.getLong(1);
- deviceIDmap.put(device_id, sync_id);
- } while (cursor.moveToNext());
- }
- cursor.close();
- }
- {
- ArrayList<SyncDevice> allDevices = this.getSyncDevices();
- for (SyncDevice device : allDevices){
- long sync_id = deviceIDmap.get(device.getDeviceID());
- device.setHighest_attack_id(sync_id);
- }
- this.updateSyncDevices(allDevices);
- }
- }
-
- public void updateSyncAttackCounter(AttackRecord record){
-
- SQLiteDatabase db = this.getWritableDatabase();
- //String mac = HelperUtils.getMacAdress(context);
- SyncDevice currentDevice = SyncDevice.currentDevice();
- ContentValues syncDeviceValues = new ContentValues();
- syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, currentDevice.getDeviceID());
- syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, System.currentTimeMillis());
- syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID, record.getAttack_id());
- //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++; }
-
- //Log.i("DBHelper", "Update number of attack: " + 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 synchronized 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 synchronized 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 synchronized 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 ArrayList<String> of all recorded BSSIDs.
- */
- public synchronized ArrayList<String> getAllBSSIDS() {
- String selectQuery = "SELECT "+NetworkEntry.COLUMN_NAME_BSSID+" FROM " + NetworkEntry.TABLE_NAME;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- ArrayList<String> bssidList = new ArrayList<String>();
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- String s = cursor.getString(0);
- bssidList.add(s);
- } while (cursor.moveToNext());
- }
- cursor.close();
- db.close();
- return bssidList;
- }
- /**
- * Returns all missing network records.
- *
- * @return a list of missing network records.
- */
- public synchronized ArrayList<NetworkRecord> getMissingNetworkRecords(ArrayList<String> otherBSSIDs) {
- String prefix = " WHERE " + "N."+NetworkEntry.COLUMN_NAME_BSSID + " NOT IN ";
- String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " N " + this.arrayToSQLString(otherBSSIDs, prefix);
- 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;
- }
- private String arrayToSQLString(ArrayList<String> a, String prefix)
- {
- String sql = "";
- if (a.size() !=0){
- sql = sql + prefix;
- sql = sql + " ( ";
- int i = 0;
- for (String s : a){
- i++;
- sql = sql + "'" +s + "'";
- if (i != a.size()){
- sql = sql + ",";
- }
- }
- sql = sql + " ) ";
- }
- return sql;
- }
- /**
- * Determines the number of different attacks in the database.
- *
- * @return The number of different attacks in the database.
- */
- public synchronized 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 synchronized 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 synchronized 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 synchronized 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 synchronized 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 synchronized 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 synchronized 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 synchronized 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 synchronized 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 synchronized 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 synchronized 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 synchronized 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 synchronized 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 synchronized 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;
- }
-
- /**
- * Gets all network related data stored in the database
- * @return An ArrayList with an Network for all Entry in the network table.
- */
- public synchronized 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 = this.createNetworkRecord(cursor);
- networkInformation.add(record);
- } while (cursor.moveToNext());
- }
- cursor.close();
- db.close();
- 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.
- * @param networkInformation ArrayList of {@link NetworkRecord NetworkRecords}
- * @see {@link HostageDBOpenHelper#updateNetworkInformation(NetworkRecord record)}
- */
- public void updateNetworkInformation(ArrayList<NetworkRecord> networkInformation) {
- 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();
- }
- /**
- * Updated the network table with a new {@link NetworkRecord}.
- * If information about this BSSID are already in the database,
- * the table will only be updated if the new {@link NetworkRecord }
- * has a newer location time stamp.
- * @param record The new {@link NetworkRecord}.
- */
- 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 deviceID The Device id
- * @param timestamp The synchronization timestamp
- */
- public void updateTimestampOfSyncDevice(String deviceID, long timestamp){
- SQLiteDatabase db = this.getReadableDatabase();
- ContentValues deviceValues = new ContentValues();
- deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, deviceID);
- 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();
- 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();
- }
- 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 the own state containing all registered devices ids and their max sync_id
- * @return {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}
- */
- public SyncInfo getOwnState(){
- ArrayList<SyncDevice> devices = this.getSyncDevices();
- HashMap<String, Long> deviceMap = new HashMap<String, Long>();
- for (SyncDevice device : devices){
- deviceMap.put(device.getDeviceID(), device.getHighest_attack_id());
- }
- SyncInfo syncInfo = new SyncInfo();
- syncInfo.deviceMap = deviceMap;
- syncInfo.bssids = this.getAllBSSIDS();
- return syncInfo;
- }
-
- /**
- * Returns a HashMap of all devices that were previously synchronized with.
- * @return HashMap containing device id's and the last synchronization timestamp.
- */
- public synchronized HashMap<String, Long> getSyncDeviceHashMap(){
- 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 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.
- * @return ArrayList<SyncInfo>
- */
- public synchronized 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;
- }
-
-
-
- /**
- * Updates the sync_info table with the information contained in the parameter.
- * @param syncInfos ArrayList of {@link SyncInfoRecord SyncInfoRecords}
- * @see {@link HostageDBOpenHelper#updateSyncInfo(SyncInfoRecord syncInfo)}
- */
- 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();
- }
-
- /**
- * Updated the network table with a new {@link SyncInfoRecord}.
- * Conflicting rows will be replaced.
- * @param syncInfo The new {@link NetworkRecord}.
- */
- public synchronized 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 de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncDeviceEntry} and also all data captured by this device in {@link SyncInfoEntry}
- */
- 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}s and {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}.
- */
- 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}s with a specific BSSID.
- *
- * @param bssid
- * The BSSID to match against.
- */
- public synchronized 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 de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEntry}s with a time stamp smaller
- * then the given
- *
- * @param date
- * A Date represented in milliseconds.
- */
- public synchronized 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();
- }
-
- /**
- * Deletes all {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord} with a specific Attack ID.
- *
- * @param attackID
- * The Attack ID to match against.
- */
- public synchronized void deleteByAttackID(long attackID) {
- SQLiteDatabase db = this.getReadableDatabase();
- db.delete(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_ATTACK_ID + " = ?", new String[] { String.valueOf(attackID) });
- db.delete(PacketEntry.TABLE_NAME, PacketEntry.COLUMN_NAME_ATTACK_ID + " = ?", new String[] { String.valueOf(attackID) });
- db.close();
- }
- /**
- * Creates a {@link MessageRecord} 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 MessageRecord} .
- */
- private synchronized 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 AttackRecord} .
- */
- private synchronized 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.setWasInternalAttack(cursor.getInt(7) == 1);
- record.setBssid(cursor.getString(8));
- record.setSync_id(cursor.getLong(9));
- record.setDevice(cursor.getString(10));
- 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
- * valid data structure a runtime exception is thrown.
- *
- * @param cursor
- * @return Returns the created {@link Record} .
- */
- private synchronized 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.setWasInternalAttack(Integer.parseInt(cursor.getString(11)) == 1);
- record.setBssid(cursor.getString(12));
- record.setSsid(cursor.getString(15));
- record.setLatitude(Double.parseDouble(cursor.getString(16)));
- record.setLongitude(Double.parseDouble(cursor.getString(17)));
- record.setAccuracy(Float.parseFloat(cursor.getString(18)));
- record.setTimestampLocation(cursor.getLong(19));
- 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 synchronized ArrayList<Record> getRecordsForFilter(LogFilter filter) {
- ArrayList<Record> recordList = new ArrayList<Record>();
- String selectQuery = this.selectionQueryFromFilter(filter, "*");
- 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 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.
- *
- * @return A arraylist with all {@link Record Records}s for an attack id.
- */
- public synchronized 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 synchronized 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 synchronized 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 synchronized 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);
- Log.i("Database", "Start loop");
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- Log.i("Database", "Add Record");
- 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 synchronized 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 synchronized ArrayList<String> getUniqueESSIDRecords() {
- return this.getUniqueDataEntryForKeyType(NetworkEntry.COLUMN_NAME_SSID, NetworkEntry.TABLE_NAME);
- }
- public synchronized ArrayList<String> getUniqueESSIDRecordsForProtocol(String protocol) {
- return this.getUniqueIDForProtocol(NetworkEntry.COLUMN_NAME_SSID, protocol);
- }
- public synchronized ArrayList<String> getUniqueBSSIDRecordsForProtocol(String protocol) {
- return this.getUniqueIDForProtocol(NetworkEntry.COLUMN_NAME_BSSID, protocol);
- }
- private synchronized 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 synchronized 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 synchronized 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 synchronized 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 synchronized 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 synchronized 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 synchronized 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;
- }
- /**
- * 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;
- }
- /**
- * Creates a {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice} from a Cursor. If the cursor does not show to a
- * valid data structure a runtime exception is thrown.
- *
- * @param cursor the cursor
- * @return Returns the created {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice} .
- */
- private synchronized SyncDevice createSyncDevice(Cursor cursor) {
- SyncDevice record = new SyncDevice();
- record.setDeviceID(cursor.getString(0));
- record.setLast_sync_timestamp(cursor.getLong(1));
- record.setHighest_attack_id(cursor.getLong(2));
- return record;
- }
- /**
- * Returns all missing / newly inserted and updated {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s.
- * @param oldDevices array of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
- * @param includeMissing boolean
- * @return array of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
- */
- public ArrayList<SyncDevice> getUpdatedDevicesFor(List<SyncDevice> oldDevices, boolean includeMissing){
- HashMap<String, Long> oldDeviceMap = new HashMap<String, Long>();
- for (SyncDevice d : oldDevices){
- oldDeviceMap.put(d.getDeviceID(),d.getHighest_attack_id());
- }
- ArrayList<SyncDevice> recordList = new ArrayList<SyncDevice>();
- String selectQuery = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- SyncDevice record = createSyncDevice(cursor);
- // Adding record to list
- if (oldDeviceMap.containsKey(record.getDeviceID())){
- Long oldSyncId = oldDeviceMap.get(record.getDeviceID());
- if (oldSyncId < record.getHighest_attack_id()){
- recordList.add(record);
- }
- } else {
- if (includeMissing)
- recordList.add(record);
- }
- } while (cursor.moveToNext());
- }
- cursor.close();
- // return record list
- db.close();
- return recordList;
- }
- /**
- * Returns all missing / newly inserted and updated {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s.
- * @param oldDeviceMap map with device id and max sync_id for the device
- * @param includeMissing boolean
- * @return array of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
- */
- public ArrayList<SyncDevice> getUpdatedDevicesFor(HashMap<String, Long> oldDeviceMap, boolean includeMissing){
- ArrayList<SyncDevice> recordList = new ArrayList<SyncDevice>();
- String selectQuery = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- SyncDevice record = createSyncDevice(cursor);
- // Adding record to list
- if (oldDeviceMap.containsKey(record.getDeviceID())){
- Long oldSyncId = oldDeviceMap.get(record.getDeviceID());
- if (oldSyncId < record.getHighest_attack_id()){
- recordList.add(record);
- }
- } else {
- if (includeMissing)
- recordList.add(record);
- }
- } while (cursor.moveToNext());
- }
- cursor.close();
- // return record list
- db.close();
- 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 prefix = " D WHERE " + SyncDeviceEntry.COLUMN_NAME_DEVICE_ID + " NOT IN ";
- String selectQuery = "SELECT "+ SyncDeviceEntry.COLUMN_NAME_DEVICE_ID +" FROM " + SyncDeviceEntry.TABLE_NAME + this.arrayToSQLString(devices,prefix);
- 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).
- * @param devices {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}
- * @param includeMissingDevices boolean
- * @return list of {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s
- */
- public ArrayList<SyncRecord> getUnsyncedAttacksFor(List<SyncDevice> devices, boolean includeMissingDevices){
- ArrayList<SyncDevice> updatedDevices = this.getUpdatedDevicesFor(devices, includeMissingDevices);
- ArrayList<SyncRecord> recordList = new ArrayList<SyncRecord>();
- SQLiteDatabase db = this.getReadableDatabase();
- for (SyncDevice sDevice : updatedDevices){
- String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A " + " NATURAL JOIN " + PacketEntry.TABLE_NAME + " P "
- + " WHERE "
- +" ( "
- + "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()
- + " ) "
- //+ " GROUP BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_DEVICE
- + " ORDER BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_SYNC_ID + " DESC ";
- Cursor cursor = db.rawQuery(selectQuery, null);
- // looping through all rows and adding to list
- if (cursor != null){
- if (cursor.moveToFirst()) {
- do {
- SyncRecord record = createSyncRecord(cursor);
- recordList.add(record);
- } while (cursor.moveToNext());
- }
- cursor.close();
- }
- }
- // return record list
- db.close();
- return recordList;
- }
- /**
- * Returns all new {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s for the given devices (including all missing devices).
- * @param deviceMap map of device id and max sync_id of it
- * @param includeMissingDevices boolean
- * @return list of {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s
- */
- public ArrayList<SyncRecord> getUnsyncedAttacksFor(HashMap<String,Long> deviceMap, boolean includeMissingDevices){
- ArrayList<SyncDevice> updatedDevices = this.getUpdatedDevicesFor(deviceMap, includeMissingDevices);
- ArrayList<SyncRecord> recordList = new ArrayList<SyncRecord>();
- SQLiteDatabase db = this.getReadableDatabase();
- for (SyncDevice sDevice : updatedDevices){
- String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A " + " NATURAL JOIN " + PacketEntry.TABLE_NAME + " P "
- + " WHERE "
- +" ( "
- + "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()
- + " ) "
- //+ " GROUP BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_DEVICE
- + " ORDER BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_SYNC_ID + " DESC ";
- Cursor cursor = db.rawQuery(selectQuery, null);
- // looping through all rows and adding to list
- if (cursor != null){
- if (cursor.moveToFirst()) {
- do {
- SyncRecord record = createSyncRecord(cursor);
- recordList.add(record);
- } while (cursor.moveToNext());
- }
- cursor.close();
- }
- }
- // return record list
- db.close();
- return recordList;
- }
- /**
- * 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;
- }
- /**
- * Inserts the given devices in the database with save.
- * @param devices list of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
- */
- public void insertSyncDevices(List<SyncDevice> devices){
- SQLiteDatabase db = this.getWritableDatabase();
- db.beginTransaction();
- try {
- for (SyncDevice device : devices){
- insertSyncDevice(device, db);
- }
- db.setTransactionSuccessful();
- } finally {
- db.endTransaction();
- }
- db.close(); // Closing database connection
- }
- /**
- * Inserts the given dives in the given SQLite Database without save.
- * @param device {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}
- * @param db {@link android.database.sqlite.SQLiteDatabase}
- */
- private void insertSyncDevice(SyncDevice device, SQLiteDatabase db){
- ContentValues recordValues = new ContentValues();
- recordValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, device.getDeviceID());
- recordValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, device.getLast_sync_timestamp());
- recordValues.put(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID, device.getHighest_attack_id());
- // Inserting Rows
- db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, recordValues, SQLiteDatabase.CONFLICT_REPLACE);
- }
- /** Returns the color for the given index
- * @return int color*/
- public Integer getColor(int index) {
- return ColorSequenceGenerator.getColorForIndex(index);
- }
- }
|