UglyDbHelper.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 BSSID){
  302. String countQuery = "SELECT * FROM " + TABLE_BSSIDS + " WHERE "
  303. + KEY_BSSID + " = " + "'" + BSSID + "'";
  304. SQLiteDatabase db = this.getReadableDatabase();
  305. Cursor cursor = db.rawQuery(countQuery, null);
  306. int result = cursor.getCount();
  307. cursor.close();
  308. db.close();
  309. return result;
  310. }
  311. public int numBssidSeen(String protocol, String BSSID) {
  312. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO
  313. + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")" + " WHERE " + TABLE_ATTACK_INFO + "." + KEY_PROTOCOL
  314. + " = " + "'" + protocol + "'" + " AND " + TABLE_BSSIDS+ "."+ KEY_BSSID + " = "
  315. + "'" + BSSID + "'";
  316. SQLiteDatabase db = this.getReadableDatabase();
  317. Cursor cursor = db.rawQuery(countQuery, null);
  318. int result = cursor.getCount();
  319. cursor.close();
  320. db.close();
  321. return result;
  322. }
  323. /**
  324. * Determines if an attack has been recorded on a specific protocol in a
  325. * network with a given BSSID.
  326. *
  327. * @param protocol
  328. * The
  329. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  330. * Protocol} to inspect.
  331. * @param BSSID
  332. * The BSSID of the network.
  333. * @return True if an attack on the given protocol has been recorded in a
  334. * network with the given BSSID, else false.
  335. */
  336. public boolean bssidSeen(String protocol, String BSSID) {
  337. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO
  338. + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")" + " WHERE " + TABLE_ATTACK_INFO + "." + KEY_PROTOCOL
  339. + " = " + "'" + protocol + "'" + " AND " + TABLE_BSSIDS+ "."+ KEY_BSSID + " = "
  340. + "'" + BSSID + "'";
  341. SQLiteDatabase db = this.getReadableDatabase();
  342. Cursor cursor = db.rawQuery(countQuery, null);
  343. int result = cursor.getCount();
  344. cursor.close();
  345. db.close();
  346. return result > 0;
  347. }
  348. /**
  349. * Deletes all records from {@link #TABLE_RECORDS}.
  350. */
  351. public void clearData() {
  352. SQLiteDatabase db = this.getReadableDatabase();
  353. db.delete(TABLE_RECORDS, null, null);
  354. db.delete(TABLE_ATTACK_INFO, null, null);
  355. db.delete(TABLE_PROFILES, null, null);
  356. db.close();
  357. }
  358. /**
  359. * Deletes all records from {@link #TABLE_RECORDS} with a specific BSSID.
  360. *
  361. * @param bssid
  362. * The BSSID to match against.
  363. */
  364. public void deleteByBSSID(String bssid) {
  365. SQLiteDatabase db = this.getReadableDatabase();
  366. db.delete(TABLE_RECORDS, KEY_BSSID + " = ?", new String[] { bssid });
  367. db.delete(TABLE_ATTACK_INFO, KEY_BSSID + " = ?", new String[] { bssid });
  368. db.close();
  369. }
  370. // TODO Delete statement �berarbeiten
  371. /**
  372. * Deletes all records from {@link #TABLE_RECORDS} with a time stamp smaller
  373. * then the given
  374. *
  375. * @param date
  376. * A Date represented in milliseconds.
  377. */
  378. public void deleteByDate(long date) {
  379. SQLiteDatabase db = this.getReadableDatabase();
  380. String deleteQuery = "DELETE FROM " + TABLE_RECORDS + " WHERE "
  381. + KEY_TIME + " < " + date;
  382. // TODO Delete statement �berarbeiten
  383. // String deleteQuery2 = "DELETE "
  384. db.execSQL(deleteQuery);
  385. db.close();
  386. }
  387. /**
  388. * Returns a String array with all BSSIDs stored in the database.
  389. *
  390. * @return String[] of all recorded BSSIDs.
  391. */
  392. public String[] getAllBSSIDS() {
  393. String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
  394. SQLiteDatabase db = this.getReadableDatabase();
  395. Cursor cursor = db.rawQuery(selectQuery, null);
  396. String[] bssidList = new String[cursor.getCount()];
  397. int counter = 0;
  398. // looping through all rows and adding to list
  399. if (cursor.moveToFirst()) {
  400. do {
  401. bssidList[counter] = cursor.getString(0);
  402. counter++;
  403. } while (cursor.moveToNext());
  404. }
  405. cursor.close();
  406. db.close();
  407. return bssidList;
  408. }
  409. /**
  410. * Gets all received {@link Record Records} for every attack identified by
  411. * its attack id and ordered by date.
  412. *
  413. * @return A ArrayList with all received {@link Record Records} for each
  414. * attack id in the Database.
  415. */
  416. public ArrayList<Record> getAllReceivedRecordsOfEachAttack() {
  417. ArrayList<Record> recordList = new ArrayList<Record>();
  418. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  419. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN "
  420. + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")" + " WHERE " + KEY_TYPE + "='RECEIVE'"
  421. + " ORDER BY " + TABLE_RECORDS+ "."+ KEY_TIME;
  422. SQLiteDatabase db = this.getReadableDatabase();
  423. Cursor cursor = db.rawQuery(selectQuery, null);
  424. // looping through all rows and adding to list
  425. if (cursor.moveToFirst()) {
  426. do {
  427. Record record = createRecord(cursor);
  428. // Adding record to list
  429. recordList.add(record);
  430. } while (cursor.moveToNext());
  431. }
  432. cursor.close();
  433. // return record list
  434. db.close();
  435. return recordList;
  436. }
  437. /**
  438. * Gets all {@link Record Records} saved in the database.
  439. *
  440. * @return A ArrayList of all the {@link Record Records} in the Database.
  441. */
  442. public ArrayList<Record> getAllRecords() {
  443. ArrayList<Record> recordList = new ArrayList<Record>();
  444. // Select All Query
  445. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  446. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN "
  447. + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")";
  448. SQLiteDatabase db = this.getWritableDatabase();
  449. Cursor cursor = db.rawQuery(selectQuery, null);
  450. Log.i("Database", "Start loop");
  451. // looping through all rows and adding to list
  452. if (cursor.moveToFirst()) {
  453. do {
  454. Log.i("Database", "Add Record");
  455. Record record = createRecord(cursor);
  456. // Adding record to list
  457. recordList.add(record);
  458. } while (cursor.moveToNext());
  459. }
  460. cursor.close();
  461. db.close();
  462. // return record list
  463. return recordList;
  464. }
  465. /**
  466. * Determines the number of different attack_ids in the database.
  467. *
  468. * @return The number of different attack_ids in the database.
  469. */
  470. public int getAttackCount() {
  471. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO;
  472. SQLiteDatabase db = this.getReadableDatabase();
  473. Cursor cursor = db.rawQuery(countQuery, null);
  474. int result = cursor.getCount();
  475. cursor.close();
  476. // return count
  477. db.close();
  478. return result;
  479. }
  480. /**
  481. * Determines the number of different attack_ids for a specific protocol in
  482. * the database.
  483. *
  484. * @param protocol
  485. * The String representation of the
  486. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  487. * Protocol}
  488. * @return The number of different attack_ids in the database.
  489. */
  490. public int getAttackPerProtocolCount(String protocol) {
  491. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO + " WHERE "
  492. + KEY_PROTOCOL + " = " + "'" + protocol + "'";
  493. SQLiteDatabase db = this.getReadableDatabase();
  494. Cursor cursor = db.rawQuery(countQuery, null);
  495. int result = cursor.getCount();
  496. cursor.close();
  497. // return count
  498. db.close();
  499. return result;
  500. }
  501. /**
  502. * Determines the highest attack id stored in the database.
  503. *
  504. * @return The highest attack id stored in the database.
  505. */
  506. public long getHighestAttackId() {
  507. String selectQuery = "SELECT MAX(" + KEY_ATTACK_ID + ") FROM "
  508. + TABLE_ATTACK_INFO;
  509. SQLiteDatabase db = this.getReadableDatabase();
  510. Cursor cursor = db.rawQuery(selectQuery, null);
  511. int result;
  512. if (cursor.moveToFirst()) {
  513. result = cursor.getInt(0);
  514. } else {
  515. result = -1;
  516. }
  517. cursor.close();
  518. db.close();
  519. return result;
  520. }
  521. public ArrayList<HashMap<String, Object>> getNetworkInformation() {
  522. String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
  523. SQLiteDatabase db = this.getReadableDatabase();
  524. Cursor cursor = db.rawQuery(selectQuery, null);
  525. ArrayList<HashMap<String, Object>> networkInformation = new ArrayList<HashMap<String, Object>>();
  526. // looping through all rows and adding to list
  527. if (cursor.moveToFirst()) {
  528. do {
  529. HashMap<String, Object> values = new HashMap<String, Object>();
  530. values.put(KEY_BSSID, cursor.getString(0));
  531. values.put(KEY_SSID, cursor.getString(1));
  532. values.put(KEY_LATITUDE,
  533. Double.parseDouble(cursor.getString(2)));
  534. values.put(KEY_LONGITUDE,
  535. Double.parseDouble(cursor.getString(3)));
  536. values.put(KEY_ACCURACY, Float.parseFloat(cursor.getString(4)));
  537. values.put(KEY_TIME, cursor.getLong(5));
  538. networkInformation.add(values);
  539. } while (cursor.moveToNext());
  540. }
  541. cursor.close();
  542. db.close();
  543. return networkInformation;
  544. }
  545. /**
  546. * Gets a single {@link Record} with the given ID from the database.
  547. *
  548. * @param id
  549. * The ID of the {@link Record};
  550. * @return The {@link Record}.
  551. */
  552. public Record getRecord(int id) {
  553. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  554. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN "
  555. + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")" + " WHERE " + TABLE_RECORDS+"."+KEY_ID + " = " + id;
  556. SQLiteDatabase db = this.getReadableDatabase();
  557. Cursor cursor = db.rawQuery(selectQuery, null);
  558. Record record = null;
  559. if (cursor.moveToFirst()) {
  560. record = createRecord(cursor);
  561. }
  562. cursor.close();
  563. db.close();
  564. // return contact
  565. return record;
  566. }
  567. /**
  568. * Determines the number of {@link Record Records} in the database.
  569. *
  570. * @return The number of {@link Record Records} in the database.
  571. */
  572. public int getRecordCount() {
  573. String countQuery = "SELECT * FROM " + TABLE_RECORDS;
  574. SQLiteDatabase db = this.getReadableDatabase();
  575. Cursor cursor = db.rawQuery(countQuery, null);
  576. int result = cursor.getCount();
  577. cursor.close();
  578. // return count
  579. db.close();
  580. return result;
  581. }
  582. /**
  583. * Gets a single {@link Record} with the given attack id from the database.
  584. *
  585. * @param attack_id
  586. * The attack id of the {@link Record};
  587. * @return The {@link Record}.
  588. */
  589. public Record getRecordOfAttackId(long attack_id) {
  590. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  591. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN "
  592. + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")" + " WHERE " + TABLE_RECORDS+"."+KEY_ATTACK_ID + " = " + attack_id
  593. + " GROUP BY " + TABLE_RECORDS+"."+KEY_ATTACK_ID;
  594. SQLiteDatabase db = this.getReadableDatabase();
  595. Cursor cursor = db.rawQuery(selectQuery, null);
  596. Record record = null;
  597. if (cursor.moveToFirst()) {
  598. record = createRecord(cursor);
  599. }
  600. cursor.close();
  601. // return record list
  602. db.close();
  603. return record;
  604. }
  605. /**
  606. * Gets a representative {@link Record} for every attack identified by its
  607. * attack id.
  608. *
  609. * @return A ArrayList with one {@link Record Records} for each attack id in
  610. * the Database.
  611. */
  612. public ArrayList<Record> getRecordOfEachAttack() {
  613. ArrayList<Record> recordList = new ArrayList<Record>();
  614. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  615. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN "
  616. + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")" + " GROUP BY " + TABLE_RECORDS+ "."+KEY_ATTACK_ID;
  617. SQLiteDatabase db = this.getReadableDatabase();
  618. Cursor cursor = db.rawQuery(selectQuery, null);
  619. // looping through all rows and adding to list
  620. if (cursor.moveToFirst()) {
  621. do {
  622. Record record = createRecord(cursor);
  623. // Adding record to list
  624. recordList.add(record);
  625. } while (cursor.moveToNext());
  626. }
  627. cursor.close();
  628. // return record list
  629. db.close();
  630. return recordList;
  631. }
  632. /**
  633. * Gets a representative {@link Record} for every attack with a higher
  634. * attack id than the specified.
  635. *
  636. * @param attack_id
  637. * The attack id to match the query against.
  638. * @return A ArrayList with one {@link Record Records} for each attack id
  639. * higher than the given.
  640. */
  641. public ArrayList<Record> getRecordOfEachAttack(long attack_id) {
  642. ArrayList<Record> recordList = new ArrayList<Record>();
  643. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  644. + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN "
  645. + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID + ")" + " WHERE " + TABLE_RECORDS+"."+KEY_ATTACK_ID + " > " + attack_id
  646. + " GROUP BY " + TABLE_RECORDS+"."+KEY_ATTACK_ID;
  647. SQLiteDatabase db = this.getReadableDatabase();
  648. Cursor cursor = db.rawQuery(selectQuery, null);
  649. // looping through all rows and adding to list
  650. if (cursor.moveToFirst()) {
  651. do {
  652. Record record = createRecord(cursor);
  653. // Adding record to list
  654. recordList.add(record);
  655. } while (cursor.moveToNext());
  656. }
  657. cursor.close();
  658. // return count
  659. db.close();
  660. return recordList;
  661. }
  662. /**
  663. * Determines the smallest attack id stored in the database.
  664. *
  665. * @return The smallest attack id stored in the database.
  666. */
  667. public long getSmallestAttackId() {
  668. String selectQuery = "SELECT MIN(" + KEY_ATTACK_ID + ") FROM "
  669. + TABLE_ATTACK_INFO;
  670. SQLiteDatabase db = this.getReadableDatabase();
  671. Cursor cursor = db.rawQuery(selectQuery, null);
  672. int result;
  673. if (cursor.moveToFirst()) {
  674. result = cursor.getInt(0);
  675. } else {
  676. result = -1;
  677. }
  678. cursor.close();
  679. db.close();
  680. return result;
  681. }
  682. /**
  683. * Gets the last recorded SSID to a given BSSID.
  684. *
  685. * @param bssid
  686. * The BSSID to match against.
  687. * @return A String of the last SSID or null if the BSSID is not in the
  688. * database.
  689. */
  690. public String getSSID(String bssid) {
  691. String selectQuery = "SELECT " + KEY_SSID + " FROM " + TABLE_BSSIDS
  692. + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
  693. SQLiteDatabase db = this.getReadableDatabase();
  694. Cursor cursor = db.rawQuery(selectQuery, null);
  695. String ssid = null;
  696. if (cursor.moveToFirst()) {
  697. ssid = cursor.getString(0);
  698. }
  699. cursor.close();
  700. db.close();
  701. return ssid;
  702. }
  703. // Creating Tables
  704. @Override
  705. public void onCreate(SQLiteDatabase db) {
  706. db.execSQL(CREATE_BSSID_TABLE);
  707. db.execSQL(CREATE_ATTACK_INFO_TABLE);
  708. db.execSQL(CREATE_RECORD_TABLE);
  709. db.execSQL(CREATE_PROFILE_TABLE);
  710. }
  711. // Upgrading database
  712. @Override
  713. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  714. // Drop older table if existed
  715. db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS);
  716. db.execSQL("DROP TABLE IF EXISTS " + TABLE_ATTACK_INFO);
  717. db.execSQL("DROP TABLE IF EXISTS " + TABLE_BSSIDS);
  718. db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROFILES);
  719. // Create tables again
  720. onCreate(db);
  721. }
  722. /**
  723. * Retrieves all the profiles from the database
  724. *
  725. * @return list of profiles
  726. */
  727. public List<Profile> getAllProfiles(){
  728. List<Profile> profiles = new LinkedList<Profile>();
  729. // Select All Query
  730. String selectQuery = "SELECT * FROM " + TABLE_PROFILES;
  731. SQLiteDatabase db = this.getWritableDatabase();
  732. Cursor cursor = db.rawQuery(selectQuery, null);
  733. // looping through all rows and adding to list
  734. if (cursor.moveToFirst()) {
  735. do {
  736. Profile profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(4) == 1);
  737. if(cursor.getInt(5) == 1){
  738. profile.mActivated = true;
  739. }
  740. // Adding record to list
  741. profiles.add(profile);
  742. } while (cursor.moveToNext());
  743. }
  744. cursor.close();
  745. db.close();
  746. // return record list
  747. return profiles;
  748. }
  749. /**
  750. * Persists the given profile into the database
  751. *
  752. * @param profile the profile which should be persisted
  753. *
  754. * @return
  755. */
  756. public long persistProfile(Profile profile){
  757. SQLiteDatabase db = this.getReadableDatabase();
  758. ContentValues values = new ContentValues();
  759. if(profile.mId != -1){
  760. values.put(KEY_PROFILE_ID, profile.mId);
  761. }
  762. values.put(KEY_PROFILE_NAME, profile.mLabel);
  763. values.put(KEY_PROFILE_DESCRIPTION, profile.mText);
  764. values.put(KEY_PROFILE_ICON, profile.mIconPath);
  765. values.put(KEY_PROFILE_ACTIVE, profile.mActivated);
  766. values.put(KEY_PROFILE_EDITABLE, profile.mEditable);
  767. return db.replace(TABLE_PROFILES, null, values);
  768. }
  769. public Profile getProfile(int id) {
  770. String selectQuery = "SELECT * FROM " + TABLE_PROFILES + " WHERE " + TABLE_PROFILES + "." + KEY_PROFILE_ID + " = " + id;
  771. SQLiteDatabase db = this.getReadableDatabase();
  772. Cursor cursor = db.rawQuery(selectQuery, null);
  773. Profile profile = null;
  774. if (cursor.moveToFirst()) {
  775. profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(4) == 1);
  776. if(cursor.getInt(5) == 1){
  777. profile.mActivated = true;
  778. }
  779. }
  780. cursor.close();
  781. db.close();
  782. // return contact
  783. return profile;
  784. }
  785. public void deleteProfile(int id){
  786. SQLiteDatabase db = this.getReadableDatabase();
  787. db.delete(TABLE_PROFILES, KEY_PROFILE_ID + "=?", new String[]{String.valueOf(id)});
  788. }
  789. public void updateNetworkInformation(
  790. ArrayList<HashMap<String, Object>> networkInformation) {
  791. Log.i("DatabaseHandler", "Starte updating");
  792. for (HashMap<String, Object> values : networkInformation) {
  793. updateNetworkInformation(values);
  794. }
  795. }
  796. public void updateNetworkInformation(
  797. HashMap<String, Object> networkInformation) {
  798. SQLiteDatabase db = this.getReadableDatabase();
  799. String bssid = (String) networkInformation.get(KEY_BSSID);
  800. String bssidQuery = "SELECT * FROM " + TABLE_BSSIDS + " WHERE "
  801. + KEY_BSSID + " = " + "'" + bssid + "'";
  802. Cursor cursor = db.rawQuery(bssidQuery, null);
  803. int result = cursor.getCount();
  804. if (cursor != null
  805. && cursor.moveToFirst()
  806. && (result <= 0 || cursor.getLong(5) < (Long) networkInformation
  807. .get(KEY_TIME)))
  808. ;
  809. {
  810. ContentValues bssidValues = new ContentValues();
  811. bssidValues.put(KEY_BSSID, bssid);
  812. bssidValues
  813. .put(KEY_SSID, (String) networkInformation.get(KEY_SSID));
  814. bssidValues.put(KEY_LATITUDE,
  815. (double) (Double) networkInformation.get(KEY_LATITUDE));
  816. bssidValues.put(KEY_LONGITUDE,
  817. (double) (Double) networkInformation.get(KEY_LONGITUDE));
  818. bssidValues.put(KEY_ACCURACY,
  819. (float) (Float) networkInformation.get(KEY_ACCURACY));
  820. bssidValues.put(KEY_TIME, (Long) networkInformation.get(KEY_TIME));
  821. db.insertWithOnConflict(TABLE_BSSIDS, null, bssidValues,
  822. SQLiteDatabase.CONFLICT_REPLACE);
  823. }
  824. cursor.close();
  825. db.close();
  826. }
  827. /**
  828. * Creates a {@link Record} from a Cursor. If the cursor does not show to a
  829. * valid data structure a runtime exception is thrown.
  830. *
  831. * @param cursor
  832. * @return Returns the created {@link Record} .
  833. */
  834. private Record createRecord(Cursor cursor) {
  835. Record record = new Record();
  836. record.setId(Integer.parseInt(cursor.getString(0)));
  837. record.setAttack_id(cursor.getLong(1));
  838. record.setType(TYPE.valueOf(cursor.getString(2)));
  839. record.setTimestamp(cursor.getLong(3));
  840. record.setPacket(cursor.getString(4));
  841. record.setProtocol(cursor.getString(5));
  842. record.setExternalIP(cursor.getString(6));
  843. record.setLocalIP(cursor.getString(7));
  844. record.setLocalHost(cursor.getString(8));
  845. record.setLocalPort(Integer.parseInt(cursor.getString(9)));
  846. record.setRemoteIP(cursor.getString(10));
  847. record.setRemoteHost(cursor.getString(11));
  848. record.setRemotePort(Integer.parseInt(cursor.getString(12)));
  849. record.setBssid(cursor.getString(13));
  850. record.setSsid(cursor.getString(14));
  851. record.setLatitude(Double.parseDouble(cursor.getString(15)));
  852. record.setLongitude(Double.parseDouble(cursor.getString(16)));
  853. record.setAccuracy(Float.parseFloat(cursor.getString(17)));
  854. record.setTimestampLocation(cursor.getLong(18));
  855. return record;
  856. }
  857. }