HostageDBOpenHelper.java 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  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 prefix = " WHERE " + "N."+NetworkEntry.COLUMN_NAME_BSSID + " NOT IN ";
  410. String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " N " + this.arrayToSQLString(otherBSSIDs, prefix);
  411. SQLiteDatabase db = this.getReadableDatabase();
  412. Cursor cursor = db.rawQuery(selectQuery, null);
  413. ArrayList<NetworkRecord> networkInformation = new ArrayList<NetworkRecord>();
  414. // looping through all rows and adding to list
  415. if (cursor.moveToFirst()) {
  416. do {
  417. NetworkRecord record = this.createNetworkRecord(cursor);
  418. networkInformation.add(record);
  419. } while (cursor.moveToNext());
  420. }
  421. cursor.close();
  422. db.close();
  423. return networkInformation;
  424. }
  425. private String arrayToSQLString(ArrayList<String> a, String prefix)
  426. {
  427. String sql = "";
  428. if (a.size() !=0){
  429. sql = sql + prefix;
  430. sql = sql + " ( ";
  431. int i = 0;
  432. for (String s : a){
  433. i++;
  434. sql = sql + "'" +s + "'";
  435. if (i != a.size()){
  436. sql = sql + ",";
  437. }
  438. }
  439. sql = sql + " ) ";
  440. }
  441. return sql;
  442. }
  443. /**
  444. * Determines the number of different attacks in the database.
  445. *
  446. * @return The number of different attacks in the database.
  447. */
  448. public synchronized int getAttackCount() {
  449. SQLiteDatabase db = this.getReadableDatabase();
  450. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  451. " WHERE " + AttackEntry.COLUMN_NAME_PROTOCOL + " <> ?";
  452. Cursor cursor = db.rawQuery(countQuery, new String[]{"PORTSCAN"});
  453. int result = cursor.getCount();
  454. cursor.close();
  455. // return count
  456. db.close();
  457. return result;
  458. }
  459. /**
  460. * Determines the number of different recorded attacks in a specific access point since the given attack_id.
  461. * The given attack_id is not included.
  462. * @param attack_id The attack id to match the query against.
  463. * @param bssid The BSSID of the access point.
  464. * @return The number of different attacks in the database since the given attack_id.
  465. */
  466. public synchronized int getAttackCount(int attack_id, String bssid) {
  467. SQLiteDatabase db = this.getReadableDatabase();
  468. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  469. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " <> ? " +
  470. "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? " +
  471. "AND " + AttackEntry.COLUMN_NAME_BSSID + " = ?";
  472. String[] selectArgs = new String[]{"PORTSCAN", attack_id + "", bssid};
  473. Cursor cursor = db.rawQuery(countQuery, selectArgs);
  474. int result = cursor.getCount();
  475. cursor.close();
  476. // return count
  477. db.close();
  478. return result;
  479. }
  480. /**
  481. * Determines the number of different attacks for a specific protocol in
  482. * the database.
  483. *
  484. * @param protocol
  485. * The String representation of the
  486. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  487. * Protocol}
  488. * @return The number of different attacks in the database.
  489. */
  490. public synchronized int getAttackPerProtocolCount(String protocol) {
  491. SQLiteDatabase db = this.getReadableDatabase();
  492. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  493. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? ";
  494. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol});
  495. int result = cursor.getCount();
  496. cursor.close();
  497. // return count
  498. db.close();
  499. return result;
  500. }
  501. /**
  502. * Determines the number of attacks for a specific protocol in
  503. * the database since the given attack_id.
  504. *
  505. * @param protocol
  506. * The String representation of the
  507. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  508. * Protocol}
  509. * @param attack_id The attack id to match the query against.
  510. * @return The number of different attacks in the database since the given attack_id.
  511. */
  512. public synchronized int getAttackPerProtocolCount(String protocol, int attack_id) {
  513. SQLiteDatabase db = this.getReadableDatabase();
  514. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  515. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? " +
  516. "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? ";
  517. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, attack_id + ""});
  518. int result = cursor.getCount();
  519. cursor.close();
  520. // return count
  521. db.close();
  522. return result;
  523. }
  524. /**
  525. * Determines the number of recorded attacks for a specific protocol and accesss point since the given attack_id.
  526. *
  527. * @param protocol
  528. * The String representation of the
  529. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  530. * Protocol}
  531. * @param attack_id The attack id to match the query against.
  532. * @param bssid The BSSID of the access point.
  533. * @return The number of different attacks in the database since the given attack_id.
  534. */
  535. public synchronized int getAttackPerProtocolCount(String protocol, int attack_id, String bssid) {
  536. SQLiteDatabase db = this.getReadableDatabase();
  537. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  538. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? " +
  539. "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? " +
  540. "AND " + AttackEntry.COLUMN_NAME_BSSID + " = ?";
  541. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, attack_id + "", bssid});
  542. int result = cursor.getCount();
  543. cursor.close();
  544. // return count
  545. db.close();
  546. return result;
  547. }
  548. /**
  549. * Determines the number of portscans stored in the database.
  550. *
  551. * @return The number of portscans stored in the database.
  552. */
  553. public synchronized int getPortscanCount() {
  554. return getAttackPerProtocolCount("PORTSCAN");
  555. }
  556. /**
  557. * Determines the number of recorded portscans since the given attack_id.
  558. * @param attack_id The attack id to match the query against.
  559. * @return The number of portscans stored in the database since the given attack_id.
  560. */
  561. public synchronized int getPortscanCount(int attack_id) {
  562. return getAttackPerProtocolCount("PORTSCAN", attack_id);
  563. }
  564. /**
  565. * Determines the number of recorded portscans in a specific access point since the given attack_id.
  566. * @param attack_id The attack id to match the query against.
  567. * @param bssid The BSSID of the access point.
  568. * @return The number of portscans stored in the database since the given attack_id.
  569. */
  570. public synchronized int getPortscanCount(int attack_id, String bssid) {
  571. return getAttackPerProtocolCount("PORTSCAN", attack_id, bssid);
  572. }
  573. /**
  574. * Determines the number of {@link Record Records} in the database.
  575. *
  576. * @return The number of {@link Record Records} in the database.
  577. */
  578. public synchronized int getRecordCount() {
  579. String countQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME;
  580. SQLiteDatabase db = this.getReadableDatabase();
  581. Cursor cursor = db.rawQuery(countQuery, null);
  582. int result = cursor.getCount();
  583. cursor.close();
  584. // return count
  585. db.close();
  586. return result;
  587. }
  588. //TODO ADD AGAIN ?
  589. /**
  590. * Returns the {@link AttackRecord} with the given attack id from the database.
  591. *
  592. * @param attack_id
  593. * The attack id of the {@link Record};
  594. * @return The {@link Record}.
  595. */
  596. /*
  597. public AttackRecord getRecordOfAttackId(long attack_id) {
  598. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " WHERE " + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id;
  599. SQLiteDatabase db = this.getReadableDatabase();
  600. Cursor cursor = db.rawQuery(selectQuery, null);
  601. AttackRecord record = null;
  602. if (cursor.moveToFirst()) {
  603. record = createAttackRecord(cursor);
  604. }
  605. cursor.close();
  606. // return record list
  607. db.close();
  608. return record;
  609. } */
  610. /**
  611. * Gets a {@link AttackRecord} for every attack identified by its attack id.
  612. *
  613. * @return A ArrayList with a {@link AttackRecord} for each attack id in the Database.
  614. */
  615. public synchronized ArrayList<AttackRecord> getRecordOfEachAttack() {
  616. ArrayList<AttackRecord> recordList = new ArrayList<AttackRecord>();
  617. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME;
  618. SQLiteDatabase db = this.getReadableDatabase();
  619. Cursor cursor = db.rawQuery(selectQuery, null);
  620. // looping through all rows and adding to list
  621. if (cursor.moveToFirst()) {
  622. do {
  623. AttackRecord record = createAttackRecord(cursor);
  624. // Adding record to list
  625. recordList.add(record);
  626. } while (cursor.moveToNext());
  627. }
  628. cursor.close();
  629. // return record list
  630. db.close();
  631. return recordList;
  632. }
  633. /**
  634. * Gets a AttackRecord for every attack with a higher attack id than the specified.
  635. *
  636. * @param attack_id
  637. * The attack id to match the query against.
  638. * @return A ArrayList with one {@link AttackRecord} for each attack id
  639. * higher than the given.
  640. */
  641. public synchronized ArrayList<AttackRecord> getRecordOfEachAttack(long attack_id) {
  642. ArrayList<AttackRecord> recordList = new ArrayList<AttackRecord>();
  643. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " WHERE " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > " + attack_id;
  644. SQLiteDatabase db = this.getReadableDatabase();
  645. Cursor cursor = db.rawQuery(selectQuery, null);
  646. // looping through all rows and adding to list
  647. if (cursor.moveToFirst()) {
  648. do {
  649. AttackRecord record = createAttackRecord(cursor);
  650. // Adding record to list
  651. recordList.add(record);
  652. } while (cursor.moveToNext());
  653. }
  654. cursor.close();
  655. // return count
  656. db.close();
  657. return recordList;
  658. }
  659. /**
  660. * Determines the highest attack id stored in the database.
  661. *
  662. * @return The highest attack id stored in the database.
  663. */
  664. public synchronized long getHighestAttackId() {
  665. String selectQuery = "SELECT MAX(" + 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. * Determines the smallest attack id stored in the database.
  680. *
  681. * @return The smallest attack id stored in the database.
  682. */
  683. public synchronized long getSmallestAttackId() {
  684. String selectQuery = "SELECT MIN(" + AttackEntry.COLUMN_NAME_ATTACK_ID + ") FROM " + AttackEntry.TABLE_NAME;
  685. SQLiteDatabase db = this.getReadableDatabase();
  686. Cursor cursor = db.rawQuery(selectQuery, null);
  687. int result;
  688. if (cursor.moveToFirst()) {
  689. result = cursor.getInt(0);
  690. } else {
  691. result = -1;
  692. }
  693. cursor.close();
  694. db.close();
  695. return result;
  696. }
  697. /**
  698. * Gets the last recorded SSID to a given BSSID.
  699. *
  700. * @param bssid
  701. * The BSSID to match against.
  702. * @return A String of the last SSID or null if the BSSID is not in the
  703. * database.
  704. */
  705. public synchronized String getSSID(String bssid) {
  706. String selectQuery = "SELECT " + NetworkEntry.COLUMN_NAME_SSID + " FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID
  707. + " = " + "'" + bssid + "'";
  708. SQLiteDatabase db = this.getReadableDatabase();
  709. Cursor cursor = db.rawQuery(selectQuery, null);
  710. String ssid = null;
  711. if (cursor.moveToFirst()) {
  712. ssid = cursor.getString(0);
  713. }
  714. cursor.close();
  715. db.close();
  716. return ssid;
  717. }
  718. /**
  719. * Gets all network related data stored in the database
  720. * @return An ArrayList with an Network for all Entry in the network table.
  721. */
  722. public synchronized ArrayList<NetworkRecord> getNetworkInformation() {
  723. String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME;
  724. SQLiteDatabase db = this.getReadableDatabase();
  725. Cursor cursor = db.rawQuery(selectQuery, null);
  726. ArrayList<NetworkRecord> networkInformation = new ArrayList<NetworkRecord>();
  727. // looping through all rows and adding to list
  728. if (cursor.moveToFirst()) {
  729. do {
  730. NetworkRecord record = this.createNetworkRecord(cursor);
  731. networkInformation.add(record);
  732. } while (cursor.moveToNext());
  733. }
  734. cursor.close();
  735. db.close();
  736. return networkInformation;
  737. }
  738. private NetworkRecord createNetworkRecord(Cursor cursor){
  739. NetworkRecord record = new NetworkRecord();
  740. record.setBssid(cursor.getString(0));
  741. record.setSsid(cursor.getString(1));
  742. record.setLatitude(Double.parseDouble(cursor.getString(2)));
  743. record.setLongitude(Double.parseDouble(cursor.getString(3)));
  744. record.setAccuracy(Float.parseFloat(cursor.getString(4)));
  745. record.setTimestampLocation(cursor.getLong(5));
  746. return record;
  747. }
  748. /**
  749. * Updates the network table with the information contained in the parameter.
  750. * @param networkInformation ArrayList of {@link NetworkRecord NetworkRecords}
  751. * @see {@link HostageDBOpenHelper#updateNetworkInformation(NetworkRecord record)}
  752. */
  753. public void updateNetworkInformation(ArrayList<NetworkRecord> networkInformation) {
  754. SQLiteDatabase db = this.getReadableDatabase();
  755. db.beginTransaction();
  756. try {
  757. for (NetworkRecord record : networkInformation) {
  758. String bssid = record.getBssid();
  759. String bssidQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
  760. Cursor cursor = db.rawQuery(bssidQuery, new String[] {bssid});
  761. if (!cursor.moveToFirst() || cursor.getLong(5) < record.getTimestampLocation()){
  762. ContentValues bssidValues = new ContentValues();
  763. bssidValues.put(NetworkEntry.COLUMN_NAME_BSSID, bssid);
  764. bssidValues.put(NetworkEntry.COLUMN_NAME_SSID, record.getSsid());
  765. bssidValues.put(NetworkEntry.COLUMN_NAME_LATITUDE, record.getLatitude());
  766. bssidValues.put(NetworkEntry.COLUMN_NAME_LONGITUDE, record.getLongitude());
  767. bssidValues.put(NetworkEntry.COLUMN_NAME_ACCURACY, record.getAccuracy());
  768. bssidValues.put(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP, record.getTimestampLocation());
  769. db.insertWithOnConflict(NetworkEntry.TABLE_NAME, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
  770. }
  771. cursor.close();
  772. }
  773. db.setTransactionSuccessful();
  774. } finally {
  775. db.endTransaction();
  776. }
  777. db.close();
  778. }
  779. /**
  780. * Updated the network table with a new {@link NetworkRecord}.
  781. * If information about this BSSID are already in the database,
  782. * the table will only be updated if the new {@link NetworkRecord }
  783. * has a newer location time stamp.
  784. * @param record The new {@link NetworkRecord}.
  785. */
  786. public void updateNetworkInformation(NetworkRecord record) {
  787. SQLiteDatabase db = this.getReadableDatabase();
  788. String bssid = record.getBssid();
  789. String bssidQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
  790. Cursor cursor = db.rawQuery(bssidQuery, new String[] {bssid});
  791. if (!cursor.moveToFirst() || cursor.getLong(5) < record.getTimestampLocation()){
  792. ContentValues bssidValues = new ContentValues();
  793. bssidValues.put(NetworkEntry.COLUMN_NAME_BSSID, bssid);
  794. bssidValues.put(NetworkEntry.COLUMN_NAME_SSID, record.getSsid());
  795. bssidValues.put(NetworkEntry.COLUMN_NAME_LATITUDE, record.getLatitude());
  796. bssidValues.put(NetworkEntry.COLUMN_NAME_LONGITUDE, record.getLongitude());
  797. bssidValues.put(NetworkEntry.COLUMN_NAME_ACCURACY, record.getAccuracy());
  798. bssidValues.put(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP, record.getTimestampLocation());
  799. db.insertWithOnConflict(NetworkEntry.TABLE_NAME, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
  800. }
  801. cursor.close();
  802. db.close();
  803. }
  804. /**
  805. * Updates the the timestamp of a single device id
  806. * @param deviceID The Device id
  807. * @param timestamp The synchronization timestamp
  808. */
  809. public void updateTimestampOfSyncDevice(String deviceID, long timestamp){
  810. SQLiteDatabase db = this.getReadableDatabase();
  811. ContentValues deviceValues = new ContentValues();
  812. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, deviceID);
  813. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, timestamp);
  814. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
  815. db.close();
  816. }
  817. /**
  818. * Updates the Timestamps of synchronization devices from a HashMap.
  819. * @param devices HashMap of device ids and their synchronization timestamps.
  820. */
  821. public void updateSyncDevices(HashMap<String, Long> devices){
  822. SQLiteDatabase db = this.getReadableDatabase();
  823. db.beginTransaction();
  824. try {
  825. for(String key : devices.keySet()){
  826. ContentValues deviceValues = new ContentValues();
  827. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, key);
  828. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, devices.get(key));
  829. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
  830. }
  831. db.setTransactionSuccessful();
  832. } finally {
  833. db.endTransaction();
  834. }
  835. db.close();
  836. }
  837. public void updateSyncDevices(ArrayList<SyncDevice> devices){
  838. SQLiteDatabase db = this.getReadableDatabase();
  839. db.beginTransaction();
  840. try {
  841. for(SyncDevice device : devices){
  842. ContentValues deviceValues = new ContentValues();
  843. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, device.getDeviceID());
  844. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, device.getLast_sync_timestamp());
  845. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID, device.getHighest_attack_id());
  846. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
  847. }
  848. db.setTransactionSuccessful();
  849. } finally {
  850. db.endTransaction();
  851. }
  852. db.close();
  853. }
  854. /**
  855. * Returns the own state containing all registered devices ids and their max sync_id
  856. * @return {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}
  857. */
  858. public SyncInfo getOwnState(){
  859. ArrayList<SyncDevice> devices = this.getSyncDevices();
  860. HashMap<String, Long> deviceMap = new HashMap<String, Long>();
  861. for (SyncDevice device : devices){
  862. deviceMap.put(device.getDeviceID(), device.getHighest_attack_id());
  863. }
  864. SyncInfo syncInfo = new SyncInfo();
  865. syncInfo.deviceMap = deviceMap;
  866. syncInfo.bssids = this.getAllBSSIDS();
  867. return syncInfo;
  868. }
  869. /**
  870. * Returns a HashMap of all devices that were previously synchronized with.
  871. * @return HashMap containing device id's and the last synchronization timestamp.
  872. */
  873. public synchronized HashMap<String, Long> getSyncDeviceHashMap(){
  874. SQLiteDatabase db = this.getReadableDatabase();
  875. HashMap<String, Long> devices = new HashMap<String, Long>();
  876. String query = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
  877. Cursor cursor = db.rawQuery(query, null);
  878. if (cursor.moveToFirst()) {
  879. do {
  880. devices.put(cursor.getString(0), cursor.getLong(1));
  881. } while (cursor.moveToNext());
  882. }
  883. cursor.close();
  884. db.close();
  885. return devices;
  886. }
  887. /**
  888. * Returns a HashMap of all devices that were previously synchronized with.
  889. * @return HashMap containing device id's and the last synchronization timestamp.
  890. */
  891. public synchronized ArrayList<SyncDevice> getSyncDevices(){
  892. SQLiteDatabase db = this.getReadableDatabase();
  893. ArrayList<SyncDevice> devices = new ArrayList<SyncDevice>();
  894. String query = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
  895. Cursor cursor = db.rawQuery(query, null);
  896. if (cursor.moveToFirst()) {
  897. do {
  898. SyncDevice device = this.createSyncDevice(cursor);
  899. devices.add(device);
  900. } while (cursor.moveToNext());
  901. }
  902. cursor.close();
  903. db.close();
  904. return devices;
  905. }
  906. /**
  907. * Returns a ArrayList containing all information stored in the SyncInfo table.
  908. * @return ArrayList<SyncInfo>
  909. */
  910. public synchronized ArrayList<SyncInfoRecord> getSyncInfo(){
  911. SQLiteDatabase db = this.getReadableDatabase();
  912. ArrayList<SyncInfoRecord> syncInfo = new ArrayList<SyncInfoRecord>();
  913. String query = "SELECT * FROM " + SyncInfoEntry.TABLE_NAME;
  914. Cursor cursor = db.rawQuery(query, null);
  915. if (cursor.moveToFirst()) {
  916. do {
  917. SyncInfoRecord info = new SyncInfoRecord();
  918. info.setDeviceID(cursor.getString(0));
  919. info.setBSSID(cursor.getString(1));
  920. info.setNumber_of_attacks(cursor.getLong(2));
  921. info.setNumber_of_portscans(cursor.getLong(3));
  922. syncInfo.add(info);
  923. } while (cursor.moveToNext());
  924. }
  925. cursor.close();
  926. db.close();
  927. return syncInfo;
  928. }
  929. /**
  930. * Updates the sync_info table with the information contained in the parameter.
  931. * @param syncInfos ArrayList of {@link SyncInfoRecord SyncInfoRecords}
  932. * @see {@link HostageDBOpenHelper#updateSyncInfo(SyncInfoRecord syncInfo)}
  933. */
  934. public synchronized void updateSyncInfo(ArrayList<SyncInfoRecord> syncInfos){
  935. SQLiteDatabase db = this.getReadableDatabase();
  936. db.beginTransaction();
  937. try {
  938. for(SyncInfoRecord syncInfo : syncInfos){
  939. ContentValues syncValues = new ContentValues();
  940. syncValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, syncInfo.getBSSID());
  941. syncValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, syncInfo.getDeviceID());
  942. syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, syncInfo.getNumber_of_attacks());
  943. syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, syncInfo.getNumber_of_portscans());
  944. db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, syncValues, SQLiteDatabase.CONFLICT_REPLACE);
  945. }
  946. db.setTransactionSuccessful();
  947. } finally {
  948. db.endTransaction();
  949. }
  950. db.close();
  951. }
  952. /**
  953. * Updated the network table with a new {@link SyncInfoRecord}.
  954. * Conflicting rows will be replaced.
  955. * @param syncInfo The new {@link NetworkRecord}.
  956. */
  957. public synchronized void updateSyncInfo(SyncInfoRecord syncInfo){
  958. SQLiteDatabase db = this.getReadableDatabase();
  959. ContentValues syncValues = new ContentValues();
  960. syncValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, syncInfo.getBSSID());
  961. syncValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, syncInfo.getDeviceID());
  962. syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, syncInfo.getNumber_of_attacks());
  963. syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, syncInfo.getNumber_of_portscans());
  964. db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, syncValues, SQLiteDatabase.CONFLICT_REPLACE);
  965. db.close();
  966. }
  967. /**
  968. * 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}
  969. */
  970. public void clearSyncInfos(){
  971. SQLiteDatabase db = this.getReadableDatabase();
  972. db.delete(SyncDeviceEntry.TABLE_NAME, null, null);
  973. db.delete(SyncInfoEntry.TABLE_NAME, null, null);
  974. db.close();
  975. }
  976. /**
  977. * Deletes all records from {@link PacketEntry}s and {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}.
  978. */
  979. public void clearData() {
  980. SQLiteDatabase db = this.getReadableDatabase();
  981. db.delete(PacketEntry.TABLE_NAME, null, null);
  982. db.delete(AttackEntry.TABLE_NAME, null, null);
  983. db.close();
  984. }
  985. /**
  986. * Deletes all records from {@link PacketEntry}s with a specific BSSID.
  987. *
  988. * @param bssid
  989. * The BSSID to match against.
  990. */
  991. public synchronized void deleteByBSSID(String bssid) {
  992. SQLiteDatabase db = this.getReadableDatabase();
  993. db.delete(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID + " = ?", new String[] { bssid });
  994. db.delete(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_BSSID + " = ?", new String[] { bssid });
  995. db.close();
  996. }
  997. /**
  998. * Deletes all records from {@link de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEntry}s with a time stamp smaller
  999. * then the given
  1000. *
  1001. * @param date
  1002. * A Date represented in milliseconds.
  1003. */
  1004. public synchronized void deleteByDate(long date) {
  1005. SQLiteDatabase db = this.getReadableDatabase();
  1006. String deleteQuery = "DELETE FROM " + PacketEntry.TABLE_NAME + " WHERE " + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP + " < " + date;
  1007. db.execSQL(deleteQuery);
  1008. db.close();
  1009. }
  1010. /**
  1011. * Deletes all {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord} with a specific Attack ID.
  1012. *
  1013. * @param attackID
  1014. * The Attack ID to match against.
  1015. */
  1016. public synchronized void deleteByAttackID(long attackID) {
  1017. SQLiteDatabase db = this.getReadableDatabase();
  1018. db.delete(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_ATTACK_ID + " = ?", new String[] { String.valueOf(attackID) });
  1019. db.delete(PacketEntry.TABLE_NAME, PacketEntry.COLUMN_NAME_ATTACK_ID + " = ?", new String[] { String.valueOf(attackID) });
  1020. db.close();
  1021. }
  1022. /**
  1023. * Creates a {@link MessageRecord} from a Cursor. If the cursor does not show to a
  1024. * valid data structure a runtime exception is thrown.
  1025. *
  1026. * @param cursor
  1027. * @return Returns the created {@link MessageRecord} .
  1028. */
  1029. private synchronized MessageRecord createMessageRecord(Cursor cursor) {
  1030. MessageRecord record = new MessageRecord();
  1031. record.setId(Integer.parseInt(cursor.getString(0)));
  1032. record.setAttack_id(cursor.getLong(1));
  1033. record.setType(MessageRecord.TYPE.valueOf(cursor.getString(2)));
  1034. record.setTimestamp(cursor.getLong(3));
  1035. record.setPacket(cursor.getString(4));
  1036. return record;
  1037. }
  1038. /**
  1039. * Creates a {@link AttackRecord} from a Cursor. If the cursor does not show to a
  1040. * valid data structure a runtime exception is thrown.
  1041. *
  1042. * @param cursor
  1043. * @return Returns the created {@link AttackRecord} .
  1044. */
  1045. private synchronized AttackRecord createAttackRecord(Cursor cursor) {
  1046. AttackRecord record = new AttackRecord();
  1047. record.setAttack_id(cursor.getLong(0));
  1048. record.setProtocol(cursor.getString(1));
  1049. record.setExternalIP(cursor.getString(2));
  1050. record.setLocalIP(cursor.getString(3));
  1051. record.setLocalPort(Integer.parseInt(cursor.getString(4)));
  1052. record.setRemoteIP(cursor.getString(5));
  1053. record.setRemotePort(Integer.parseInt(cursor.getString(6)));
  1054. record.setWasInternalAttack(cursor.getInt(7) == 1);
  1055. record.setBssid(cursor.getString(8));
  1056. record.setSync_id(cursor.getLong(9));
  1057. record.setDevice(cursor.getString(10));
  1058. return record;
  1059. }
  1060. /**
  1061. * Creates a {@link SyncRecord} from a Cursor. If the cursor does not show to a
  1062. * valid data structure a runtime exception is thrown.
  1063. *
  1064. * @param cursor
  1065. * @return Returns the created {@link SyncRecord} .
  1066. */
  1067. private synchronized SyncRecord createSyncRecord(Cursor cursor){
  1068. SyncRecord record = new SyncRecord();
  1069. record.setAttack_id(cursor.getLong(0));
  1070. record.setProtocol(cursor.getString(1));
  1071. record.setExternalIP(cursor.getString(2));
  1072. record.setLocalIP(cursor.getString(3));
  1073. record.setLocalPort(Integer.parseInt(cursor.getString(4)));
  1074. record.setRemoteIP(cursor.getString(5));
  1075. record.setRemotePort(Integer.parseInt(cursor.getString(6)));
  1076. record.setWasInternalAttack(cursor.getInt(7) == 1);
  1077. record.setBssid(cursor.getString(8));
  1078. record.setSync_id(cursor.getLong(9));
  1079. record.setDevice(cursor.getString(10));
  1080. record.setId(Integer.parseInt(cursor.getString(11)));
  1081. //record.setAttack_id(cursor.getLong(12));
  1082. record.setType(MessageRecord.TYPE.valueOf(cursor.getString(13)));
  1083. record.setTimestamp(cursor.getLong(14));
  1084. record.setPacket(cursor.getString(15));
  1085. return record;
  1086. }
  1087. /**
  1088. * Creates a {@link Record} from a Cursor. If the cursor does not show to a
  1089. * valid data structure a runtime exception is thrown.
  1090. *
  1091. * @param cursor
  1092. * @return Returns the created {@link Record} .
  1093. */
  1094. private synchronized Record createRecord(Cursor cursor) {
  1095. Record record = new Record();
  1096. record.setId(Integer.parseInt(cursor.getString(0)));
  1097. record.setAttack_id(cursor.getLong(1));
  1098. record.setType(TYPE.valueOf(cursor.getString(2)));
  1099. record.setTimestamp(cursor.getLong(3));
  1100. record.setPacket(cursor.getString(4));
  1101. record.setProtocol(cursor.getString(5));
  1102. record.setExternalIP(cursor.getString(6));
  1103. record.setLocalIP(cursor.getString(7));
  1104. record.setLocalPort(Integer.parseInt(cursor.getString(8)));
  1105. record.setRemoteIP(cursor.getString(9));
  1106. record.setRemotePort(Integer.parseInt(cursor.getString(10)));
  1107. record.setWasInternalAttack(Integer.parseInt(cursor.getString(11)) == 1);
  1108. record.setBssid(cursor.getString(12));
  1109. record.setSsid(cursor.getString(15));
  1110. record.setLatitude(Double.parseDouble(cursor.getString(16)));
  1111. record.setLongitude(Double.parseDouble(cursor.getString(17)));
  1112. record.setAccuracy(Float.parseFloat(cursor.getString(18)));
  1113. record.setTimestampLocation(cursor.getLong(19));
  1114. return record;
  1115. }
  1116. /**
  1117. * Gets all received {@link Record Records} for the specified information in
  1118. * the LogFilter ordered by date.
  1119. *
  1120. * @return A ArrayList with all received {@link Record Records} for the
  1121. * LogFilter.
  1122. */
  1123. public synchronized ArrayList<Record> getRecordsForFilter(LogFilter filter) {
  1124. ArrayList<Record> recordList = new ArrayList<Record>();
  1125. String selectQuery = this.selectionQueryFromFilter(filter, "*");
  1126. SQLiteDatabase db = this.getReadableDatabase();
  1127. Cursor cursor = db.rawQuery(selectQuery, null);
  1128. // looping through all rows and adding to list
  1129. if (cursor.moveToFirst()) {
  1130. do {
  1131. Record record = createRecord(cursor);
  1132. // Adding record to list
  1133. recordList.add(record);
  1134. } while (cursor.moveToNext());
  1135. }
  1136. cursor.close();
  1137. // return record list
  1138. db.close();
  1139. return recordList;
  1140. }
  1141. /**
  1142. * Returns the query for the given filter.
  1143. * @param filter (LogFilter)
  1144. * @param selectionString (String) for everything: "*"
  1145. * @return (String) query string
  1146. */
  1147. public String selectionQueryFromFilter(LogFilter filter, String selectionString)
  1148. {
  1149. String selectQuery = "SELECT " + selectionString + " FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  1150. + ")";
  1151. if (filter == null) return selectQuery;
  1152. // TIMESTAMPS
  1153. selectQuery = selectQuery + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  1154. selectQuery = selectQuery + " < " + filter.getBelowTimestamp();
  1155. selectQuery = selectQuery + " AND " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  1156. selectQuery = selectQuery + " > " + filter.getAboveTimestamp();
  1157. if (filter.getBSSIDs() != null && filter.getBSSIDs().size() > 0) {
  1158. selectQuery = selectQuery + " AND ";
  1159. selectQuery = selectQuery + filter.getBSSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID);
  1160. }
  1161. if (filter.getESSIDs() != null && filter.getESSIDs().size() > 0) {
  1162. selectQuery = selectQuery + " AND ";
  1163. selectQuery = selectQuery + filter.getESSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_SSID);
  1164. }
  1165. if (filter.getProtocols() != null && filter.getProtocols().size() > 0) {
  1166. selectQuery = selectQuery + " AND ";
  1167. selectQuery = selectQuery + filter.getProtocolsQueryStatement(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_PROTOCOL);
  1168. }
  1169. selectQuery = selectQuery + " GROUP BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID;
  1170. if (filter.getSorttype() == LogFilter.SortType.packet_timestamp) {
  1171. // DESC
  1172. selectQuery = selectQuery + " ORDER BY " + filter.getSorttype() + " DESC";
  1173. } else {
  1174. selectQuery = selectQuery + " ORDER BY " + filter.getSorttype();
  1175. }
  1176. System.out.println(selectQuery);
  1177. return selectQuery;
  1178. }
  1179. /**
  1180. * Returns the Conversation of a specific attack id
  1181. *
  1182. * @param attack_id Tha attack id to match the query against.
  1183. *
  1184. * @return A arraylist with all {@link Record Records}s for an attack id.
  1185. */
  1186. public synchronized ArrayList<Record> getConversationForAttackID(long attack_id) {
  1187. ArrayList<Record> recordList = new ArrayList<Record>();
  1188. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  1189. + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id;
  1190. SQLiteDatabase db = this.getReadableDatabase();
  1191. Cursor cursor = db.rawQuery(selectQuery, null);
  1192. if (cursor.moveToFirst()) {
  1193. do {
  1194. Record record = createRecord(cursor);
  1195. recordList.add(record);
  1196. } while (cursor.moveToNext());
  1197. }
  1198. cursor.close();
  1199. db.close();
  1200. return recordList;
  1201. }
  1202. /**
  1203. * Gets a single {@link Record} with the given attack id from the database.
  1204. *
  1205. * @param attack_id
  1206. * The attack id of the {@link Record};
  1207. * @return The {@link Record}.
  1208. */
  1209. public synchronized Record getRecordOfAttackId(long attack_id) {
  1210. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  1211. + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id + " GROUP BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ID;
  1212. SQLiteDatabase db = this.getReadableDatabase();
  1213. Cursor cursor = db.rawQuery(selectQuery, null);
  1214. Record record = null;
  1215. if (cursor.moveToFirst()) {
  1216. record = createRecord(cursor);
  1217. }
  1218. cursor.close();
  1219. // return record list
  1220. db.close();
  1221. return record;
  1222. }
  1223. /**
  1224. * Gets a single {@link Record} with the given ID from the database.
  1225. *
  1226. * @param id
  1227. * The ID of the {@link Record};
  1228. * @return The {@link Record}.
  1229. */
  1230. public synchronized Record getRecord(int id) {
  1231. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + PacketEntry.COLUMN_NAME_ATTACK_ID
  1232. + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ID + " = " + id;
  1233. SQLiteDatabase db = this.getReadableDatabase();
  1234. Cursor cursor = db.rawQuery(selectQuery, null);
  1235. Record record = null;
  1236. if (cursor.moveToFirst()) {
  1237. record = createRecord(cursor);
  1238. }
  1239. cursor.close();
  1240. db.close();
  1241. // return contact
  1242. return record;
  1243. }
  1244. /**
  1245. * Gets all {@link Record Records} saved in the database.
  1246. *
  1247. * @return A ArrayList of all the {@link Record Records} in the Database.
  1248. */
  1249. public synchronized ArrayList<Record> getAllRecords() {
  1250. ArrayList<Record> recordList = new ArrayList<Record>();
  1251. // Select All Query
  1252. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  1253. + ")";
  1254. SQLiteDatabase db = this.getWritableDatabase();
  1255. Cursor cursor = db.rawQuery(selectQuery, null);
  1256. Log.i("Database", "Start loop");
  1257. // looping through all rows and adding to list
  1258. if (cursor.moveToFirst()) {
  1259. do {
  1260. Log.i("Database", "Add Record");
  1261. Record record = createRecord(cursor);
  1262. // Adding record to list
  1263. recordList.add(record);
  1264. } while (cursor.moveToNext());
  1265. }
  1266. cursor.close();
  1267. db.close();
  1268. // return record list
  1269. return recordList;
  1270. }
  1271. /**
  1272. * Gets all non duplicate Records For the key BSSID.
  1273. *
  1274. * @return A ArrayList with received Records.
  1275. */
  1276. public synchronized ArrayList<String> getUniqueBSSIDRecords() {
  1277. return this.getUniqueDataEntryForKeyType(NetworkEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME);
  1278. }
  1279. /**
  1280. * Gets all non duplicate Records For the key ESSID.
  1281. *
  1282. * @return A ArrayList with received Records.
  1283. */
  1284. public synchronized ArrayList<String> getUniqueESSIDRecords() {
  1285. return this.getUniqueDataEntryForKeyType(NetworkEntry.COLUMN_NAME_SSID, NetworkEntry.TABLE_NAME);
  1286. }
  1287. public synchronized ArrayList<String> getUniqueESSIDRecordsForProtocol(String protocol) {
  1288. return this.getUniqueIDForProtocol(NetworkEntry.COLUMN_NAME_SSID, protocol);
  1289. }
  1290. public synchronized ArrayList<String> getUniqueBSSIDRecordsForProtocol(String protocol) {
  1291. return this.getUniqueIDForProtocol(NetworkEntry.COLUMN_NAME_BSSID, protocol);
  1292. }
  1293. private synchronized ArrayList<String> getUniqueIDForProtocol(String id, String protocol) {
  1294. ArrayList<String> recordList = new ArrayList<String>();
  1295. String selectQuery = "SELECT DISTINCT " + id + " FROM " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID + ") " + " WHERE "
  1296. + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_PROTOCOL + " = " + "'" + protocol + "'" + " ORDER BY " + "'"+id+"'"; // " NATURAL JOIN "
  1297. // +
  1298. // TABLE_ATTACK_INFO
  1299. // +
  1300. // " NATURAL JOIN "
  1301. // +
  1302. // TABLE_BSSIDS
  1303. // +
  1304. // " NATURAL JOIN "
  1305. // +
  1306. // TABLE_PORTS
  1307. // +
  1308. // ORDERED BY TIME
  1309. System.out.println(selectQuery);
  1310. SQLiteDatabase db = this.getReadableDatabase();
  1311. Cursor cursor = db.rawQuery(selectQuery, null);
  1312. // looping through all rows and adding to list
  1313. if (cursor.moveToFirst()) {
  1314. do {
  1315. String record = cursor.getString(0);
  1316. recordList.add(record);
  1317. } while (cursor.moveToNext());
  1318. }
  1319. cursor.close();
  1320. // return record list
  1321. db.close();
  1322. return recordList;
  1323. }
  1324. /**
  1325. * Gets all non duplicate Data Entry For a specific KeyType ( e.g. BSSIDs).
  1326. *
  1327. * @return A ArrayList with received Records.
  1328. */
  1329. public synchronized ArrayList<String> getUniqueDataEntryForKeyType(String keyType, String table) {
  1330. ArrayList<String> recordList = new ArrayList<String>();
  1331. // String selectQuery = "SELECT * FROM " + TABLE_RECORDS +
  1332. // " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " +
  1333. // TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS;
  1334. String selectQuery = "SELECT DISTINCT " + keyType + " FROM " + table + " ORDER BY " + keyType; // " NATURAL JOIN "
  1335. // +
  1336. // TABLE_ATTACK_INFO
  1337. // +
  1338. // " NATURAL JOIN "
  1339. // +
  1340. // TABLE_BSSIDS
  1341. // +
  1342. // " NATURAL JOIN "
  1343. // +
  1344. // TABLE_PORTS
  1345. // +
  1346. // ORDERED BY TIME
  1347. System.out.println(selectQuery);
  1348. SQLiteDatabase db = this.getReadableDatabase();
  1349. Cursor cursor = db.rawQuery(selectQuery, null);
  1350. // looping through all rows and adding to list
  1351. if (cursor.moveToFirst()) {
  1352. do {
  1353. String record = cursor.getString(0);
  1354. recordList.add(record);
  1355. } while (cursor.moveToNext());
  1356. }
  1357. cursor.close();
  1358. // return record list
  1359. db.close();
  1360. return recordList;
  1361. }
  1362. //TODO PROFILE DATABASE QUERIES - STILL NEEDED?
  1363. /**
  1364. * Retrieves all the profiles from the database
  1365. *
  1366. * @return list of profiles
  1367. */
  1368. public synchronized List<Profile> getAllProfiles() {
  1369. List<Profile> profiles = new LinkedList<Profile>();
  1370. // Select All Query
  1371. String selectQuery = "SELECT * FROM " + ProfileEntry.TABLE_NAME;
  1372. SQLiteDatabase db = this.getWritableDatabase();
  1373. Cursor cursor = db.rawQuery(selectQuery, null);
  1374. // looping through all rows and adding to list
  1375. if (cursor.moveToFirst()) {
  1376. do {
  1377. Profile profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
  1378. if (cursor.getInt(6) == 1) {
  1379. profile.mActivated = true;
  1380. }
  1381. profile.mIconName = cursor.getString(4);
  1382. // Adding record to list
  1383. profiles.add(profile);
  1384. } while (cursor.moveToNext());
  1385. }
  1386. cursor.close();
  1387. db.close();
  1388. // return record list
  1389. return profiles;
  1390. }
  1391. /**
  1392. * Persists the given profile into the database
  1393. *
  1394. * @param profile
  1395. * the profile which should be persisted
  1396. *
  1397. * @return
  1398. */
  1399. public synchronized long persistProfile(Profile profile) {
  1400. SQLiteDatabase db = this.getReadableDatabase();
  1401. ContentValues values = new ContentValues();
  1402. if (profile.mId != -1) {
  1403. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ID, profile.mId);
  1404. }
  1405. values.put(ProfileEntry.COLUMN_NAME_PROFILE_NAME, profile.mLabel);
  1406. values.put(ProfileEntry.COLUMN_NAME_PROFILE_DESCRIPTION, profile.mText);
  1407. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ICON, profile.mIconPath);
  1408. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ICON_NAME, profile.mIconName);
  1409. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ACTIVE, profile.mActivated);
  1410. values.put(ProfileEntry.COLUMN_NAME_PROFILE_EDITABLE, profile.mEditable);
  1411. return db.replace(ProfileEntry.TABLE_NAME, null, values);
  1412. }
  1413. /**
  1414. * private static final String CREATE_PROFILE_TABLE = "CREATE TABLE " +
  1415. * TABLE_PROFILES + "(" + KEY_PROFILE_ID +
  1416. * " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_PROFILE_NAME + " TEXT," +
  1417. * KEY_PROFILE_DESCRIPTION + " TEXT," + KEY_PROFILE_ICON + " TEXT," +
  1418. * KEY_PROFILE_ICON_ID + " INTEGER," + KEY_PROFILE_EDITABLE + " INTEGER," +
  1419. * KEY_PROFILE_ACTIVE + " INTEGER" + ")";
  1420. */
  1421. public synchronized Profile getProfile(int id) {
  1422. String selectQuery = "SELECT * FROM " + ProfileEntry.TABLE_NAME + " WHERE " + ProfileEntry.TABLE_NAME + "." + ProfileEntry.COLUMN_NAME_PROFILE_ID + " = " + id;
  1423. SQLiteDatabase db = this.getReadableDatabase();
  1424. Cursor cursor = db.rawQuery(selectQuery, null);
  1425. Profile profile = null;
  1426. if (cursor.moveToFirst()) {
  1427. profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
  1428. if (cursor.getInt(6) == 1) {
  1429. profile.mActivated = true;
  1430. }
  1431. profile.mIconName = cursor.getString(5);
  1432. }
  1433. cursor.close();
  1434. db.close();
  1435. // return contact
  1436. return profile;
  1437. }
  1438. public synchronized void deleteProfile(int id) {
  1439. SQLiteDatabase db = this.getReadableDatabase();
  1440. db.delete(ProfileEntry.TABLE_NAME, ProfileEntry.COLUMN_NAME_PROFILE_ID + "=?", new String[] { String.valueOf(id) });
  1441. }
  1442. /**
  1443. * Gets all received {@link Record Records} for every attack identified by
  1444. * its attack id and ordered by date.
  1445. *
  1446. * @return A ArrayList with all received {@link Record Records} for each
  1447. * attack id in the Database.
  1448. */
  1449. public synchronized ArrayList<Record> getAllReceivedRecordsOfEachAttack() {
  1450. ArrayList<Record> recordList = new ArrayList<Record>();
  1451. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  1452. + ")" + " WHERE " + PacketEntry.COLUMN_NAME_TYPE + "='RECEIVE'" + " ORDER BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  1453. SQLiteDatabase db = this.getReadableDatabase();
  1454. Cursor cursor = db.rawQuery(selectQuery, null);
  1455. // looping through all rows and adding to list
  1456. if (cursor.moveToFirst()) {
  1457. do {
  1458. Record record = createRecord(cursor);
  1459. // Adding record to list
  1460. recordList.add(record);
  1461. } while (cursor.moveToNext());
  1462. }
  1463. cursor.close();
  1464. // return record list
  1465. db.close();
  1466. return recordList;
  1467. }
  1468. /**
  1469. * Returns PlotComparisionItems for attacks per essid.
  1470. * @param filter (LogFilter) filter object
  1471. * @return ArrayList<PlotComparisonItem>
  1472. */
  1473. public synchronized ArrayList<PlotComparisonItem> attacksPerESSID(LogFilter filter) {
  1474. String filterQuery = this.selectionQueryFromFilter(filter, AttackEntry.COLUMN_NAME_ATTACK_ID);
  1475. String attackPerESSID_Query = "SELECT " + NetworkEntry.COLUMN_NAME_SSID + " , " + "COUNT( " + AttackEntry.COLUMN_NAME_ATTACK_ID + " ) " + " "
  1476. + " FROM " + AttackEntry.TABLE_NAME + " a " + " , " + NetworkEntry.TABLE_NAME
  1477. + " WHERE " + " a." + AttackEntry.COLUMN_NAME_ATTACK_ID + " IN " + " ( " + filterQuery + " ) "
  1478. + " GROUP BY " + NetworkEntry.TABLE_NAME+"."+NetworkEntry.COLUMN_NAME_SSID;
  1479. SQLiteDatabase db = this.getReadableDatabase();
  1480. Cursor cursor = db.rawQuery(attackPerESSID_Query, null);
  1481. ArrayList<PlotComparisonItem> plots = new ArrayList<PlotComparisonItem>();
  1482. int counter = 0;
  1483. if (cursor.moveToFirst()) {
  1484. do {
  1485. String title = cursor.getString(0); // COLUMN_NAME_SSID
  1486. double value = cursor.getDouble(1); // COUNT
  1487. if (value == 0.) continue;
  1488. PlotComparisonItem plotItem = new PlotComparisonItem(title, this.getColor(counter), 0. , value);
  1489. plots.add(plotItem);
  1490. counter++;
  1491. } while (cursor.moveToNext());
  1492. }
  1493. cursor.close();
  1494. db.close();
  1495. return plots;
  1496. }
  1497. /**
  1498. * Creates a {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice} from a Cursor. If the cursor does not show to a
  1499. * valid data structure a runtime exception is thrown.
  1500. *
  1501. * @param cursor the cursor
  1502. * @return Returns the created {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice} .
  1503. */
  1504. private synchronized SyncDevice createSyncDevice(Cursor cursor) {
  1505. SyncDevice record = new SyncDevice();
  1506. record.setDeviceID(cursor.getString(0));
  1507. record.setLast_sync_timestamp(cursor.getLong(1));
  1508. record.setHighest_attack_id(cursor.getLong(2));
  1509. return record;
  1510. }
  1511. /**
  1512. * Returns all missing / newly inserted and updated {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s.
  1513. * @param oldDevices array of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
  1514. * @param includeMissing boolean
  1515. * @return array of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
  1516. */
  1517. public ArrayList<SyncDevice> getUpdatedDevicesFor(List<SyncDevice> oldDevices, boolean includeMissing){
  1518. HashMap<String, Long> oldDeviceMap = new HashMap<String, Long>();
  1519. for (SyncDevice d : oldDevices){
  1520. oldDeviceMap.put(d.getDeviceID(),d.getHighest_attack_id());
  1521. }
  1522. ArrayList<SyncDevice> recordList = new ArrayList<SyncDevice>();
  1523. String selectQuery = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
  1524. SQLiteDatabase db = this.getReadableDatabase();
  1525. Cursor cursor = db.rawQuery(selectQuery, null);
  1526. // looping through all rows and adding to list
  1527. if (cursor.moveToFirst()) {
  1528. do {
  1529. SyncDevice record = createSyncDevice(cursor);
  1530. // Adding record to list
  1531. if (oldDeviceMap.containsKey(record.getDeviceID())){
  1532. Long oldSyncId = oldDeviceMap.get(record.getDeviceID());
  1533. if (oldSyncId < record.getHighest_attack_id()){
  1534. recordList.add(record);
  1535. }
  1536. } else {
  1537. if (includeMissing)
  1538. recordList.add(record);
  1539. }
  1540. } while (cursor.moveToNext());
  1541. }
  1542. cursor.close();
  1543. // return record list
  1544. db.close();
  1545. return recordList;
  1546. }
  1547. /**
  1548. * Returns all missing / newly inserted and updated {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s.
  1549. * @param oldDeviceMap map with device id and max sync_id for the device
  1550. * @param includeMissing boolean
  1551. * @return array of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
  1552. */
  1553. public ArrayList<SyncDevice> getUpdatedDevicesFor(HashMap<String, Long> oldDeviceMap, boolean includeMissing){
  1554. ArrayList<SyncDevice> recordList = new ArrayList<SyncDevice>();
  1555. String selectQuery = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
  1556. SQLiteDatabase db = this.getReadableDatabase();
  1557. Cursor cursor = db.rawQuery(selectQuery, null);
  1558. // looping through all rows and adding to list
  1559. if (cursor.moveToFirst()) {
  1560. do {
  1561. SyncDevice record = createSyncDevice(cursor);
  1562. // Adding record to list
  1563. if (oldDeviceMap.containsKey(record.getDeviceID())){
  1564. Long oldSyncId = oldDeviceMap.get(record.getDeviceID());
  1565. if (oldSyncId < record.getHighest_attack_id()){
  1566. recordList.add(record);
  1567. }
  1568. } else {
  1569. if (includeMissing)
  1570. recordList.add(record);
  1571. }
  1572. } while (cursor.moveToNext());
  1573. }
  1574. cursor.close();
  1575. // return record list
  1576. db.close();
  1577. return recordList;
  1578. }
  1579. /**
  1580. * Returns all device ids.
  1581. * @return list of all device ids.
  1582. */
  1583. public ArrayList<String> getAllDevicesIds(){
  1584. String selectQuery = "SELECT "+ SyncDeviceEntry.COLUMN_NAME_DEVICE_ID+" FROM " + SyncDeviceEntry.TABLE_NAME;
  1585. SQLiteDatabase db = this.getReadableDatabase();
  1586. Cursor cursor = db.rawQuery(selectQuery, null);
  1587. ArrayList<String> ids = new ArrayList<String>();
  1588. // looping through all rows and adding to list
  1589. if (cursor.moveToFirst()) {
  1590. do {
  1591. String s = cursor.getString(0);
  1592. ids.add(s);
  1593. } while (cursor.moveToNext());
  1594. }
  1595. cursor.close();
  1596. // return record list
  1597. db.close();
  1598. return ids;
  1599. }
  1600. /***
  1601. * Returns all missing devices ids
  1602. * @param devices owned device ids
  1603. * @return list of missing devices ids
  1604. */
  1605. public ArrayList<String> getMissingDeviceIds(ArrayList<String> devices){
  1606. ArrayList<String> ids = new ArrayList<String>();
  1607. String prefix = " D WHERE " + SyncDeviceEntry.COLUMN_NAME_DEVICE_ID + " NOT IN ";
  1608. String selectQuery = "SELECT "+ SyncDeviceEntry.COLUMN_NAME_DEVICE_ID +" FROM " + SyncDeviceEntry.TABLE_NAME + this.arrayToSQLString(devices,prefix);
  1609. SQLiteDatabase db = this.getReadableDatabase();
  1610. Cursor cursor = db.rawQuery(selectQuery, null);
  1611. if (cursor.moveToFirst()) {
  1612. do {
  1613. String deviceID = cursor.getString(0);
  1614. ids.add(deviceID);
  1615. } while (cursor.moveToNext());
  1616. }
  1617. cursor.close();
  1618. db.close();
  1619. return ids;
  1620. }
  1621. /**
  1622. * Returns all new {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s for the given devices (including all missing devices).
  1623. * @param devices {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}
  1624. * @param includeMissingDevices boolean
  1625. * @return list of {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s
  1626. */
  1627. public ArrayList<SyncRecord> getUnsyncedAttacksFor(List<SyncDevice> devices, boolean includeMissingDevices){
  1628. ArrayList<SyncDevice> updatedDevices = this.getUpdatedDevicesFor(devices, includeMissingDevices);
  1629. ArrayList<SyncRecord> recordList = new ArrayList<SyncRecord>();
  1630. SQLiteDatabase db = this.getReadableDatabase();
  1631. for (SyncDevice sDevice : updatedDevices){
  1632. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A " + " NATURAL JOIN " + PacketEntry.TABLE_NAME + " P "
  1633. + " WHERE "
  1634. +" ( "
  1635. + "A." + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + " P."+ PacketEntry.COLUMN_NAME_ATTACK_ID
  1636. + " AND " + " A." + AttackEntry.COLUMN_NAME_DEVICE + " = " + "'" + sDevice.getDeviceID() + "'"
  1637. + " AND " + " A." + AttackEntry.COLUMN_NAME_SYNC_ID + " > " + sDevice.getHighest_attack_id()
  1638. + " ) "
  1639. //+ " GROUP BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_DEVICE
  1640. + " ORDER BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_SYNC_ID + " DESC ";
  1641. Cursor cursor = db.rawQuery(selectQuery, null);
  1642. // looping through all rows and adding to list
  1643. if (cursor != null){
  1644. if (cursor.moveToFirst()) {
  1645. do {
  1646. SyncRecord record = createSyncRecord(cursor);
  1647. recordList.add(record);
  1648. } while (cursor.moveToNext());
  1649. }
  1650. cursor.close();
  1651. }
  1652. }
  1653. // return record list
  1654. db.close();
  1655. return recordList;
  1656. }
  1657. /**
  1658. * Returns all new {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s for the given devices (including all missing devices).
  1659. * @param deviceMap map of device id and max sync_id of it
  1660. * @param includeMissingDevices boolean
  1661. * @return list of {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s
  1662. */
  1663. public ArrayList<SyncRecord> getUnsyncedAttacksFor(HashMap<String,Long> deviceMap, boolean includeMissingDevices){
  1664. ArrayList<SyncDevice> updatedDevices = this.getUpdatedDevicesFor(deviceMap, includeMissingDevices);
  1665. ArrayList<SyncRecord> recordList = new ArrayList<SyncRecord>();
  1666. SQLiteDatabase db = this.getReadableDatabase();
  1667. for (SyncDevice sDevice : updatedDevices){
  1668. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A " + " NATURAL JOIN " + PacketEntry.TABLE_NAME + " P "
  1669. + " WHERE "
  1670. +" ( "
  1671. + "A." + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + " P."+ PacketEntry.COLUMN_NAME_ATTACK_ID
  1672. + " AND " + " A." + AttackEntry.COLUMN_NAME_DEVICE + " = " + "'" + sDevice.getDeviceID() + "'"
  1673. + " AND " + " A." + AttackEntry.COLUMN_NAME_SYNC_ID + " > " + sDevice.getHighest_attack_id()
  1674. + " ) "
  1675. //+ " GROUP BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_DEVICE
  1676. + " ORDER BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_SYNC_ID + " DESC ";
  1677. Cursor cursor = db.rawQuery(selectQuery, null);
  1678. // looping through all rows and adding to list
  1679. if (cursor != null){
  1680. if (cursor.moveToFirst()) {
  1681. do {
  1682. SyncRecord record = createSyncRecord(cursor);
  1683. recordList.add(record);
  1684. } while (cursor.moveToNext());
  1685. }
  1686. cursor.close();
  1687. }
  1688. }
  1689. // return record list
  1690. db.close();
  1691. return recordList;
  1692. }
  1693. /**
  1694. * Attacks per BSSID
  1695. * @param filter (LogFilter) query filter
  1696. * @return ArrayList<PlotComparisonItem>
  1697. */
  1698. public synchronized ArrayList<PlotComparisonItem> attacksPerBSSID(LogFilter filter) {
  1699. String filterQuery = this.selectionQueryFromFilter(filter, AttackEntry.COLUMN_NAME_ATTACK_ID);
  1700. String attackPerBSSID_Query = "SELECT " + NetworkEntry.COLUMN_NAME_BSSID + " , " + "COUNT( " + AttackEntry.COLUMN_NAME_ATTACK_ID + " ) " + " "
  1701. + " FROM " + AttackEntry.TABLE_NAME + " a " + " , " + NetworkEntry.TABLE_NAME
  1702. + " WHERE " + " a." + AttackEntry.COLUMN_NAME_ATTACK_ID + " IN " + " ( " + filterQuery + " ) "
  1703. + " GROUP BY " + NetworkEntry.TABLE_NAME + "." + NetworkEntry.COLUMN_NAME_BSSID;
  1704. SQLiteDatabase db = this.getReadableDatabase();
  1705. Cursor cursor = db.rawQuery(attackPerBSSID_Query, null);
  1706. ArrayList<PlotComparisonItem> plots = new ArrayList<PlotComparisonItem>();
  1707. int counter = 0;
  1708. if (cursor.moveToFirst()) {
  1709. do {
  1710. String title = cursor.getString(0); // COLUMN_NAME_BSSID
  1711. double value = cursor.getDouble(1); // COUNT
  1712. if (value == 0.) continue;
  1713. PlotComparisonItem plotItem = new PlotComparisonItem(title, this.getColor(counter), 0. , value);
  1714. plots.add(plotItem);
  1715. counter++;
  1716. } while (cursor.moveToNext());
  1717. }
  1718. cursor.close();
  1719. db.close();
  1720. return plots;
  1721. }
  1722. /**
  1723. * Inserts the given devices in the database with save.
  1724. * @param devices list of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
  1725. */
  1726. public void insertSyncDevices(List<SyncDevice> devices){
  1727. SQLiteDatabase db = this.getWritableDatabase();
  1728. db.beginTransaction();
  1729. try {
  1730. for (SyncDevice device : devices){
  1731. insertSyncDevice(device, db);
  1732. }
  1733. db.setTransactionSuccessful();
  1734. } finally {
  1735. db.endTransaction();
  1736. }
  1737. db.close(); // Closing database connection
  1738. }
  1739. /**
  1740. * Inserts the given dives in the given SQLite Database without save.
  1741. * @param device {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}
  1742. * @param db {@link android.database.sqlite.SQLiteDatabase}
  1743. */
  1744. private void insertSyncDevice(SyncDevice device, SQLiteDatabase db){
  1745. ContentValues recordValues = new ContentValues();
  1746. recordValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, device.getDeviceID());
  1747. recordValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, device.getLast_sync_timestamp());
  1748. recordValues.put(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID, device.getHighest_attack_id());
  1749. // Inserting Rows
  1750. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, recordValues, SQLiteDatabase.CONFLICT_REPLACE);
  1751. }
  1752. /** Returns the color for the given index
  1753. * @return int color*/
  1754. public Integer getColor(int index) {
  1755. return ColorSequenceGenerator.getColorForIndex(index);
  1756. }
  1757. }