HostageDBOpenHelper.java 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  1. package de.tudarmstadt.informatik.hostage.persistence;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import android.content.ContentValues;
  7. import android.content.Context;
  8. import android.database.Cursor;
  9. import android.database.CursorIndexOutOfBoundsException;
  10. import android.database.sqlite.SQLiteDatabase;
  11. import android.database.sqlite.SQLiteOpenHelper;
  12. import android.util.Log;
  13. import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
  14. import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
  15. import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
  16. import de.tudarmstadt.informatik.hostage.logging.Record;
  17. import de.tudarmstadt.informatik.hostage.logging.SyncDevice;
  18. import de.tudarmstadt.informatik.hostage.logging.SyncInfo;
  19. import de.tudarmstadt.informatik.hostage.logging.SyncInfoRecord;
  20. import de.tudarmstadt.informatik.hostage.logging.MessageRecord.TYPE;
  21. import de.tudarmstadt.informatik.hostage.logging.SyncRecord;
  22. import de.tudarmstadt.informatik.hostage.model.Profile;
  23. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.AttackEntry;
  24. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.NetworkEntry;
  25. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEntry;
  26. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.ProfileEntry;
  27. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncDeviceEntry;
  28. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncInfoEntry;
  29. import de.tudarmstadt.informatik.hostage.ui.helper.ColorSequenceGenerator;
  30. import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
  31. import de.tudarmstadt.informatik.hostage.ui.model.PlotComparisonItem;
  32. /**
  33. * Database Helper class to create, read and write the database.
  34. * @author Mihai Plasoianu
  35. * @author Lars Pandikow
  36. *
  37. */
  38. public class HostageDBOpenHelper extends SQLiteOpenHelper {
  39. private static final String DATABASE_NAME = "hostage.db";
  40. private static final int DATABASE_VERSION = 3;
  41. private Context context;
  42. static {
  43. // NETWORK
  44. StringBuilder networkSQLBuilder = new StringBuilder("CREATE TABLE ").append(NetworkEntry.TABLE_NAME).append("(");
  45. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_BSSID).append(" TEXT PRIMARY KEY,");
  46. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_SSID).append(" TEXT,");
  47. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_LATITUDE).append(" INTEGER,");
  48. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_LONGITUDE).append(" INTEGER,");
  49. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_ACCURACY).append(" INTEGER,");
  50. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP).append(" INTEGER");
  51. networkSQLBuilder.append(")");
  52. SQL_CREATE_NETWORK_ENTRIES = networkSQLBuilder.toString();
  53. // ATTACK
  54. StringBuilder attackSQLBuilder = new StringBuilder("CREATE TABLE ").append(AttackEntry.TABLE_NAME).append("(");
  55. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_ATTACK_ID).append(" INTEGER PRIMARY KEY,");
  56. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_PROTOCOL).append(" TEXT,");
  57. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_EXTERNAL_IP).append(" TEXT,");
  58. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_LOCAL_IP).append(" BLOB,");
  59. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_LOCAL_PORT).append(" INTEGER,");
  60. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_REMOTE_IP).append(" BLOB,");
  61. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_REMOTE_PORT).append(" INTEGER,");
  62. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_INTERNAL_ATTACK).append(" INTEGER,");
  63. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_BSSID).append(" TEXT,");
  64. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_SYNC_ID).append(" INTEGER,");
  65. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_DEVICE).append(" TEXT,");
  66. attackSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s) ON DELETE CASCADE ON UPDATE CASCADE,", AttackEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME,
  67. NetworkEntry.COLUMN_NAME_BSSID));
  68. attackSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s) ON DELETE CASCADE ON UPDATE CASCADE", AttackEntry.COLUMN_NAME_DEVICE, SyncDeviceEntry.TABLE_NAME,
  69. SyncDeviceEntry.COLUMN_NAME_DEVICE_ID));
  70. attackSQLBuilder.append(")");
  71. SQL_CREATE_ATTACK_ENTRIES = attackSQLBuilder.toString();
  72. // PACKET
  73. StringBuilder packetSQLBuilder = new StringBuilder("CREATE TABLE ").append(PacketEntry.TABLE_NAME).append("(");
  74. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_ID).append(" INTEGER NOT NULL,");
  75. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_ATTACK_ID).append(" INTEGER NOT NULL,");
  76. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_TYPE).append(" TEXT,");
  77. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP).append(" INTEGER,");
  78. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_PACKET).append(" TEXT,");
  79. packetSQLBuilder.append(String.format("PRIMARY KEY(%s,%s)", PacketEntry.COLUMN_NAME_ID, PacketEntry.COLUMN_NAME_ATTACK_ID));
  80. packetSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", PacketEntry.COLUMN_NAME_ATTACK_ID, AttackEntry.TABLE_NAME,
  81. AttackEntry.COLUMN_NAME_ATTACK_ID));
  82. packetSQLBuilder.append(")");
  83. SQL_CREATE_PACKET_ENTRIES = packetSQLBuilder.toString();
  84. // SyncDeviceEntry
  85. StringBuilder syncDevicesSQLBuilder = new StringBuilder("CREATE TABLE ").append(SyncDeviceEntry.TABLE_NAME).append("(");
  86. syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID).append(" TEXT PRIMARY KEY,");
  87. syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP).append(" INTEGER");
  88. syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID).append(" INTEGER");
  89. syncDevicesSQLBuilder.append(")");
  90. SQL_CREATE_SYNC_DEVICES_ENTRIES = syncDevicesSQLBuilder.toString();
  91. // SyncInfoEntry
  92. StringBuilder syncInfoSQLBuilder = new StringBuilder("CREATE TABLE ").append(SyncInfoEntry.TABLE_NAME).append("(");
  93. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_DEVICE_ID).append(" TEXT,");
  94. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_BSSID).append(" TEXT,");
  95. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS).append(" INTEGER,");
  96. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS).append(" INTEGER,");
  97. syncInfoSQLBuilder.append(String.format("PRIMARY KEY(%s,%s)", SyncInfoEntry.COLUMN_NAME_DEVICE_ID, SyncInfoEntry.COLUMN_NAME_BSSID));
  98. syncInfoSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", SyncInfoEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME,
  99. NetworkEntry.COLUMN_NAME_BSSID));
  100. syncInfoSQLBuilder.append(")");
  101. SQL_CREATE_SYNC_INFO_ENTRIES = syncInfoSQLBuilder.toString();
  102. // ProfileEntry
  103. StringBuilder profilSQLBuilder = new StringBuilder("CREATE TABLE ").append(ProfileEntry.TABLE_NAME).append("(");
  104. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT,");
  105. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_NAME).append(" TEXT,");
  106. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_DESCRIPTION ).append(" TEXT,");
  107. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ICON).append(" TEXT,");
  108. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ICON_NAME).append(" TEXT,");
  109. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_EDITABLE).append(" INTEGER,");
  110. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ACTIVE).append(" INTEGER");
  111. profilSQLBuilder.append(")");
  112. SQL_CREATE_PROFILE_ENTRIES = profilSQLBuilder.toString();
  113. }
  114. private static final String SQL_CREATE_NETWORK_ENTRIES;
  115. private static final String SQL_CREATE_ATTACK_ENTRIES;
  116. private static final String SQL_CREATE_PACKET_ENTRIES;
  117. private static final String SQL_CREATE_PROFILE_ENTRIES;
  118. private static final String SQL_CREATE_SYNC_DEVICES_ENTRIES;
  119. private static final String SQL_CREATE_SYNC_INFO_ENTRIES;
  120. private static final String SQL_DELETE_PACKET_ENTRIES = "DROP TABLE IF EXISTS " + PacketEntry.TABLE_NAME;
  121. private static final String SQL_DELETE_ATTACK_ENTRIES = "DROP TABLE IF EXISTS " + AttackEntry.TABLE_NAME;
  122. private static final String SQL_DELETE_NETWORK_ENTRIES = "DROP TABLE IF EXISTS " + NetworkEntry.TABLE_NAME;
  123. private static final String SQL_DELETE_PROFILE_ENTRIES = "DROP TABLE IF EXISTS " + ProfileEntry.TABLE_NAME;
  124. private static final String SQL_DELETE_SYNC_DEVICES_ENTRIES = "DROP TABLE IF EXISTS " + SyncDeviceEntry.TABLE_NAME;
  125. private static final String SQL_DELETE_SYNC_INFO_ENTRIES = "DROP TABLE IF EXISTS " + SyncInfoEntry.TABLE_NAME;
  126. public HostageDBOpenHelper(Context context) {
  127. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  128. this.context = context;
  129. }
  130. @Override
  131. public void onCreate(SQLiteDatabase db) {
  132. db.execSQL(SQL_CREATE_SYNC_DEVICES_ENTRIES);
  133. db.execSQL(SQL_CREATE_NETWORK_ENTRIES);
  134. db.execSQL(SQL_CREATE_ATTACK_ENTRIES);
  135. db.execSQL(SQL_CREATE_PACKET_ENTRIES);
  136. db.execSQL(SQL_CREATE_PROFILE_ENTRIES);
  137. db.execSQL(SQL_CREATE_SYNC_INFO_ENTRIES);
  138. }
  139. @Override
  140. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  141. db.execSQL(SQL_DELETE_SYNC_DEVICES_ENTRIES);
  142. db.execSQL(SQL_DELETE_SYNC_INFO_ENTRIES);
  143. db.execSQL(SQL_DELETE_PACKET_ENTRIES);
  144. db.execSQL(SQL_DELETE_ATTACK_ENTRIES);
  145. db.execSQL(SQL_DELETE_PROFILE_ENTRIES);
  146. db.execSQL(SQL_DELETE_NETWORK_ENTRIES);
  147. onCreate(db);
  148. }
  149. /**
  150. * Adds a given {@link MessageRecord} to the database.
  151. *
  152. * @param record
  153. * The added {@link MessageRecord} .
  154. */
  155. public void addMessageRecord(MessageRecord record) {
  156. SQLiteDatabase db = this.getWritableDatabase();
  157. this.insertMessageRecordWithOnConflict(record, db);
  158. db.close(); // Closing database connection
  159. }
  160. /**
  161. * Adds a given {@link de.tudarmstadt.informatik.hostage.logging.MessageRecord}s to the database.
  162. *
  163. * @param records {@link List}<MessageRecord>
  164. * The added {@link de.tudarmstadt.informatik.hostage.logging.MessageRecord}s .
  165. */
  166. public void insertMessageRecords(List<MessageRecord> records){
  167. SQLiteDatabase db = this.getWritableDatabase();
  168. db.beginTransaction();
  169. try {
  170. for (MessageRecord record : records){
  171. this.insertMessageRecordWithOnConflict(record,db);
  172. }
  173. db.setTransactionSuccessful();
  174. } finally {
  175. db.endTransaction();
  176. }
  177. db.close();
  178. }
  179. private void insertMessageRecordWithOnConflict(MessageRecord record, SQLiteDatabase db){
  180. ContentValues recordValues = new ContentValues();
  181. //recordValues.put(PacketEntry.COLUMN_NAME_ID, record.getId()); // Log Message Number
  182. recordValues.put(PacketEntry.COLUMN_NAME_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  183. recordValues.put(PacketEntry.COLUMN_NAME_TYPE, record.getType().name()); // Log Type
  184. recordValues.put(PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP, record.getTimestamp()); // Log Timestamp
  185. recordValues.put(PacketEntry.COLUMN_NAME_PACKET, record.getPacket()); // Log Packet
  186. // Inserting Rows
  187. db.insertWithOnConflict(PacketEntry.TABLE_NAME, null, recordValues, SQLiteDatabase.CONFLICT_REPLACE);
  188. }
  189. /**
  190. * Adds a given {@link AttackRecord} to the database.
  191. *
  192. * @param record
  193. * The added {@link AttackRecord} .
  194. */
  195. public void addAttackRecord(AttackRecord record) {
  196. //Log.i("DBHelper", "Add Attack Record with id: " + record.getAttack_id());
  197. SQLiteDatabase db = this.getWritableDatabase();
  198. this.insertAttackRecordWithOnConflict(record,db);
  199. db.close(); // Closing database connection
  200. }
  201. /**
  202. * Adds a given {@link AttackRecord}s to the database.
  203. *
  204. * @param records {@link List}<AttackRecord>
  205. * The added {@link AttackRecord}s .
  206. */
  207. public void insertAttackRecords(List<AttackRecord> records) {
  208. //Log.i("DBHelper", "Add Attack Record with id: " + record.getAttack_id());
  209. SQLiteDatabase db = this.getWritableDatabase();
  210. db.beginTransaction();
  211. try {
  212. for (AttackRecord record : records){
  213. this.insertAttackRecordWithOnConflict(record,db);
  214. }
  215. db.setTransactionSuccessful();
  216. } finally {
  217. db.endTransaction();
  218. }
  219. db.close(); // Closing database connection
  220. }
  221. private void insertAttackRecordWithOnConflict(AttackRecord record, SQLiteDatabase db){
  222. ContentValues attackValues = new ContentValues();
  223. attackValues.put(AttackEntry.COLUMN_NAME_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  224. attackValues.put(AttackEntry.COLUMN_NAME_PROTOCOL, record.getProtocol().toString());
  225. attackValues.put(AttackEntry.COLUMN_NAME_EXTERNAL_IP, record.getExternalIP());
  226. attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_IP, record.getLocalIP()); // Log Local IP
  227. attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_PORT, record.getLocalPort());
  228. attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_IP, record.getRemoteIP()); // Log Remote IP
  229. attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
  230. attackValues.put(AttackEntry.COLUMN_NAME_INTERNAL_ATTACK, record.getWasInternalAttack());
  231. attackValues.put(AttackEntry.COLUMN_NAME_BSSID, record.getBssid());
  232. // Inserting Rows
  233. db.insertWithOnConflict(AttackEntry.TABLE_NAME, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
  234. }
  235. /**
  236. * Adds a given {@link SyncRecord}s to the database.
  237. *
  238. * @param records {@link List}<AttackRecord>
  239. * The added {@link SyncRecord}s .
  240. */
  241. public void insertSyncRecords(List<SyncRecord> records) {
  242. //Log.i("DBHelper", "Add Attack Record with id: " + record.getAttack_id());
  243. SQLiteDatabase db = this.getWritableDatabase();
  244. db.beginTransaction();
  245. try {
  246. for (SyncRecord record : records){
  247. this.insertAttackRecordWithOnConflict(record.getAttackRecord(), db);
  248. this.insertMessageRecordWithOnConflict(record.getMessageRecord(), db);
  249. }
  250. db.setTransactionSuccessful();
  251. } finally {
  252. db.endTransaction();
  253. }
  254. this.updateSyncDevicesMaxID(db);
  255. db.close(); // Closing database connection
  256. }
  257. /**
  258. * Updates the sync devices max sync id.
  259. */
  260. public void updateSyncDevicesMaxID(SQLiteDatabase db){
  261. HashMap<String, Long> deviceIDmap = new HashMap<String, Long>();
  262. 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 + " )";
  263. {
  264. Cursor cursor = db.rawQuery(selectQuery, null);
  265. // looping through all rows and adding to list
  266. if (cursor.moveToFirst()) {
  267. do {
  268. String device_id = cursor.getString(0);
  269. long sync_id = cursor.getLong(1);
  270. deviceIDmap.put(device_id, sync_id);
  271. } while (cursor.moveToNext());
  272. }
  273. cursor.close();
  274. }
  275. {
  276. ArrayList<SyncDevice> allDevices = this.getSyncDevices();
  277. for (SyncDevice device : allDevices){
  278. long sync_id = deviceIDmap.get(device.getDeviceID());
  279. device.setHighest_attack_id(sync_id);
  280. }
  281. this.updateSyncDevices(allDevices);
  282. }
  283. }
  284. public void updateSyncAttackCounter(AttackRecord record){
  285. SQLiteDatabase db = this.getWritableDatabase();
  286. //String mac = HelperUtils.getMacAdress(context);
  287. SyncDevice currentDevice = SyncDevice.currentDevice();
  288. ContentValues syncDeviceValues = new ContentValues();
  289. syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, currentDevice.getDeviceID());
  290. syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, System.currentTimeMillis());
  291. syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID, record.getAttack_id());
  292. //String query = "SELECT * FROM " + SyncInfoEntry.TABLE_NAME +
  293. // " WHERE " + SyncInfoEntry.COLUMN_NAME_DEVICE_ID + " = ? " +
  294. // "AND " + SyncInfoEntry.COLUMN_NAME_BSSID + " = ?";
  295. //Cursor cursor = db.rawQuery(query, new String[] {mac, record.getBssid()});
  296. //long attackCount = 0;
  297. //long portscanCount = 0;
  298. //if (cursor.moveToFirst()){
  299. // attackCount = cursor.getLong(2);
  300. // portscanCount = cursor.getLong(3);
  301. //}
  302. //if("PORTSCAN".equals(record.getProtocol())){
  303. // portscanCount++;
  304. //}else { attackCount++; }
  305. //Log.i("DBHelper", "Update number of attack: " + attackCount);
  306. //ContentValues synInfoValues = new ContentValues();
  307. //synInfoValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, record.getBssid());
  308. //synInfoValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, mac);
  309. //synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, attackCount);
  310. //synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, portscanCount);
  311. // Inserting Rows
  312. //db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, synInfoValues, SQLiteDatabase.CONFLICT_REPLACE);
  313. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, syncDeviceValues, SQLiteDatabase.CONFLICT_REPLACE);
  314. db.close(); // Closing database connection
  315. }
  316. /**
  317. * Determines if a network with given BSSID has already been recorded as malicious.
  318. *
  319. * @param BSSID
  320. * The BSSID of the network.
  321. * @return True if an attack has been recorded in a network with the given
  322. * BSSID, else false.
  323. */
  324. public synchronized boolean bssidSeen(String BSSID) {
  325. String countQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
  326. SQLiteDatabase db = this.getReadableDatabase();
  327. Cursor cursor = db.rawQuery(countQuery, new String[] {BSSID});
  328. int result = cursor.getCount();
  329. cursor.close();
  330. db.close();
  331. return result > 0;
  332. }
  333. /**
  334. * Determines if an attack has been recorded on a specific protocol in a
  335. * network with a given BSSID.
  336. *
  337. * @param protocol
  338. * The
  339. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  340. * Protocol} to inspect.
  341. * @param BSSID
  342. * The BSSID of the network.
  343. * @return True if an attack on the given protocol has been recorded in a
  344. * network with the given BSSID, else false.
  345. */
  346. public synchronized boolean bssidSeen(String protocol, String BSSID) {
  347. if(BSSID == null || protocol == null){
  348. return false;
  349. }
  350. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " NATURAL JOIN " + NetworkEntry.TABLE_NAME + " WHERE "
  351. + AttackEntry.COLUMN_NAME_PROTOCOL + " = ? AND " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
  352. SQLiteDatabase db = this.getReadableDatabase();
  353. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, BSSID});
  354. int result = cursor.getCount();
  355. cursor.close();
  356. db.close();
  357. return result > 0;
  358. }
  359. public synchronized int numBssidSeen(String BSSID) {
  360. String countQuery = "SELECT COUNT(*) FROM " + AttackEntry.TABLE_NAME + " WHERE "
  361. + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_BSSID + " = " + "'" + BSSID + "'";
  362. SQLiteDatabase db = this.getReadableDatabase();
  363. Cursor cursor = db.rawQuery(countQuery, null);
  364. cursor.moveToFirst();
  365. int result = cursor.getInt(0);
  366. cursor.close();
  367. db.close();
  368. return result;
  369. }
  370. public int numBssidSeen(String protocol, String BSSID) {
  371. String countQuery = "SELECT COUNT(*) FROM " + AttackEntry.TABLE_NAME
  372. + " WHERE " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_PROTOCOL + " = " + "'" + protocol + "'"
  373. + " AND " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_BSSID + " = " + "'" + BSSID + "'";
  374. SQLiteDatabase db = this.getReadableDatabase();
  375. Cursor cursor = db.rawQuery(countQuery, null);
  376. cursor.moveToFirst();
  377. int result = cursor.getInt(0);
  378. cursor.close();
  379. db.close();
  380. return result;
  381. }
  382. /**
  383. * Returns a String array with all BSSIDs stored in the database.
  384. *
  385. * @return ArrayList<String> of all recorded BSSIDs.
  386. */
  387. public synchronized ArrayList<String> getAllBSSIDS() {
  388. String selectQuery = "SELECT "+NetworkEntry.COLUMN_NAME_BSSID+" FROM " + NetworkEntry.TABLE_NAME;
  389. SQLiteDatabase db = this.getReadableDatabase();
  390. Cursor cursor = db.rawQuery(selectQuery, null);
  391. ArrayList<String> bssidList = new ArrayList<String>();
  392. // looping through all rows and adding to list
  393. if (cursor.moveToFirst()) {
  394. do {
  395. String s = cursor.getString(0);
  396. bssidList.add(s);
  397. } while (cursor.moveToNext());
  398. }
  399. cursor.close();
  400. db.close();
  401. return bssidList;
  402. }
  403. /**
  404. * Returns all missing network records.
  405. *
  406. * @return a list of missing network records.
  407. */
  408. public synchronized ArrayList<NetworkRecord> getMissingNetworkRecords(ArrayList<String> otherBSSIDs) {
  409. String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " N WHERE " + "N."+NetworkEntry.COLUMN_NAME_BSSID + " NOT IN " + otherBSSIDs;
  410. SQLiteDatabase db = this.getReadableDatabase();
  411. Cursor cursor = db.rawQuery(selectQuery, null);
  412. ArrayList<NetworkRecord> networkInformation = new ArrayList<NetworkRecord>();
  413. // looping through all rows and adding to list
  414. if (cursor.moveToFirst()) {
  415. do {
  416. NetworkRecord record = this.createNetworkRecord(cursor);
  417. networkInformation.add(record);
  418. } while (cursor.moveToNext());
  419. }
  420. cursor.close();
  421. db.close();
  422. return networkInformation;
  423. }
  424. /**
  425. * Determines the number of different attacks in the database.
  426. *
  427. * @return The number of different attacks in the database.
  428. */
  429. public synchronized int getAttackCount() {
  430. SQLiteDatabase db = this.getReadableDatabase();
  431. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  432. " WHERE " + AttackEntry.COLUMN_NAME_PROTOCOL + " <> ?";
  433. Cursor cursor = db.rawQuery(countQuery, new String[]{"PORTSCAN"});
  434. int result = cursor.getCount();
  435. cursor.close();
  436. // return count
  437. db.close();
  438. return result;
  439. }
  440. /**
  441. * Determines the number of different recorded attacks in a specific access point since the given attack_id.
  442. * The given attack_id is not included.
  443. * @param attack_id The attack id to match the query against.
  444. * @param bssid The BSSID of the access point.
  445. * @return The number of different attacks in the database since the given attack_id.
  446. */
  447. public synchronized int getAttackCount(int attack_id, String bssid) {
  448. SQLiteDatabase db = this.getReadableDatabase();
  449. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  450. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " <> ? " +
  451. "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? " +
  452. "AND " + AttackEntry.COLUMN_NAME_BSSID + " = ?";
  453. String[] selectArgs = new String[]{"PORTSCAN", attack_id + "", bssid};
  454. Cursor cursor = db.rawQuery(countQuery, selectArgs);
  455. int result = cursor.getCount();
  456. cursor.close();
  457. // return count
  458. db.close();
  459. return result;
  460. }
  461. /**
  462. * Determines the number of different attacks for a specific protocol in
  463. * the database.
  464. *
  465. * @param protocol
  466. * The String representation of the
  467. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  468. * Protocol}
  469. * @return The number of different attacks in the database.
  470. */
  471. public synchronized int getAttackPerProtocolCount(String protocol) {
  472. SQLiteDatabase db = this.getReadableDatabase();
  473. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  474. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? ";
  475. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol});
  476. int result = cursor.getCount();
  477. cursor.close();
  478. // return count
  479. db.close();
  480. return result;
  481. }
  482. /**
  483. * Determines the number of attacks for a specific protocol in
  484. * the database since the given attack_id.
  485. *
  486. * @param protocol
  487. * The String representation of the
  488. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  489. * Protocol}
  490. * @param attack_id The attack id to match the query against.
  491. * @return The number of different attacks in the database since the given attack_id.
  492. */
  493. public synchronized int getAttackPerProtocolCount(String protocol, int attack_id) {
  494. SQLiteDatabase db = this.getReadableDatabase();
  495. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  496. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? " +
  497. "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? ";
  498. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, attack_id + ""});
  499. int result = cursor.getCount();
  500. cursor.close();
  501. // return count
  502. db.close();
  503. return result;
  504. }
  505. /**
  506. * Determines the number of recorded attacks for a specific protocol and accesss point since the given attack_id.
  507. *
  508. * @param protocol
  509. * The String representation of the
  510. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  511. * Protocol}
  512. * @param attack_id The attack id to match the query against.
  513. * @param bssid The BSSID of the access point.
  514. * @return The number of different attacks in the database since the given attack_id.
  515. */
  516. public synchronized int getAttackPerProtocolCount(String protocol, int attack_id, String bssid) {
  517. SQLiteDatabase db = this.getReadableDatabase();
  518. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  519. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? " +
  520. "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? " +
  521. "AND " + AttackEntry.COLUMN_NAME_BSSID + " = ?";
  522. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, attack_id + "", bssid});
  523. int result = cursor.getCount();
  524. cursor.close();
  525. // return count
  526. db.close();
  527. return result;
  528. }
  529. /**
  530. * Determines the number of portscans stored in the database.
  531. *
  532. * @return The number of portscans stored in the database.
  533. */
  534. public synchronized int getPortscanCount() {
  535. return getAttackPerProtocolCount("PORTSCAN");
  536. }
  537. /**
  538. * Determines the number of recorded portscans since the given attack_id.
  539. * @param attack_id The attack id to match the query against.
  540. * @return The number of portscans stored in the database since the given attack_id.
  541. */
  542. public synchronized int getPortscanCount(int attack_id) {
  543. return getAttackPerProtocolCount("PORTSCAN", attack_id);
  544. }
  545. /**
  546. * Determines the number of recorded portscans in a specific access point since the given attack_id.
  547. * @param attack_id The attack id to match the query against.
  548. * @param bssid The BSSID of the access point.
  549. * @return The number of portscans stored in the database since the given attack_id.
  550. */
  551. public synchronized int getPortscanCount(int attack_id, String bssid) {
  552. return getAttackPerProtocolCount("PORTSCAN", attack_id, bssid);
  553. }
  554. /**
  555. * Determines the number of {@link Record Records} in the database.
  556. *
  557. * @return The number of {@link Record Records} in the database.
  558. */
  559. public synchronized int getRecordCount() {
  560. String countQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME;
  561. SQLiteDatabase db = this.getReadableDatabase();
  562. Cursor cursor = db.rawQuery(countQuery, null);
  563. int result = cursor.getCount();
  564. cursor.close();
  565. // return count
  566. db.close();
  567. return result;
  568. }
  569. //TODO ADD AGAIN ?
  570. /**
  571. * Returns the {@link AttackRecord} with the given attack id from the database.
  572. *
  573. * @param attack_id
  574. * The attack id of the {@link Record};
  575. * @return The {@link Record}.
  576. */
  577. /*
  578. public AttackRecord getRecordOfAttackId(long attack_id) {
  579. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " WHERE " + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id;
  580. SQLiteDatabase db = this.getReadableDatabase();
  581. Cursor cursor = db.rawQuery(selectQuery, null);
  582. AttackRecord record = null;
  583. if (cursor.moveToFirst()) {
  584. record = createAttackRecord(cursor);
  585. }
  586. cursor.close();
  587. // return record list
  588. db.close();
  589. return record;
  590. } */
  591. /**
  592. * Gets a {@link AttackRecord} for every attack identified by its attack id.
  593. *
  594. * @return A ArrayList with a {@link AttackRecord} for each attack id in the Database.
  595. */
  596. public synchronized ArrayList<AttackRecord> getRecordOfEachAttack() {
  597. ArrayList<AttackRecord> recordList = new ArrayList<AttackRecord>();
  598. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME;
  599. SQLiteDatabase db = this.getReadableDatabase();
  600. Cursor cursor = db.rawQuery(selectQuery, null);
  601. // looping through all rows and adding to list
  602. if (cursor.moveToFirst()) {
  603. do {
  604. AttackRecord record = createAttackRecord(cursor);
  605. // Adding record to list
  606. recordList.add(record);
  607. } while (cursor.moveToNext());
  608. }
  609. cursor.close();
  610. // return record list
  611. db.close();
  612. return recordList;
  613. }
  614. /**
  615. * Gets a AttackRecord for every attack with a higher attack id than the specified.
  616. *
  617. * @param attack_id
  618. * The attack id to match the query against.
  619. * @return A ArrayList with one {@link AttackRecord} for each attack id
  620. * higher than the given.
  621. */
  622. public synchronized ArrayList<AttackRecord> getRecordOfEachAttack(long attack_id) {
  623. ArrayList<AttackRecord> recordList = new ArrayList<AttackRecord>();
  624. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " WHERE " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > " + attack_id;
  625. SQLiteDatabase db = this.getReadableDatabase();
  626. Cursor cursor = db.rawQuery(selectQuery, null);
  627. // looping through all rows and adding to list
  628. if (cursor.moveToFirst()) {
  629. do {
  630. AttackRecord record = createAttackRecord(cursor);
  631. // Adding record to list
  632. recordList.add(record);
  633. } while (cursor.moveToNext());
  634. }
  635. cursor.close();
  636. // return count
  637. db.close();
  638. return recordList;
  639. }
  640. /**
  641. * Determines the highest attack id stored in the database.
  642. *
  643. * @return The highest attack id stored in the database.
  644. */
  645. public synchronized long getHighestAttackId() {
  646. String selectQuery = "SELECT MAX(" + AttackEntry.COLUMN_NAME_ATTACK_ID + ") FROM " + AttackEntry.TABLE_NAME;
  647. SQLiteDatabase db = this.getReadableDatabase();
  648. Cursor cursor = db.rawQuery(selectQuery, null);
  649. int result;
  650. if (cursor.moveToFirst()) {
  651. result = cursor.getInt(0);
  652. } else {
  653. result = -1;
  654. }
  655. cursor.close();
  656. db.close();
  657. return result;
  658. }
  659. /**
  660. * Determines the smallest attack id stored in the database.
  661. *
  662. * @return The smallest attack id stored in the database.
  663. */
  664. public synchronized long getSmallestAttackId() {
  665. String selectQuery = "SELECT MIN(" + AttackEntry.COLUMN_NAME_ATTACK_ID + ") FROM " + AttackEntry.TABLE_NAME;
  666. SQLiteDatabase db = this.getReadableDatabase();
  667. Cursor cursor = db.rawQuery(selectQuery, null);
  668. int result;
  669. if (cursor.moveToFirst()) {
  670. result = cursor.getInt(0);
  671. } else {
  672. result = -1;
  673. }
  674. cursor.close();
  675. db.close();
  676. return result;
  677. }
  678. /**
  679. * Gets the last recorded SSID to a given BSSID.
  680. *
  681. * @param bssid
  682. * The BSSID to match against.
  683. * @return A String of the last SSID or null if the BSSID is not in the
  684. * database.
  685. */
  686. public synchronized String getSSID(String bssid) {
  687. String selectQuery = "SELECT " + NetworkEntry.COLUMN_NAME_SSID + " FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID
  688. + " = " + "'" + bssid + "'";
  689. SQLiteDatabase db = this.getReadableDatabase();
  690. Cursor cursor = db.rawQuery(selectQuery, null);
  691. String ssid = null;
  692. if (cursor.moveToFirst()) {
  693. ssid = cursor.getString(0);
  694. }
  695. cursor.close();
  696. db.close();
  697. return ssid;
  698. }
  699. /**
  700. * Gets all network related data stored in the database
  701. * @return An ArrayList with an Network for all Entry in the network table.
  702. */
  703. public synchronized ArrayList<NetworkRecord> getNetworkInformation() {
  704. String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME;
  705. SQLiteDatabase db = this.getReadableDatabase();
  706. Cursor cursor = db.rawQuery(selectQuery, null);
  707. ArrayList<NetworkRecord> networkInformation = new ArrayList<NetworkRecord>();
  708. // looping through all rows and adding to list
  709. if (cursor.moveToFirst()) {
  710. do {
  711. NetworkRecord record = this.createNetworkRecord(cursor);
  712. networkInformation.add(record);
  713. } while (cursor.moveToNext());
  714. }
  715. cursor.close();
  716. db.close();
  717. return networkInformation;
  718. }
  719. private NetworkRecord createNetworkRecord(Cursor cursor){
  720. NetworkRecord record = new NetworkRecord();
  721. record.setBssid(cursor.getString(0));
  722. record.setSsid(cursor.getString(1));
  723. record.setLatitude(Double.parseDouble(cursor.getString(2)));
  724. record.setLongitude(Double.parseDouble(cursor.getString(3)));
  725. record.setAccuracy(Float.parseFloat(cursor.getString(4)));
  726. record.setTimestampLocation(cursor.getLong(5));
  727. return record;
  728. }
  729. /**
  730. * Updates the network table with the information contained in the parameter.
  731. * @param networkInformation ArrayList of {@link NetworkRecord NetworkRecords}
  732. * @see {@link HostageDBOpenHelper#updateNetworkInformation(NetworkRecord record)}
  733. */
  734. public void updateNetworkInformation(ArrayList<NetworkRecord> networkInformation) {
  735. SQLiteDatabase db = this.getReadableDatabase();
  736. db.beginTransaction();
  737. try {
  738. for (NetworkRecord record : networkInformation) {
  739. String bssid = record.getBssid();
  740. String bssidQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
  741. Cursor cursor = db.rawQuery(bssidQuery, new String[] {bssid});
  742. if (!cursor.moveToFirst() || cursor.getLong(5) < record.getTimestampLocation()){
  743. ContentValues bssidValues = new ContentValues();
  744. bssidValues.put(NetworkEntry.COLUMN_NAME_BSSID, bssid);
  745. bssidValues.put(NetworkEntry.COLUMN_NAME_SSID, record.getSsid());
  746. bssidValues.put(NetworkEntry.COLUMN_NAME_LATITUDE, record.getLatitude());
  747. bssidValues.put(NetworkEntry.COLUMN_NAME_LONGITUDE, record.getLongitude());
  748. bssidValues.put(NetworkEntry.COLUMN_NAME_ACCURACY, record.getAccuracy());
  749. bssidValues.put(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP, record.getTimestampLocation());
  750. db.insertWithOnConflict(NetworkEntry.TABLE_NAME, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
  751. }
  752. cursor.close();
  753. }
  754. db.setTransactionSuccessful();
  755. } finally {
  756. db.endTransaction();
  757. }
  758. db.close();
  759. }
  760. /**
  761. * Updated the network table with a new {@link NetworkRecord}.
  762. * If information about this BSSID are already in the database,
  763. * the table will only be updated if the new {@link NetworkRecord }
  764. * has a newer location time stamp.
  765. * @param record The new {@link NetworkRecord}.
  766. */
  767. public void updateNetworkInformation(NetworkRecord record) {
  768. SQLiteDatabase db = this.getReadableDatabase();
  769. String bssid = record.getBssid();
  770. String bssidQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
  771. Cursor cursor = db.rawQuery(bssidQuery, new String[] {bssid});
  772. if (!cursor.moveToFirst() || cursor.getLong(5) < record.getTimestampLocation()){
  773. ContentValues bssidValues = new ContentValues();
  774. bssidValues.put(NetworkEntry.COLUMN_NAME_BSSID, bssid);
  775. bssidValues.put(NetworkEntry.COLUMN_NAME_SSID, record.getSsid());
  776. bssidValues.put(NetworkEntry.COLUMN_NAME_LATITUDE, record.getLatitude());
  777. bssidValues.put(NetworkEntry.COLUMN_NAME_LONGITUDE, record.getLongitude());
  778. bssidValues.put(NetworkEntry.COLUMN_NAME_ACCURACY, record.getAccuracy());
  779. bssidValues.put(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP, record.getTimestampLocation());
  780. db.insertWithOnConflict(NetworkEntry.TABLE_NAME, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
  781. }
  782. cursor.close();
  783. db.close();
  784. }
  785. /**
  786. * Updates the the timestamp of a single device id
  787. * @param deviceID The Device id
  788. * @param timestamp The synchronization timestamp
  789. */
  790. public void updateTimestampOfSyncDevice(String deviceID, long timestamp){
  791. SQLiteDatabase db = this.getReadableDatabase();
  792. ContentValues deviceValues = new ContentValues();
  793. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, deviceID);
  794. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, timestamp);
  795. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
  796. db.close();
  797. }
  798. /**
  799. * Updates the Timestamps of synchronization devices from a HashMap.
  800. * @param devices HashMap of device ids and their synchronization timestamps.
  801. */
  802. public void updateSyncDevices(HashMap<String, Long> devices){
  803. SQLiteDatabase db = this.getReadableDatabase();
  804. db.beginTransaction();
  805. try {
  806. for(String key : devices.keySet()){
  807. ContentValues deviceValues = new ContentValues();
  808. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, key);
  809. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, devices.get(key));
  810. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
  811. }
  812. db.setTransactionSuccessful();
  813. } finally {
  814. db.endTransaction();
  815. }
  816. db.close();
  817. }
  818. public void updateSyncDevices(ArrayList<SyncDevice> devices){
  819. SQLiteDatabase db = this.getReadableDatabase();
  820. db.beginTransaction();
  821. try {
  822. for(SyncDevice device : devices){
  823. ContentValues deviceValues = new ContentValues();
  824. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, device.getDeviceID());
  825. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, device.getLast_sync_timestamp());
  826. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID, device.getHighest_attack_id());
  827. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
  828. }
  829. db.setTransactionSuccessful();
  830. } finally {
  831. db.endTransaction();
  832. }
  833. db.close();
  834. }
  835. /**
  836. * Returns the own state containing all registered devices ids and their max sync_id
  837. * @return {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}
  838. */
  839. public SyncInfo getOwnState(){
  840. ArrayList<SyncDevice> devices = this.getSyncDevices();
  841. HashMap<String, Long> deviceMap = new HashMap<String, Long>();
  842. for (SyncDevice device : devices){
  843. deviceMap.put(device.getDeviceID(), device.getHighest_attack_id());
  844. }
  845. SyncInfo syncInfo = new SyncInfo();
  846. syncInfo.deviceMap = deviceMap;
  847. syncInfo.bssids = this.getAllBSSIDS();
  848. return syncInfo;
  849. }
  850. /**
  851. * Returns a HashMap of all devices that were previously synchronized with.
  852. * @return HashMap containing device id's and the last synchronization timestamp.
  853. */
  854. public synchronized HashMap<String, Long> getSyncDeviceHashMap(){
  855. SQLiteDatabase db = this.getReadableDatabase();
  856. HashMap<String, Long> devices = new HashMap<String, Long>();
  857. String query = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
  858. Cursor cursor = db.rawQuery(query, null);
  859. if (cursor.moveToFirst()) {
  860. do {
  861. devices.put(cursor.getString(0), cursor.getLong(1));
  862. } while (cursor.moveToNext());
  863. }
  864. cursor.close();
  865. db.close();
  866. return devices;
  867. }
  868. /**
  869. * Returns a HashMap of all devices that were previously synchronized with.
  870. * @return HashMap containing device id's and the last synchronization timestamp.
  871. */
  872. public synchronized ArrayList<SyncDevice> getSyncDevices(){
  873. SQLiteDatabase db = this.getReadableDatabase();
  874. ArrayList<SyncDevice> devices = new ArrayList<SyncDevice>();
  875. String query = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
  876. Cursor cursor = db.rawQuery(query, null);
  877. if (cursor.moveToFirst()) {
  878. do {
  879. SyncDevice device = this.createSyncDevice(cursor);
  880. devices.add(device);
  881. } while (cursor.moveToNext());
  882. }
  883. cursor.close();
  884. db.close();
  885. return devices;
  886. }
  887. /**
  888. * Returns a ArrayList containing all information stored in the SyncInfo table.
  889. * @return ArrayList<SyncInfo>
  890. */
  891. public synchronized ArrayList<SyncInfoRecord> getSyncInfo(){
  892. SQLiteDatabase db = this.getReadableDatabase();
  893. ArrayList<SyncInfoRecord> syncInfo = new ArrayList<SyncInfoRecord>();
  894. String query = "SELECT * FROM " + SyncInfoEntry.TABLE_NAME;
  895. Cursor cursor = db.rawQuery(query, null);
  896. if (cursor.moveToFirst()) {
  897. do {
  898. SyncInfoRecord info = new SyncInfoRecord();
  899. info.setDeviceID(cursor.getString(0));
  900. info.setBSSID(cursor.getString(1));
  901. info.setNumber_of_attacks(cursor.getLong(2));
  902. info.setNumber_of_portscans(cursor.getLong(3));
  903. syncInfo.add(info);
  904. } while (cursor.moveToNext());
  905. }
  906. cursor.close();
  907. db.close();
  908. return syncInfo;
  909. }
  910. /**
  911. * Updates the sync_info table with the information contained in the parameter.
  912. * @param syncInfos ArrayList of {@link SyncInfoRecord SyncInfoRecords}
  913. * @see {@link HostageDBOpenHelper#updateSyncInfo(SyncInfoRecord syncInfo)}
  914. */
  915. public synchronized void updateSyncInfo(ArrayList<SyncInfoRecord> syncInfos){
  916. SQLiteDatabase db = this.getReadableDatabase();
  917. db.beginTransaction();
  918. try {
  919. for(SyncInfoRecord syncInfo : syncInfos){
  920. ContentValues syncValues = new ContentValues();
  921. syncValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, syncInfo.getBSSID());
  922. syncValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, syncInfo.getDeviceID());
  923. syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, syncInfo.getNumber_of_attacks());
  924. syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, syncInfo.getNumber_of_portscans());
  925. db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, syncValues, SQLiteDatabase.CONFLICT_REPLACE);
  926. }
  927. db.setTransactionSuccessful();
  928. } finally {
  929. db.endTransaction();
  930. }
  931. db.close();
  932. }
  933. /**
  934. * Updated the network table with a new {@link SyncInfoRecord}.
  935. * Conflicting rows will be replaced.
  936. * @param syncInfo The new {@link NetworkRecord}.
  937. */
  938. public synchronized void updateSyncInfo(SyncInfoRecord syncInfo){
  939. SQLiteDatabase db = this.getReadableDatabase();
  940. ContentValues syncValues = new ContentValues();
  941. syncValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, syncInfo.getBSSID());
  942. syncValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, syncInfo.getDeviceID());
  943. syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, syncInfo.getNumber_of_attacks());
  944. syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, syncInfo.getNumber_of_portscans());
  945. db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, syncValues, SQLiteDatabase.CONFLICT_REPLACE);
  946. db.close();
  947. }
  948. /**
  949. * 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}
  950. */
  951. public void clearSyncInfos(){
  952. SQLiteDatabase db = this.getReadableDatabase();
  953. db.delete(SyncDeviceEntry.TABLE_NAME, null, null);
  954. db.delete(SyncInfoEntry.TABLE_NAME, null, null);
  955. db.close();
  956. }
  957. /**
  958. * Deletes all records from {@link PacketEntry}s and {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}.
  959. */
  960. public void clearData() {
  961. SQLiteDatabase db = this.getReadableDatabase();
  962. db.delete(PacketEntry.TABLE_NAME, null, null);
  963. db.delete(AttackEntry.TABLE_NAME, null, null);
  964. db.close();
  965. }
  966. /**
  967. * Deletes all records from {@link PacketEntry}s with a specific BSSID.
  968. *
  969. * @param bssid
  970. * The BSSID to match against.
  971. */
  972. public synchronized void deleteByBSSID(String bssid) {
  973. SQLiteDatabase db = this.getReadableDatabase();
  974. db.delete(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID + " = ?", new String[] { bssid });
  975. db.delete(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_BSSID + " = ?", new String[] { bssid });
  976. db.close();
  977. }
  978. /**
  979. * Deletes all records from {@link de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEntry}s with a time stamp smaller
  980. * then the given
  981. *
  982. * @param date
  983. * A Date represented in milliseconds.
  984. */
  985. public synchronized void deleteByDate(long date) {
  986. SQLiteDatabase db = this.getReadableDatabase();
  987. String deleteQuery = "DELETE FROM " + PacketEntry.TABLE_NAME + " WHERE " + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP + " < " + date;
  988. db.execSQL(deleteQuery);
  989. db.close();
  990. }
  991. /**
  992. * Deletes all {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord} with a specific Attack ID.
  993. *
  994. * @param attackID
  995. * The Attack ID to match against.
  996. */
  997. public synchronized void deleteByAttackID(long attackID) {
  998. SQLiteDatabase db = this.getReadableDatabase();
  999. db.delete(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_ATTACK_ID + " = ?", new String[] { String.valueOf(attackID) });
  1000. db.delete(PacketEntry.TABLE_NAME, PacketEntry.COLUMN_NAME_ATTACK_ID + " = ?", new String[] { String.valueOf(attackID) });
  1001. db.close();
  1002. }
  1003. /**
  1004. * Creates a {@link MessageRecord} from a Cursor. If the cursor does not show to a
  1005. * valid data structure a runtime exception is thrown.
  1006. *
  1007. * @param cursor
  1008. * @return Returns the created {@link MessageRecord} .
  1009. */
  1010. private synchronized MessageRecord createMessageRecord(Cursor cursor) {
  1011. MessageRecord record = new MessageRecord();
  1012. record.setId(Integer.parseInt(cursor.getString(0)));
  1013. record.setAttack_id(cursor.getLong(1));
  1014. record.setType(MessageRecord.TYPE.valueOf(cursor.getString(2)));
  1015. record.setTimestamp(cursor.getLong(3));
  1016. record.setPacket(cursor.getString(4));
  1017. return record;
  1018. }
  1019. /**
  1020. * Creates a {@link AttackRecord} from a Cursor. If the cursor does not show to a
  1021. * valid data structure a runtime exception is thrown.
  1022. *
  1023. * @param cursor
  1024. * @return Returns the created {@link AttackRecord} .
  1025. */
  1026. private synchronized AttackRecord createAttackRecord(Cursor cursor) {
  1027. AttackRecord record = new AttackRecord();
  1028. record.setAttack_id(cursor.getLong(0));
  1029. record.setProtocol(cursor.getString(1));
  1030. record.setExternalIP(cursor.getString(2));
  1031. record.setLocalIP(cursor.getString(3));
  1032. record.setLocalPort(Integer.parseInt(cursor.getString(4)));
  1033. record.setRemoteIP(cursor.getString(5));
  1034. record.setRemotePort(Integer.parseInt(cursor.getString(6)));
  1035. record.setWasInternalAttack(cursor.getInt(7) == 1);
  1036. record.setBssid(cursor.getString(8));
  1037. record.setSync_id(cursor.getLong(9));
  1038. record.setDevice(cursor.getString(10));
  1039. return record;
  1040. }
  1041. /**
  1042. * Creates a {@link SyncRecord} from a Cursor. If the cursor does not show to a
  1043. * valid data structure a runtime exception is thrown.
  1044. *
  1045. * @param cursor
  1046. * @return Returns the created {@link SyncRecord} .
  1047. */
  1048. private synchronized SyncRecord createSyncRecord(Cursor cursor){
  1049. SyncRecord record = new SyncRecord();
  1050. record.setAttack_id(cursor.getLong(0));
  1051. record.setProtocol(cursor.getString(1));
  1052. record.setExternalIP(cursor.getString(2));
  1053. record.setLocalIP(cursor.getString(3));
  1054. record.setLocalPort(Integer.parseInt(cursor.getString(4)));
  1055. record.setRemoteIP(cursor.getString(5));
  1056. record.setRemotePort(Integer.parseInt(cursor.getString(6)));
  1057. record.setWasInternalAttack(cursor.getInt(7) == 1);
  1058. record.setBssid(cursor.getString(8));
  1059. record.setSync_id(cursor.getLong(9));
  1060. record.setDevice(cursor.getString(10));
  1061. record.setId(Integer.parseInt(cursor.getString(11)));
  1062. //record.setAttack_id(cursor.getLong(12));
  1063. record.setType(MessageRecord.TYPE.valueOf(cursor.getString(13)));
  1064. record.setTimestamp(cursor.getLong(14));
  1065. record.setPacket(cursor.getString(15));
  1066. return record;
  1067. }
  1068. /**
  1069. * Creates a {@link Record} from a Cursor. If the cursor does not show to a
  1070. * valid data structure a runtime exception is thrown.
  1071. *
  1072. * @param cursor
  1073. * @return Returns the created {@link Record} .
  1074. */
  1075. private synchronized Record createRecord(Cursor cursor) {
  1076. Record record = new Record();
  1077. record.setId(Integer.parseInt(cursor.getString(0)));
  1078. record.setAttack_id(cursor.getLong(1));
  1079. record.setType(TYPE.valueOf(cursor.getString(2)));
  1080. record.setTimestamp(cursor.getLong(3));
  1081. record.setPacket(cursor.getString(4));
  1082. record.setProtocol(cursor.getString(5));
  1083. record.setExternalIP(cursor.getString(6));
  1084. record.setLocalIP(cursor.getString(7));
  1085. record.setLocalPort(Integer.parseInt(cursor.getString(8)));
  1086. record.setRemoteIP(cursor.getString(9));
  1087. record.setRemotePort(Integer.parseInt(cursor.getString(10)));
  1088. record.setWasInternalAttack(Integer.parseInt(cursor.getString(11)) == 1);
  1089. record.setBssid(cursor.getString(12));
  1090. record.setSsid(cursor.getString(13));
  1091. record.setLatitude(Double.parseDouble(cursor.getString(14)));
  1092. record.setLongitude(Double.parseDouble(cursor.getString(15)));
  1093. record.setAccuracy(Float.parseFloat(cursor.getString(16)));
  1094. record.setTimestampLocation(cursor.getLong(17));
  1095. return record;
  1096. }
  1097. /**
  1098. * Gets all received {@link Record Records} for the specified information in
  1099. * the LogFilter ordered by date.
  1100. *
  1101. * @return A ArrayList with all received {@link Record Records} for the
  1102. * LogFilter.
  1103. */
  1104. public synchronized ArrayList<Record> getRecordsForFilter(LogFilter filter) {
  1105. ArrayList<Record> recordList = new ArrayList<Record>();
  1106. String selectQuery = this.selectionQueryFromFilter(filter, "*");
  1107. SQLiteDatabase db = this.getReadableDatabase();
  1108. Cursor cursor = db.rawQuery(selectQuery, null);
  1109. // looping through all rows and adding to list
  1110. if (cursor.moveToFirst()) {
  1111. do {
  1112. Record record = createRecord(cursor);
  1113. // Adding record to list
  1114. recordList.add(record);
  1115. } while (cursor.moveToNext());
  1116. }
  1117. cursor.close();
  1118. // return record list
  1119. db.close();
  1120. return recordList;
  1121. }
  1122. /**
  1123. * Returns the query for the given filter.
  1124. * @param filter (LogFilter)
  1125. * @param selectionString (String) for everything: "*"
  1126. * @return (String) query string
  1127. */
  1128. public String selectionQueryFromFilter(LogFilter filter, String selectionString)
  1129. {
  1130. String selectQuery = "SELECT " + selectionString + " FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  1131. + ")";
  1132. if (filter == null) return selectQuery;
  1133. // TIMESTAMPS
  1134. selectQuery = selectQuery + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  1135. selectQuery = selectQuery + " < " + filter.getBelowTimestamp();
  1136. selectQuery = selectQuery + " AND " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  1137. selectQuery = selectQuery + " > " + filter.getAboveTimestamp();
  1138. if (filter.getBSSIDs() != null && filter.getBSSIDs().size() > 0) {
  1139. selectQuery = selectQuery + " AND ";
  1140. selectQuery = selectQuery + filter.getBSSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID);
  1141. }
  1142. if (filter.getESSIDs() != null && filter.getESSIDs().size() > 0) {
  1143. selectQuery = selectQuery + " AND ";
  1144. selectQuery = selectQuery + filter.getESSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_SSID);
  1145. }
  1146. if (filter.getProtocols() != null && filter.getProtocols().size() > 0) {
  1147. selectQuery = selectQuery + " AND ";
  1148. selectQuery = selectQuery + filter.getProtocolsQueryStatement(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_PROTOCOL);
  1149. }
  1150. selectQuery = selectQuery + " GROUP BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID;
  1151. if (filter.getSorttype() == LogFilter.SortType.packet_timestamp) {
  1152. // DESC
  1153. selectQuery = selectQuery + " ORDER BY " + filter.getSorttype() + " DESC";
  1154. } else {
  1155. selectQuery = selectQuery + " ORDER BY " + filter.getSorttype();
  1156. }
  1157. System.out.println(selectQuery);
  1158. return selectQuery;
  1159. }
  1160. /**
  1161. * Returns the Conversation of a specific attack id
  1162. *
  1163. * @param attack_id Tha attack id to match the query against.
  1164. *
  1165. * @return A arraylist with all {@link Record Records}s for an attack id.
  1166. */
  1167. public synchronized ArrayList<Record> getConversationForAttackID(long attack_id) {
  1168. ArrayList<Record> recordList = new ArrayList<Record>();
  1169. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  1170. + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id;
  1171. SQLiteDatabase db = this.getReadableDatabase();
  1172. Cursor cursor = db.rawQuery(selectQuery, null);
  1173. if (cursor.moveToFirst()) {
  1174. do {
  1175. Record record = createRecord(cursor);
  1176. recordList.add(record);
  1177. } while (cursor.moveToNext());
  1178. }
  1179. cursor.close();
  1180. db.close();
  1181. return recordList;
  1182. }
  1183. /**
  1184. * Gets a single {@link Record} with the given attack id from the database.
  1185. *
  1186. * @param attack_id
  1187. * The attack id of the {@link Record};
  1188. * @return The {@link Record}.
  1189. */
  1190. public synchronized Record getRecordOfAttackId(long attack_id) {
  1191. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  1192. + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id + " GROUP BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ID;
  1193. SQLiteDatabase db = this.getReadableDatabase();
  1194. Cursor cursor = db.rawQuery(selectQuery, null);
  1195. Record record = null;
  1196. if (cursor.moveToFirst()) {
  1197. record = createRecord(cursor);
  1198. }
  1199. cursor.close();
  1200. // return record list
  1201. db.close();
  1202. return record;
  1203. }
  1204. /**
  1205. * Gets a single {@link Record} with the given ID from the database.
  1206. *
  1207. * @param id
  1208. * The ID of the {@link Record};
  1209. * @return The {@link Record}.
  1210. */
  1211. public synchronized Record getRecord(int id) {
  1212. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + PacketEntry.COLUMN_NAME_ATTACK_ID
  1213. + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ID + " = " + id;
  1214. SQLiteDatabase db = this.getReadableDatabase();
  1215. Cursor cursor = db.rawQuery(selectQuery, null);
  1216. Record record = null;
  1217. if (cursor.moveToFirst()) {
  1218. record = createRecord(cursor);
  1219. }
  1220. cursor.close();
  1221. db.close();
  1222. // return contact
  1223. return record;
  1224. }
  1225. /**
  1226. * Gets all {@link Record Records} saved in the database.
  1227. *
  1228. * @return A ArrayList of all the {@link Record Records} in the Database.
  1229. */
  1230. public synchronized ArrayList<Record> getAllRecords() {
  1231. ArrayList<Record> recordList = new ArrayList<Record>();
  1232. // Select All Query
  1233. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  1234. + ")";
  1235. SQLiteDatabase db = this.getWritableDatabase();
  1236. Cursor cursor = db.rawQuery(selectQuery, null);
  1237. Log.i("Database", "Start loop");
  1238. // looping through all rows and adding to list
  1239. if (cursor.moveToFirst()) {
  1240. do {
  1241. Log.i("Database", "Add Record");
  1242. Record record = createRecord(cursor);
  1243. // Adding record to list
  1244. recordList.add(record);
  1245. } while (cursor.moveToNext());
  1246. }
  1247. cursor.close();
  1248. db.close();
  1249. // return record list
  1250. return recordList;
  1251. }
  1252. /**
  1253. * Gets all non duplicate Records For the key BSSID.
  1254. *
  1255. * @return A ArrayList with received Records.
  1256. */
  1257. public synchronized ArrayList<String> getUniqueBSSIDRecords() {
  1258. return this.getUniqueDataEntryForKeyType(NetworkEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME);
  1259. }
  1260. /**
  1261. * Gets all non duplicate Records For the key ESSID.
  1262. *
  1263. * @return A ArrayList with received Records.
  1264. */
  1265. public synchronized ArrayList<String> getUniqueESSIDRecords() {
  1266. return this.getUniqueDataEntryForKeyType(NetworkEntry.COLUMN_NAME_SSID, NetworkEntry.TABLE_NAME);
  1267. }
  1268. public synchronized ArrayList<String> getUniqueESSIDRecordsForProtocol(String protocol) {
  1269. return this.getUniqueIDForProtocol(NetworkEntry.COLUMN_NAME_SSID, protocol);
  1270. }
  1271. public synchronized ArrayList<String> getUniqueBSSIDRecordsForProtocol(String protocol) {
  1272. return this.getUniqueIDForProtocol(NetworkEntry.COLUMN_NAME_BSSID, protocol);
  1273. }
  1274. private synchronized ArrayList<String> getUniqueIDForProtocol(String id, String protocol) {
  1275. ArrayList<String> recordList = new ArrayList<String>();
  1276. String selectQuery = "SELECT DISTINCT " + id + " FROM " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID + ") " + " WHERE "
  1277. + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_PROTOCOL + " = " + "'" + protocol + "'" + " ORDER BY " + id; // " NATURAL JOIN "
  1278. // +
  1279. // TABLE_ATTACK_INFO
  1280. // +
  1281. // " NATURAL JOIN "
  1282. // +
  1283. // TABLE_BSSIDS
  1284. // +
  1285. // " NATURAL JOIN "
  1286. // +
  1287. // TABLE_PORTS
  1288. // +
  1289. // ORDERED BY TIME
  1290. System.out.println(selectQuery);
  1291. SQLiteDatabase db = this.getReadableDatabase();
  1292. Cursor cursor = db.rawQuery(selectQuery, null);
  1293. // looping through all rows and adding to list
  1294. if (cursor.moveToFirst()) {
  1295. do {
  1296. String record = cursor.getString(0);
  1297. recordList.add(record);
  1298. } while (cursor.moveToNext());
  1299. }
  1300. cursor.close();
  1301. // return record list
  1302. db.close();
  1303. return recordList;
  1304. }
  1305. /**
  1306. * Gets all non duplicate Data Entry For a specific KeyType ( e.g. BSSIDs).
  1307. *
  1308. * @return A ArrayList with received Records.
  1309. */
  1310. public synchronized ArrayList<String> getUniqueDataEntryForKeyType(String keyType, String table) {
  1311. ArrayList<String> recordList = new ArrayList<String>();
  1312. // String selectQuery = "SELECT * FROM " + TABLE_RECORDS +
  1313. // " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " +
  1314. // TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS;
  1315. String selectQuery = "SELECT DISTINCT " + keyType + " FROM " + table + " ORDER BY " + keyType; // " NATURAL JOIN "
  1316. // +
  1317. // TABLE_ATTACK_INFO
  1318. // +
  1319. // " NATURAL JOIN "
  1320. // +
  1321. // TABLE_BSSIDS
  1322. // +
  1323. // " NATURAL JOIN "
  1324. // +
  1325. // TABLE_PORTS
  1326. // +
  1327. // ORDERED BY TIME
  1328. System.out.println(selectQuery);
  1329. SQLiteDatabase db = this.getReadableDatabase();
  1330. Cursor cursor = db.rawQuery(selectQuery, null);
  1331. // looping through all rows and adding to list
  1332. if (cursor.moveToFirst()) {
  1333. do {
  1334. String record = cursor.getString(0);
  1335. recordList.add(record);
  1336. } while (cursor.moveToNext());
  1337. }
  1338. cursor.close();
  1339. // return record list
  1340. db.close();
  1341. return recordList;
  1342. }
  1343. //TODO PROFILE DATABASE QUERIES - STILL NEEDED?
  1344. /**
  1345. * Retrieves all the profiles from the database
  1346. *
  1347. * @return list of profiles
  1348. */
  1349. public synchronized List<Profile> getAllProfiles() {
  1350. List<Profile> profiles = new LinkedList<Profile>();
  1351. // Select All Query
  1352. String selectQuery = "SELECT * FROM " + ProfileEntry.TABLE_NAME;
  1353. SQLiteDatabase db = this.getWritableDatabase();
  1354. Cursor cursor = db.rawQuery(selectQuery, null);
  1355. // looping through all rows and adding to list
  1356. if (cursor.moveToFirst()) {
  1357. do {
  1358. Profile profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
  1359. if (cursor.getInt(6) == 1) {
  1360. profile.mActivated = true;
  1361. }
  1362. profile.mIconName = cursor.getString(4);
  1363. // Adding record to list
  1364. profiles.add(profile);
  1365. } while (cursor.moveToNext());
  1366. }
  1367. cursor.close();
  1368. db.close();
  1369. // return record list
  1370. return profiles;
  1371. }
  1372. /**
  1373. * Persists the given profile into the database
  1374. *
  1375. * @param profile
  1376. * the profile which should be persisted
  1377. *
  1378. * @return
  1379. */
  1380. public synchronized long persistProfile(Profile profile) {
  1381. SQLiteDatabase db = this.getReadableDatabase();
  1382. ContentValues values = new ContentValues();
  1383. if (profile.mId != -1) {
  1384. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ID, profile.mId);
  1385. }
  1386. values.put(ProfileEntry.COLUMN_NAME_PROFILE_NAME, profile.mLabel);
  1387. values.put(ProfileEntry.COLUMN_NAME_PROFILE_DESCRIPTION, profile.mText);
  1388. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ICON, profile.mIconPath);
  1389. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ICON_NAME, profile.mIconName);
  1390. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ACTIVE, profile.mActivated);
  1391. values.put(ProfileEntry.COLUMN_NAME_PROFILE_EDITABLE, profile.mEditable);
  1392. return db.replace(ProfileEntry.TABLE_NAME, null, values);
  1393. }
  1394. /**
  1395. * private static final String CREATE_PROFILE_TABLE = "CREATE TABLE " +
  1396. * TABLE_PROFILES + "(" + KEY_PROFILE_ID +
  1397. * " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_PROFILE_NAME + " TEXT," +
  1398. * KEY_PROFILE_DESCRIPTION + " TEXT," + KEY_PROFILE_ICON + " TEXT," +
  1399. * KEY_PROFILE_ICON_ID + " INTEGER," + KEY_PROFILE_EDITABLE + " INTEGER," +
  1400. * KEY_PROFILE_ACTIVE + " INTEGER" + ")";
  1401. */
  1402. public synchronized Profile getProfile(int id) {
  1403. String selectQuery = "SELECT * FROM " + ProfileEntry.TABLE_NAME + " WHERE " + ProfileEntry.TABLE_NAME + "." + ProfileEntry.COLUMN_NAME_PROFILE_ID + " = " + id;
  1404. SQLiteDatabase db = this.getReadableDatabase();
  1405. Cursor cursor = db.rawQuery(selectQuery, null);
  1406. Profile profile = null;
  1407. if (cursor.moveToFirst()) {
  1408. profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
  1409. if (cursor.getInt(6) == 1) {
  1410. profile.mActivated = true;
  1411. }
  1412. profile.mIconName = cursor.getString(5);
  1413. }
  1414. cursor.close();
  1415. db.close();
  1416. // return contact
  1417. return profile;
  1418. }
  1419. public synchronized void deleteProfile(int id) {
  1420. SQLiteDatabase db = this.getReadableDatabase();
  1421. db.delete(ProfileEntry.TABLE_NAME, ProfileEntry.COLUMN_NAME_PROFILE_ID + "=?", new String[] { String.valueOf(id) });
  1422. }
  1423. /**
  1424. * Gets all received {@link Record Records} for every attack identified by
  1425. * its attack id and ordered by date.
  1426. *
  1427. * @return A ArrayList with all received {@link Record Records} for each
  1428. * attack id in the Database.
  1429. */
  1430. public synchronized ArrayList<Record> getAllReceivedRecordsOfEachAttack() {
  1431. ArrayList<Record> recordList = new ArrayList<Record>();
  1432. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  1433. + ")" + " WHERE " + PacketEntry.COLUMN_NAME_TYPE + "='RECEIVE'" + " ORDER BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  1434. SQLiteDatabase db = this.getReadableDatabase();
  1435. Cursor cursor = db.rawQuery(selectQuery, null);
  1436. // looping through all rows and adding to list
  1437. if (cursor.moveToFirst()) {
  1438. do {
  1439. Record record = createRecord(cursor);
  1440. // Adding record to list
  1441. recordList.add(record);
  1442. } while (cursor.moveToNext());
  1443. }
  1444. cursor.close();
  1445. // return record list
  1446. db.close();
  1447. return recordList;
  1448. }
  1449. /**
  1450. * Returns PlotComparisionItems for attacks per essid.
  1451. * @param filter (LogFilter) filter object
  1452. * @return ArrayList<PlotComparisonItem>
  1453. */
  1454. public synchronized ArrayList<PlotComparisonItem> attacksPerESSID(LogFilter filter) {
  1455. String filterQuery = this.selectionQueryFromFilter(filter, AttackEntry.COLUMN_NAME_ATTACK_ID);
  1456. String attackPerESSID_Query = "SELECT " + NetworkEntry.COLUMN_NAME_SSID + " , " + "COUNT( " + AttackEntry.COLUMN_NAME_ATTACK_ID + " ) " + " "
  1457. + " FROM " + AttackEntry.TABLE_NAME + " a " + " , " + NetworkEntry.TABLE_NAME
  1458. + " WHERE " + " a." + AttackEntry.COLUMN_NAME_ATTACK_ID + " IN " + " ( " + filterQuery + " ) "
  1459. + " GROUP BY " + NetworkEntry.TABLE_NAME+"."+NetworkEntry.COLUMN_NAME_SSID;
  1460. SQLiteDatabase db = this.getReadableDatabase();
  1461. Cursor cursor = db.rawQuery(attackPerESSID_Query, null);
  1462. ArrayList<PlotComparisonItem> plots = new ArrayList<PlotComparisonItem>();
  1463. int counter = 0;
  1464. if (cursor.moveToFirst()) {
  1465. do {
  1466. String title = cursor.getString(0); // COLUMN_NAME_SSID
  1467. double value = cursor.getDouble(1); // COUNT
  1468. if (value == 0.) continue;
  1469. PlotComparisonItem plotItem = new PlotComparisonItem(title, this.getColor(counter), 0. , value);
  1470. plots.add(plotItem);
  1471. counter++;
  1472. } while (cursor.moveToNext());
  1473. }
  1474. cursor.close();
  1475. db.close();
  1476. return plots;
  1477. }
  1478. /**
  1479. * Creates a {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice} from a Cursor. If the cursor does not show to a
  1480. * valid data structure a runtime exception is thrown.
  1481. *
  1482. * @param cursor the cursor
  1483. * @return Returns the created {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice} .
  1484. */
  1485. private synchronized SyncDevice createSyncDevice(Cursor cursor) {
  1486. SyncDevice record = new SyncDevice();
  1487. record.setDeviceID(cursor.getString(0));
  1488. record.setLast_sync_timestamp(cursor.getLong(1));
  1489. record.setHighest_attack_id(cursor.getLong(2));
  1490. return record;
  1491. }
  1492. /**
  1493. * Returns all missing / newly inserted and updated {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s.
  1494. * @param oldDevices array of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
  1495. * @param includeMissing boolean
  1496. * @return array of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
  1497. */
  1498. public ArrayList<SyncDevice> getUpdatedDevicesFor(List<SyncDevice> oldDevices, boolean includeMissing){
  1499. HashMap<String, Long> oldDeviceMap = new HashMap<String, Long>();
  1500. for (SyncDevice d : oldDevices){
  1501. oldDeviceMap.put(d.getDeviceID(),d.getHighest_attack_id());
  1502. }
  1503. ArrayList<SyncDevice> recordList = new ArrayList<SyncDevice>();
  1504. String selectQuery = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
  1505. SQLiteDatabase db = this.getReadableDatabase();
  1506. Cursor cursor = db.rawQuery(selectQuery, null);
  1507. // looping through all rows and adding to list
  1508. if (cursor.moveToFirst()) {
  1509. do {
  1510. SyncDevice record = createSyncDevice(cursor);
  1511. // Adding record to list
  1512. if (oldDeviceMap.containsKey(record.getDeviceID())){
  1513. Long oldSyncId = oldDeviceMap.get(record.getDeviceID());
  1514. if (oldSyncId < record.getHighest_attack_id()){
  1515. recordList.add(record);
  1516. }
  1517. } else {
  1518. if (includeMissing)
  1519. recordList.add(record);
  1520. }
  1521. } while (cursor.moveToNext());
  1522. }
  1523. cursor.close();
  1524. // return record list
  1525. db.close();
  1526. return recordList;
  1527. }
  1528. /**
  1529. * Returns all missing / newly inserted and updated {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s.
  1530. * @param oldDeviceMap map with device id and max sync_id for the device
  1531. * @param includeMissing boolean
  1532. * @return array of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
  1533. */
  1534. public ArrayList<SyncDevice> getUpdatedDevicesFor(HashMap<String, Long> oldDeviceMap, boolean includeMissing){
  1535. ArrayList<SyncDevice> recordList = new ArrayList<SyncDevice>();
  1536. String selectQuery = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
  1537. SQLiteDatabase db = this.getReadableDatabase();
  1538. Cursor cursor = db.rawQuery(selectQuery, null);
  1539. // looping through all rows and adding to list
  1540. if (cursor.moveToFirst()) {
  1541. do {
  1542. SyncDevice record = createSyncDevice(cursor);
  1543. // Adding record to list
  1544. if (oldDeviceMap.containsKey(record.getDeviceID())){
  1545. Long oldSyncId = oldDeviceMap.get(record.getDeviceID());
  1546. if (oldSyncId < record.getHighest_attack_id()){
  1547. recordList.add(record);
  1548. }
  1549. } else {
  1550. if (includeMissing)
  1551. recordList.add(record);
  1552. }
  1553. } while (cursor.moveToNext());
  1554. }
  1555. cursor.close();
  1556. // return record list
  1557. db.close();
  1558. return recordList;
  1559. }
  1560. /**
  1561. * Returns all device ids.
  1562. * @return list of all device ids.
  1563. */
  1564. public ArrayList<String> getAllDevicesIds(){
  1565. String selectQuery = "SELECT "+ SyncDeviceEntry.COLUMN_NAME_DEVICE_ID+" FROM " + SyncDeviceEntry.TABLE_NAME;
  1566. SQLiteDatabase db = this.getReadableDatabase();
  1567. Cursor cursor = db.rawQuery(selectQuery, null);
  1568. ArrayList<String> ids = new ArrayList<String>();
  1569. // looping through all rows and adding to list
  1570. if (cursor.moveToFirst()) {
  1571. do {
  1572. String s = cursor.getString(0);
  1573. ids.add(s);
  1574. } while (cursor.moveToNext());
  1575. }
  1576. cursor.close();
  1577. // return record list
  1578. db.close();
  1579. return ids;
  1580. }
  1581. /***
  1582. * Returns all missing devices ids
  1583. * @param devices owned device ids
  1584. * @return list of missing devices ids
  1585. */
  1586. public ArrayList<String> getMissingDeviceIds(ArrayList<String> devices){
  1587. ArrayList<String> ids = new ArrayList<String>();
  1588. String selectQuery = "SELECT "+ SyncDeviceEntry.COLUMN_NAME_DEVICE_ID +" FROM " + SyncDeviceEntry.TABLE_NAME + " D WHERE D." + SyncDeviceEntry.COLUMN_NAME_DEVICE_ID + " NOT IN " + devices;
  1589. SQLiteDatabase db = this.getReadableDatabase();
  1590. Cursor cursor = db.rawQuery(selectQuery, null);
  1591. if (cursor.moveToFirst()) {
  1592. do {
  1593. String deviceID = cursor.getString(0);
  1594. ids.add(deviceID);
  1595. } while (cursor.moveToNext());
  1596. }
  1597. cursor.close();
  1598. db.close();
  1599. return ids;
  1600. }
  1601. /**
  1602. * Returns all new {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s for the given devices (including all missing devices).
  1603. * @param devices {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}
  1604. * @param includeMissingDevices boolean
  1605. * @return list of {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s
  1606. */
  1607. public ArrayList<SyncRecord> getUnsyncedAttacksFor(List<SyncDevice> devices, boolean includeMissingDevices){
  1608. ArrayList<SyncDevice> updatedDevices = this.getUpdatedDevicesFor(devices, includeMissingDevices);
  1609. ArrayList<SyncRecord> recordList = new ArrayList<SyncRecord>();
  1610. SQLiteDatabase db = this.getReadableDatabase();
  1611. for (SyncDevice sDevice : updatedDevices){
  1612. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A " + " NATURAL JOIN " + PacketEntry.TABLE_NAME + " P "
  1613. + " WHERE "
  1614. +" ( "
  1615. + "A." + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + " P."+ PacketEntry.COLUMN_NAME_ATTACK_ID
  1616. + " AND " + " A." + AttackEntry.COLUMN_NAME_DEVICE + " = " + "'" + sDevice.getDeviceID() + "'"
  1617. + " AND " + " A." + AttackEntry.COLUMN_NAME_SYNC_ID + " > " + sDevice.getHighest_attack_id()
  1618. + " ) "
  1619. //+ " GROUP BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_DEVICE
  1620. + " ORDER BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_SYNC_ID + " DESC ";
  1621. Cursor cursor = db.rawQuery(selectQuery, null);
  1622. // looping through all rows and adding to list
  1623. if (cursor != null){
  1624. if (cursor.moveToFirst()) {
  1625. do {
  1626. SyncRecord record = createSyncRecord(cursor);
  1627. recordList.add(record);
  1628. } while (cursor.moveToNext());
  1629. }
  1630. cursor.close();
  1631. }
  1632. }
  1633. // return record list
  1634. db.close();
  1635. return recordList;
  1636. }
  1637. /**
  1638. * Returns all new {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s for the given devices (including all missing devices).
  1639. * @param deviceMap map of device id and max sync_id of it
  1640. * @param includeMissingDevices boolean
  1641. * @return list of {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s
  1642. */
  1643. public ArrayList<SyncRecord> getUnsyncedAttacksFor(HashMap<String,Long> deviceMap, boolean includeMissingDevices){
  1644. ArrayList<SyncDevice> updatedDevices = this.getUpdatedDevicesFor(deviceMap, includeMissingDevices);
  1645. ArrayList<SyncRecord> recordList = new ArrayList<SyncRecord>();
  1646. SQLiteDatabase db = this.getReadableDatabase();
  1647. for (SyncDevice sDevice : updatedDevices){
  1648. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A " + " NATURAL JOIN " + PacketEntry.TABLE_NAME + " P "
  1649. + " WHERE "
  1650. +" ( "
  1651. + "A." + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + " P."+ PacketEntry.COLUMN_NAME_ATTACK_ID
  1652. + " AND " + " A." + AttackEntry.COLUMN_NAME_DEVICE + " = " + "'" + sDevice.getDeviceID() + "'"
  1653. + " AND " + " A." + AttackEntry.COLUMN_NAME_SYNC_ID + " > " + sDevice.getHighest_attack_id()
  1654. + " ) "
  1655. //+ " GROUP BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_DEVICE
  1656. + " ORDER BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_SYNC_ID + " DESC ";
  1657. Cursor cursor = db.rawQuery(selectQuery, null);
  1658. // looping through all rows and adding to list
  1659. if (cursor != null){
  1660. if (cursor.moveToFirst()) {
  1661. do {
  1662. SyncRecord record = createSyncRecord(cursor);
  1663. recordList.add(record);
  1664. } while (cursor.moveToNext());
  1665. }
  1666. cursor.close();
  1667. }
  1668. }
  1669. // return record list
  1670. db.close();
  1671. return recordList;
  1672. }
  1673. /**
  1674. * Attacks per BSSID
  1675. * @param filter (LogFilter) query filter
  1676. * @return ArrayList<PlotComparisonItem>
  1677. */
  1678. public synchronized ArrayList<PlotComparisonItem> attacksPerBSSID(LogFilter filter) {
  1679. String filterQuery = this.selectionQueryFromFilter(filter, AttackEntry.COLUMN_NAME_ATTACK_ID);
  1680. String attackPerBSSID_Query = "SELECT " + NetworkEntry.COLUMN_NAME_BSSID + " , " + "COUNT( " + AttackEntry.COLUMN_NAME_ATTACK_ID + " ) " + " "
  1681. + " FROM " + AttackEntry.TABLE_NAME + " a " + " , " + NetworkEntry.TABLE_NAME
  1682. + " WHERE " + " a." + AttackEntry.COLUMN_NAME_ATTACK_ID + " IN " + " ( " + filterQuery + " ) "
  1683. + " GROUP BY " + NetworkEntry.TABLE_NAME + "." + NetworkEntry.COLUMN_NAME_BSSID;
  1684. SQLiteDatabase db = this.getReadableDatabase();
  1685. Cursor cursor = db.rawQuery(attackPerBSSID_Query, null);
  1686. ArrayList<PlotComparisonItem> plots = new ArrayList<PlotComparisonItem>();
  1687. int counter = 0;
  1688. if (cursor.moveToFirst()) {
  1689. do {
  1690. String title = cursor.getString(0); // COLUMN_NAME_BSSID
  1691. double value = cursor.getDouble(1); // COUNT
  1692. if (value == 0.) continue;
  1693. PlotComparisonItem plotItem = new PlotComparisonItem(title, this.getColor(counter), 0. , value);
  1694. plots.add(plotItem);
  1695. counter++;
  1696. } while (cursor.moveToNext());
  1697. }
  1698. cursor.close();
  1699. db.close();
  1700. return plots;
  1701. }
  1702. /**
  1703. * Inserts the given devices in the database with save.
  1704. * @param devices list of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
  1705. */
  1706. public void insertSyncDevices(List<SyncDevice> devices){
  1707. SQLiteDatabase db = this.getWritableDatabase();
  1708. db.beginTransaction();
  1709. try {
  1710. for (SyncDevice device : devices){
  1711. insertSyncDevice(device, db);
  1712. }
  1713. db.setTransactionSuccessful();
  1714. } finally {
  1715. db.endTransaction();
  1716. }
  1717. db.close(); // Closing database connection
  1718. }
  1719. /**
  1720. * Inserts the given dives in the given SQLite Database without save.
  1721. * @param device {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}
  1722. * @param db {@link android.database.sqlite.SQLiteDatabase}
  1723. */
  1724. private void insertSyncDevice(SyncDevice device, SQLiteDatabase db){
  1725. ContentValues recordValues = new ContentValues();
  1726. recordValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, device.getDeviceID());
  1727. recordValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, device.getLast_sync_timestamp());
  1728. recordValues.put(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID, device.getHighest_attack_id());
  1729. // Inserting Rows
  1730. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, recordValues, SQLiteDatabase.CONFLICT_REPLACE);
  1731. }
  1732. /** Returns the color for the given index
  1733. * @return int color*/
  1734. public Integer getColor(int index) {
  1735. return ColorSequenceGenerator.getColorForIndex(index);
  1736. }
  1737. }