HostageDBOpenHelper.java 77 KB

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