HostageDBOpenHelper.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. package de.tudarmstadt.informatik.hostage.persistence;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9. import android.util.Log;
  10. import de.tudarmstadt.informatik.hostage.logging.Record;
  11. import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
  12. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.AttackEntry;
  13. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.NetworkEntry;
  14. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEntry;
  15. public class HostageDBOpenHelper extends SQLiteOpenHelper {
  16. private static final String DATABASE_NAME = "hostage.db";
  17. private static final int DATABASE_VERSION = 1;
  18. static {
  19. StringBuilder networkSQLBuilder = new StringBuilder("CREATE TABLE ").append(NetworkEntry.TABLE_NAME).append("(");
  20. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_BSSID).append(" TEXT PRIMARY KEY,");
  21. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_SSID).append(" TEXT,");
  22. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_LATITUDE).append(" INTEGER,");
  23. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_LONGITUDE).append(" INTEGER,");
  24. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_ACCURACY).append(" INTEGER,");
  25. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP).append(" INTEGER");
  26. networkSQLBuilder.append(")");
  27. SQL_CREATE_NETWORK_ENTRIES = networkSQLBuilder.toString();
  28. StringBuilder attackSQLBuilder = new StringBuilder("CREATE TABLE ").append(AttackEntry.TABLE_NAME).append("(");
  29. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_ATTACK_ID).append(" INTEGER PRIMARY KEY,");
  30. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_PROTOCOL).append(" TEXT,");
  31. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_EXTERNAL_IP).append(" TEXT,");
  32. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_LOCAL_IP).append(" BLOB,");
  33. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_LOCAL_PORT).append(" INTEGER,");
  34. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_REMOTE_IP).append(" BLOB,");
  35. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_REMOTE_PORT).append(" INTEGER,");
  36. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_BSSID).append(" TEXT,");
  37. attackSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", AttackEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME,
  38. NetworkEntry.COLUMN_NAME_BSSID));
  39. attackSQLBuilder.append(")");
  40. SQL_CREATE_ATTACK_ENTRIES = attackSQLBuilder.toString();
  41. StringBuilder packetSQLBuilder = new StringBuilder("CREATE TABLE ").append(PacketEntry.TABLE_NAME).append("(");
  42. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_ID).append(" INTEGER NOT NULL,");
  43. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_ATTACK_ID).append(" INTEGER NOT NULL,");
  44. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_TYPE).append(" TEXT,");
  45. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP).append(" INTEGER,");
  46. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_PACKET).append(" TEXT,");
  47. packetSQLBuilder.append(String.format("PRIMARY KEY(%s,%s)", PacketEntry.COLUMN_NAME_ID, PacketEntry.COLUMN_NAME_ATTACK_ID));
  48. packetSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", PacketEntry.COLUMN_NAME_ATTACK_ID, AttackEntry.TABLE_NAME,
  49. AttackEntry.COLUMN_NAME_ATTACK_ID));
  50. packetSQLBuilder.append(")");
  51. SQL_CREATE_PACKET_ENTRIES = packetSQLBuilder.toString();
  52. }
  53. private static final String SQL_CREATE_NETWORK_ENTRIES;
  54. private static final String SQL_CREATE_ATTACK_ENTRIES;
  55. private static final String SQL_CREATE_PACKET_ENTRIES;
  56. private static final String SQL_DELETE_PACKET_ENTRIES = "DROP TABLE IF EXISTS " + PacketEntry.TABLE_NAME;
  57. private static final String SQL_DELETE_ATTACK_ENTRIES = "DROP TABLE IF EXISTS " + AttackEntry.TABLE_NAME;
  58. private static final String SQL_DELETE_NETWORK_ENTRIES = "DROP TABLE IF EXISTS " + NetworkEntry.TABLE_NAME;
  59. public HostageDBOpenHelper(Context context) {
  60. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  61. }
  62. @Override
  63. public void onCreate(SQLiteDatabase db) {
  64. db.execSQL(SQL_CREATE_PACKET_ENTRIES);
  65. db.execSQL(SQL_CREATE_ATTACK_ENTRIES);
  66. db.execSQL(SQL_CREATE_NETWORK_ENTRIES);
  67. }
  68. @Override
  69. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  70. db.execSQL(SQL_DELETE_PACKET_ENTRIES);
  71. db.execSQL(SQL_DELETE_ATTACK_ENTRIES);
  72. db.execSQL(SQL_DELETE_NETWORK_ENTRIES);
  73. onCreate(db);
  74. }
  75. /**
  76. * Adds a given {@link Record} to the database.
  77. *
  78. * @param record
  79. * The added {@link Record} .
  80. */
  81. public void addRecord(Record record) {
  82. SQLiteDatabase db = this.getWritableDatabase();
  83. HashMap<String, Object> bssidValues = new HashMap<String, Object>();
  84. bssidValues.put(NetworkEntry.COLUMN_NAME_BSSID, record.getBssid());
  85. bssidValues.put(NetworkEntry.COLUMN_NAME_SSID, record.getSsid());
  86. bssidValues.put(NetworkEntry.COLUMN_NAME_LATITUDE, record.getLatitude());
  87. bssidValues.put(NetworkEntry.COLUMN_NAME_LONGITUDE, record.getLongitude());
  88. bssidValues.put(NetworkEntry.COLUMN_NAME_ACCURACY, record.getAccuracy());
  89. bssidValues.put(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP, record.getTimestampLocation());
  90. ContentValues attackValues = new ContentValues();
  91. attackValues.put(AttackEntry.COLUMN_NAME_ATTACK_ID, record.getAttack_id()); // Log
  92. // Attack
  93. // ID
  94. attackValues.put(AttackEntry.COLUMN_NAME_PROTOCOL, record.getProtocol().toString());
  95. attackValues.put(AttackEntry.COLUMN_NAME_EXTERNAL_IP, record.getExternalIP());
  96. attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_IP, record.getLocalIP()); // Log
  97. // Local
  98. // IP
  99. attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_PORT, record.getLocalPort());
  100. attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_IP, record.getRemoteIP()); // Log
  101. // Remote
  102. // IP
  103. attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_PORT, record.getRemotePort()); // Log
  104. // Remote
  105. // Port
  106. attackValues.put(AttackEntry.COLUMN_NAME_BSSID, record.getBssid());
  107. ContentValues recordValues = new ContentValues();
  108. recordValues.put(PacketEntry.COLUMN_NAME_ID, record.getId()); // Log
  109. // Message
  110. // Number
  111. recordValues.put(PacketEntry.COLUMN_NAME_ATTACK_ID, record.getAttack_id()); // Log
  112. // Attack
  113. // ID
  114. recordValues.put(PacketEntry.COLUMN_NAME_TYPE, record.getType().name()); // Log
  115. // Type
  116. recordValues.put(PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP, record.getTimestamp()); // Log
  117. // Timestamp
  118. recordValues.put(PacketEntry.COLUMN_NAME_PACKET, record.getPacket()); // Log
  119. // Packet
  120. // Inserting Rows
  121. db.insertWithOnConflict(AttackEntry.TABLE_NAME, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
  122. db.insert(PacketEntry.TABLE_NAME, null, recordValues);
  123. db.close(); // Closing database connection
  124. // Update Network Information
  125. updateNetworkInformation(bssidValues);
  126. }
  127. /**
  128. * Determines if a network with given BSSID has already been recorded as
  129. * malicious.
  130. *
  131. * @param BSSID
  132. * The BSSID of the network.
  133. * @return True if an attack has been recorded in a network with the given
  134. * BSSID, else false.
  135. */
  136. public boolean bssidSeen(String BSSID) {
  137. String countQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID + " = " + "'" + BSSID + "'";
  138. SQLiteDatabase db = this.getReadableDatabase();
  139. Cursor cursor = db.rawQuery(countQuery, null);
  140. int result = cursor.getCount();
  141. cursor.close();
  142. db.close();
  143. return result > 0;
  144. }
  145. /**
  146. * Determines if an attack has been recorded on a specific protocol in a
  147. * network with a given BSSID.
  148. *
  149. * @param protocol
  150. * The
  151. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  152. * Protocol} to inspect.
  153. * @param BSSID
  154. * The BSSID of the network.
  155. * @return True if an attack on the given protocol has been recorded in a
  156. * network with the given BSSID, else false.
  157. */
  158. public boolean bssidSeen(String protocol, String BSSID) {
  159. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " NATURAL JOIN " + NetworkEntry.TABLE_NAME + " WHERE "
  160. + AttackEntry.COLUMN_NAME_PROTOCOL + " = " + "'" + protocol + "'" + " AND " + NetworkEntry.COLUMN_NAME_BSSID + " = " + "'" + BSSID + "'";
  161. SQLiteDatabase db = this.getReadableDatabase();
  162. Cursor cursor = db.rawQuery(countQuery, null);
  163. int result = cursor.getCount();
  164. cursor.close();
  165. db.close();
  166. return result > 0;
  167. }
  168. /**
  169. * Deletes all records from {@link #PacketEntry.TABLE_NAME}.
  170. */
  171. public void clearData() {
  172. SQLiteDatabase db = this.getReadableDatabase();
  173. db.delete(PacketEntry.TABLE_NAME, null, null);
  174. db.delete(AttackEntry.TABLE_NAME, null, null);
  175. db.close();
  176. }
  177. /**
  178. * Deletes all records from {@link #PacketEntry.TABLE_NAME} with a specific BSSID.
  179. *
  180. * @param bssid
  181. * The BSSID to match against.
  182. */
  183. public void deleteByBSSID(String bssid) {
  184. SQLiteDatabase db = this.getReadableDatabase();
  185. db.delete(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID + " = ?", new String[] { bssid });
  186. db.delete(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_BSSID + " = ?", new String[] { bssid });
  187. db.close();
  188. }
  189. /**
  190. * Deletes all records from {@link #PacketEntry.TABLE_NAME} with a time stamp smaller
  191. * then the given
  192. *
  193. * @param date
  194. * A Date represented in milliseconds.
  195. */
  196. public void deleteByDate(long date) {
  197. SQLiteDatabase db = this.getReadableDatabase();
  198. String deleteQuery = "DELETE FROM " + PacketEntry.TABLE_NAME + " WHERE " + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP + " < " + date;
  199. db.execSQL(deleteQuery);
  200. db.close();
  201. }
  202. /**
  203. * Returns a String array with all BSSIDs stored in the database.
  204. *
  205. * @return String[] of all recorded BSSIDs.
  206. */
  207. public String[] getAllBSSIDS() {
  208. String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME;
  209. SQLiteDatabase db = this.getReadableDatabase();
  210. Cursor cursor = db.rawQuery(selectQuery, null);
  211. String[] bssidList = new String[cursor.getCount()];
  212. int counter = 0;
  213. // looping through all rows and adding to list
  214. if (cursor.moveToFirst()) {
  215. do {
  216. bssidList[counter] = cursor.getString(0);
  217. counter++;
  218. } while (cursor.moveToNext());
  219. }
  220. cursor.close();
  221. db.close();
  222. return bssidList;
  223. }
  224. /**
  225. * Gets all received {@link Record Records} for every attack identified by
  226. * its attack id and ordered by date.
  227. *
  228. * @return A ArrayList with all received {@link Record Records} for each
  229. * attack id in the Database.
  230. */
  231. public ArrayList<Record> getAllReceivedRecordsOfEachAttack() {
  232. ArrayList<Record> recordList = new ArrayList<Record>();
  233. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " NATURAL JOIN "
  234. + NetworkEntry.TABLE_NAME + " WHERE " + PacketEntry.COLUMN_NAME_TYPE + "='RECEIVE'" + " ORDER BY " + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  235. SQLiteDatabase db = this.getReadableDatabase();
  236. Cursor cursor = db.rawQuery(selectQuery, null);
  237. // looping through all rows and adding to list
  238. if (cursor.moveToFirst()) {
  239. do {
  240. Record record = createRecord(cursor);
  241. // Adding record to list
  242. recordList.add(record);
  243. } while (cursor.moveToNext());
  244. }
  245. cursor.close();
  246. // return record list
  247. db.close();
  248. return recordList;
  249. }
  250. /**
  251. * Gets all {@link Record Records} saved in the database.
  252. *
  253. * @return A ArrayList of all the {@link Record Records} in the Database.
  254. */
  255. public ArrayList<Record> getAllRecords() {
  256. ArrayList<Record> recordList = new ArrayList<Record>();
  257. // Select All Query
  258. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME
  259. + " USING (_bssid)";
  260. SQLiteDatabase db = this.getWritableDatabase();
  261. Cursor cursor = db.rawQuery(selectQuery, null);
  262. Log.i("Database", "Start loop");
  263. // looping through all rows and adding to list
  264. if (cursor.moveToFirst()) {
  265. do {
  266. Log.i("Database", "Add Record");
  267. Record record = createRecord(cursor);
  268. // Adding record to list
  269. recordList.add(record);
  270. } while (cursor.moveToNext());
  271. }
  272. cursor.close();
  273. db.close();
  274. // return record list
  275. return recordList;
  276. }
  277. /**
  278. * Determines the number of different attack_ids in the database.
  279. *
  280. * @return The number of different attack_ids in the database.
  281. */
  282. public int getAttackCount() {
  283. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME;
  284. SQLiteDatabase db = this.getReadableDatabase();
  285. Cursor cursor = db.rawQuery(countQuery, null);
  286. int result = cursor.getCount();
  287. cursor.close();
  288. // return count
  289. db.close();
  290. return result;
  291. }
  292. /**
  293. * Determines the number of different attack_ids for a specific protocol in
  294. * the database.
  295. *
  296. * @param protocol
  297. * The String representation of the
  298. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  299. * Protocol}
  300. * @return The number of different attack_ids in the database.
  301. */
  302. public int getAttackPerProtocolCount(String protocol) {
  303. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " WHERE " + AttackEntry.COLUMN_NAME_PROTOCOL + " = " + "'" + protocol + "'";
  304. SQLiteDatabase db = this.getReadableDatabase();
  305. Cursor cursor = db.rawQuery(countQuery, null);
  306. int result = cursor.getCount();
  307. cursor.close();
  308. // return count
  309. db.close();
  310. return result;
  311. }
  312. /**
  313. * Determines the highest attack id stored in the database.
  314. *
  315. * @return The highest attack id stored in the database.
  316. */
  317. public long getHighestAttackId() {
  318. String selectQuery = "SELECT MAX(" + AttackEntry.COLUMN_NAME_ATTACK_ID + ") FROM " + AttackEntry.TABLE_NAME;
  319. SQLiteDatabase db = this.getReadableDatabase();
  320. Cursor cursor = db.rawQuery(selectQuery, null);
  321. int result;
  322. if (cursor.moveToFirst()) {
  323. result = cursor.getInt(0);
  324. } else {
  325. result = -1;
  326. }
  327. cursor.close();
  328. db.close();
  329. return result;
  330. }
  331. public ArrayList<HashMap<String, Object>> getNetworkInformation() {
  332. String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME;
  333. SQLiteDatabase db = this.getReadableDatabase();
  334. Cursor cursor = db.rawQuery(selectQuery, null);
  335. ArrayList<HashMap<String, Object>> networkInformation = new ArrayList<HashMap<String, Object>>();
  336. // looping through all rows and adding to list
  337. if (cursor.moveToFirst()) {
  338. do {
  339. HashMap<String, Object> values = new HashMap<String, Object>();
  340. values.put(NetworkEntry.COLUMN_NAME_BSSID, cursor.getString(0));
  341. values.put(NetworkEntry.COLUMN_NAME_SSID, cursor.getString(1));
  342. values.put(NetworkEntry.COLUMN_NAME_LATITUDE, Double.parseDouble(cursor.getString(2)));
  343. values.put(NetworkEntry.COLUMN_NAME_LONGITUDE, Double.parseDouble(cursor.getString(3)));
  344. values.put(NetworkEntry.COLUMN_NAME_ACCURACY, Float.parseFloat(cursor.getString(4)));
  345. values.put(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP, cursor.getLong(5));
  346. networkInformation.add(values);
  347. } while (cursor.moveToNext());
  348. }
  349. cursor.close();
  350. db.close();
  351. return networkInformation;
  352. }
  353. /**
  354. * Gets a single {@link Record} with the given ID from the database.
  355. *
  356. * @param id
  357. * The ID of the {@link Record};
  358. * @return The {@link Record}.
  359. */
  360. public Record getRecord(int id) {
  361. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " NATURAL JOIN "
  362. + NetworkEntry.TABLE_NAME + " WHERE " + PacketEntry.COLUMN_NAME_ID + " = " + id;
  363. SQLiteDatabase db = this.getReadableDatabase();
  364. Cursor cursor = db.rawQuery(selectQuery, null);
  365. Record record = null;
  366. if (cursor.moveToFirst()) {
  367. record = createRecord(cursor);
  368. }
  369. cursor.close();
  370. db.close();
  371. // return contact
  372. return record;
  373. }
  374. /**
  375. * Determines the number of {@link Record Records} in the database.
  376. *
  377. * @return The number of {@link Record Records} in the database.
  378. */
  379. public int getRecordCount() {
  380. String countQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME;
  381. SQLiteDatabase db = this.getReadableDatabase();
  382. Cursor cursor = db.rawQuery(countQuery, null);
  383. int result = cursor.getCount();
  384. cursor.close();
  385. // return count
  386. db.close();
  387. return result;
  388. }
  389. /**
  390. * Gets a single {@link Record} with the given attack id from the database.
  391. *
  392. * @param attack_id
  393. * The attack id of the {@link Record};
  394. * @return The {@link Record}.
  395. */
  396. public Record getRecordOfAttackId(long attack_id) {
  397. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " NATURAL JOIN "
  398. + NetworkEntry.TABLE_NAME + " WHERE " + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id + " GROUP BY "
  399. + AttackEntry.COLUMN_NAME_ATTACK_ID;
  400. SQLiteDatabase db = this.getReadableDatabase();
  401. Cursor cursor = db.rawQuery(selectQuery, null);
  402. Record record = null;
  403. if (cursor.moveToFirst()) {
  404. record = createRecord(cursor);
  405. }
  406. cursor.close();
  407. // return record list
  408. db.close();
  409. return record;
  410. }
  411. /**
  412. * Gets a representative {@link Record} for every attack identified by its
  413. * attack id.
  414. *
  415. * @return A ArrayList with one {@link Record Records} for each attack id in
  416. * the Database.
  417. */
  418. public ArrayList<Record> getRecordOfEachAttack() {
  419. ArrayList<Record> recordList = new ArrayList<Record>();
  420. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " NATURAL JOIN "
  421. + NetworkEntry.TABLE_NAME + " GROUP BY " + AttackEntry.COLUMN_NAME_ATTACK_ID;
  422. SQLiteDatabase db = this.getReadableDatabase();
  423. Cursor cursor = db.rawQuery(selectQuery, null);
  424. // looping through all rows and adding to list
  425. if (cursor.moveToFirst()) {
  426. do {
  427. Record record = createRecord(cursor);
  428. // Adding record to list
  429. recordList.add(record);
  430. } while (cursor.moveToNext());
  431. }
  432. cursor.close();
  433. // return record list
  434. db.close();
  435. return recordList;
  436. }
  437. /**
  438. * Gets a representative {@link Record} for every attack with a higher
  439. * attack id than the specified.
  440. *
  441. * @param attack_id
  442. * The attack id to match the query against.
  443. * @return A ArrayList with one {@link Record Records} for each attack id
  444. * higher than the given.
  445. */
  446. public ArrayList<Record> getRecordOfEachAttack(long attack_id) {
  447. ArrayList<Record> recordList = new ArrayList<Record>();
  448. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " NATURAL JOIN "
  449. + NetworkEntry.TABLE_NAME + " WHERE " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > " + attack_id + " GROUP BY "
  450. + AttackEntry.COLUMN_NAME_ATTACK_ID;
  451. SQLiteDatabase db = this.getReadableDatabase();
  452. Cursor cursor = db.rawQuery(selectQuery, null);
  453. // looping through all rows and adding to list
  454. if (cursor.moveToFirst()) {
  455. do {
  456. Record record = createRecord(cursor);
  457. // Adding record to list
  458. recordList.add(record);
  459. } while (cursor.moveToNext());
  460. }
  461. cursor.close();
  462. // return count
  463. db.close();
  464. return recordList;
  465. }
  466. /**
  467. * Determines the smallest attack id stored in the database.
  468. *
  469. * @return The smallest attack id stored in the database.
  470. */
  471. public long getSmallestAttackId() {
  472. String selectQuery = "SELECT MIN(" + AttackEntry.COLUMN_NAME_ATTACK_ID + ") FROM " + AttackEntry.TABLE_NAME;
  473. SQLiteDatabase db = this.getReadableDatabase();
  474. Cursor cursor = db.rawQuery(selectQuery, null);
  475. int result;
  476. if (cursor.moveToFirst()) {
  477. result = cursor.getInt(0);
  478. } else {
  479. result = -1;
  480. }
  481. cursor.close();
  482. db.close();
  483. return result;
  484. }
  485. /**
  486. * Gets the last recorded SSID to a given BSSID.
  487. *
  488. * @param bssid
  489. * The BSSID to match against.
  490. * @return A String of the last SSID or null if the BSSID is not in the
  491. * database.
  492. */
  493. public String getSSID(String bssid) {
  494. String selectQuery = "SELECT " + NetworkEntry.COLUMN_NAME_SSID + " FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID
  495. + " = " + "'" + bssid + "'";
  496. SQLiteDatabase db = this.getReadableDatabase();
  497. Cursor cursor = db.rawQuery(selectQuery, null);
  498. String ssid = null;
  499. if (cursor.moveToFirst()) {
  500. ssid = cursor.getString(0);
  501. }
  502. cursor.close();
  503. db.close();
  504. return ssid;
  505. }
  506. public void updateNetworkInformation(ArrayList<HashMap<String, Object>> networkInformation) {
  507. Log.i("DatabaseHandler", "Starte updating");
  508. for (HashMap<String, Object> values : networkInformation) {
  509. updateNetworkInformation(values);
  510. }
  511. }
  512. public void updateNetworkInformation(HashMap<String, Object> networkInformation) {
  513. SQLiteDatabase db = this.getReadableDatabase();
  514. String bssid = (String) networkInformation.get(NetworkEntry.COLUMN_NAME_BSSID);
  515. String bssidQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID + " = " + "'" + bssid + "'";
  516. Cursor cursor = db.rawQuery(bssidQuery, null);
  517. int result = cursor.getCount();
  518. if (cursor != null && cursor.moveToFirst()
  519. && (result <= 0 || cursor.getLong(5) < (Long) networkInformation.get(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP)))
  520. ;
  521. {
  522. ContentValues bssidValues = new ContentValues();
  523. bssidValues.put(NetworkEntry.COLUMN_NAME_BSSID, bssid);
  524. bssidValues.put(NetworkEntry.COLUMN_NAME_SSID, (String) networkInformation.get(NetworkEntry.COLUMN_NAME_SSID));
  525. bssidValues.put(NetworkEntry.COLUMN_NAME_LATITUDE, (double) (Double) networkInformation.get(NetworkEntry.COLUMN_NAME_LATITUDE));
  526. bssidValues.put(NetworkEntry.COLUMN_NAME_LONGITUDE, (double) (Double) networkInformation.get(NetworkEntry.COLUMN_NAME_LONGITUDE));
  527. bssidValues.put(NetworkEntry.COLUMN_NAME_ACCURACY, (float) (Float) networkInformation.get(NetworkEntry.COLUMN_NAME_ACCURACY));
  528. bssidValues.put(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP, (Long) networkInformation.get(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP));
  529. db.insertWithOnConflict(NetworkEntry.TABLE_NAME, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
  530. }
  531. cursor.close();
  532. db.close();
  533. }
  534. /**
  535. * Creates a {@link Record} from a Cursor. If the cursor does not show to a
  536. * valid data structure a runtime exception is thrown.
  537. *
  538. * @param cursor
  539. * @return Returns the created {@link Record} .
  540. */
  541. private Record createRecord(Cursor cursor) {
  542. Record record = new Record();
  543. record.setId(Integer.parseInt(cursor.getString(0)));
  544. record.setAttack_id(cursor.getLong(1));
  545. record.setType(TYPE.valueOf(cursor.getString(2)));
  546. record.setTimestamp(cursor.getLong(3));
  547. record.setPacket(cursor.getString(4));
  548. record.setProtocol(cursor.getString(5));
  549. record.setExternalIP(cursor.getString(6));
  550. record.setLocalIP(cursor.getString(7));
  551. record.setLocalPort(Integer.parseInt(cursor.getString(8)));
  552. record.setRemoteIP(cursor.getString(9));
  553. record.setRemotePort(Integer.parseInt(cursor.getString(10)));
  554. record.setBssid(cursor.getString(11));
  555. record.setSsid(cursor.getString(12));
  556. record.setLatitude(Double.parseDouble(cursor.getString(13)));
  557. record.setLongitude(Double.parseDouble(cursor.getString(14)));
  558. record.setAccuracy(Float.parseFloat(cursor.getString(15)));
  559. record.setTimestampLocation(cursor.getLong(16));
  560. return record;
  561. }
  562. }