UglyDbHelper.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. package de.tudarmstadt.informatik.hostage.logging;
  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.TYPE;
  11. /**
  12. * This class creates SQL tables and handles all access to the database.<br>
  13. * It contains several methods with predefined queries to extract different
  14. * kinds of information from the database.<br>
  15. * The database contains two tables: {@link #TABLE_RECORDS} and
  16. * {@link #TABLE_BSSIDS}:<br>
  17. * {@link #TABLE_RECORDS} contains all logging information of a single message
  18. * record except the SSID.<br>
  19. * {@link #TABLE_BSSIDS} contains the BSSID of all recorded Networks and the
  20. * corresponding SSID.<br>
  21. *
  22. * @author Lars Pandikow
  23. */
  24. public class UglyDbHelper extends SQLiteOpenHelper {
  25. // All Static variables
  26. // Database Version
  27. private static final int DATABASE_VERSION = 1;
  28. // Database Name
  29. private static final String DATABASE_NAME = "recordManager";
  30. // Contacts table names
  31. private static final String TABLE_ATTACK_INFO = "attack_info";
  32. private static final String TABLE_RECORDS = "records";
  33. private static final String TABLE_BSSIDS = "bssids";
  34. // Contacts Table Columns names
  35. public static final String KEY_ID = "_id";
  36. public static final String KEY_ATTACK_ID = "_attack_id";
  37. public static final String KEY_TYPE = "type";
  38. public static final String KEY_TIME = "timestamp";
  39. public static final String KEY_PACKET = "packet";
  40. public static final String KEY_PROTOCOL = "protocol";
  41. public static final String KEY_EXTERNAL_IP = "externalIP";
  42. public static final String KEY_LOCAL_IP = "localIP";
  43. public static final String KEY_LOCAL_HOSTNAME = "localHostName";
  44. public static final String KEY_LOCAL_PORT = "localPort";
  45. public static final String KEY_REMOTE_IP = "remoteIP";
  46. public static final String KEY_REMOTE_HOSTNAME = "remoteHostName";
  47. public static final String KEY_REMOTE_PORT = "remotePort";
  48. public static final String KEY_BSSID = "_bssid";
  49. public static final String KEY_SSID = "ssid";
  50. public static final String KEY_LATITUDE = "latitude";
  51. public static final String KEY_LONGITUDE = "longitude";
  52. public static final String KEY_ACCURACY = "accuracy";
  53. // Database sql create statements
  54. private static final String CREATE_RECORD_TABLE = "CREATE TABLE "
  55. + TABLE_RECORDS + "(" + KEY_ID + " INTEGER NOT NULL,"
  56. + KEY_ATTACK_ID + " INTEGER NOT NULL," + KEY_TYPE + " TEXT,"
  57. + KEY_TIME + " INTEGER," + KEY_PACKET + " TEXT," + "FOREIGN KEY("
  58. + KEY_ATTACK_ID + ") REFERENCES " + TABLE_ATTACK_INFO + "("
  59. + KEY_ATTACK_ID + ")," + "PRIMARY KEY(" + KEY_ID + ", "
  60. + KEY_ATTACK_ID + ")" + ")";
  61. private static final String CREATE_ATTACK_INFO_TABLE = "CREATE TABLE "
  62. + TABLE_ATTACK_INFO + "(" + KEY_ATTACK_ID + " INTEGER PRIMARY KEY,"
  63. + KEY_PROTOCOL + " TEXT," + KEY_EXTERNAL_IP + " TEXT,"
  64. + KEY_LOCAL_IP + " BLOB," + KEY_LOCAL_HOSTNAME + " TEXT,"
  65. + KEY_LOCAL_PORT + " INTEGER," + KEY_REMOTE_IP + " BLOB,"
  66. + KEY_REMOTE_HOSTNAME + " TEXT," + KEY_REMOTE_PORT + " INTEGER,"
  67. + KEY_BSSID + " TEXT," + "FOREIGN KEY(" + KEY_BSSID
  68. + ") REFERENCES " + TABLE_BSSIDS + "(" + KEY_BSSID + ")" + ")";
  69. private static final String CREATE_BSSID_TABLE = "CREATE TABLE "
  70. + TABLE_BSSIDS + "(" + KEY_BSSID + " TEXT PRIMARY KEY," + KEY_SSID
  71. + " TEXT," + KEY_LATITUDE + " INTEGER," + KEY_LONGITUDE
  72. + " INTEGER," + KEY_ACCURACY + " INTEGER," + KEY_TIME + " INTEGER"
  73. + ")";
  74. public UglyDbHelper(Context context) {
  75. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  76. }
  77. /**
  78. * Adds a given {@link Record} to the database.
  79. *
  80. * @param record
  81. * The added {@link Record} .
  82. */
  83. public void addRecord(Record record) {
  84. SQLiteDatabase db = this.getWritableDatabase();
  85. HashMap<String, Object> bssidValues = new HashMap<String, Object>();
  86. bssidValues.put(KEY_BSSID, record.getBssid());
  87. bssidValues.put(KEY_SSID, record.getSsid());
  88. bssidValues.put(KEY_LATITUDE, record.getLatitude());
  89. bssidValues.put(KEY_LONGITUDE, record.getLongitude());
  90. bssidValues.put(KEY_ACCURACY, record.getAccuracy());
  91. bssidValues.put(KEY_TIME, record.getTimestampLocation());
  92. ContentValues attackValues = new ContentValues();
  93. attackValues.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  94. attackValues.put(KEY_PROTOCOL, record.getProtocol().toString());
  95. attackValues.put(KEY_EXTERNAL_IP, record.getExternalIP());
  96. attackValues.put(KEY_LOCAL_IP, record.getLocalIP()); // Log Local IP
  97. attackValues.put(KEY_LOCAL_HOSTNAME, record.getLocalHost());
  98. attackValues.put(KEY_LOCAL_PORT, record.getLocalPort());
  99. attackValues.put(KEY_REMOTE_IP, record.getRemoteIP()); // Log Remote IP
  100. attackValues.put(KEY_REMOTE_HOSTNAME, record.getRemoteHost());
  101. attackValues.put(KEY_REMOTE_PORT, record.getRemotePort()); // Log Remote
  102. // Port
  103. attackValues.put(KEY_BSSID, record.getBssid());
  104. ContentValues recordValues = new ContentValues();
  105. recordValues.put(KEY_ID, record.getId()); // Log Message Number
  106. recordValues.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  107. recordValues.put(KEY_TYPE, record.getType().name()); // Log Type
  108. recordValues.put(KEY_TIME, record.getTimestamp()); // Log Timestamp
  109. recordValues.put(KEY_PACKET, record.getPacket()); // Log Packet
  110. // Inserting Rows
  111. db.insertWithOnConflict(TABLE_ATTACK_INFO, null, attackValues,
  112. SQLiteDatabase.CONFLICT_REPLACE);
  113. db.insert(TABLE_RECORDS, null, recordValues);
  114. db.close(); // Closing database connection
  115. // Update Network Information
  116. updateNetworkInformation(bssidValues);
  117. }
  118. /**
  119. * Determines if a network with given BSSID has already been recorded as
  120. * malicious.
  121. *
  122. * @param BSSID
  123. * The BSSID of the network.
  124. * @return True if an attack has been recorded in a network with the given
  125. * BSSID, else false.
  126. */
  127. public boolean bssidSeen(String BSSID) {
  128. String countQuery = "SELECT * FROM " + TABLE_BSSIDS + " WHERE "
  129. + KEY_BSSID + " = " + "'" + BSSID + "'";
  130. SQLiteDatabase db = this.getReadableDatabase();
  131. Cursor cursor = db.rawQuery(countQuery, null);
  132. int result = cursor.getCount();
  133. cursor.close();
  134. db.close();
  135. return result > 0;
  136. }
  137. /**
  138. * Determines if an attack has been recorded on a specific protocol in a
  139. * network with a given BSSID.
  140. *
  141. * @param protocol
  142. * The
  143. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  144. * Protocol} to inspect.
  145. * @param BSSID
  146. * The BSSID of the network.
  147. * @return True if an attack on the given protocol has been recorded in a
  148. * network with the given BSSID, else false.
  149. */
  150. public boolean bssidSeen(String protocol, String BSSID) {
  151. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO
  152. + " NATURAL JOIN " + TABLE_BSSIDS + " WHERE " + KEY_PROTOCOL
  153. + " = " + "'" + protocol + "'" + " AND " + KEY_BSSID + " = "
  154. + "'" + BSSID + "'";
  155. SQLiteDatabase db = this.getReadableDatabase();
  156. Cursor cursor = db.rawQuery(countQuery, null);
  157. int result = cursor.getCount();
  158. cursor.close();
  159. db.close();
  160. return result > 0;
  161. }
  162. /**
  163. * Deletes all records from {@link #TABLE_RECORDS}.
  164. */
  165. public void clearData() {
  166. SQLiteDatabase db = this.getReadableDatabase();
  167. db.delete(TABLE_RECORDS, null, null);
  168. db.delete(TABLE_ATTACK_INFO, null, null);
  169. db.close();
  170. }
  171. /**
  172. * Deletes all records from {@link #TABLE_RECORDS} with a specific BSSID.
  173. *
  174. * @param bssid
  175. * The BSSID to match against.
  176. */
  177. public void deleteByBSSID(String bssid) {
  178. SQLiteDatabase db = this.getReadableDatabase();
  179. db.delete(TABLE_RECORDS, KEY_BSSID + " = ?", new String[] { bssid });
  180. db.delete(TABLE_ATTACK_INFO, KEY_BSSID + " = ?", new String[] { bssid });
  181. db.close();
  182. }
  183. // TODO Delete statement �berarbeiten
  184. /**
  185. * Deletes all records from {@link #TABLE_RECORDS} with a time stamp smaller
  186. * then the given
  187. *
  188. * @param date
  189. * A Date represented in milliseconds.
  190. */
  191. public void deleteByDate(long date) {
  192. SQLiteDatabase db = this.getReadableDatabase();
  193. String deleteQuery = "DELETE FROM " + TABLE_RECORDS + " WHERE "
  194. + KEY_TIME + " < " + date;
  195. // TODO Delete statement �berarbeiten
  196. // String deleteQuery2 = "DELETE "
  197. db.execSQL(deleteQuery);
  198. db.close();
  199. }
  200. /**
  201. * Returns a String array with all BSSIDs stored in the database.
  202. *
  203. * @return String[] of all recorded BSSIDs.
  204. */
  205. public String[] getAllBSSIDS() {
  206. String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
  207. SQLiteDatabase db = this.getReadableDatabase();
  208. Cursor cursor = db.rawQuery(selectQuery, null);
  209. String[] bssidList = new String[cursor.getCount()];
  210. int counter = 0;
  211. // looping through all rows and adding to list
  212. if (cursor.moveToFirst()) {
  213. do {
  214. bssidList[counter] = cursor.getString(0);
  215. counter++;
  216. } while (cursor.moveToNext());
  217. }
  218. cursor.close();
  219. db.close();
  220. return bssidList;
  221. }
  222. /**
  223. * Gets all received {@link Record Records} for every attack identified by
  224. * its attack id and ordered by date.
  225. *
  226. * @return A ArrayList with all received {@link Record Records} for each
  227. * attack id in the Database.
  228. */
  229. public ArrayList<Record> getAllReceivedRecordsOfEachAttack() {
  230. ArrayList<Record> recordList = new ArrayList<Record>();
  231. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  232. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN "
  233. + TABLE_BSSIDS + " WHERE " + KEY_TYPE + "='RECEIVE'"
  234. + " ORDER BY " + KEY_TIME;
  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 " + TABLE_RECORDS
  259. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN "
  260. + TABLE_BSSIDS;
  261. SQLiteDatabase db = this.getWritableDatabase();
  262. Cursor cursor = db.rawQuery(selectQuery, null);
  263. Log.i("Database", "Start loop");
  264. // looping through all rows and adding to list
  265. if (cursor.moveToFirst()) {
  266. do {
  267. Log.i("Database", "Add Record");
  268. Record record = createRecord(cursor);
  269. // Adding record to list
  270. recordList.add(record);
  271. } while (cursor.moveToNext());
  272. }
  273. cursor.close();
  274. db.close();
  275. // return record list
  276. return recordList;
  277. }
  278. /**
  279. * Determines the number of different attack_ids in the database.
  280. *
  281. * @return The number of different attack_ids in the database.
  282. */
  283. public int getAttackCount() {
  284. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO;
  285. SQLiteDatabase db = this.getReadableDatabase();
  286. Cursor cursor = db.rawQuery(countQuery, null);
  287. int result = cursor.getCount();
  288. cursor.close();
  289. // return count
  290. db.close();
  291. return result;
  292. }
  293. /**
  294. * Determines the number of different attack_ids for a specific protocol in
  295. * the database.
  296. *
  297. * @param protocol
  298. * The String representation of the
  299. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  300. * Protocol}
  301. * @return The number of different attack_ids in the database.
  302. */
  303. public int getAttackPerProtocolCount(String protocol) {
  304. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO + " WHERE "
  305. + KEY_PROTOCOL + " = " + "'" + protocol + "'";
  306. SQLiteDatabase db = this.getReadableDatabase();
  307. Cursor cursor = db.rawQuery(countQuery, null);
  308. int result = cursor.getCount();
  309. cursor.close();
  310. // return count
  311. db.close();
  312. return result;
  313. }
  314. /**
  315. * Determines the highest attack id stored in the database.
  316. *
  317. * @return The highest attack id stored in the database.
  318. */
  319. public long getHighestAttackId() {
  320. String selectQuery = "SELECT MAX(" + KEY_ATTACK_ID + ") FROM "
  321. + TABLE_ATTACK_INFO;
  322. SQLiteDatabase db = this.getReadableDatabase();
  323. Cursor cursor = db.rawQuery(selectQuery, null);
  324. int result;
  325. if (cursor.moveToFirst()) {
  326. result = cursor.getInt(0);
  327. } else {
  328. result = -1;
  329. }
  330. cursor.close();
  331. db.close();
  332. return result;
  333. }
  334. public ArrayList<HashMap<String, Object>> getNetworkInformation() {
  335. String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
  336. SQLiteDatabase db = this.getReadableDatabase();
  337. Cursor cursor = db.rawQuery(selectQuery, null);
  338. ArrayList<HashMap<String, Object>> networkInformation = new ArrayList<HashMap<String, Object>>();
  339. // looping through all rows and adding to list
  340. if (cursor.moveToFirst()) {
  341. do {
  342. HashMap<String, Object> values = new HashMap<String, Object>();
  343. values.put(KEY_BSSID, cursor.getString(0));
  344. values.put(KEY_SSID, cursor.getString(1));
  345. values.put(KEY_LATITUDE,
  346. Double.parseDouble(cursor.getString(2)));
  347. values.put(KEY_LONGITUDE,
  348. Double.parseDouble(cursor.getString(3)));
  349. values.put(KEY_ACCURACY, Float.parseFloat(cursor.getString(4)));
  350. values.put(KEY_TIME, cursor.getLong(5));
  351. networkInformation.add(values);
  352. } while (cursor.moveToNext());
  353. }
  354. cursor.close();
  355. db.close();
  356. return networkInformation;
  357. }
  358. /**
  359. * Gets a single {@link Record} with the given ID from the database.
  360. *
  361. * @param id
  362. * The ID of the {@link Record};
  363. * @return The {@link Record}.
  364. */
  365. public Record getRecord(int id) {
  366. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  367. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN "
  368. + TABLE_BSSIDS + " WHERE " + KEY_ID + " = " + id;
  369. SQLiteDatabase db = this.getReadableDatabase();
  370. Cursor cursor = db.rawQuery(selectQuery, null);
  371. Record record = null;
  372. if (cursor.moveToFirst()) {
  373. record = createRecord(cursor);
  374. }
  375. cursor.close();
  376. db.close();
  377. // return contact
  378. return record;
  379. }
  380. /**
  381. * Determines the number of {@link Record Records} in the database.
  382. *
  383. * @return The number of {@link Record Records} in the database.
  384. */
  385. public int getRecordCount() {
  386. String countQuery = "SELECT * FROM " + TABLE_RECORDS;
  387. SQLiteDatabase db = this.getReadableDatabase();
  388. Cursor cursor = db.rawQuery(countQuery, null);
  389. int result = cursor.getCount();
  390. cursor.close();
  391. // return count
  392. db.close();
  393. return result;
  394. }
  395. /**
  396. * Gets a single {@link Record} with the given attack id from the database.
  397. *
  398. * @param attack_id
  399. * The attack id of the {@link Record};
  400. * @return The {@link Record}.
  401. */
  402. public Record getRecordOfAttackId(long attack_id) {
  403. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  404. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN "
  405. + TABLE_BSSIDS + " WHERE " + KEY_ATTACK_ID + " = " + attack_id
  406. + " GROUP BY " + KEY_ATTACK_ID;
  407. SQLiteDatabase db = this.getReadableDatabase();
  408. Cursor cursor = db.rawQuery(selectQuery, null);
  409. Record record = null;
  410. if (cursor.moveToFirst()) {
  411. record = createRecord(cursor);
  412. }
  413. cursor.close();
  414. // return record list
  415. db.close();
  416. return record;
  417. }
  418. /**
  419. * Gets a representative {@link Record} for every attack identified by its
  420. * attack id.
  421. *
  422. * @return A ArrayList with one {@link Record Records} for each attack id in
  423. * the Database.
  424. */
  425. public ArrayList<Record> getRecordOfEachAttack() {
  426. ArrayList<Record> recordList = new ArrayList<Record>();
  427. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  428. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN "
  429. + TABLE_BSSIDS + " GROUP BY " + KEY_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 " + TABLE_RECORDS
  457. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN "
  458. + TABLE_BSSIDS + " WHERE " + KEY_ATTACK_ID + " > " + attack_id
  459. + " GROUP BY " + KEY_ATTACK_ID;
  460. SQLiteDatabase db = this.getReadableDatabase();
  461. Cursor cursor = db.rawQuery(selectQuery, null);
  462. // looping through all rows and adding to list
  463. if (cursor.moveToFirst()) {
  464. do {
  465. Record record = createRecord(cursor);
  466. // Adding record to list
  467. recordList.add(record);
  468. } while (cursor.moveToNext());
  469. }
  470. cursor.close();
  471. // return count
  472. db.close();
  473. return recordList;
  474. }
  475. /**
  476. * Determines the smallest attack id stored in the database.
  477. *
  478. * @return The smallest attack id stored in the database.
  479. */
  480. public long getSmallestAttackId() {
  481. String selectQuery = "SELECT MIN(" + KEY_ATTACK_ID + ") FROM "
  482. + TABLE_ATTACK_INFO;
  483. SQLiteDatabase db = this.getReadableDatabase();
  484. Cursor cursor = db.rawQuery(selectQuery, null);
  485. int result;
  486. if (cursor.moveToFirst()) {
  487. result = cursor.getInt(0);
  488. } else {
  489. result = -1;
  490. }
  491. cursor.close();
  492. db.close();
  493. return result;
  494. }
  495. /**
  496. * Gets the last recorded SSID to a given BSSID.
  497. *
  498. * @param bssid
  499. * The BSSID to match against.
  500. * @return A String of the last SSID or null if the BSSID is not in the
  501. * database.
  502. */
  503. public String getSSID(String bssid) {
  504. String selectQuery = "SELECT " + KEY_SSID + " FROM " + TABLE_BSSIDS
  505. + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
  506. SQLiteDatabase db = this.getReadableDatabase();
  507. Cursor cursor = db.rawQuery(selectQuery, null);
  508. String ssid = null;
  509. if (cursor.moveToFirst()) {
  510. ssid = cursor.getString(0);
  511. }
  512. cursor.close();
  513. db.close();
  514. return ssid;
  515. }
  516. // Creating Tables
  517. @Override
  518. public void onCreate(SQLiteDatabase db) {
  519. db.execSQL(CREATE_BSSID_TABLE);
  520. db.execSQL(CREATE_ATTACK_INFO_TABLE);
  521. db.execSQL(CREATE_RECORD_TABLE);
  522. }
  523. // Upgrading database
  524. @Override
  525. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  526. // Drop older table if existed
  527. db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS);
  528. db.execSQL("DROP TABLE IF EXISTS " + TABLE_ATTACK_INFO);
  529. db.execSQL("DROP TABLE IF EXISTS " + TABLE_BSSIDS);
  530. // Create tables again
  531. onCreate(db);
  532. }
  533. public void updateNetworkInformation(
  534. ArrayList<HashMap<String, Object>> networkInformation) {
  535. Log.i("DatabaseHandler", "Starte updating");
  536. for (HashMap<String, Object> values : networkInformation) {
  537. updateNetworkInformation(values);
  538. }
  539. }
  540. public void updateNetworkInformation(
  541. HashMap<String, Object> networkInformation) {
  542. SQLiteDatabase db = this.getReadableDatabase();
  543. String bssid = (String) networkInformation.get(KEY_BSSID);
  544. String bssidQuery = "SELECT * FROM " + TABLE_BSSIDS + " WHERE "
  545. + KEY_BSSID + " = " + "'" + bssid + "'";
  546. Cursor cursor = db.rawQuery(bssidQuery, null);
  547. int result = cursor.getCount();
  548. if (cursor != null
  549. && cursor.moveToFirst()
  550. && (result <= 0 || cursor.getLong(5) < (Long) networkInformation
  551. .get(KEY_TIME)))
  552. ;
  553. {
  554. ContentValues bssidValues = new ContentValues();
  555. bssidValues.put(KEY_BSSID, bssid);
  556. bssidValues
  557. .put(KEY_SSID, (String) networkInformation.get(KEY_SSID));
  558. bssidValues.put(KEY_LATITUDE,
  559. (double) (Double) networkInformation.get(KEY_LATITUDE));
  560. bssidValues.put(KEY_LONGITUDE,
  561. (double) (Double) networkInformation.get(KEY_LONGITUDE));
  562. bssidValues.put(KEY_ACCURACY,
  563. (float) (Float) networkInformation.get(KEY_ACCURACY));
  564. bssidValues.put(KEY_TIME, (Long) networkInformation.get(KEY_TIME));
  565. db.insertWithOnConflict(TABLE_BSSIDS, null, bssidValues,
  566. SQLiteDatabase.CONFLICT_REPLACE);
  567. }
  568. cursor.close();
  569. db.close();
  570. }
  571. /**
  572. * Creates a {@link Record} from a Cursor. If the cursor does not show to a
  573. * valid data structure a runtime exception is thrown.
  574. *
  575. * @param cursor
  576. * @return Returns the created {@link Record} .
  577. */
  578. private Record createRecord(Cursor cursor) {
  579. Record record = new Record();
  580. record.setId(Integer.parseInt(cursor.getString(0)));
  581. record.setAttack_id(cursor.getLong(1));
  582. record.setType(cursor.getString(2).equals("SEND") ? TYPE.SEND
  583. : TYPE.RECEIVE);
  584. record.setTimestamp(cursor.getLong(3));
  585. record.setPacket(cursor.getString(4));
  586. record.setProtocol(cursor.getString(5));
  587. record.setExternalIP(cursor.getString(6));
  588. record.setLocalIP(cursor.getString(7));
  589. record.setLocalHost(cursor.getString(8));
  590. record.setLocalPort(Integer.parseInt(cursor.getString(9)));
  591. record.setRemoteIP(cursor.getString(10));
  592. record.setRemoteHost(cursor.getString(11));
  593. record.setRemotePort(Integer.parseInt(cursor.getString(12)));
  594. record.setBssid(cursor.getString(13));
  595. record.setSsid(cursor.getString(14));
  596. record.setLatitude(Double.parseDouble(cursor.getString(15)));
  597. record.setLongitude(Double.parseDouble(cursor.getString(16)));
  598. record.setAccuracy(Float.parseFloat(cursor.getString(17)));
  599. record.setTimestampLocation(cursor.getLong(18));
  600. return record;
  601. }
  602. }