UglyDbHelper.java 34 KB

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