UglyDbHelper.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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.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_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. * Deletes all records from {@link #TABLE_RECORDS} with a specific Attack ID.
  380. *
  381. * @param attackID
  382. * The Attack ID to match against.
  383. */
  384. public void deleteByAttackID(long attackID) {
  385. SQLiteDatabase db = this.getReadableDatabase();
  386. db.delete(TABLE_RECORDS, KEY_ATTACK_ID + " = ?", new String[] { String.valueOf(attackID) });
  387. db.delete(TABLE_ATTACK_INFO, KEY_ATTACK_ID + " = ?", new String[] { String.valueOf(attackID) });
  388. db.close();
  389. }
  390. /**
  391. * Returns a String array with all BSSIDs stored in the database.
  392. *
  393. * @return String[] of all recorded BSSIDs.
  394. */
  395. public String[] getAllBSSIDS() {
  396. String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
  397. SQLiteDatabase db = this.getReadableDatabase();
  398. Cursor cursor = db.rawQuery(selectQuery, null);
  399. String[] bssidList = new String[cursor.getCount()];
  400. int counter = 0;
  401. // looping through all rows and adding to list
  402. if (cursor.moveToFirst()) {
  403. do {
  404. bssidList[counter] = cursor.getString(0);
  405. counter++;
  406. } while (cursor.moveToNext());
  407. }
  408. cursor.close();
  409. db.close();
  410. return bssidList;
  411. }
  412. /**
  413. * Gets all received {@link Record Records} for every attack identified by
  414. * its attack id and ordered by date.
  415. *
  416. * @return A ArrayList with all received {@link Record Records} for each
  417. * attack id in the Database.
  418. */
  419. public ArrayList<Record> getAllReceivedRecordsOfEachAttack() {
  420. ArrayList<Record> recordList = new ArrayList<Record>();
  421. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID
  422. + ")" + " WHERE " + KEY_TYPE + "='RECEIVE'" + " ORDER BY " + TABLE_RECORDS + "." + KEY_TIME;
  423. SQLiteDatabase db = this.getReadableDatabase();
  424. Cursor cursor = db.rawQuery(selectQuery, null);
  425. // looping through all rows and adding to list
  426. if (cursor.moveToFirst()) {
  427. do {
  428. Record record = createRecord(cursor);
  429. // Adding record to list
  430. recordList.add(record);
  431. } while (cursor.moveToNext());
  432. }
  433. cursor.close();
  434. // return record list
  435. db.close();
  436. return recordList;
  437. }
  438. /**
  439. * Gets all {@link Record Records} saved in the database.
  440. *
  441. * @return A ArrayList of all the {@link Record Records} in the Database.
  442. */
  443. public ArrayList<Record> getAllRecords() {
  444. ArrayList<Record> recordList = new ArrayList<Record>();
  445. // Select All Query
  446. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID
  447. + ")";
  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. if (!cursor.moveToFirst()) return 0;
  475. int result = cursor.getCount();
  476. cursor.close();
  477. // return count
  478. db.close();
  479. return result;
  480. }
  481. /**
  482. * Determines the number of different attack_ids for a specific protocol in
  483. * the database.
  484. *
  485. * @param protocol
  486. * The String representation of the
  487. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  488. * Protocol}
  489. * @return The number of different attack_ids in the database.
  490. */
  491. public int getAttackPerProtocolCount(String protocol) {
  492. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO + " WHERE " + KEY_PROTOCOL + " = " + "'" + protocol + "'";
  493. SQLiteDatabase db = this.getReadableDatabase();
  494. Cursor cursor = db.rawQuery(countQuery, null);
  495. if (!cursor.moveToFirst()) return 0;
  496. int result = cursor.getCount();
  497. cursor.close();
  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 " + TABLE_ATTACK_INFO;
  508. SQLiteDatabase db = this.getReadableDatabase();
  509. Cursor cursor = db.rawQuery(selectQuery, null);
  510. int result;
  511. if (cursor.moveToFirst()) {
  512. result = cursor.getInt(0);
  513. } else {
  514. result = -1;
  515. }
  516. cursor.close();
  517. db.close();
  518. return result;
  519. }
  520. public ArrayList<HashMap<String, Object>> getNetworkInformation() {
  521. String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
  522. SQLiteDatabase db = this.getReadableDatabase();
  523. Cursor cursor = db.rawQuery(selectQuery, null);
  524. ArrayList<HashMap<String, Object>> networkInformation = new ArrayList<HashMap<String, Object>>();
  525. // looping through all rows and adding to list
  526. if (cursor.moveToFirst()) {
  527. do {
  528. HashMap<String, Object> values = new HashMap<String, Object>();
  529. values.put(KEY_BSSID, cursor.getString(0));
  530. values.put(KEY_SSID, cursor.getString(1));
  531. values.put(KEY_LATITUDE, Double.parseDouble(cursor.getString(2)));
  532. values.put(KEY_LONGITUDE, Double.parseDouble(cursor.getString(3)));
  533. values.put(KEY_ACCURACY, Float.parseFloat(cursor.getString(4)));
  534. values.put(KEY_GEO_TIMESTAMP, cursor.getLong(5));
  535. networkInformation.add(values);
  536. } while (cursor.moveToNext());
  537. }
  538. cursor.close();
  539. db.close();
  540. return networkInformation;
  541. }
  542. /**
  543. * Gets a single {@link Record} with the given ID from the database.
  544. *
  545. * @param id
  546. * The ID of the {@link Record};
  547. * @return The {@link Record}.
  548. */
  549. public Record getRecord(int id) {
  550. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID
  551. + ")" + " WHERE " + TABLE_RECORDS + "." + KEY_ID + " = " + id;
  552. SQLiteDatabase db = this.getReadableDatabase();
  553. Cursor cursor = db.rawQuery(selectQuery, null);
  554. Record record = null;
  555. if (cursor.moveToFirst()) {
  556. record = createRecord(cursor);
  557. }
  558. cursor.close();
  559. db.close();
  560. // return contact
  561. return record;
  562. }
  563. /**
  564. * Determines the number of {@link Record Records} in the database.
  565. *
  566. * @return The number of {@link Record Records} in the database.
  567. */
  568. public int getRecordCount() {
  569. String countQuery = "SELECT * FROM " + TABLE_RECORDS;
  570. SQLiteDatabase db = this.getReadableDatabase();
  571. Cursor cursor = db.rawQuery(countQuery, null);
  572. int result = cursor.getCount();
  573. cursor.close();
  574. // return count
  575. db.close();
  576. return result;
  577. }
  578. /**
  579. * Gets a single {@link Record} with the given attack id from the database.
  580. *
  581. * @param attack_id
  582. * The attack id of the {@link Record};
  583. * @return The {@link Record}.
  584. */
  585. public Record getRecordOfAttackId(long attack_id) {
  586. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID
  587. + ")" + " WHERE " + TABLE_RECORDS + "." + KEY_ATTACK_ID + " = " + attack_id + " GROUP BY " + TABLE_RECORDS + "." + KEY_ATTACK_ID;
  588. SQLiteDatabase db = this.getReadableDatabase();
  589. Cursor cursor = db.rawQuery(selectQuery, null);
  590. Record record = null;
  591. if (cursor.moveToFirst()) {
  592. record = createRecord(cursor);
  593. }
  594. cursor.close();
  595. // return record list
  596. db.close();
  597. return record;
  598. }
  599. /**
  600. * Gets a representative {@link Record} for every attack identified by its
  601. * attack id.
  602. *
  603. * @return A ArrayList with one {@link Record Records} for each attack id in
  604. * the Database.
  605. */
  606. public ArrayList<Record> getRecordOfEachAttack() {
  607. ArrayList<Record> recordList = new ArrayList<Record>();
  608. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID
  609. + ")" + " GROUP BY " + TABLE_RECORDS + "." + KEY_ATTACK_ID;
  610. SQLiteDatabase db = this.getReadableDatabase();
  611. Cursor cursor = db.rawQuery(selectQuery, null);
  612. // looping through all rows and adding to list
  613. if (cursor.moveToFirst()) {
  614. do {
  615. Record record = createRecord(cursor);
  616. // Adding record to list
  617. recordList.add(record);
  618. } while (cursor.moveToNext());
  619. }
  620. cursor.close();
  621. // return record list
  622. db.close();
  623. return recordList;
  624. }
  625. /*
  626. * Returns the Conversation of a specific attack id
  627. *
  628. * @param attack_id Tha attack id to match the query against.
  629. *
  630. * @return A arraylist with all {@link Record Records}s for an attack id.
  631. */
  632. public ArrayList<Record> getConversationForAttackID(long attack_id) {
  633. ArrayList<Record> recordList = new ArrayList<Record>();
  634. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID
  635. + ")" + " WHERE " + TABLE_RECORDS + "." + KEY_ATTACK_ID + " = " + attack_id;
  636. SQLiteDatabase db = this.getReadableDatabase();
  637. Cursor cursor = db.rawQuery(selectQuery, null);
  638. if (cursor.moveToFirst()) {
  639. do {
  640. Record record = createRecord(cursor);
  641. recordList.add(record);
  642. } while (cursor.moveToNext());
  643. }
  644. cursor.close();
  645. db.close();
  646. return recordList;
  647. }
  648. /**
  649. * Gets a representative {@link Record} for every attack with a higher
  650. * attack id than the specified.
  651. *
  652. * @param attack_id
  653. * The attack id to match the query against.
  654. * @return A ArrayList with one {@link Record Records} for each attack id
  655. * higher than the given.
  656. */
  657. public ArrayList<Record> getRecordOfEachAttack(long attack_id) {
  658. ArrayList<Record> recordList = new ArrayList<Record>();
  659. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " JOIN " + TABLE_BSSIDS + " USING " + "(" + KEY_BSSID
  660. + ")" + " WHERE " + TABLE_RECORDS + "." + KEY_ATTACK_ID + " > " + attack_id + " GROUP BY " + TABLE_RECORDS + "." + KEY_ATTACK_ID;
  661. SQLiteDatabase db = this.getReadableDatabase();
  662. Cursor cursor = db.rawQuery(selectQuery, null);
  663. // looping through all rows and adding to list
  664. if (cursor.moveToFirst()) {
  665. do {
  666. Record record = createRecord(cursor);
  667. // Adding record to list
  668. recordList.add(record);
  669. } while (cursor.moveToNext());
  670. }
  671. cursor.close();
  672. // return count
  673. db.close();
  674. return recordList;
  675. }
  676. /**
  677. * Determines the smallest attack id stored in the database.
  678. *
  679. * @return The smallest attack id stored in the database.
  680. */
  681. public long getSmallestAttackId() {
  682. String selectQuery = "SELECT MIN(" + KEY_ATTACK_ID + ") FROM " + TABLE_ATTACK_INFO;
  683. SQLiteDatabase db = this.getReadableDatabase();
  684. Cursor cursor = db.rawQuery(selectQuery, null);
  685. int result;
  686. if (cursor.moveToFirst()) {
  687. result = cursor.getInt(0);
  688. } else {
  689. result = -1;
  690. }
  691. cursor.close();
  692. db.close();
  693. return result;
  694. }
  695. /**
  696. * Gets the last recorded SSID to a given BSSID.
  697. *
  698. * @param bssid
  699. * The BSSID to match against.
  700. * @return A String of the last SSID or null if the BSSID is not in the
  701. * database.
  702. */
  703. public String getSSID(String bssid) {
  704. String selectQuery = "SELECT " + KEY_SSID + " FROM " + TABLE_BSSIDS + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
  705. SQLiteDatabase db = this.getReadableDatabase();
  706. Cursor cursor = db.rawQuery(selectQuery, null);
  707. String ssid = null;
  708. if (cursor.moveToFirst()) {
  709. ssid = cursor.getString(0);
  710. }
  711. cursor.close();
  712. db.close();
  713. return ssid;
  714. }
  715. // Creating Tables
  716. @Override
  717. public void onCreate(SQLiteDatabase db) {
  718. db.execSQL(CREATE_BSSID_TABLE);
  719. db.execSQL(CREATE_ATTACK_INFO_TABLE);
  720. db.execSQL(CREATE_RECORD_TABLE);
  721. db.execSQL(CREATE_PROFILE_TABLE);
  722. }
  723. // Upgrading database
  724. @Override
  725. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  726. // Drop older table if existed
  727. db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS);
  728. db.execSQL("DROP TABLE IF EXISTS " + TABLE_ATTACK_INFO);
  729. db.execSQL("DROP TABLE IF EXISTS " + TABLE_BSSIDS);
  730. db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROFILES);
  731. // Create tables again
  732. onCreate(db);
  733. }
  734. /**
  735. * Retrieves all the profiles from the database
  736. *
  737. * @return list of profiles
  738. */
  739. public List<Profile> getAllProfiles() {
  740. List<Profile> profiles = new LinkedList<Profile>();
  741. // Select All Query
  742. String selectQuery = "SELECT * FROM " + TABLE_PROFILES;
  743. SQLiteDatabase db = this.getWritableDatabase();
  744. Cursor cursor = db.rawQuery(selectQuery, null);
  745. // looping through all rows and adding to list
  746. if (cursor.moveToFirst()) {
  747. do {
  748. Profile profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
  749. if (cursor.getInt(6) == 1) {
  750. profile.mActivated = true;
  751. }
  752. profile.mIconName = cursor.getString(4);
  753. // Adding record to list
  754. profiles.add(profile);
  755. } while (cursor.moveToNext());
  756. }
  757. cursor.close();
  758. db.close();
  759. // return record list
  760. return profiles;
  761. }
  762. /**
  763. * Persists the given profile into the database
  764. *
  765. * @param profile
  766. * the profile which should be persisted
  767. *
  768. * @return
  769. */
  770. public long persistProfile(Profile profile) {
  771. SQLiteDatabase db = this.getReadableDatabase();
  772. ContentValues values = new ContentValues();
  773. if (profile.mId != -1) {
  774. values.put(KEY_PROFILE_ID, profile.mId);
  775. }
  776. values.put(KEY_PROFILE_NAME, profile.mLabel);
  777. values.put(KEY_PROFILE_DESCRIPTION, profile.mText);
  778. values.put(KEY_PROFILE_ICON, profile.mIconPath);
  779. values.put(KEY_PROFILE_ICON_NAME, profile.mIconName);
  780. values.put(KEY_PROFILE_ACTIVE, profile.mActivated);
  781. values.put(KEY_PROFILE_EDITABLE, profile.mEditable);
  782. return db.replace(TABLE_PROFILES, null, values);
  783. }
  784. /**
  785. * private static final String CREATE_PROFILE_TABLE = "CREATE TABLE " +
  786. * TABLE_PROFILES + "(" + KEY_PROFILE_ID +
  787. * " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_PROFILE_NAME + " TEXT," +
  788. * KEY_PROFILE_DESCRIPTION + " TEXT," + KEY_PROFILE_ICON + " TEXT," +
  789. * KEY_PROFILE_ICON_ID + " INTEGER," + KEY_PROFILE_EDITABLE + " INTEGER," +
  790. * KEY_PROFILE_ACTIVE + " INTEGER" + ")";
  791. */
  792. public Profile getProfile(int id) {
  793. String selectQuery = "SELECT * FROM " + TABLE_PROFILES + " WHERE " + TABLE_PROFILES + "." + KEY_PROFILE_ID + " = " + id;
  794. SQLiteDatabase db = this.getReadableDatabase();
  795. Cursor cursor = db.rawQuery(selectQuery, null);
  796. Profile profile = null;
  797. if (cursor.moveToFirst()) {
  798. profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
  799. if (cursor.getInt(6) == 1) {
  800. profile.mActivated = true;
  801. }
  802. profile.mIconName = cursor.getString(5);
  803. }
  804. cursor.close();
  805. db.close();
  806. // return contact
  807. return profile;
  808. }
  809. public void deleteProfile(int id) {
  810. SQLiteDatabase db = this.getReadableDatabase();
  811. db.delete(TABLE_PROFILES, KEY_PROFILE_ID + "=?", new String[] { String.valueOf(id) });
  812. }
  813. public void updateNetworkInformation(ArrayList<HashMap<String, Object>> networkInformation) {
  814. Log.i("DatabaseHandler", "Starte updating");
  815. for (HashMap<String, Object> values : networkInformation) {
  816. updateNetworkInformation(values);
  817. }
  818. }
  819. public void updateNetworkInformation(HashMap<String, Object> networkInformation) {
  820. SQLiteDatabase db = this.getReadableDatabase();
  821. String bssid = (String) networkInformation.get(KEY_BSSID);
  822. String bssidQuery = "SELECT * FROM " + TABLE_BSSIDS + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
  823. Cursor cursor = db.rawQuery(bssidQuery, null);
  824. int result = cursor.getCount();
  825. if (cursor != null && cursor.moveToFirst() && (result <= 0 || cursor.getLong(5) < (Long) networkInformation.get(KEY_GEO_TIMESTAMP)))
  826. ;
  827. {
  828. ContentValues bssidValues = new ContentValues();
  829. bssidValues.put(KEY_BSSID, bssid);
  830. bssidValues.put(KEY_SSID, (String) networkInformation.get(KEY_SSID));
  831. bssidValues.put(KEY_LATITUDE, (double) (Double) networkInformation.get(KEY_LATITUDE));
  832. bssidValues.put(KEY_LONGITUDE, (double) (Double) networkInformation.get(KEY_LONGITUDE));
  833. bssidValues.put(KEY_ACCURACY, (float) (Float) networkInformation.get(KEY_ACCURACY));
  834. bssidValues.put(KEY_GEO_TIMESTAMP, (Long) networkInformation.get(KEY_GEO_TIMESTAMP));
  835. db.insertWithOnConflict(TABLE_BSSIDS, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
  836. }
  837. cursor.close();
  838. db.close();
  839. }
  840. /**
  841. * Creates a {@link Record} from a Cursor. If the cursor does not show to a
  842. * valid data structure a runtime exception is thrown.
  843. *
  844. * @param cursor
  845. * @return Returns the created {@link Record} .
  846. */
  847. private Record createRecord(Cursor cursor) {
  848. Record record = new Record();
  849. record.setId(Integer.parseInt(cursor.getString(0)));
  850. record.setAttack_id(cursor.getLong(1));
  851. record.setType(TYPE.valueOf(cursor.getString(2)));
  852. record.setTimestamp(cursor.getLong(3));
  853. record.setPacket(cursor.getString(4));
  854. record.setProtocol(cursor.getString(5));
  855. record.setExternalIP(cursor.getString(6));
  856. record.setLocalIP(cursor.getString(7));
  857. record.setLocalPort(Integer.parseInt(cursor.getString(8)));
  858. record.setRemoteIP(cursor.getString(9));
  859. record.setRemotePort(Integer.parseInt(cursor.getString(10)));
  860. record.setBssid(cursor.getString(11));
  861. record.setSsid(cursor.getString(12));
  862. record.setLatitude(Double.parseDouble(cursor.getString(13)));
  863. record.setLongitude(Double.parseDouble(cursor.getString(14)));
  864. record.setAccuracy(Float.parseFloat(cursor.getString(15)));
  865. record.setTimestampLocation(cursor.getLong(16));
  866. return record;
  867. }
  868. }