UglyDbHelper.java 27 KB

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