HostageDBOpenHelper.java 78 KB

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