UglyDbHelper.java 27 KB

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