HostageDBOpenHelper.java 49 KB

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