UglyDbHelper.java 27 KB

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