HostageDBOpenHelper.java 78 KB

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