HostageDBOpenHelper.java 78 KB

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