HostageDbHelper.java 22 KB

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