HostageDBOpenHelper.java 24 KB

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