DatabaseHandler.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. package de.tudarmstadt.informatik.hostage.logging;
  2. import java.net.InetAddress;
  3. import java.net.UnknownHostException;
  4. import java.util.ArrayList;
  5. import android.content.ContentValues;
  6. import android.content.Context;
  7. import android.database.Cursor;
  8. import android.database.sqlite.SQLiteDatabase;
  9. import android.database.sqlite.SQLiteOpenHelper;
  10. import android.util.Log;
  11. import de.tudarmstadt.informatik.hostage.R;
  12. import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
  13. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  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 kinds of information from the database.<br>
  18. * The database contains two tables: {@link #TABLE_RECORDS} and {@link #TABLE_BSSIDS}:<br>
  19. * {@link #TABLE_RECORDS} contains all logging information of a single message record except the SSID.<br>
  20. * {@link #TABLE_BSSIDS} contains the BSSID of all recorded Networks and the corresponding SSID.<br>
  21. * @author Lars Pandikow
  22. */
  23. public class DatabaseHandler extends SQLiteOpenHelper {
  24. // All Static variables
  25. // Database Version
  26. private static final int DATABASE_VERSION = 1;
  27. // Database Name
  28. private static final String DATABASE_NAME = "recordManager";
  29. // Contacts table names
  30. private static final String TABLE_ATTACK_INFO = "attack_info";
  31. private static final String TABLE_RECORDS = "records";
  32. private static final String TABLE_BSSIDS = "bssids";
  33. private static final String TABLE_PORTS = "ports";
  34. // Contacts Table Columns names
  35. private static final String KEY_ID = "_id";
  36. private static final String KEY_ATTACK_ID = "_attack_id";
  37. private static final String KEY_TYPE = "type";
  38. private static final String KEY_TIME = "timestamp";
  39. private static final String KEY_PACKET = "packet";
  40. private static final String KEY_PROTOCOL = "protocol";
  41. private static final String KEY_EXTERNAL_IP ="externalIP";
  42. private static final String KEY_LOCAL_IP = "localIP";
  43. private static final String KEY_LOCAL_HOSTNAME = "localHostName";
  44. private static final String KEY_LOCAL_PORT = "localPort";
  45. private static final String KEY_REMOTE_IP = "remoteIP";
  46. private static final String KEY_REMOTE_HOSTNAME = "remoteHostName";
  47. private static final String KEY_REMOTE_PORT = "remotePort";
  48. private static final String KEY_BSSID = "_bssid";
  49. private static final String KEY_SSID = "ssid";
  50. private static final String KEY_LATITUDE = "latitude";
  51. private static final String KEY_LONGITUDE = "longitude";
  52. private static final String KEY_ACCURACY = "accuracy";
  53. // Database sql create statements
  54. private static final String CREATE_RECORD_TABLE = "CREATE TABLE " + TABLE_RECORDS + "("
  55. + KEY_ID + " INTEGER NOT NULL," + KEY_ATTACK_ID + " INTEGER NOT NULL,"
  56. + KEY_TYPE + " TEXT," + KEY_TIME + " INTEGER," + KEY_PACKET + " TEXT,"
  57. + "FOREIGN KEY("+ KEY_ATTACK_ID +") REFERENCES " + TABLE_ATTACK_INFO + "("+KEY_ATTACK_ID+")"
  58. + "PRIMARY KEY("+ KEY_ID + ", " + KEY_ATTACK_ID + ")"
  59. + ")";
  60. private static final String CREATE_ATTACK_INFO_TABLE = "CREATE TABLE " + TABLE_ATTACK_INFO + "("
  61. + KEY_ATTACK_ID + " INTEGER PRIMARY KEY," + KEY_PROTOCOL + " TEXT,"
  62. + KEY_EXTERNAL_IP + " TEXT," + KEY_LOCAL_IP + " BLOB," + KEY_LOCAL_HOSTNAME + " TEXT,"
  63. + KEY_REMOTE_IP + " BLOB," + KEY_REMOTE_HOSTNAME + " TEXT," + KEY_REMOTE_PORT + " INTEGER," + KEY_BSSID + " TEXT,"
  64. + "FOREIGN KEY("+ KEY_BSSID +") REFERENCES " + TABLE_BSSIDS + "("+KEY_BSSID+")"
  65. + "FOREIGN KEY("+ KEY_PROTOCOL +") REFERENCES " + TABLE_PORTS + "("+KEY_PROTOCOL+")"
  66. + ")";
  67. private static final String CREATE_BSSID_TABLE = "CREATE TABLE " + TABLE_BSSIDS + "("
  68. + KEY_BSSID + " TEXT PRIMARY KEY," + KEY_SSID + " TEXT," + KEY_LATITUDE + " INTEGER,"
  69. + KEY_LONGITUDE + " INTEGER," + KEY_ACCURACY + " INTEGER"
  70. + ")";
  71. private static final String CREATE_PORT_TABLE = "CREATE TABLE " + TABLE_PORTS + "("
  72. + KEY_PROTOCOL + " TEXT PRIMARY KEY," + KEY_LOCAL_PORT + " INTEGER"
  73. + ")";
  74. private Context context;
  75. public DatabaseHandler(Context context) {
  76. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  77. this.context = context;
  78. }
  79. // Creating Tables
  80. @Override
  81. public void onCreate(SQLiteDatabase db) {
  82. db.execSQL(CREATE_PORT_TABLE);
  83. db.execSQL(CREATE_BSSID_TABLE);
  84. db.execSQL(CREATE_ATTACK_INFO_TABLE);
  85. db.execSQL(CREATE_RECORD_TABLE);
  86. String[] protocols = context.getResources().getStringArray(R.array.protocols);
  87. String packageName = Protocol.class.getPackage().getName();
  88. //Initialize Port Table
  89. for (String protocol : protocols) {
  90. try {
  91. int port = ((Protocol<?>) Class.forName(String.format("%s.%s", packageName, protocol)).newInstance()).getPort();
  92. db.execSQL("INSERT INTO " + TABLE_PORTS + " VALUES ( '" + protocol + "'," + port + ")");
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. }
  98. // Upgrading database
  99. @Override
  100. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  101. // Drop older table if existed
  102. db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS);
  103. db.execSQL("DROP TABLE IF EXISTS " + TABLE_ATTACK_INFO);
  104. db.execSQL("DROP TABLE IF EXISTS " + TABLE_BSSIDS);
  105. db.execSQL("DROP TABLE IF EXISTS " + TABLE_PORTS);
  106. // Create tables again
  107. onCreate(db);
  108. }
  109. /**
  110. * Adds a given {@link Record} to the database.
  111. * @param record The added {@link Record} .
  112. */
  113. public void addRecord(Record record) {
  114. SQLiteDatabase db = this.getWritableDatabase();
  115. ContentValues bssidValues = new ContentValues();
  116. bssidValues.put(KEY_BSSID, record.getBSSID());
  117. bssidValues.put(KEY_SSID, record.getSSID());
  118. bssidValues.put(KEY_LATITUDE, record.getLatitude());
  119. bssidValues.put(KEY_LONGITUDE, record.getLongitude());
  120. bssidValues.put(KEY_ACCURACY, record.getAccuracy());
  121. ContentValues attackValues = new ContentValues();
  122. attackValues.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  123. attackValues.put(KEY_PROTOCOL, record.getProtocol().toString());
  124. attackValues.put(KEY_EXTERNAL_IP, record.getExternalIP());
  125. attackValues.put(KEY_LOCAL_IP, record.getLocalIP().getAddress()); // Log Local IP
  126. attackValues.put(KEY_LOCAL_HOSTNAME, record.getLocalIP().getHostName());
  127. attackValues.put(KEY_REMOTE_IP, record.getRemoteIP().getAddress()); // Log Remote IP
  128. attackValues.put(KEY_REMOTE_HOSTNAME, record.getRemoteIP().getHostName());
  129. attackValues.put(KEY_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
  130. attackValues.put(KEY_BSSID, record.getBSSID());
  131. ContentValues recordValues = new ContentValues();
  132. recordValues.put(KEY_ID, record.getId()); // Log Message Number
  133. recordValues.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  134. recordValues.put(KEY_TYPE, record.getType().name()); // Log Type
  135. recordValues.put(KEY_TIME, record.getTimestamp()); // Log Timestamp
  136. recordValues.put(KEY_PACKET, record.getPacket()); // Log Packet
  137. // Inserting Rows
  138. db.insertWithOnConflict(TABLE_BSSIDS, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
  139. db.insertWithOnConflict(TABLE_ATTACK_INFO, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
  140. db.insert(TABLE_RECORDS, null, recordValues);
  141. db.close(); // Closing database connection
  142. }
  143. /**
  144. * Creates a {@link Record} from a Cursor. If the cursor does not show to a valid data structure a runtime exception is thrown.
  145. * @param cursor
  146. * @return Returns the created {@link Record} .
  147. */
  148. private Record createRecord(Cursor cursor){
  149. Record record = new Record();
  150. try {
  151. record.setId(Integer.parseInt(cursor.getString(0)));
  152. record.setAttack_id(cursor.getLong(1));
  153. record.setType(cursor.getString(2).equals("SEND") ? TYPE.SEND : TYPE.RECEIVE);
  154. record.setTimestamp(cursor.getLong(3));
  155. record.setPacket(cursor.getString(4));
  156. record.setProtocol(cursor.getString(5));
  157. record.setExternalIP(cursor.getString(6));
  158. record.setLocalIP(InetAddress.getByAddress(cursor.getString(8), cursor.getBlob(7)));
  159. record.setRemoteIP(InetAddress.getByAddress(cursor.getString(10), cursor.getBlob(9)));
  160. record.setRemotePort(Integer.parseInt(cursor.getString(11)));
  161. record.setBSSID(cursor.getString(12));
  162. record.setSSID(cursor.getString(13));
  163. record.setLatitude(Double.parseDouble(cursor.getString(14)));
  164. record.setLongitude(Double.parseDouble(cursor.getString(15)));
  165. record.setAccuracy(Float.parseFloat(cursor.getString(16)));
  166. record.setLocalPort(Integer.parseInt(cursor.getString(17)));
  167. } catch (UnknownHostException e) {
  168. e.printStackTrace();
  169. }
  170. return record;
  171. }
  172. /**
  173. * Gets a single {@link Record} with the given ID from the database.
  174. * @param id The ID of the {@link Record};
  175. * @return The {@link Record}.
  176. */
  177. public Record getRecord(int id) {
  178. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS + " WHERE " + KEY_ID + " = " + id;
  179. SQLiteDatabase db = this.getReadableDatabase();
  180. Cursor cursor = db.rawQuery(selectQuery, null);
  181. Record record = null;
  182. if (cursor.moveToFirst()){
  183. record = createRecord(cursor);
  184. }
  185. cursor.close();
  186. db.close();
  187. // return contact
  188. return record;
  189. }
  190. /**
  191. * Gets all {@link Record Records} saved in the database.
  192. * @return A ArrayList of all the {@link Record Records} in the Database.
  193. */
  194. public ArrayList<Record> getAllRecords() {
  195. ArrayList<Record> recordList = new ArrayList<Record>();
  196. // Select All Query
  197. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS;
  198. SQLiteDatabase db = this.getWritableDatabase();
  199. Cursor cursor = db.rawQuery(selectQuery, null);
  200. Log.i("Database", "Start loop");
  201. // looping through all rows and adding to list
  202. if (cursor.moveToFirst()) {
  203. do {
  204. Log.i("Database", "Add Record");
  205. Record record = createRecord(cursor);
  206. // Adding record to list
  207. recordList.add(record);
  208. } while (cursor.moveToNext());
  209. }
  210. cursor.close();
  211. db.close();
  212. // return record list
  213. return recordList;
  214. }
  215. /**
  216. * Gets a single {@link Record} with the given attack id from the database.
  217. * @param attack_id The attack id of the {@link Record};
  218. * @return The {@link Record}.
  219. */
  220. public Record getRecordOfAttackId(long attack_id) {
  221. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS + " WHERE " + KEY_ATTACK_ID + " = " + attack_id + " GROUP BY " + KEY_ATTACK_ID;
  222. SQLiteDatabase db = this.getReadableDatabase();
  223. Cursor cursor = db.rawQuery(selectQuery, null);
  224. Record record = null;
  225. if (cursor.moveToFirst()) {
  226. record = createRecord(cursor);
  227. }
  228. cursor.close();
  229. // return record list
  230. db.close();
  231. return record;
  232. }
  233. /**
  234. * Gets all received {@link Record Records} for every attack identified by its attack id and ordered by date.
  235. * @return A ArrayList with all received {@link Record Records} for each attack id in the Database.
  236. */
  237. public ArrayList<Record> getAllReceivedRecordsOfEachAttack() {
  238. ArrayList<Record> recordList = new ArrayList<Record>();
  239. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS + " WHERE " + KEY_TYPE + "='RECEIVE'" + " ORDER BY " + KEY_TIME;
  240. SQLiteDatabase db = this.getReadableDatabase();
  241. Cursor cursor = db.rawQuery(selectQuery, null);
  242. // looping through all rows and adding to list
  243. if (cursor.moveToFirst()) {
  244. do {
  245. Record record = createRecord(cursor);
  246. // Adding record to list
  247. recordList.add(record);
  248. } while (cursor.moveToNext());
  249. }
  250. cursor.close();
  251. // return record list
  252. db.close();
  253. return recordList;
  254. }
  255. /*
  256. // Contacts Table Columns names
  257. private static final String KEY_ID = "_id";
  258. private static final String KEY_ATTACK_ID = "_attack_id";
  259. private static final String KEY_TYPE = "type";
  260. private static final String KEY_TIME = "timestamp";
  261. private static final String KEY_PACKET = "packet";
  262. private static final String KEY_PROTOCOL = "protocol";
  263. private static final String KEY_EXTERNAL_IP ="externalIP";
  264. private static final String KEY_LOCAL_IP = "localIP";
  265. private static final String KEY_LOCAL_HOSTNAME = "localHostName";
  266. private static final String KEY_LOCAL_PORT = "localPort";
  267. private static final String KEY_REMOTE_IP = "remoteIP";
  268. private static final String KEY_REMOTE_HOSTNAME = "remoteHostName";
  269. private static final String KEY_REMOTE_PORT = "remotePort";
  270. private static final String KEY_BSSID = "_bssid";
  271. private static final String KEY_SSID = "ssid";
  272. private static final String KEY_LATITUDE = "latitude";
  273. private static final String KEY_LONGITUDE = "longitude";
  274. private static final String KEY_ACCURACY = "accuracy";
  275. */
  276. /**
  277. * Gets all received {@link Record Records} for the specified information in the LogFilter ordered by date.
  278. * @return A ArrayList with all received {@link Record Records} for the LogFilter.
  279. */
  280. public ArrayList<Record> getRecordsForFilter(LogFilter filter) {
  281. ArrayList<Record> recordList = new ArrayList<Record>();
  282. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS;
  283. // TIMESTAMPS
  284. selectQuery = selectQuery + " WHERE " + KEY_TIME;
  285. selectQuery = selectQuery + " < " + filter.getBelowTimestamp();
  286. selectQuery = selectQuery + " AND " + KEY_TIME;
  287. selectQuery = selectQuery + " > " + filter.getAboveTimestamp();
  288. if (filter.getBSSIDs() != null && filter.getBSSIDs().size() > 0) {
  289. selectQuery = selectQuery + " AND ";
  290. selectQuery = selectQuery + filter.getBSSIDQueryStatement(KEY_BSSID);
  291. }
  292. if (filter.getESSIDs() != null && filter.getESSIDs().size() > 0) {
  293. selectQuery = selectQuery + " AND ";
  294. selectQuery = selectQuery + filter.getESSIDQueryStatement(KEY_SSID);
  295. }
  296. if (filter.getProtocols() != null && filter.getProtocols().size() > 0) {
  297. selectQuery = selectQuery + " AND ";
  298. selectQuery = selectQuery + filter.getProtocolsQueryStatement(KEY_PROTOCOL);
  299. }
  300. // ORDERED BY TIME
  301. selectQuery = selectQuery + " ORDER BY " + filter.getSorttype();
  302. System.out.println(selectQuery);
  303. SQLiteDatabase db = this.getReadableDatabase();
  304. Cursor cursor = db.rawQuery(selectQuery, null);
  305. // looping through all rows and adding to list
  306. if (cursor.moveToFirst()) {
  307. do {
  308. Record record = createRecord(cursor);
  309. // Adding record to list
  310. recordList.add(record);
  311. } while (cursor.moveToNext());
  312. }
  313. cursor.close();
  314. // return record list
  315. db.close();
  316. return recordList;
  317. }
  318. /**
  319. * Gets all non duplicate Records For the key BSSID.
  320. * @return A ArrayList with received Records.
  321. */
  322. public ArrayList<String> getUniqueBSSIDRecords(){
  323. return this.getUniqueDataEntryForKeyType(KEY_BSSID, TABLE_BSSIDS);
  324. }
  325. /**
  326. * Gets all non duplicate Records For the key ESSID.
  327. * @return A ArrayList with received Records.
  328. */
  329. public ArrayList<String> getUniqueESSIDRecords(){
  330. return this.getUniqueDataEntryForKeyType(KEY_SSID, TABLE_BSSIDS);
  331. }
  332. /**
  333. * Gets all non duplicate Records For the key protocol.
  334. * @return A ArrayList with received Records.
  335. */
  336. public ArrayList<String> getUniqueProtocolRecords(){
  337. return this.getUniqueDataEntryForKeyType(KEY_PROTOCOL, TABLE_PORTS);
  338. }
  339. /**
  340. * Gets all non duplicate Data Entry For a specific KeyType ( e.g. BSSIDs).
  341. * @return A ArrayList with received Records.
  342. */
  343. public ArrayList<String> getUniqueDataEntryForKeyType(String keyType, String table) {
  344. ArrayList<String> recordList = new ArrayList<String>();
  345. //String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS;
  346. String selectQuery = "SELECT DISTINCT " + keyType + " FROM " + table + " ORDER BY " + keyType; // " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS +
  347. // ORDERED BY TIME
  348. System.out.println(selectQuery);
  349. SQLiteDatabase db = this.getReadableDatabase();
  350. Cursor cursor = db.rawQuery(selectQuery, null);
  351. // looping through all rows and adding to list
  352. if (cursor.moveToFirst()) {
  353. do {
  354. String record = cursor.getString(0);
  355. recordList.add(record);
  356. } while (cursor.moveToNext());
  357. }
  358. cursor.close();
  359. // return record list
  360. db.close();
  361. return recordList;
  362. }
  363. /**
  364. * Gets a representative {@link Record} for every attack identified by its attack id.
  365. * @return A ArrayList with one {@link Record Records} for each attack id in the Database.
  366. */
  367. public ArrayList<Record> getRecordOfEachAttack() {
  368. ArrayList<Record> recordList = new ArrayList<Record>();
  369. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS + " GROUP BY " + KEY_ATTACK_ID;
  370. SQLiteDatabase db = this.getReadableDatabase();
  371. Cursor cursor = db.rawQuery(selectQuery, null);
  372. // looping through all rows and adding to list
  373. if (cursor.moveToFirst()) {
  374. do {
  375. Record record = createRecord(cursor);
  376. // Adding record to list
  377. recordList.add(record);
  378. } while (cursor.moveToNext());
  379. }
  380. cursor.close();
  381. // return record list
  382. db.close();
  383. return recordList;
  384. }
  385. /**
  386. * Gets a representative {@link Record} for every attack with a higher attack id than the specified.
  387. * @param attack_id The attack id to match the query against.
  388. * @return A ArrayList with one {@link Record Records} for each attack id higher than the given.
  389. */
  390. public ArrayList<Record> getRecordOfEachAttack(long attack_id) {
  391. ArrayList<Record> recordList = new ArrayList<Record>();
  392. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS + " WHERE " + KEY_ATTACK_ID + " > " + attack_id + " GROUP BY " + KEY_ATTACK_ID;
  393. SQLiteDatabase db = this.getReadableDatabase();
  394. Cursor cursor = db.rawQuery(selectQuery, null);
  395. // looping through all rows and adding to list
  396. if (cursor.moveToFirst()) {
  397. do {
  398. Record record = createRecord(cursor);
  399. // Adding record to list
  400. recordList.add(record);
  401. } while (cursor.moveToNext());
  402. }
  403. cursor.close();
  404. // return count
  405. db.close();
  406. return recordList;
  407. }
  408. /**
  409. * Determines the number of {@link Record Records} in the database.
  410. * @return The number of {@link Record Records} in the database.
  411. */
  412. public int getRecordCount() {
  413. String countQuery = "SELECT * FROM " + TABLE_RECORDS;
  414. SQLiteDatabase db = this.getReadableDatabase();
  415. Cursor cursor = db.rawQuery(countQuery, null);
  416. int result = cursor.getCount();
  417. cursor.close();
  418. // return count
  419. db.close();
  420. return result;
  421. }
  422. /**
  423. * Determines the number of different attack_ids in the database.
  424. * @return The number of different attack_ids in the database.
  425. */
  426. public int getAttackCount() {
  427. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO;
  428. SQLiteDatabase db = this.getReadableDatabase();
  429. Cursor cursor = db.rawQuery(countQuery, null);
  430. int result = cursor.getCount();
  431. cursor.close();
  432. // return count
  433. db.close();
  434. return result;
  435. }
  436. /**
  437. * Determines the number of different attack_ids for a specific protocol in the database.
  438. * @param protocol The String representation of the {@link de.tudarmstadt.informatik.hostage.protocol.Protocol Protocol}
  439. * @return The number of different attack_ids in the database.
  440. */
  441. public int getAttackPerProtokolCount(String protocol) {
  442. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO + " WHERE " + KEY_PROTOCOL + " = " + "'" + protocol + "'";
  443. SQLiteDatabase db = this.getReadableDatabase();
  444. Cursor cursor = db.rawQuery(countQuery, null);
  445. int result = cursor.getCount();
  446. cursor.close();
  447. // return count
  448. db.close();
  449. return result;
  450. }
  451. /**
  452. * Determines the smallest attack id stored in the database.
  453. * @return The smallest attack id stored in the database.
  454. */
  455. public long getSmallestAttackId(){
  456. String selectQuery = "SELECT MIN(" + KEY_ATTACK_ID +") FROM " + TABLE_ATTACK_INFO;
  457. SQLiteDatabase db = this.getReadableDatabase();
  458. Cursor cursor = db.rawQuery(selectQuery, null);
  459. int result;
  460. if (cursor.moveToFirst()) {
  461. result = cursor.getInt(0);
  462. } else{
  463. result = -1;
  464. }
  465. cursor.close();
  466. db.close();
  467. return result;
  468. }
  469. /**
  470. * Determines the highest attack id stored in the database.
  471. * @return The highest attack id stored in the database.
  472. */
  473. public long getHighestAttackId(){
  474. String selectQuery = "SELECT MAX(" + KEY_ATTACK_ID +") FROM " + TABLE_ATTACK_INFO;
  475. SQLiteDatabase db = this.getReadableDatabase();
  476. Cursor cursor = db.rawQuery(selectQuery, null);
  477. int result;
  478. if (cursor.moveToFirst()) {
  479. result = cursor.getInt(0);
  480. } else{
  481. result = -1;
  482. }
  483. cursor.close();
  484. db.close();
  485. return result;
  486. }
  487. /**
  488. * Determines if an attack has been recorded on a specific protocol in a network with a given BSSID.
  489. * @param protocol The {@link de.tudarmstadt.informatik.hostage.protocol.Protocol Protocol} to inspect.
  490. * @param BSSID The BSSID of the network.
  491. * @return True if an attack on the given protocol has been recorded in a network with the given BSSID, else false.
  492. */
  493. public boolean bssidSeen(String protocol, String BSSID){
  494. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS+ " WHERE " + KEY_PROTOCOL + " = " + "'" + protocol + "'" + " AND " + KEY_BSSID + " = " + "'" + BSSID + "'";
  495. SQLiteDatabase db = this.getReadableDatabase();
  496. Cursor cursor = db.rawQuery(countQuery, null);
  497. int result = cursor.getCount();
  498. cursor.close();
  499. db.close();
  500. return result > 0;
  501. }
  502. /**
  503. * Returns a String array with all BSSIDs stored in the database.
  504. * @return String[] of all recorded BSSIDs.
  505. */
  506. public String[] getAllBSSIDS(){
  507. String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
  508. SQLiteDatabase db = this.getReadableDatabase();
  509. Cursor cursor = db.rawQuery(selectQuery, null);
  510. String[] bssidList = new String[cursor.getCount()];
  511. int counter = 0;
  512. // looping through all rows and adding to list
  513. if (cursor.moveToFirst()) {
  514. do {
  515. bssidList[counter] = cursor.getString(0);
  516. counter++;
  517. } while (cursor.moveToNext());
  518. }
  519. cursor.close();
  520. db.close();
  521. return bssidList;
  522. }
  523. /**
  524. * Gets the last recorded SSID to a given BSSID.
  525. * @param bssid The BSSID to match against.
  526. * @return A String of the last SSID or null if the BSSID is not in the database.
  527. */
  528. public String getSSID(String bssid){
  529. String selectQuery = "SELECT "+ KEY_SSID +" FROM " + TABLE_BSSIDS + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
  530. SQLiteDatabase db = this.getReadableDatabase();
  531. Cursor cursor = db.rawQuery(selectQuery, null);
  532. String ssid = null;
  533. if(cursor.moveToFirst()){
  534. ssid = cursor.getString(0);
  535. }
  536. cursor.close();
  537. db.close();
  538. return ssid;
  539. }
  540. /**
  541. * Deletes all records from {@link #TABLE_RECORDS} with a specific BSSID.
  542. * @param bssid The BSSID to match against.
  543. */
  544. public void deleteByBSSID(String bssid){
  545. SQLiteDatabase db = this.getReadableDatabase();
  546. db.delete(TABLE_RECORDS, KEY_BSSID + " = ?", new String[]{bssid});
  547. db.delete(TABLE_ATTACK_INFO, KEY_BSSID + " = ?", new String[]{bssid});
  548. db.close();
  549. }
  550. /**
  551. * Deletes all records from {@link #TABLE_RECORDS} with a time stamp smaller then the given
  552. * @param date A Date represented in milliseconds.
  553. */
  554. public void deleteByDate(long date){
  555. SQLiteDatabase db = this.getReadableDatabase();
  556. String deleteQuery = "DELETE FROM " + TABLE_RECORDS + " WHERE " + KEY_TIME + " < " + date;
  557. //TODO Delete statement �berarbeiten
  558. // String deleteQuery2 = "DELETE "
  559. db.execSQL(deleteQuery);
  560. db.close();
  561. }
  562. /**
  563. * Deletes all records from {@link #TABLE_RECORDS}.
  564. */
  565. public void clearData(){
  566. SQLiteDatabase db = this.getReadableDatabase();
  567. db.delete(TABLE_RECORDS, null, null);
  568. db.delete(TABLE_ATTACK_INFO, null, null);
  569. db.close();
  570. }
  571. }