UglyDbHelper.java 34 KB

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