UglyDbHelper.java 27 KB

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