UglyDbHelper.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. package de.tudarmstadt.informatik.hostage.deprecated;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import android.content.ContentValues;
  7. import android.content.Context;
  8. import android.database.Cursor;
  9. import android.database.sqlite.SQLiteDatabase;
  10. import android.database.sqlite.SQLiteOpenHelper;
  11. import android.util.Log;
  12. import de.tudarmstadt.informatik.hostage.logging.Record;
  13. import de.tudarmstadt.informatik.hostage.logging.MessageRecord.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_TIME, 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. int result = cursor.getCount();
  463. cursor.close();
  464. // return count
  465. db.close();
  466. return result;
  467. }
  468. /**
  469. * Determines the number of different attack_ids for a specific protocol in
  470. * the database.
  471. *
  472. * @param protocol
  473. * The String representation of the
  474. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  475. * Protocol}
  476. * @return The number of different attack_ids in the database.
  477. */
  478. public int getAttackPerProtocolCount(String protocol) {
  479. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO + " WHERE " + KEY_PROTOCOL + " = " + "'" + protocol + "'";
  480. SQLiteDatabase db = this.getReadableDatabase();
  481. Cursor cursor = db.rawQuery(countQuery, null);
  482. int result = cursor.getCount();
  483. cursor.close();
  484. // return count
  485. db.close();
  486. return result;
  487. }
  488. /**
  489. * Determines the highest attack id stored in the database.
  490. *
  491. * @return The highest attack id stored in the database.
  492. */
  493. public long getHighestAttackId() {
  494. String selectQuery = "SELECT MAX(" + KEY_ATTACK_ID + ") FROM " + TABLE_ATTACK_INFO;
  495. SQLiteDatabase db = this.getReadableDatabase();
  496. Cursor cursor = db.rawQuery(selectQuery, null);
  497. int result;
  498. if (cursor.moveToFirst()) {
  499. result = cursor.getInt(0);
  500. } else {
  501. result = -1;
  502. }
  503. cursor.close();
  504. db.close();
  505. return result;
  506. }
  507. public ArrayList<HashMap<String, Object>> getNetworkInformation() {
  508. String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
  509. SQLiteDatabase db = this.getReadableDatabase();
  510. Cursor cursor = db.rawQuery(selectQuery, null);
  511. ArrayList<HashMap<String, Object>> networkInformation = new ArrayList<HashMap<String, Object>>();
  512. // looping through all rows and adding to list
  513. if (cursor.moveToFirst()) {
  514. do {
  515. HashMap<String, Object> values = new HashMap<String, Object>();
  516. values.put(KEY_BSSID, cursor.getString(0));
  517. values.put(KEY_SSID, cursor.getString(1));
  518. values.put(KEY_LATITUDE, Double.parseDouble(cursor.getString(2)));
  519. values.put(KEY_LONGITUDE, Double.parseDouble(cursor.getString(3)));
  520. values.put(KEY_ACCURACY, Float.parseFloat(cursor.getString(4)));
  521. values.put(KEY_TIME, cursor.getLong(5));
  522. networkInformation.add(values);
  523. } while (cursor.moveToNext());
  524. }
  525. cursor.close();
  526. db.close();
  527. return networkInformation;
  528. }
  529. /**
  530. * Gets a single {@link Record} with the given ID from the database.
  531. *
  532. * @param id
  533. * The ID of the {@link Record};
  534. * @return The {@link Record}.
  535. */
  536. public Record getRecord(int id) {
  537. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID
  538. + ")" + " WHERE " + TABLE_RECORDS + "." + KEY_ID + " = " + id;
  539. SQLiteDatabase db = this.getReadableDatabase();
  540. Cursor cursor = db.rawQuery(selectQuery, null);
  541. Record record = null;
  542. if (cursor.moveToFirst()) {
  543. record = createRecord(cursor);
  544. }
  545. cursor.close();
  546. db.close();
  547. // return contact
  548. return record;
  549. }
  550. /**
  551. * Determines the number of {@link Record Records} in the database.
  552. *
  553. * @return The number of {@link Record Records} in the database.
  554. */
  555. public int getRecordCount() {
  556. String countQuery = "SELECT * FROM " + TABLE_RECORDS;
  557. SQLiteDatabase db = this.getReadableDatabase();
  558. Cursor cursor = db.rawQuery(countQuery, null);
  559. int result = cursor.getCount();
  560. cursor.close();
  561. // return count
  562. db.close();
  563. return result;
  564. }
  565. /**
  566. * Gets a single {@link Record} with the given attack id from the database.
  567. *
  568. * @param attack_id
  569. * The attack id of the {@link Record};
  570. * @return The {@link Record}.
  571. */
  572. public Record getRecordOfAttackId(long attack_id) {
  573. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID
  574. + ")" + " WHERE " + TABLE_RECORDS + "." + KEY_ATTACK_ID + " = " + attack_id + " GROUP BY " + TABLE_RECORDS + "." + KEY_ATTACK_ID;
  575. SQLiteDatabase db = this.getReadableDatabase();
  576. Cursor cursor = db.rawQuery(selectQuery, null);
  577. Record record = null;
  578. if (cursor.moveToFirst()) {
  579. record = createRecord(cursor);
  580. }
  581. cursor.close();
  582. // return record list
  583. db.close();
  584. return record;
  585. }
  586. /**
  587. * Gets a representative {@link Record} for every attack identified by its
  588. * attack id.
  589. *
  590. * @return A ArrayList with one {@link Record Records} for each attack id in
  591. * the Database.
  592. */
  593. public ArrayList<Record> getRecordOfEachAttack() {
  594. ArrayList<Record> recordList = new ArrayList<Record>();
  595. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID
  596. + ")" + " GROUP BY " + TABLE_RECORDS + "." + KEY_ATTACK_ID;
  597. SQLiteDatabase db = this.getReadableDatabase();
  598. Cursor cursor = db.rawQuery(selectQuery, null);
  599. // looping through all rows and adding to list
  600. if (cursor.moveToFirst()) {
  601. do {
  602. Record record = createRecord(cursor);
  603. // Adding record to list
  604. recordList.add(record);
  605. } while (cursor.moveToNext());
  606. }
  607. cursor.close();
  608. // return record list
  609. db.close();
  610. return recordList;
  611. }
  612. /*
  613. * Returns the Conversation of a specific attack id
  614. *
  615. * @param attack_id Tha attack id to match the query against.
  616. *
  617. * @return A arraylist with all {@link Record Records}s for an attack id.
  618. */
  619. public ArrayList<Record> getConversationForAttackID(long attack_id) {
  620. ArrayList<Record> recordList = new ArrayList<Record>();
  621. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID
  622. + ")" + " WHERE " + TABLE_RECORDS + "." + KEY_ATTACK_ID + " = " + attack_id;
  623. SQLiteDatabase db = this.getReadableDatabase();
  624. Cursor cursor = db.rawQuery(selectQuery, null);
  625. if (cursor.moveToFirst()) {
  626. do {
  627. Record record = createRecord(cursor);
  628. recordList.add(record);
  629. } while (cursor.moveToNext());
  630. }
  631. cursor.close();
  632. db.close();
  633. return recordList;
  634. }
  635. /**
  636. * Gets a representative {@link Record} for every attack with a higher
  637. * attack id than the specified.
  638. *
  639. * @param attack_id
  640. * The attack id to match the query against.
  641. * @return A ArrayList with one {@link Record Records} for each attack id
  642. * higher than the given.
  643. */
  644. public ArrayList<Record> getRecordOfEachAttack(long attack_id) {
  645. ArrayList<Record> recordList = new ArrayList<Record>();
  646. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID
  647. + ")" + " WHERE " + TABLE_RECORDS + "." + KEY_ATTACK_ID + " > " + attack_id + " GROUP BY " + TABLE_RECORDS + "." + KEY_ATTACK_ID;
  648. SQLiteDatabase db = this.getReadableDatabase();
  649. Cursor cursor = db.rawQuery(selectQuery, null);
  650. // looping through all rows and adding to list
  651. if (cursor.moveToFirst()) {
  652. do {
  653. Record record = createRecord(cursor);
  654. // Adding record to list
  655. recordList.add(record);
  656. } while (cursor.moveToNext());
  657. }
  658. cursor.close();
  659. // return count
  660. db.close();
  661. return recordList;
  662. }
  663. /**
  664. * Determines the smallest attack id stored in the database.
  665. *
  666. * @return The smallest attack id stored in the database.
  667. */
  668. public long getSmallestAttackId() {
  669. String selectQuery = "SELECT MIN(" + KEY_ATTACK_ID + ") FROM " + 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 + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
  692. SQLiteDatabase db = this.getReadableDatabase();
  693. Cursor cursor = db.rawQuery(selectQuery, null);
  694. String ssid = null;
  695. if (cursor.moveToFirst()) {
  696. ssid = cursor.getString(0);
  697. }
  698. cursor.close();
  699. db.close();
  700. return ssid;
  701. }
  702. // Creating Tables
  703. @Override
  704. public void onCreate(SQLiteDatabase db) {
  705. db.execSQL(CREATE_BSSID_TABLE);
  706. db.execSQL(CREATE_ATTACK_INFO_TABLE);
  707. db.execSQL(CREATE_RECORD_TABLE);
  708. db.execSQL(CREATE_PROFILE_TABLE);
  709. }
  710. // Upgrading database
  711. @Override
  712. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  713. // Drop older table if existed
  714. db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS);
  715. db.execSQL("DROP TABLE IF EXISTS " + TABLE_ATTACK_INFO);
  716. db.execSQL("DROP TABLE IF EXISTS " + TABLE_BSSIDS);
  717. db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROFILES);
  718. // Create tables again
  719. onCreate(db);
  720. }
  721. /**
  722. * Retrieves all the profiles from the database
  723. *
  724. * @return list of profiles
  725. */
  726. public List<Profile> getAllProfiles() {
  727. List<Profile> profiles = new LinkedList<Profile>();
  728. // Select All Query
  729. String selectQuery = "SELECT * FROM " + TABLE_PROFILES;
  730. SQLiteDatabase db = this.getWritableDatabase();
  731. Cursor cursor = db.rawQuery(selectQuery, null);
  732. // looping through all rows and adding to list
  733. if (cursor.moveToFirst()) {
  734. do {
  735. Profile profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
  736. if (cursor.getInt(6) == 1) {
  737. profile.mActivated = true;
  738. }
  739. profile.mIconName = cursor.getString(4);
  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
  753. * the profile which should be persisted
  754. *
  755. * @return
  756. */
  757. public long persistProfile(Profile profile) {
  758. SQLiteDatabase db = this.getReadableDatabase();
  759. ContentValues values = new ContentValues();
  760. if (profile.mId != -1) {
  761. values.put(KEY_PROFILE_ID, profile.mId);
  762. }
  763. values.put(KEY_PROFILE_NAME, profile.mLabel);
  764. values.put(KEY_PROFILE_DESCRIPTION, profile.mText);
  765. values.put(KEY_PROFILE_ICON, profile.mIconPath);
  766. values.put(KEY_PROFILE_ICON_NAME, profile.mIconName);
  767. values.put(KEY_PROFILE_ACTIVE, profile.mActivated);
  768. values.put(KEY_PROFILE_EDITABLE, profile.mEditable);
  769. return db.replace(TABLE_PROFILES, null, values);
  770. }
  771. /**
  772. * private static final String CREATE_PROFILE_TABLE = "CREATE TABLE " +
  773. * TABLE_PROFILES + "(" + KEY_PROFILE_ID +
  774. * " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_PROFILE_NAME + " TEXT," +
  775. * KEY_PROFILE_DESCRIPTION + " TEXT," + KEY_PROFILE_ICON + " TEXT," +
  776. * KEY_PROFILE_ICON_ID + " INTEGER," + KEY_PROFILE_EDITABLE + " INTEGER," +
  777. * KEY_PROFILE_ACTIVE + " INTEGER" + ")";
  778. */
  779. public Profile getProfile(int id) {
  780. String selectQuery = "SELECT * FROM " + TABLE_PROFILES + " WHERE " + TABLE_PROFILES + "." + KEY_PROFILE_ID + " = " + id;
  781. SQLiteDatabase db = this.getReadableDatabase();
  782. Cursor cursor = db.rawQuery(selectQuery, null);
  783. Profile profile = null;
  784. if (cursor.moveToFirst()) {
  785. profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
  786. if (cursor.getInt(6) == 1) {
  787. profile.mActivated = true;
  788. }
  789. profile.mIconName = cursor.getString(5);
  790. }
  791. cursor.close();
  792. db.close();
  793. // return contact
  794. return profile;
  795. }
  796. public void deleteProfile(int id) {
  797. SQLiteDatabase db = this.getReadableDatabase();
  798. db.delete(TABLE_PROFILES, KEY_PROFILE_ID + "=?", new String[] { String.valueOf(id) });
  799. }
  800. public void updateNetworkInformation(ArrayList<HashMap<String, Object>> networkInformation) {
  801. Log.i("DatabaseHandler", "Starte updating");
  802. for (HashMap<String, Object> values : networkInformation) {
  803. updateNetworkInformation(values);
  804. }
  805. }
  806. public void updateNetworkInformation(HashMap<String, Object> networkInformation) {
  807. SQLiteDatabase db = this.getReadableDatabase();
  808. String bssid = (String) networkInformation.get(KEY_BSSID);
  809. String bssidQuery = "SELECT * FROM " + TABLE_BSSIDS + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
  810. Cursor cursor = db.rawQuery(bssidQuery, null);
  811. int result = cursor.getCount();
  812. if (cursor != null && cursor.moveToFirst() && (result <= 0 || cursor.getLong(5) < (Long) networkInformation.get(KEY_TIME)))
  813. ;
  814. {
  815. ContentValues bssidValues = new ContentValues();
  816. bssidValues.put(KEY_BSSID, bssid);
  817. bssidValues.put(KEY_SSID, (String) networkInformation.get(KEY_SSID));
  818. bssidValues.put(KEY_LATITUDE, (double) (Double) networkInformation.get(KEY_LATITUDE));
  819. bssidValues.put(KEY_LONGITUDE, (double) (Double) networkInformation.get(KEY_LONGITUDE));
  820. bssidValues.put(KEY_ACCURACY, (float) (Float) networkInformation.get(KEY_ACCURACY));
  821. bssidValues.put(KEY_TIME, (Long) networkInformation.get(KEY_TIME));
  822. db.insertWithOnConflict(TABLE_BSSIDS, null, bssidValues, 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.setLocalPort(Integer.parseInt(cursor.getString(8)));
  845. record.setRemoteIP(cursor.getString(9));
  846. record.setRemotePort(Integer.parseInt(cursor.getString(10)));
  847. record.setBssid(cursor.getString(11));
  848. record.setSsid(cursor.getString(12));
  849. record.setLatitude(Double.parseDouble(cursor.getString(13)));
  850. record.setLongitude(Double.parseDouble(cursor.getString(14)));
  851. record.setAccuracy(Float.parseFloat(cursor.getString(15)));
  852. record.setTimestampLocation(cursor.getLong(16));
  853. return record;
  854. }
  855. }