HostageDBOpenHelper.java 78 KB

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