UglyDbHelper.java 34 KB

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