HostageDBOpenHelper.java 30 KB

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