HostageDBOpenHelper.java 64 KB

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