UglyDbHelper.java 31 KB

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