HostageDBOpenHelper.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. package de.tudarmstadt.informatik.hostage.persistence;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9. import android.util.Log;
  10. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  11. import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
  12. import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
  13. import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
  14. import de.tudarmstadt.informatik.hostage.logging.SyncInfoRecord;
  15. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.AttackEntry;
  16. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.NetworkEntry;
  17. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEntry;
  18. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncDeviceEntry;
  19. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncInfoEntry;
  20. public class HostageDBOpenHelper extends SQLiteOpenHelper {
  21. private static final String DATABASE_NAME = "hostage.db";
  22. private static final int DATABASE_VERSION = 1;
  23. private Context context;
  24. static {
  25. StringBuilder networkSQLBuilder = new StringBuilder("CREATE TABLE ").append(NetworkEntry.TABLE_NAME).append("(");
  26. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_BSSID).append(" TEXT PRIMARY KEY,");
  27. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_SSID).append(" TEXT,");
  28. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_LATITUDE).append(" INTEGER,");
  29. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_LONGITUDE).append(" INTEGER,");
  30. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_ACCURACY).append(" INTEGER,");
  31. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP).append(" INTEGER");
  32. networkSQLBuilder.append(")");
  33. SQL_CREATE_NETWORK_ENTRIES = networkSQLBuilder.toString();
  34. StringBuilder attackSQLBuilder = new StringBuilder("CREATE TABLE ").append(AttackEntry.TABLE_NAME).append("(");
  35. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_ATTACK_ID).append(" INTEGER PRIMARY KEY,");
  36. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_PROTOCOL).append(" TEXT,");
  37. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_EXTERNAL_IP).append(" TEXT,");
  38. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_LOCAL_IP).append(" BLOB,");
  39. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_LOCAL_PORT).append(" INTEGER,");
  40. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_REMOTE_IP).append(" BLOB,");
  41. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_REMOTE_PORT).append(" INTEGER,");
  42. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_BSSID).append(" TEXT,");
  43. attackSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", AttackEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME,
  44. NetworkEntry.COLUMN_NAME_BSSID));
  45. attackSQLBuilder.append(")");
  46. SQL_CREATE_ATTACK_ENTRIES = attackSQLBuilder.toString();
  47. StringBuilder packetSQLBuilder = new StringBuilder("CREATE TABLE ").append(PacketEntry.TABLE_NAME).append("(");
  48. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_ID).append(" INTEGER NOT NULL,");
  49. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_ATTACK_ID).append(" INTEGER NOT NULL,");
  50. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_TYPE).append(" TEXT,");
  51. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP).append(" INTEGER,");
  52. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_PACKET).append(" TEXT,");
  53. packetSQLBuilder.append(String.format("PRIMARY KEY(%s,%s)", PacketEntry.COLUMN_NAME_ID, PacketEntry.COLUMN_NAME_ATTACK_ID));
  54. packetSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", PacketEntry.COLUMN_NAME_ATTACK_ID, AttackEntry.TABLE_NAME,
  55. AttackEntry.COLUMN_NAME_ATTACK_ID));
  56. packetSQLBuilder.append(")");
  57. SQL_CREATE_PACKET_ENTRIES = packetSQLBuilder.toString();
  58. StringBuilder syncDevicesSQLBuilder = new StringBuilder("CREATE TABLE ").append(SyncDeviceEntry.TABLE_NAME).append("(");
  59. syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID).append(" TEXT PRIMARY KEY,");
  60. syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP).append(" INTEGER");
  61. syncDevicesSQLBuilder.append(")");
  62. SQL_CREATE_SYNC_DEVICES_ENTRIES = syncDevicesSQLBuilder.toString();
  63. StringBuilder syncInfoSQLBuilder = new StringBuilder("CREATE TABLE ").append(SyncInfoEntry.TABLE_NAME).append("(");
  64. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_DEVICE_ID).append(" TEXT,");
  65. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_BSSID).append(" TEXT,");
  66. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS).append(" INTEGER,");
  67. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS).append(" INTEGER,");
  68. syncInfoSQLBuilder.append(String.format("PRIMARY KEY(%s,%s)", SyncInfoEntry.COLUMN_NAME_DEVICE_ID, SyncInfoEntry.COLUMN_NAME_BSSID));
  69. syncInfoSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", SyncInfoEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME,
  70. NetworkEntry.COLUMN_NAME_BSSID));
  71. syncInfoSQLBuilder.append(")");
  72. SQL_CREATE_SYNC_INFO_ENTRIES = syncInfoSQLBuilder.toString();
  73. }
  74. private static final String SQL_CREATE_NETWORK_ENTRIES;
  75. private static final String SQL_CREATE_ATTACK_ENTRIES;
  76. private static final String SQL_CREATE_PACKET_ENTRIES;
  77. private static final String SQL_CREATE_SYNC_DEVICES_ENTRIES;
  78. private static final String SQL_CREATE_SYNC_INFO_ENTRIES;
  79. private static final String SQL_DELETE_PACKET_ENTRIES = "DROP TABLE IF EXISTS " + PacketEntry.TABLE_NAME;
  80. private static final String SQL_DELETE_ATTACK_ENTRIES = "DROP TABLE IF EXISTS " + AttackEntry.TABLE_NAME;
  81. private static final String SQL_DELETE_NETWORK_ENTRIES = "DROP TABLE IF EXISTS " + NetworkEntry.TABLE_NAME;
  82. private static final String SQL_DELETE_SYNC_DEVICES_ENTRIES = "DROP TABLE IF EXISTS " + SyncDeviceEntry.TABLE_NAME;
  83. private static final String SQL_DELETE_SYNC_INFO_ENTRIES = "DROP TABLE IF EXISTS " + SyncInfoEntry.TABLE_NAME;
  84. public HostageDBOpenHelper(Context context) {
  85. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  86. this.context = context;
  87. }
  88. @Override
  89. public void onCreate(SQLiteDatabase db) {
  90. db.execSQL(SQL_CREATE_NETWORK_ENTRIES);
  91. db.execSQL(SQL_CREATE_ATTACK_ENTRIES);
  92. db.execSQL(SQL_CREATE_PACKET_ENTRIES);
  93. db.execSQL(SQL_CREATE_SYNC_DEVICES_ENTRIES);
  94. db.execSQL(SQL_CREATE_SYNC_INFO_ENTRIES);
  95. }
  96. @Override
  97. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  98. db.execSQL(SQL_DELETE_SYNC_INFO_ENTRIES);
  99. db.execSQL(SQL_DELETE_PACKET_ENTRIES);
  100. db.execSQL(SQL_DELETE_ATTACK_ENTRIES);
  101. db.execSQL(SQL_DELETE_NETWORK_ENTRIES);
  102. db.execSQL(SQL_DELETE_SYNC_DEVICES_ENTRIES);
  103. onCreate(db);
  104. }
  105. /**
  106. * Adds a given {@link MessageRecord} to the database.
  107. *
  108. * @param record
  109. * The added {@link MessageRecord} .
  110. */
  111. public void addMessageRecord(MessageRecord record) {
  112. SQLiteDatabase db = this.getWritableDatabase();
  113. ContentValues recordValues = new ContentValues();
  114. recordValues.put(PacketEntry.COLUMN_NAME_ID, record.getId()); // Log Message Number
  115. recordValues.put(PacketEntry.COLUMN_NAME_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  116. recordValues.put(PacketEntry.COLUMN_NAME_TYPE, record.getType().name()); // Log Type
  117. recordValues.put(PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP, record.getTimestamp()); // Log Timestamp
  118. recordValues.put(PacketEntry.COLUMN_NAME_PACKET, record.getPacket()); // Log Packet
  119. // Inserting Rows
  120. db.insert(PacketEntry.TABLE_NAME, null, recordValues);
  121. db.close(); // Closing database connection
  122. }
  123. /**
  124. * Adds a given {@link AttackRecord} to the database.
  125. *
  126. * @param record
  127. * The added {@link AttackRecord} .
  128. */
  129. public void addAttackRecord(AttackRecord record) {
  130. Log.i("DBHelper", "Add Attack Record with id: " + record.getAttack_id());
  131. SQLiteDatabase db = this.getWritableDatabase();
  132. ContentValues attackValues = new ContentValues();
  133. attackValues.put(AttackEntry.COLUMN_NAME_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  134. attackValues.put(AttackEntry.COLUMN_NAME_PROTOCOL, record.getProtocol().toString());
  135. attackValues.put(AttackEntry.COLUMN_NAME_EXTERNAL_IP, record.getExternalIP());
  136. attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_IP, record.getLocalIP()); // Log Local IP
  137. attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_PORT, record.getLocalPort());
  138. attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_IP, record.getRemoteIP()); // Log Remote IP
  139. attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
  140. attackValues.put(AttackEntry.COLUMN_NAME_BSSID, record.getBssid());
  141. // Inserting Rows
  142. db.insertWithOnConflict(AttackEntry.TABLE_NAME, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
  143. db.close(); // Closing database connection
  144. }
  145. public void updateSyncAttackCounter(AttackRecord record){
  146. SQLiteDatabase db = this.getWritableDatabase();
  147. String mac = HelperUtils.getMacAdress(context);
  148. ContentValues syncDeviceValues = new ContentValues();
  149. syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, mac);
  150. syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, System.currentTimeMillis());
  151. String query = "SELECT * FROM " + SyncInfoEntry.TABLE_NAME +
  152. " WHERE " + SyncInfoEntry.COLUMN_NAME_DEVICE_ID + " = ? " +
  153. "AND " + SyncInfoEntry.COLUMN_NAME_BSSID + " = ?";
  154. Cursor cursor = db.rawQuery(query, new String[] {mac, record.getBssid()});
  155. long attackCount = 0;
  156. long portscanCount = 0;
  157. if (cursor.moveToFirst()){
  158. attackCount = cursor.getLong(2);
  159. portscanCount = cursor.getLong(3);
  160. }
  161. if("PORTSCAN".equals(record.getProtocol())){
  162. portscanCount++;
  163. }else { attackCount++; }
  164. Log.i("DBHelper", "Update number of attack: " + attackCount);
  165. ContentValues synInfoValues = new ContentValues();
  166. synInfoValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, record.getBssid());
  167. synInfoValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, mac);
  168. synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, attackCount);
  169. synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, portscanCount);
  170. // Inserting Rows
  171. db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, synInfoValues, SQLiteDatabase.CONFLICT_REPLACE);
  172. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, syncDeviceValues, SQLiteDatabase.CONFLICT_REPLACE);
  173. db.close(); // Closing database connection
  174. }
  175. /**
  176. * Determines if a network with given BSSID has already been recorded as malicious.
  177. *
  178. * @param BSSID
  179. * The BSSID of the network.
  180. * @return True if an attack has been recorded in a network with the given
  181. * BSSID, else false.
  182. */
  183. public boolean bssidSeen(String BSSID) {
  184. String countQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
  185. SQLiteDatabase db = this.getReadableDatabase();
  186. Cursor cursor = db.rawQuery(countQuery, new String[] {BSSID});
  187. int result = cursor.getCount();
  188. cursor.close();
  189. db.close();
  190. return result > 0;
  191. }
  192. /**
  193. * Determines if an attack has been recorded on a specific protocol in a
  194. * network with a given BSSID.
  195. *
  196. * @param protocol
  197. * The
  198. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  199. * Protocol} to inspect.
  200. * @param BSSID
  201. * The BSSID of the network.
  202. * @return True if an attack on the given protocol has been recorded in a
  203. * network with the given BSSID, else false.
  204. */
  205. public boolean bssidSeen(String protocol, String BSSID) {
  206. if(BSSID == null || protocol == null){
  207. return false;
  208. }
  209. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " NATURAL JOIN " + NetworkEntry.TABLE_NAME + " WHERE "
  210. + AttackEntry.COLUMN_NAME_PROTOCOL + " = ? AND " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
  211. SQLiteDatabase db = this.getReadableDatabase();
  212. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, BSSID});
  213. int result = cursor.getCount();
  214. cursor.close();
  215. db.close();
  216. return result > 0;
  217. }
  218. /**
  219. * Returns a String array with all BSSIDs stored in the database.
  220. *
  221. * @return String[] of all recorded BSSIDs.
  222. */
  223. public String[] getAllBSSIDS() {
  224. String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME;
  225. SQLiteDatabase db = this.getReadableDatabase();
  226. Cursor cursor = db.rawQuery(selectQuery, null);
  227. String[] bssidList = new String[cursor.getCount()];
  228. int counter = 0;
  229. // looping through all rows and adding to list
  230. if (cursor.moveToFirst()) {
  231. do {
  232. bssidList[counter] = cursor.getString(0);
  233. counter++;
  234. } while (cursor.moveToNext());
  235. }
  236. cursor.close();
  237. db.close();
  238. return bssidList;
  239. }
  240. /**
  241. * Determines the number of different attacks in the database.
  242. *
  243. * @return The number of different attacks in the database.
  244. */
  245. public int getAttackCount() {
  246. SQLiteDatabase db = this.getReadableDatabase();
  247. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  248. " WHERE " + AttackEntry.COLUMN_NAME_PROTOCOL + " <> ?";
  249. Cursor cursor = db.rawQuery(countQuery, new String[]{"PORTSCAN"});
  250. int result = cursor.getCount();
  251. cursor.close();
  252. // return count
  253. db.close();
  254. return result;
  255. }
  256. /**
  257. * Determines the number of different recorded attacks in a specific access point since the given attack_id.
  258. * The given attack_id is not included.
  259. * @param attack_id The attack id to match the query against.
  260. * @param bssid The BSSID of the access point.
  261. * @return The number of different attacks in the database since the given attack_id.
  262. */
  263. public int getAttackCount(int attack_id, String bssid) {
  264. SQLiteDatabase db = this.getReadableDatabase();
  265. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  266. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " <> ? " +
  267. "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? " +
  268. "AND " + AttackEntry.COLUMN_NAME_BSSID + " = ?";
  269. String[] selectArgs = new String[]{"PORTSCAN", attack_id + "", bssid};
  270. Cursor cursor = db.rawQuery(countQuery, selectArgs);
  271. int result = cursor.getCount();
  272. cursor.close();
  273. // return count
  274. db.close();
  275. return result;
  276. }
  277. /**
  278. * Determines the number of different attacks for a specific protocol in
  279. * the database.
  280. *
  281. * @param protocol
  282. * The String representation of the
  283. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  284. * Protocol}
  285. * @return The number of different attacks in the database.
  286. */
  287. public int getAttackPerProtocolCount(String protocol) {
  288. SQLiteDatabase db = this.getReadableDatabase();
  289. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  290. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? ";
  291. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol});
  292. int result = cursor.getCount();
  293. cursor.close();
  294. // return count
  295. db.close();
  296. return result;
  297. }
  298. /**
  299. * Determines the number of attacks for a specific protocol in
  300. * the database since the given attack_id.
  301. *
  302. * @param protocol
  303. * The String representation of the
  304. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  305. * Protocol}
  306. * @param attack_id The attack id to match the query against.
  307. * @return The number of different attacks in the database since the given attack_id.
  308. */
  309. public int getAttackPerProtocolCount(String protocol, int attack_id) {
  310. SQLiteDatabase db = this.getReadableDatabase();
  311. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  312. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? " +
  313. "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? ";
  314. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, attack_id + ""});
  315. int result = cursor.getCount();
  316. cursor.close();
  317. // return count
  318. db.close();
  319. return result;
  320. }
  321. /**
  322. * Determines the number of recorded attacks for a specific protocol and accesss point since the given attack_id.
  323. *
  324. * @param protocol
  325. * The String representation of the
  326. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  327. * Protocol}
  328. * @param attack_id The attack id to match the query against.
  329. * @param bssid The BSSID of the access point.
  330. * @return The number of different attacks in the database since the given attack_id.
  331. */
  332. public int getAttackPerProtocolCount(String protocol, int attack_id, String bssid) {
  333. SQLiteDatabase db = this.getReadableDatabase();
  334. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  335. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? " +
  336. "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? " +
  337. "AND " + AttackEntry.COLUMN_NAME_BSSID + " = ?";
  338. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, attack_id + "", bssid});
  339. int result = cursor.getCount();
  340. cursor.close();
  341. // return count
  342. db.close();
  343. return result;
  344. }
  345. /**
  346. * Determines the number of portscans stored in the database.
  347. *
  348. * @return The number of portscans stored in the database.
  349. */
  350. public int getPortscanCount() {
  351. return getAttackPerProtocolCount("PORTSCAN");
  352. }
  353. /**
  354. * Determines the number of recorded portscans since the given attack_id.
  355. * @param attack_id The attack id to match the query against.
  356. * @return The number of portscans stored in the database since the given attack_id.
  357. */
  358. public int getPortscanCount(int attack_id) {
  359. return getAttackPerProtocolCount("PORTSCAN", attack_id);
  360. }
  361. /**
  362. * Determines the number of recorded portscans in a specific access point since the given attack_id.
  363. * @param attack_id The attack id to match the query against.
  364. * @param bssid The BSSID of the access point.
  365. * @return The number of portscans stored in the database since the given attack_id.
  366. */
  367. public int getPortscanCount(int attack_id, String bssid) {
  368. return getAttackPerProtocolCount("PORTSCAN", attack_id, bssid);
  369. }
  370. /**
  371. * Determines the number of {@link Record Records} in the database.
  372. *
  373. * @return The number of {@link Record Records} in the database.
  374. */
  375. public int getRecordCount() {
  376. String countQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME;
  377. SQLiteDatabase db = this.getReadableDatabase();
  378. Cursor cursor = db.rawQuery(countQuery, null);
  379. int result = cursor.getCount();
  380. cursor.close();
  381. // return count
  382. db.close();
  383. return result;
  384. }
  385. /**
  386. * Returns the {@link AttackRecord} with the given attack id from the database.
  387. *
  388. * @param attack_id
  389. * The attack id of the {@link Record};
  390. * @return The {@link Record}.
  391. */
  392. public AttackRecord getRecordOfAttackId(long attack_id) {
  393. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " WHERE " + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id;
  394. SQLiteDatabase db = this.getReadableDatabase();
  395. Cursor cursor = db.rawQuery(selectQuery, null);
  396. AttackRecord record = null;
  397. if (cursor.moveToFirst()) {
  398. record = createAttackRecord(cursor);
  399. }
  400. cursor.close();
  401. // return record list
  402. db.close();
  403. return record;
  404. }
  405. /**
  406. * Gets a {@link AttackRecord} for every attack identified by its attack id.
  407. *
  408. * @return A ArrayList with a {@link AttackRecord} for each attack id in the Database.
  409. */
  410. public ArrayList<AttackRecord> getRecordOfEachAttack() {
  411. ArrayList<AttackRecord> recordList = new ArrayList<AttackRecord>();
  412. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME;
  413. SQLiteDatabase db = this.getReadableDatabase();
  414. Cursor cursor = db.rawQuery(selectQuery, null);
  415. // looping through all rows and adding to list
  416. if (cursor.moveToFirst()) {
  417. do {
  418. AttackRecord record = createAttackRecord(cursor);
  419. // Adding record to list
  420. recordList.add(record);
  421. } while (cursor.moveToNext());
  422. }
  423. cursor.close();
  424. // return record list
  425. db.close();
  426. return recordList;
  427. }
  428. /**
  429. * Gets a AttackRecord for every attack with a higher attack id than the specified.
  430. *
  431. * @param attack_id
  432. * The attack id to match the query against.
  433. * @return A ArrayList with one {@link AttackRecord} for each attack id
  434. * higher than the given.
  435. */
  436. public ArrayList<AttackRecord> getRecordOfEachAttack(long attack_id) {
  437. ArrayList<AttackRecord> recordList = new ArrayList<AttackRecord>();
  438. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " WHERE " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > " + attack_id;
  439. SQLiteDatabase db = this.getReadableDatabase();
  440. Cursor cursor = db.rawQuery(selectQuery, null);
  441. // looping through all rows and adding to list
  442. if (cursor.moveToFirst()) {
  443. do {
  444. AttackRecord record = createAttackRecord(cursor);
  445. // Adding record to list
  446. recordList.add(record);
  447. } while (cursor.moveToNext());
  448. }
  449. cursor.close();
  450. // return count
  451. db.close();
  452. return recordList;
  453. }
  454. /**
  455. * Determines the highest attack id stored in the database.
  456. *
  457. * @return The highest attack id stored in the database.
  458. */
  459. public long getHighestAttackId() {
  460. String selectQuery = "SELECT MAX(" + AttackEntry.COLUMN_NAME_ATTACK_ID + ") FROM " + AttackEntry.TABLE_NAME;
  461. SQLiteDatabase db = this.getReadableDatabase();
  462. Cursor cursor = db.rawQuery(selectQuery, null);
  463. int result;
  464. if (cursor.moveToFirst()) {
  465. result = cursor.getInt(0);
  466. } else {
  467. result = -1;
  468. }
  469. cursor.close();
  470. db.close();
  471. return result;
  472. }
  473. /**
  474. * Determines the smallest attack id stored in the database.
  475. *
  476. * @return The smallest attack id stored in the database.
  477. */
  478. public long getSmallestAttackId() {
  479. String selectQuery = "SELECT MIN(" + AttackEntry.COLUMN_NAME_ATTACK_ID + ") FROM " + AttackEntry.TABLE_NAME;
  480. SQLiteDatabase db = this.getReadableDatabase();
  481. Cursor cursor = db.rawQuery(selectQuery, null);
  482. int result;
  483. if (cursor.moveToFirst()) {
  484. result = cursor.getInt(0);
  485. } else {
  486. result = -1;
  487. }
  488. cursor.close();
  489. db.close();
  490. return result;
  491. }
  492. /**
  493. * Gets the last recorded SSID to a given BSSID.
  494. *
  495. * @param bssid
  496. * The BSSID to match against.
  497. * @return A String of the last SSID or null if the BSSID is not in the
  498. * database.
  499. */
  500. public String getSSID(String bssid) {
  501. String selectQuery = "SELECT " + NetworkEntry.COLUMN_NAME_SSID + " FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID
  502. + " = " + "'" + bssid + "'";
  503. SQLiteDatabase db = this.getReadableDatabase();
  504. Cursor cursor = db.rawQuery(selectQuery, null);
  505. String ssid = null;
  506. if (cursor.moveToFirst()) {
  507. ssid = cursor.getString(0);
  508. }
  509. cursor.close();
  510. db.close();
  511. return ssid;
  512. }
  513. public ArrayList<NetworkRecord> getNetworkInformation() {
  514. String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME;
  515. SQLiteDatabase db = this.getReadableDatabase();
  516. Cursor cursor = db.rawQuery(selectQuery, null);
  517. ArrayList<NetworkRecord> networkInformation = new ArrayList<NetworkRecord>();
  518. // looping through all rows and adding to list
  519. if (cursor.moveToFirst()) {
  520. do {
  521. NetworkRecord record = new NetworkRecord();
  522. record.setBssid(cursor.getString(0));
  523. record.setSsid(cursor.getString(1));
  524. record.setLatitude(Double.parseDouble(cursor.getString(2)));
  525. record.setLongitude(Double.parseDouble(cursor.getString(3)));
  526. record.setAccuracy(Float.parseFloat(cursor.getString(4)));
  527. record.setTimestampLocation(cursor.getLong(5));
  528. networkInformation.add(record);
  529. } while (cursor.moveToNext());
  530. }
  531. cursor.close();
  532. db.close();
  533. return networkInformation;
  534. }
  535. public void updateNetworkInformation(ArrayList<NetworkRecord> networkInformation) {
  536. Log.i("DatabaseHandler", "Starte updating");
  537. for (NetworkRecord record : networkInformation) {
  538. updateNetworkInformation(record);
  539. }
  540. }
  541. public void updateNetworkInformation(NetworkRecord record) {
  542. SQLiteDatabase db = this.getReadableDatabase();
  543. String bssid = record.getBssid();
  544. String bssidQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
  545. Cursor cursor = db.rawQuery(bssidQuery, new String[] {bssid});
  546. if (!cursor.moveToFirst() || cursor.getLong(5) < record.getTimestampLocation()){
  547. ContentValues bssidValues = new ContentValues();
  548. bssidValues.put(NetworkEntry.COLUMN_NAME_BSSID, bssid);
  549. bssidValues.put(NetworkEntry.COLUMN_NAME_SSID, record.getSsid());
  550. bssidValues.put(NetworkEntry.COLUMN_NAME_LATITUDE, record.getLatitude());
  551. bssidValues.put(NetworkEntry.COLUMN_NAME_LONGITUDE, record.getLongitude());
  552. bssidValues.put(NetworkEntry.COLUMN_NAME_ACCURACY, record.getAccuracy());
  553. bssidValues.put(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP, record.getTimestampLocation());
  554. db.insertWithOnConflict(NetworkEntry.TABLE_NAME, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
  555. }
  556. cursor.close();
  557. db.close();
  558. }
  559. public void updateSyncDevices(HashMap<String, Long> devices){
  560. SQLiteDatabase db = this.getReadableDatabase();
  561. for(String key : devices.keySet()){
  562. ContentValues deviceValues = new ContentValues();
  563. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, key);
  564. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, devices.get(key));
  565. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
  566. }
  567. db.close();
  568. }
  569. /**
  570. * Returns a HashMap of all devices that were previously synchronized with.
  571. * @return HashMap containing device id's and the last synchronization timestamp.
  572. */
  573. public HashMap<String, Long> getSyncDevices(){
  574. SQLiteDatabase db = this.getReadableDatabase();
  575. HashMap<String, Long> devices = new HashMap<String, Long>();
  576. String query = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
  577. Cursor cursor = db.rawQuery(query, null);
  578. if (cursor.moveToFirst()) {
  579. do {
  580. devices.put(cursor.getString(0), cursor.getLong(1));
  581. } while (cursor.moveToNext());
  582. }
  583. cursor.close();
  584. db.close();
  585. return devices;
  586. }
  587. /**
  588. * Returns a ArrayList containing all information stored in the SyncInfo table.
  589. * @return ArrayList<SyncInfo>
  590. */
  591. public ArrayList<SyncInfoRecord> getSyncInfo(){
  592. SQLiteDatabase db = this.getReadableDatabase();
  593. ArrayList<SyncInfoRecord> syncInfo = new ArrayList<SyncInfoRecord>();
  594. String query = "SELECT * FROM " + SyncInfoEntry.TABLE_NAME;
  595. Cursor cursor = db.rawQuery(query, null);
  596. if (cursor.moveToFirst()) {
  597. do {
  598. SyncInfoRecord info = new SyncInfoRecord();
  599. info.setDeviceID(cursor.getString(0));
  600. info.setBSSID(cursor.getString(1));
  601. info.setNumber_of_attacks(cursor.getLong(2));
  602. info.setNumber_of_portscans(cursor.getLong(3));
  603. syncInfo.add(info);
  604. } while (cursor.moveToNext());
  605. }
  606. cursor.close();
  607. db.close();
  608. return syncInfo;
  609. }
  610. public void updateSyncInfo(ArrayList<SyncInfoRecord> syncInfo){
  611. for(SyncInfoRecord info : syncInfo){
  612. updateSyncInfo(info);
  613. }
  614. }
  615. public void updateSyncInfo(SyncInfoRecord syncInfo){
  616. SQLiteDatabase db = this.getReadableDatabase();
  617. ContentValues syncValues = new ContentValues();
  618. syncValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, syncInfo.getBSSID());
  619. syncValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, syncInfo.getDeviceID());
  620. syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, syncInfo.getNumber_of_attacks());
  621. syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, syncInfo.getNumber_of_portscans());
  622. db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, syncValues, SQLiteDatabase.CONFLICT_REPLACE);
  623. db.close();
  624. }
  625. /**
  626. * Deletes a device with given id from the device {@link SyncDeviceEntry.TABLE_NAME} and also all data captured by this device in {@link SyncInfoEntry.TABLE_NAME}
  627. * @param device_id The id of the device that is to be deleted.
  628. */
  629. public void clearSyncInfos(){
  630. SQLiteDatabase db = this.getReadableDatabase();
  631. db.delete(SyncDeviceEntry.TABLE_NAME, null, null);
  632. db.delete(SyncInfoEntry.TABLE_NAME, null, null);
  633. db.close();
  634. }
  635. /**
  636. * Deletes all records from {@link #PacketEntry.TABLE_NAME}.
  637. */
  638. public void clearData() {
  639. SQLiteDatabase db = this.getReadableDatabase();
  640. db.delete(PacketEntry.TABLE_NAME, null, null);
  641. db.delete(AttackEntry.TABLE_NAME, null, null);
  642. db.close();
  643. }
  644. /**
  645. * Deletes all records from {@link #PacketEntry.TABLE_NAME} with a specific BSSID.
  646. *
  647. * @param bssid
  648. * The BSSID to match against.
  649. */
  650. public void deleteByBSSID(String bssid) {
  651. SQLiteDatabase db = this.getReadableDatabase();
  652. db.delete(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID + " = ?", new String[] { bssid });
  653. db.delete(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_BSSID + " = ?", new String[] { bssid });
  654. db.close();
  655. }
  656. /**
  657. * Deletes all records from {@link #PacketEntry.TABLE_NAME} with a time stamp smaller
  658. * then the given
  659. *
  660. * @param date
  661. * A Date represented in milliseconds.
  662. */
  663. public void deleteByDate(long date) {
  664. SQLiteDatabase db = this.getReadableDatabase();
  665. String deleteQuery = "DELETE FROM " + PacketEntry.TABLE_NAME + " WHERE " + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP + " < " + date;
  666. db.execSQL(deleteQuery);
  667. db.close();
  668. }
  669. /**
  670. * Creates a {@link Record} from a Cursor. If the cursor does not show to a
  671. * valid data structure a runtime exception is thrown.
  672. *
  673. * @param cursor
  674. * @return Returns the created {@link Record} .
  675. */
  676. private MessageRecord createMessageRecord(Cursor cursor) {
  677. MessageRecord record = new MessageRecord();
  678. record.setId(Integer.parseInt(cursor.getString(0)));
  679. record.setAttack_id(cursor.getLong(1));
  680. record.setType(MessageRecord.TYPE.valueOf(cursor.getString(2)));
  681. record.setTimestamp(cursor.getLong(3));
  682. record.setPacket(cursor.getString(4));
  683. return record;
  684. }
  685. /**
  686. * Creates a {@link AttackRecord} from a Cursor. If the cursor does not show to a
  687. * valid data structure a runtime exception is thrown.
  688. *
  689. * @param cursor
  690. * @return Returns the created {@link Record} .
  691. */
  692. private AttackRecord createAttackRecord(Cursor cursor) {
  693. AttackRecord record = new AttackRecord();
  694. record.setAttack_id(cursor.getLong(0));
  695. record.setProtocol(cursor.getString(1));
  696. record.setExternalIP(cursor.getString(2));
  697. record.setLocalIP(cursor.getString(3));
  698. record.setLocalPort(Integer.parseInt(cursor.getString(4)));
  699. record.setRemoteIP(cursor.getString(5));
  700. record.setRemotePort(Integer.parseInt(cursor.getString(6)));
  701. record.setBssid(cursor.getString(11));
  702. return record;
  703. }
  704. }