UglyDbHelper.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. if (filter.getSorttype() == LogFilter.SortType.timestamp){
  157. //DESC
  158. selectQuery = selectQuery + " ORDER BY " + filter.getSorttype() + " DESC";
  159. } else {
  160. selectQuery = selectQuery + " ORDER BY " + filter.getSorttype();
  161. }
  162. System.out.println(selectQuery);
  163. SQLiteDatabase db = this.getReadableDatabase();
  164. Cursor cursor = db.rawQuery(selectQuery, null);
  165. // looping through all rows and adding to list
  166. if (cursor.moveToFirst()) {
  167. do {
  168. Record record = createRecord(cursor);
  169. // Adding record to list
  170. recordList.add(record);
  171. } while (cursor.moveToNext());
  172. }
  173. cursor.close();
  174. // return record list
  175. db.close();
  176. return recordList;
  177. }
  178. /**
  179. * Gets all non duplicate Records For the key BSSID.
  180. * @return A ArrayList with received Records.
  181. */
  182. public ArrayList<String> getUniqueBSSIDRecords(){
  183. return this.getUniqueDataEntryForKeyType(KEY_BSSID, TABLE_BSSIDS);
  184. }
  185. /**
  186. * Gets all non duplicate Records For the key ESSID.
  187. * @return A ArrayList with received Records.
  188. */
  189. public ArrayList<String> getUniqueESSIDRecords(){
  190. return this.getUniqueDataEntryForKeyType(KEY_SSID, TABLE_BSSIDS);
  191. }
  192. public ArrayList<String> getUniqueESSIDRecordsForProtocol(String protocol){
  193. return this.getUniqueIDForProtocol(KEY_SSID, protocol);
  194. }
  195. public ArrayList<String> getUniqueBSSIDRecordsForProtocol(String protocol){
  196. return this.getUniqueIDForProtocol(KEY_BSSID, protocol);
  197. }
  198. private ArrayList<String> getUniqueIDForProtocol(String id, String protocol){
  199. ArrayList<String> recordList = new ArrayList<String>();
  200. String selectQuery = "SELECT DISTINCT " + id + " FROM " + TABLE_ATTACK_INFO + " JOIN " + TABLE_BSSIDS + " USING "+ "(" + id + ") " + " WHERE " + TABLE_ATTACK_INFO + "."+ KEY_PROTOCOL + " = " + "'" + protocol + "'"+ " ORDER BY " + id; // " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS +
  201. // ORDERED BY TIME
  202. System.out.println(selectQuery);
  203. SQLiteDatabase db = this.getReadableDatabase();
  204. Cursor cursor = db.rawQuery(selectQuery, null);
  205. // looping through all rows and adding to list
  206. if (cursor.moveToFirst()) {
  207. do {
  208. String record = cursor.getString(0);
  209. recordList.add(record);
  210. } while (cursor.moveToNext());
  211. }
  212. cursor.close();
  213. // return record list
  214. db.close();
  215. return recordList;
  216. }
  217. /**
  218. * Gets all non duplicate Data Entry For a specific KeyType ( e.g. BSSIDs).
  219. * @return A ArrayList with received Records.
  220. */
  221. public ArrayList<String> getUniqueDataEntryForKeyType(String keyType, String table) {
  222. ArrayList<String> recordList = new ArrayList<String>();
  223. //String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS;
  224. String selectQuery = "SELECT DISTINCT " + keyType + " FROM " + table + " ORDER BY " + keyType; // " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS +
  225. // ORDERED BY TIME
  226. System.out.println(selectQuery);
  227. SQLiteDatabase db = this.getReadableDatabase();
  228. Cursor cursor = db.rawQuery(selectQuery, null);
  229. // looping through all rows and adding to list
  230. if (cursor.moveToFirst()) {
  231. do {
  232. String record = cursor.getString(0);
  233. recordList.add(record);
  234. } while (cursor.moveToNext());
  235. }
  236. cursor.close();
  237. // return record list
  238. db.close();
  239. return recordList;
  240. }
  241. /**
  242. * Adds a given {@link Record} to the database.
  243. *
  244. * @param record
  245. * The added {@link Record} .
  246. */
  247. public void addRecord(Record record) {
  248. SQLiteDatabase db = this.getWritableDatabase();
  249. HashMap<String, Object> bssidValues = new HashMap<String, Object>();
  250. bssidValues.put(KEY_BSSID, record.getBssid());
  251. bssidValues.put(KEY_SSID, record.getSsid());
  252. bssidValues.put(KEY_LATITUDE, record.getLatitude());
  253. bssidValues.put(KEY_LONGITUDE, record.getLongitude());
  254. bssidValues.put(KEY_ACCURACY, record.getAccuracy());
  255. bssidValues.put(KEY_TIME, record.getTimestampLocation());
  256. ContentValues attackValues = new ContentValues();
  257. attackValues.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  258. attackValues.put(KEY_PROTOCOL, record.getProtocol().toString());
  259. attackValues.put(KEY_EXTERNAL_IP, record.getExternalIP());
  260. attackValues.put(KEY_LOCAL_IP, record.getLocalIP()); // Log Local IP
  261. attackValues.put(KEY_LOCAL_HOSTNAME, record.getLocalHost());
  262. attackValues.put(KEY_LOCAL_PORT, record.getLocalPort());
  263. attackValues.put(KEY_REMOTE_IP, record.getRemoteIP()); // Log Remote IP
  264. attackValues.put(KEY_REMOTE_HOSTNAME, record.getRemoteHost());
  265. attackValues.put(KEY_REMOTE_PORT, record.getRemotePort()); // Log Remote
  266. // Port
  267. attackValues.put(KEY_BSSID, record.getBssid());
  268. ContentValues recordValues = new ContentValues();
  269. recordValues.put(KEY_ID, record.getId()); // Log Message Number
  270. recordValues.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  271. recordValues.put(KEY_TYPE, record.getType().name()); // Log Type
  272. recordValues.put(KEY_TIME, record.getTimestamp()); // Log Timestamp
  273. recordValues.put(KEY_PACKET, record.getPacket()); // Log Packet
  274. // Inserting Rows
  275. db.insertWithOnConflict(TABLE_ATTACK_INFO, null, attackValues,
  276. SQLiteDatabase.CONFLICT_REPLACE);
  277. db.insert(TABLE_RECORDS, null, recordValues);
  278. db.close(); // Closing database connection
  279. // Update Network Information
  280. updateNetworkInformation(bssidValues);
  281. }
  282. /**
  283. * Determines if a network with given BSSID has already been recorded as
  284. * malicious.
  285. *
  286. * @param BSSID
  287. * The BSSID of the network.
  288. * @return True if an attack has been recorded in a network with the given
  289. * BSSID, else false.
  290. */
  291. public boolean bssidSeen(String BSSID) {
  292. String countQuery = "SELECT * FROM " + TABLE_BSSIDS + " WHERE "
  293. + KEY_BSSID + " = " + "'" + BSSID + "'";
  294. SQLiteDatabase db = this.getReadableDatabase();
  295. Cursor cursor = db.rawQuery(countQuery, null);
  296. int result = cursor.getCount();
  297. cursor.close();
  298. db.close();
  299. return result > 0;
  300. }
  301. public int numBssidSeen(String protocol, String BSSID) {
  302. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO
  303. + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")" + " WHERE " + TABLE_ATTACK_INFO + "." + KEY_PROTOCOL
  304. + " = " + "'" + protocol + "'" + " AND " + TABLE_BSSIDS+ "."+ KEY_BSSID + " = "
  305. + "'" + BSSID + "'";
  306. SQLiteDatabase db = this.getReadableDatabase();
  307. Cursor cursor = db.rawQuery(countQuery, null);
  308. int result = cursor.getCount();
  309. cursor.close();
  310. db.close();
  311. return result;
  312. }
  313. /**
  314. * Determines if an attack has been recorded on a specific protocol in a
  315. * network with a given BSSID.
  316. *
  317. * @param protocol
  318. * The
  319. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  320. * Protocol} to inspect.
  321. * @param BSSID
  322. * The BSSID of the network.
  323. * @return True if an attack on the given protocol has been recorded in a
  324. * network with the given BSSID, else false.
  325. */
  326. public boolean bssidSeen(String protocol, String BSSID) {
  327. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO
  328. + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")" + " WHERE " + TABLE_ATTACK_INFO + "." + KEY_PROTOCOL
  329. + " = " + "'" + protocol + "'" + " AND " + TABLE_BSSIDS+ "."+ KEY_BSSID + " = "
  330. + "'" + BSSID + "'";
  331. SQLiteDatabase db = this.getReadableDatabase();
  332. Cursor cursor = db.rawQuery(countQuery, null);
  333. int result = cursor.getCount();
  334. cursor.close();
  335. db.close();
  336. return result > 0;
  337. }
  338. /**
  339. * Deletes all records from {@link #TABLE_RECORDS}.
  340. */
  341. public void clearData() {
  342. SQLiteDatabase db = this.getReadableDatabase();
  343. db.delete(TABLE_RECORDS, null, null);
  344. db.delete(TABLE_ATTACK_INFO, null, null);
  345. db.delete(TABLE_PROFILES, null, null);
  346. db.close();
  347. }
  348. /**
  349. * Deletes all records from {@link #TABLE_RECORDS} with a specific BSSID.
  350. *
  351. * @param bssid
  352. * The BSSID to match against.
  353. */
  354. public void deleteByBSSID(String bssid) {
  355. SQLiteDatabase db = this.getReadableDatabase();
  356. db.delete(TABLE_RECORDS, KEY_BSSID + " = ?", new String[] { bssid });
  357. db.delete(TABLE_ATTACK_INFO, KEY_BSSID + " = ?", new String[] { bssid });
  358. db.close();
  359. }
  360. // TODO Delete statement �berarbeiten
  361. /**
  362. * Deletes all records from {@link #TABLE_RECORDS} with a time stamp smaller
  363. * then the given
  364. *
  365. * @param date
  366. * A Date represented in milliseconds.
  367. */
  368. public void deleteByDate(long date) {
  369. SQLiteDatabase db = this.getReadableDatabase();
  370. String deleteQuery = "DELETE FROM " + TABLE_RECORDS + " WHERE "
  371. + KEY_TIME + " < " + date;
  372. // TODO Delete statement �berarbeiten
  373. // String deleteQuery2 = "DELETE "
  374. db.execSQL(deleteQuery);
  375. db.close();
  376. }
  377. /**
  378. * Returns a String array with all BSSIDs stored in the database.
  379. *
  380. * @return String[] of all recorded BSSIDs.
  381. */
  382. public String[] getAllBSSIDS() {
  383. String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
  384. SQLiteDatabase db = this.getReadableDatabase();
  385. Cursor cursor = db.rawQuery(selectQuery, null);
  386. String[] bssidList = new String[cursor.getCount()];
  387. int counter = 0;
  388. // looping through all rows and adding to list
  389. if (cursor.moveToFirst()) {
  390. do {
  391. bssidList[counter] = cursor.getString(0);
  392. counter++;
  393. } while (cursor.moveToNext());
  394. }
  395. cursor.close();
  396. db.close();
  397. return bssidList;
  398. }
  399. /**
  400. * Gets all received {@link Record Records} for every attack identified by
  401. * its attack id and ordered by date.
  402. *
  403. * @return A ArrayList with all received {@link Record Records} for each
  404. * attack id in the Database.
  405. */
  406. public ArrayList<Record> getAllReceivedRecordsOfEachAttack() {
  407. ArrayList<Record> recordList = new ArrayList<Record>();
  408. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  409. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN "
  410. + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")" + " WHERE " + KEY_TYPE + "='RECEIVE'"
  411. + " ORDER BY " + TABLE_RECORDS+ "."+ KEY_TIME;
  412. SQLiteDatabase db = this.getReadableDatabase();
  413. Cursor cursor = db.rawQuery(selectQuery, null);
  414. // looping through all rows and adding to list
  415. if (cursor.moveToFirst()) {
  416. do {
  417. Record record = createRecord(cursor);
  418. // Adding record to list
  419. recordList.add(record);
  420. } while (cursor.moveToNext());
  421. }
  422. cursor.close();
  423. // return record list
  424. db.close();
  425. return recordList;
  426. }
  427. /**
  428. * Gets all {@link Record Records} saved in the database.
  429. *
  430. * @return A ArrayList of all the {@link Record Records} in the Database.
  431. */
  432. public ArrayList<Record> getAllRecords() {
  433. ArrayList<Record> recordList = new ArrayList<Record>();
  434. // Select All Query
  435. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  436. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN "
  437. + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")";
  438. SQLiteDatabase db = this.getWritableDatabase();
  439. Cursor cursor = db.rawQuery(selectQuery, null);
  440. Log.i("Database", "Start loop");
  441. // looping through all rows and adding to list
  442. if (cursor.moveToFirst()) {
  443. do {
  444. Log.i("Database", "Add Record");
  445. Record record = createRecord(cursor);
  446. // Adding record to list
  447. recordList.add(record);
  448. } while (cursor.moveToNext());
  449. }
  450. cursor.close();
  451. db.close();
  452. // return record list
  453. return recordList;
  454. }
  455. /**
  456. * Determines the number of different attack_ids in the database.
  457. *
  458. * @return The number of different attack_ids in the database.
  459. */
  460. public int getAttackCount() {
  461. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO;
  462. SQLiteDatabase db = this.getReadableDatabase();
  463. Cursor cursor = db.rawQuery(countQuery, null);
  464. int result = cursor.getCount();
  465. cursor.close();
  466. // return count
  467. db.close();
  468. return result;
  469. }
  470. /**
  471. * Determines the number of different attack_ids for a specific protocol in
  472. * the database.
  473. *
  474. * @param protocol
  475. * The String representation of the
  476. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  477. * Protocol}
  478. * @return The number of different attack_ids in the database.
  479. */
  480. public int getAttackPerProtocolCount(String protocol) {
  481. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO + " WHERE "
  482. + KEY_PROTOCOL + " = " + "'" + protocol + "'";
  483. SQLiteDatabase db = this.getReadableDatabase();
  484. Cursor cursor = db.rawQuery(countQuery, null);
  485. int result = cursor.getCount();
  486. cursor.close();
  487. // return count
  488. db.close();
  489. return result;
  490. }
  491. /**
  492. * Determines the highest attack id stored in the database.
  493. *
  494. * @return The highest attack id stored in the database.
  495. */
  496. public long getHighestAttackId() {
  497. String selectQuery = "SELECT MAX(" + KEY_ATTACK_ID + ") FROM "
  498. + TABLE_ATTACK_INFO;
  499. SQLiteDatabase db = this.getReadableDatabase();
  500. Cursor cursor = db.rawQuery(selectQuery, null);
  501. int result;
  502. if (cursor.moveToFirst()) {
  503. result = cursor.getInt(0);
  504. } else {
  505. result = -1;
  506. }
  507. cursor.close();
  508. db.close();
  509. return result;
  510. }
  511. public ArrayList<HashMap<String, Object>> getNetworkInformation() {
  512. String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
  513. SQLiteDatabase db = this.getReadableDatabase();
  514. Cursor cursor = db.rawQuery(selectQuery, null);
  515. ArrayList<HashMap<String, Object>> networkInformation = new ArrayList<HashMap<String, Object>>();
  516. // looping through all rows and adding to list
  517. if (cursor.moveToFirst()) {
  518. do {
  519. HashMap<String, Object> values = new HashMap<String, Object>();
  520. values.put(KEY_BSSID, cursor.getString(0));
  521. values.put(KEY_SSID, cursor.getString(1));
  522. values.put(KEY_LATITUDE,
  523. Double.parseDouble(cursor.getString(2)));
  524. values.put(KEY_LONGITUDE,
  525. Double.parseDouble(cursor.getString(3)));
  526. values.put(KEY_ACCURACY, Float.parseFloat(cursor.getString(4)));
  527. values.put(KEY_TIME, cursor.getLong(5));
  528. networkInformation.add(values);
  529. } while (cursor.moveToNext());
  530. }
  531. cursor.close();
  532. db.close();
  533. return networkInformation;
  534. }
  535. /**
  536. * Gets a single {@link Record} with the given ID from the database.
  537. *
  538. * @param id
  539. * The ID of the {@link Record};
  540. * @return The {@link Record}.
  541. */
  542. public Record getRecord(int id) {
  543. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  544. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN "
  545. + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")" + " WHERE " + TABLE_RECORDS+"."+KEY_ID + " = " + id;
  546. SQLiteDatabase db = this.getReadableDatabase();
  547. Cursor cursor = db.rawQuery(selectQuery, null);
  548. Record record = null;
  549. if (cursor.moveToFirst()) {
  550. record = createRecord(cursor);
  551. }
  552. cursor.close();
  553. db.close();
  554. // return contact
  555. return record;
  556. }
  557. /**
  558. * Determines the number of {@link Record Records} in the database.
  559. *
  560. * @return The number of {@link Record Records} in the database.
  561. */
  562. public int getRecordCount() {
  563. String countQuery = "SELECT * FROM " + TABLE_RECORDS;
  564. SQLiteDatabase db = this.getReadableDatabase();
  565. Cursor cursor = db.rawQuery(countQuery, null);
  566. int result = cursor.getCount();
  567. cursor.close();
  568. // return count
  569. db.close();
  570. return result;
  571. }
  572. /**
  573. * Gets a single {@link Record} with the given attack id from the database.
  574. *
  575. * @param attack_id
  576. * The attack id of the {@link Record};
  577. * @return The {@link Record}.
  578. */
  579. public Record getRecordOfAttackId(long attack_id) {
  580. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  581. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN "
  582. + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")" + " WHERE " + TABLE_RECORDS+"."+KEY_ATTACK_ID + " = " + attack_id
  583. + " GROUP BY " + TABLE_RECORDS+"."+KEY_ATTACK_ID;
  584. SQLiteDatabase db = this.getReadableDatabase();
  585. Cursor cursor = db.rawQuery(selectQuery, null);
  586. Record record = null;
  587. if (cursor.moveToFirst()) {
  588. record = createRecord(cursor);
  589. }
  590. cursor.close();
  591. // return record list
  592. db.close();
  593. return record;
  594. }
  595. /**
  596. * Gets a representative {@link Record} for every attack identified by its
  597. * attack id.
  598. *
  599. * @return A ArrayList with one {@link Record Records} for each attack id in
  600. * the Database.
  601. */
  602. public ArrayList<Record> getRecordOfEachAttack() {
  603. ArrayList<Record> recordList = new ArrayList<Record>();
  604. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  605. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN "
  606. + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")" + " GROUP BY " + TABLE_RECORDS+ "."+KEY_ATTACK_ID;
  607. SQLiteDatabase db = this.getReadableDatabase();
  608. Cursor cursor = db.rawQuery(selectQuery, null);
  609. // looping through all rows and adding to list
  610. if (cursor.moveToFirst()) {
  611. do {
  612. Record record = createRecord(cursor);
  613. // Adding record to list
  614. recordList.add(record);
  615. } while (cursor.moveToNext());
  616. }
  617. cursor.close();
  618. // return record list
  619. db.close();
  620. return recordList;
  621. }
  622. /**
  623. * Gets a representative {@link Record} for every attack with a higher
  624. * attack id than the specified.
  625. *
  626. * @param attack_id
  627. * The attack id to match the query against.
  628. * @return A ArrayList with one {@link Record Records} for each attack id
  629. * higher than the given.
  630. */
  631. public ArrayList<Record> getRecordOfEachAttack(long attack_id) {
  632. ArrayList<Record> recordList = new ArrayList<Record>();
  633. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  634. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN "
  635. + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")" + " WHERE " + TABLE_RECORDS+"."+KEY_ATTACK_ID + " > " + attack_id
  636. + " GROUP BY " + TABLE_RECORDS+"."+KEY_ATTACK_ID;
  637. SQLiteDatabase db = this.getReadableDatabase();
  638. Cursor cursor = db.rawQuery(selectQuery, null);
  639. // looping through all rows and adding to list
  640. if (cursor.moveToFirst()) {
  641. do {
  642. Record record = createRecord(cursor);
  643. // Adding record to list
  644. recordList.add(record);
  645. } while (cursor.moveToNext());
  646. }
  647. cursor.close();
  648. // return count
  649. db.close();
  650. return recordList;
  651. }
  652. /**
  653. * Determines the smallest attack id stored in the database.
  654. *
  655. * @return The smallest attack id stored in the database.
  656. */
  657. public long getSmallestAttackId() {
  658. String selectQuery = "SELECT MIN(" + KEY_ATTACK_ID + ") FROM "
  659. + TABLE_ATTACK_INFO;
  660. SQLiteDatabase db = this.getReadableDatabase();
  661. Cursor cursor = db.rawQuery(selectQuery, null);
  662. int result;
  663. if (cursor.moveToFirst()) {
  664. result = cursor.getInt(0);
  665. } else {
  666. result = -1;
  667. }
  668. cursor.close();
  669. db.close();
  670. return result;
  671. }
  672. /**
  673. * Gets the last recorded SSID to a given BSSID.
  674. *
  675. * @param bssid
  676. * The BSSID to match against.
  677. * @return A String of the last SSID or null if the BSSID is not in the
  678. * database.
  679. */
  680. public String getSSID(String bssid) {
  681. String selectQuery = "SELECT " + KEY_SSID + " FROM " + TABLE_BSSIDS
  682. + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
  683. SQLiteDatabase db = this.getReadableDatabase();
  684. Cursor cursor = db.rawQuery(selectQuery, null);
  685. String ssid = null;
  686. if (cursor.moveToFirst()) {
  687. ssid = cursor.getString(0);
  688. }
  689. cursor.close();
  690. db.close();
  691. return ssid;
  692. }
  693. // Creating Tables
  694. @Override
  695. public void onCreate(SQLiteDatabase db) {
  696. db.execSQL(CREATE_BSSID_TABLE);
  697. db.execSQL(CREATE_ATTACK_INFO_TABLE);
  698. db.execSQL(CREATE_RECORD_TABLE);
  699. db.execSQL(CREATE_PROFILE_TABLE);
  700. }
  701. // Upgrading database
  702. @Override
  703. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  704. // Drop older table if existed
  705. db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS);
  706. db.execSQL("DROP TABLE IF EXISTS " + TABLE_ATTACK_INFO);
  707. db.execSQL("DROP TABLE IF EXISTS " + TABLE_BSSIDS);
  708. db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROFILES);
  709. // Create tables again
  710. onCreate(db);
  711. }
  712. /**
  713. * Retrieves all the profiles from the database
  714. *
  715. * @return list of profiles
  716. */
  717. public List<Profile> getAllProfiles(){
  718. List<Profile> profiles = new LinkedList<Profile>();
  719. // Select All Query
  720. String selectQuery = "SELECT * FROM " + TABLE_PROFILES;
  721. SQLiteDatabase db = this.getWritableDatabase();
  722. Cursor cursor = db.rawQuery(selectQuery, null);
  723. // looping through all rows and adding to list
  724. if (cursor.moveToFirst()) {
  725. do {
  726. Profile profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(4) == 1);
  727. if(cursor.getInt(5) == 1){
  728. profile.mActivated = true;
  729. }
  730. // Adding record to list
  731. profiles.add(profile);
  732. } while (cursor.moveToNext());
  733. }
  734. cursor.close();
  735. db.close();
  736. // return record list
  737. return profiles;
  738. }
  739. /**
  740. * Persists the given profile into the database
  741. *
  742. * @param profile the profile which should be persisted
  743. *
  744. * @return
  745. */
  746. public long persistProfile(Profile profile){
  747. SQLiteDatabase db = this.getReadableDatabase();
  748. ContentValues values = new ContentValues();
  749. if(profile.mId != -1){
  750. values.put(KEY_PROFILE_ID, profile.mId);
  751. }
  752. values.put(KEY_PROFILE_NAME, profile.mLabel);
  753. values.put(KEY_PROFILE_DESCRIPTION, profile.mText);
  754. values.put(KEY_PROFILE_ICON, profile.mIconPath);
  755. values.put(KEY_PROFILE_ACTIVE, profile.mActivated);
  756. values.put(KEY_PROFILE_EDITABLE, profile.mEditable);
  757. return db.replace(TABLE_PROFILES, null, values);
  758. }
  759. public Profile getProfile(int id) {
  760. String selectQuery = "SELECT * FROM " + TABLE_PROFILES + " WHERE " + TABLE_PROFILES + "." + KEY_PROFILE_ID + " = " + id;
  761. SQLiteDatabase db = this.getReadableDatabase();
  762. Cursor cursor = db.rawQuery(selectQuery, null);
  763. Profile profile = null;
  764. if (cursor.moveToFirst()) {
  765. profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(4) == 1);
  766. if(cursor.getInt(5) == 1){
  767. profile.mActivated = true;
  768. }
  769. }
  770. cursor.close();
  771. db.close();
  772. // return contact
  773. return profile;
  774. }
  775. public void deleteProfile(int id){
  776. SQLiteDatabase db = this.getReadableDatabase();
  777. db.delete(TABLE_PROFILES, KEY_PROFILE_ID + "=?", new String[]{String.valueOf(id)});
  778. }
  779. public void updateNetworkInformation(
  780. ArrayList<HashMap<String, Object>> networkInformation) {
  781. Log.i("DatabaseHandler", "Starte updating");
  782. for (HashMap<String, Object> values : networkInformation) {
  783. updateNetworkInformation(values);
  784. }
  785. }
  786. public void updateNetworkInformation(
  787. HashMap<String, Object> networkInformation) {
  788. SQLiteDatabase db = this.getReadableDatabase();
  789. String bssid = (String) networkInformation.get(KEY_BSSID);
  790. String bssidQuery = "SELECT * FROM " + TABLE_BSSIDS + " WHERE "
  791. + KEY_BSSID + " = " + "'" + bssid + "'";
  792. Cursor cursor = db.rawQuery(bssidQuery, null);
  793. int result = cursor.getCount();
  794. if (cursor != null
  795. && cursor.moveToFirst()
  796. && (result <= 0 || cursor.getLong(5) < (Long) networkInformation
  797. .get(KEY_TIME)))
  798. ;
  799. {
  800. ContentValues bssidValues = new ContentValues();
  801. bssidValues.put(KEY_BSSID, bssid);
  802. bssidValues
  803. .put(KEY_SSID, (String) networkInformation.get(KEY_SSID));
  804. bssidValues.put(KEY_LATITUDE,
  805. (double) (Double) networkInformation.get(KEY_LATITUDE));
  806. bssidValues.put(KEY_LONGITUDE,
  807. (double) (Double) networkInformation.get(KEY_LONGITUDE));
  808. bssidValues.put(KEY_ACCURACY,
  809. (float) (Float) networkInformation.get(KEY_ACCURACY));
  810. bssidValues.put(KEY_TIME, (Long) networkInformation.get(KEY_TIME));
  811. db.insertWithOnConflict(TABLE_BSSIDS, null, bssidValues,
  812. SQLiteDatabase.CONFLICT_REPLACE);
  813. }
  814. cursor.close();
  815. db.close();
  816. }
  817. /**
  818. * Creates a {@link Record} from a Cursor. If the cursor does not show to a
  819. * valid data structure a runtime exception is thrown.
  820. *
  821. * @param cursor
  822. * @return Returns the created {@link Record} .
  823. */
  824. private Record createRecord(Cursor cursor) {
  825. Record record = new Record();
  826. record.setId(Integer.parseInt(cursor.getString(0)));
  827. record.setAttack_id(cursor.getLong(1));
  828. record.setType(TYPE.valueOf(cursor.getString(2)));
  829. record.setTimestamp(cursor.getLong(3));
  830. record.setPacket(cursor.getString(4));
  831. record.setProtocol(cursor.getString(5));
  832. record.setExternalIP(cursor.getString(6));
  833. record.setLocalIP(cursor.getString(7));
  834. record.setLocalHost(cursor.getString(8));
  835. record.setLocalPort(Integer.parseInt(cursor.getString(9)));
  836. record.setRemoteIP(cursor.getString(10));
  837. record.setRemoteHost(cursor.getString(11));
  838. record.setRemotePort(Integer.parseInt(cursor.getString(12)));
  839. record.setBssid(cursor.getString(13));
  840. record.setSsid(cursor.getString(14));
  841. record.setLatitude(Double.parseDouble(cursor.getString(15)));
  842. record.setLongitude(Double.parseDouble(cursor.getString(16)));
  843. record.setAccuracy(Float.parseFloat(cursor.getString(17)));
  844. record.setTimestampLocation(cursor.getLong(18));
  845. return record;
  846. }
  847. }