HostageDBOpenHelper.java 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. package de.tudarmstadt.informatik.hostage.persistence;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import android.content.ContentValues;
  7. import android.content.Context;
  8. import android.database.Cursor;
  9. import android.database.sqlite.SQLiteDatabase;
  10. import android.database.sqlite.SQLiteOpenHelper;
  11. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  12. import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
  13. import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
  14. import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
  15. import de.tudarmstadt.informatik.hostage.logging.Record;
  16. import de.tudarmstadt.informatik.hostage.logging.SyncInfoRecord;
  17. import de.tudarmstadt.informatik.hostage.logging.MessageRecord.TYPE;
  18. import de.tudarmstadt.informatik.hostage.model.Profile;
  19. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.AttackEntry;
  20. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.NetworkEntry;
  21. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEntry;
  22. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.ProfileEntry;
  23. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncDeviceEntry;
  24. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncInfoEntry;
  25. import de.tudarmstadt.informatik.hostage.ui.LogFilter;
  26. public class HostageDBOpenHelper extends SQLiteOpenHelper {
  27. private static final String DATABASE_NAME = "hostage.db";
  28. private static final int DATABASE_VERSION = 1;
  29. private Context context;
  30. static {
  31. StringBuilder networkSQLBuilder = new StringBuilder("CREATE TABLE ").append(NetworkEntry.TABLE_NAME).append("(");
  32. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_BSSID).append(" TEXT PRIMARY KEY,");
  33. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_SSID).append(" TEXT,");
  34. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_LATITUDE).append(" INTEGER,");
  35. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_LONGITUDE).append(" INTEGER,");
  36. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_ACCURACY).append(" INTEGER,");
  37. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP).append(" INTEGER");
  38. networkSQLBuilder.append(")");
  39. SQL_CREATE_NETWORK_ENTRIES = networkSQLBuilder.toString();
  40. StringBuilder attackSQLBuilder = new StringBuilder("CREATE TABLE ").append(AttackEntry.TABLE_NAME).append("(");
  41. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_ATTACK_ID).append(" INTEGER PRIMARY KEY,");
  42. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_PROTOCOL).append(" TEXT,");
  43. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_EXTERNAL_IP).append(" TEXT,");
  44. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_LOCAL_IP).append(" BLOB,");
  45. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_LOCAL_PORT).append(" INTEGER,");
  46. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_REMOTE_IP).append(" BLOB,");
  47. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_REMOTE_PORT).append(" INTEGER,");
  48. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_BSSID).append(" TEXT,");
  49. attackSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", AttackEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME,
  50. NetworkEntry.COLUMN_NAME_BSSID));
  51. attackSQLBuilder.append(")");
  52. SQL_CREATE_ATTACK_ENTRIES = attackSQLBuilder.toString();
  53. StringBuilder packetSQLBuilder = new StringBuilder("CREATE TABLE ").append(PacketEntry.TABLE_NAME).append("(");
  54. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_ID).append(" INTEGER NOT NULL,");
  55. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_ATTACK_ID).append(" INTEGER NOT NULL,");
  56. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_TYPE).append(" TEXT,");
  57. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP).append(" INTEGER,");
  58. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_PACKET).append(" TEXT,");
  59. packetSQLBuilder.append(String.format("PRIMARY KEY(%s,%s)", PacketEntry.COLUMN_NAME_ID, PacketEntry.COLUMN_NAME_ATTACK_ID));
  60. packetSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", PacketEntry.COLUMN_NAME_ATTACK_ID, AttackEntry.TABLE_NAME,
  61. AttackEntry.COLUMN_NAME_ATTACK_ID));
  62. packetSQLBuilder.append(")");
  63. SQL_CREATE_PACKET_ENTRIES = packetSQLBuilder.toString();
  64. StringBuilder syncDevicesSQLBuilder = new StringBuilder("CREATE TABLE ").append(SyncDeviceEntry.TABLE_NAME).append("(");
  65. syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID).append(" TEXT PRIMARY KEY,");
  66. syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP).append(" INTEGER");
  67. syncDevicesSQLBuilder.append(")");
  68. SQL_CREATE_SYNC_DEVICES_ENTRIES = syncDevicesSQLBuilder.toString();
  69. StringBuilder syncInfoSQLBuilder = new StringBuilder("CREATE TABLE ").append(SyncInfoEntry.TABLE_NAME).append("(");
  70. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_DEVICE_ID).append(" TEXT,");
  71. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_BSSID).append(" TEXT,");
  72. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS).append(" INTEGER,");
  73. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS).append(" INTEGER,");
  74. syncInfoSQLBuilder.append(String.format("PRIMARY KEY(%s,%s)", SyncInfoEntry.COLUMN_NAME_DEVICE_ID, SyncInfoEntry.COLUMN_NAME_BSSID));
  75. syncInfoSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", SyncInfoEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME,
  76. NetworkEntry.COLUMN_NAME_BSSID));
  77. syncInfoSQLBuilder.append(")");
  78. SQL_CREATE_SYNC_INFO_ENTRIES = syncInfoSQLBuilder.toString();
  79. StringBuilder profilSQLBuilder = new StringBuilder("CREATE TABLE ").append(ProfileEntry.TABLE_NAME).append("(");
  80. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT,");
  81. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_NAME).append(" TEXT,");
  82. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_DESCRIPTION ).append(" TEXT,");
  83. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ICON).append(" TEXT,");
  84. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ICON_NAME).append(" TEXT,");
  85. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_EDITABLE).append(" INTEGER,");
  86. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ACTIVE).append(" INTEGER");
  87. profilSQLBuilder.append(")");
  88. SQL_CREATE_PROFIL_ENTRIES = profilSQLBuilder.toString();
  89. }
  90. private static final String SQL_CREATE_NETWORK_ENTRIES;
  91. private static final String SQL_CREATE_ATTACK_ENTRIES;
  92. private static final String SQL_CREATE_PACKET_ENTRIES;
  93. private static final String SQL_CREATE_PROFIL_ENTRIES;
  94. private static final String SQL_CREATE_SYNC_DEVICES_ENTRIES;
  95. private static final String SQL_CREATE_SYNC_INFO_ENTRIES;
  96. private static final String SQL_DELETE_PACKET_ENTRIES = "DROP TABLE IF EXISTS " + PacketEntry.TABLE_NAME;
  97. private static final String SQL_DELETE_ATTACK_ENTRIES = "DROP TABLE IF EXISTS " + AttackEntry.TABLE_NAME;
  98. private static final String SQL_DELETE_NETWORK_ENTRIES = "DROP TABLE IF EXISTS " + NetworkEntry.TABLE_NAME;
  99. private static final String SQL_DELETE_PROFIL_ENTRIES = "DROP TABLE IF EXISTS " + ProfileEntry.TABLE_NAME;
  100. private static final String SQL_DELETE_SYNC_DEVICES_ENTRIES = "DROP TABLE IF EXISTS " + SyncDeviceEntry.TABLE_NAME;
  101. private static final String SQL_DELETE_SYNC_INFO_ENTRIES = "DROP TABLE IF EXISTS " + SyncInfoEntry.TABLE_NAME;
  102. public HostageDBOpenHelper(Context context) {
  103. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  104. this.context = context;
  105. }
  106. @Override
  107. public void onCreate(SQLiteDatabase db) {
  108. db.execSQL(SQL_CREATE_NETWORK_ENTRIES);
  109. db.execSQL(SQL_CREATE_ATTACK_ENTRIES);
  110. db.execSQL(SQL_CREATE_PACKET_ENTRIES);
  111. db.execSQL(SQL_CREATE_PROFIL_ENTRIES);
  112. db.execSQL(SQL_CREATE_SYNC_DEVICES_ENTRIES);
  113. db.execSQL(SQL_CREATE_SYNC_INFO_ENTRIES);
  114. }
  115. @Override
  116. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  117. db.execSQL(SQL_DELETE_SYNC_INFO_ENTRIES);
  118. db.execSQL(SQL_DELETE_PACKET_ENTRIES);
  119. db.execSQL(SQL_DELETE_ATTACK_ENTRIES);
  120. db.execSQL(SQL_DELETE_PROFIL_ENTRIES);
  121. db.execSQL(SQL_DELETE_NETWORK_ENTRIES);
  122. db.execSQL(SQL_DELETE_SYNC_DEVICES_ENTRIES);
  123. onCreate(db);
  124. }
  125. /**
  126. * Adds a given {@link MessageRecord} to the database.
  127. *
  128. * @param record
  129. * The added {@link MessageRecord} .
  130. */
  131. public void addMessageRecord(MessageRecord record) {
  132. SQLiteDatabase db = this.getWritableDatabase();
  133. ContentValues recordValues = new ContentValues();
  134. recordValues.put(PacketEntry.COLUMN_NAME_ID, record.getId()); // Log Message Number
  135. recordValues.put(PacketEntry.COLUMN_NAME_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  136. recordValues.put(PacketEntry.COLUMN_NAME_TYPE, record.getType().name()); // Log Type
  137. recordValues.put(PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP, record.getTimestamp()); // Log Timestamp
  138. recordValues.put(PacketEntry.COLUMN_NAME_PACKET, record.getPacket()); // Log Packet
  139. // Inserting Rows
  140. db.insert(PacketEntry.TABLE_NAME, null, recordValues);
  141. db.close(); // Closing database connection
  142. }
  143. /**
  144. * Adds a given {@link AttackRecord} to the database.
  145. *
  146. * @param record
  147. * The added {@link AttackRecord} .
  148. */
  149. public void addAttackRecord(AttackRecord record) {
  150. SQLiteDatabase db = this.getWritableDatabase();
  151. ContentValues attackValues = new ContentValues();
  152. attackValues.put(AttackEntry.COLUMN_NAME_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  153. attackValues.put(AttackEntry.COLUMN_NAME_PROTOCOL, record.getProtocol().toString());
  154. attackValues.put(AttackEntry.COLUMN_NAME_EXTERNAL_IP, record.getExternalIP());
  155. attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_IP, record.getLocalIP()); // Log Local IP
  156. attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_PORT, record.getLocalPort());
  157. attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_IP, record.getRemoteIP()); // Log Remote IP
  158. attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
  159. attackValues.put(AttackEntry.COLUMN_NAME_BSSID, record.getBssid());
  160. // Inserting Rows
  161. db.insertWithOnConflict(AttackEntry.TABLE_NAME, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
  162. db.close(); // Closing database connection
  163. }
  164. public void updateSyncAttackCounter(AttackRecord record){
  165. SQLiteDatabase db = this.getWritableDatabase();
  166. String mac = HelperUtils.getMacAdress(context);
  167. ContentValues syncDeviceValues = new ContentValues();
  168. syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, mac);
  169. syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, System.currentTimeMillis());
  170. String query = "SELECT * FROM " + SyncInfoEntry.TABLE_NAME +
  171. " WHERE " + SyncInfoEntry.COLUMN_NAME_DEVICE_ID + " = ? " +
  172. "AND " + SyncInfoEntry.COLUMN_NAME_BSSID + " = ?";
  173. Cursor cursor = db.rawQuery(query, new String[] {mac, record.getBssid()});
  174. long attackCount = 0;
  175. long portscanCount = 0;
  176. if (cursor.moveToFirst()){
  177. attackCount = cursor.getLong(2);
  178. portscanCount = cursor.getLong(3);
  179. }
  180. if("PORTSCAN".equals(record.getProtocol())){
  181. portscanCount++;
  182. }else { attackCount++; }
  183. ContentValues synInfoValues = new ContentValues();
  184. synInfoValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, record.getBssid());
  185. synInfoValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, mac);
  186. synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, attackCount);
  187. synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, portscanCount);
  188. // Inserting Rows
  189. db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, synInfoValues, SQLiteDatabase.CONFLICT_REPLACE);
  190. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, syncDeviceValues, SQLiteDatabase.CONFLICT_REPLACE);
  191. db.close(); // Closing database connection
  192. }
  193. /**
  194. * Determines if a network with given BSSID has already been recorded as malicious.
  195. *
  196. * @param BSSID
  197. * The BSSID of the network.
  198. * @return True if an attack has been recorded in a network with the given
  199. * BSSID, else false.
  200. */
  201. public boolean bssidSeen(String BSSID) {
  202. String countQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
  203. SQLiteDatabase db = this.getReadableDatabase();
  204. Cursor cursor = db.rawQuery(countQuery, new String[] {BSSID});
  205. int result = cursor.getCount();
  206. cursor.close();
  207. db.close();
  208. return result > 0;
  209. }
  210. /**
  211. * Determines if an attack has been recorded on a specific protocol in a
  212. * network with a given BSSID.
  213. *
  214. * @param protocol
  215. * The
  216. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  217. * Protocol} to inspect.
  218. * @param BSSID
  219. * The BSSID of the network.
  220. * @return True if an attack on the given protocol has been recorded in a
  221. * network with the given BSSID, else false.
  222. */
  223. public boolean bssidSeen(String protocol, String BSSID) {
  224. if(BSSID == null || protocol == null){
  225. return false;
  226. }
  227. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " NATURAL JOIN " + NetworkEntry.TABLE_NAME + " WHERE "
  228. + AttackEntry.COLUMN_NAME_PROTOCOL + " = ? AND " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
  229. SQLiteDatabase db = this.getReadableDatabase();
  230. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, BSSID});
  231. int result = cursor.getCount();
  232. cursor.close();
  233. db.close();
  234. return result > 0;
  235. }
  236. public int numBssidSeen(String BSSID) {
  237. String countQuery = "SELECT COUNT(*) FROM " + AttackEntry.TABLE_NAME + " WHERE "
  238. + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_BSSID + " = " + "'" + BSSID + "'";
  239. SQLiteDatabase db = this.getReadableDatabase();
  240. Cursor cursor = db.rawQuery(countQuery, null);
  241. cursor.moveToFirst();
  242. int result = cursor.getInt(0);
  243. cursor.close();
  244. db.close();
  245. return result;
  246. }
  247. public int numBssidSeen(String protocol, String BSSID) {
  248. String countQuery = "SELECT COUNT(*) FROM " + AttackEntry.TABLE_NAME
  249. + " WHERE " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_PROTOCOL + " = " + "'" + protocol + "'"
  250. + " AND " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_BSSID + " = " + "'" + BSSID + "'";
  251. SQLiteDatabase db = this.getReadableDatabase();
  252. Cursor cursor = db.rawQuery(countQuery, null);
  253. cursor.moveToFirst();
  254. int result = cursor.getInt(0);
  255. cursor.close();
  256. db.close();
  257. return result;
  258. }
  259. /**
  260. * Returns a String array with all BSSIDs stored in the database.
  261. *
  262. * @return String[] of all recorded BSSIDs.
  263. */
  264. public String[] getAllBSSIDS() {
  265. String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME;
  266. SQLiteDatabase db = this.getReadableDatabase();
  267. Cursor cursor = db.rawQuery(selectQuery, null);
  268. String[] bssidList = new String[cursor.getCount()];
  269. int counter = 0;
  270. // looping through all rows and adding to list
  271. if (cursor.moveToFirst()) {
  272. do {
  273. bssidList[counter] = cursor.getString(0);
  274. counter++;
  275. } while (cursor.moveToNext());
  276. }
  277. cursor.close();
  278. db.close();
  279. return bssidList;
  280. }
  281. /**
  282. * Determines the number of different attacks in the database.
  283. *
  284. * @return The number of different attacks in the database.
  285. */
  286. public int getAttackCount() {
  287. SQLiteDatabase db = this.getReadableDatabase();
  288. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  289. " WHERE " + AttackEntry.COLUMN_NAME_PROTOCOL + " <> ?";
  290. Cursor cursor = db.rawQuery(countQuery, new String[]{"PORTSCAN"});
  291. int result = cursor.getCount();
  292. cursor.close();
  293. // return count
  294. db.close();
  295. return result;
  296. }
  297. /**
  298. * Determines the number of different recorded attacks in a specific access point since the given attack_id.
  299. * The given attack_id is not included.
  300. * @param attack_id The attack id to match the query against.
  301. * @param bssid The BSSID of the access point.
  302. * @return The number of different attacks in the database since the given attack_id.
  303. */
  304. public int getAttackCount(int attack_id, String bssid) {
  305. SQLiteDatabase db = this.getReadableDatabase();
  306. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  307. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " <> ? " +
  308. "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? " +
  309. "AND " + AttackEntry.COLUMN_NAME_BSSID + " = ?";
  310. String[] selectArgs = new String[]{"PORTSCAN", attack_id + "", bssid};
  311. Cursor cursor = db.rawQuery(countQuery, selectArgs);
  312. int result = cursor.getCount();
  313. cursor.close();
  314. // return count
  315. db.close();
  316. return result;
  317. }
  318. /**
  319. * Determines the number of different attacks for a specific protocol in
  320. * the database.
  321. *
  322. * @param protocol
  323. * The String representation of the
  324. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  325. * Protocol}
  326. * @return The number of different attacks in the database.
  327. */
  328. public int getAttackPerProtocolCount(String protocol) {
  329. SQLiteDatabase db = this.getReadableDatabase();
  330. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  331. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? ";
  332. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol});
  333. int result = cursor.getCount();
  334. cursor.close();
  335. // return count
  336. db.close();
  337. return result;
  338. }
  339. /**
  340. * Determines the number of attacks for a specific protocol in
  341. * the database since the given attack_id.
  342. *
  343. * @param protocol
  344. * The String representation of the
  345. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  346. * Protocol}
  347. * @param attack_id The attack id to match the query against.
  348. * @return The number of different attacks in the database since the given attack_id.
  349. */
  350. public int getAttackPerProtocolCount(String protocol, int attack_id) {
  351. SQLiteDatabase db = this.getReadableDatabase();
  352. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  353. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? " +
  354. "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? ";
  355. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, attack_id + ""});
  356. int result = cursor.getCount();
  357. cursor.close();
  358. // return count
  359. db.close();
  360. return result;
  361. }
  362. /**
  363. * Determines the number of recorded attacks for a specific protocol and accesss point since the given attack_id.
  364. *
  365. * @param protocol
  366. * The String representation of the
  367. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  368. * Protocol}
  369. * @param attack_id The attack id to match the query against.
  370. * @param bssid The BSSID of the access point.
  371. * @return The number of different attacks in the database since the given attack_id.
  372. */
  373. public int getAttackPerProtocolCount(String protocol, int attack_id, String bssid) {
  374. SQLiteDatabase db = this.getReadableDatabase();
  375. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  376. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? " +
  377. "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? " +
  378. "AND " + AttackEntry.COLUMN_NAME_BSSID + " = ?";
  379. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, attack_id + "", bssid});
  380. int result = cursor.getCount();
  381. cursor.close();
  382. // return count
  383. db.close();
  384. return result;
  385. }
  386. /**
  387. * Determines the number of portscans stored in the database.
  388. *
  389. * @return The number of portscans stored in the database.
  390. */
  391. public int getPortscanCount() {
  392. return getAttackPerProtocolCount("PORTSCAN");
  393. }
  394. /**
  395. * Determines the number of recorded portscans since the given attack_id.
  396. * @param attack_id The attack id to match the query against.
  397. * @return The number of portscans stored in the database since the given attack_id.
  398. */
  399. public int getPortscanCount(int attack_id) {
  400. return getAttackPerProtocolCount("PORTSCAN", attack_id);
  401. }
  402. /**
  403. * Determines the number of recorded portscans in a specific access point since the given attack_id.
  404. * @param attack_id The attack id to match the query against.
  405. * @param bssid The BSSID of the access point.
  406. * @return The number of portscans stored in the database since the given attack_id.
  407. */
  408. public int getPortscanCount(int attack_id, String bssid) {
  409. return getAttackPerProtocolCount("PORTSCAN", attack_id, bssid);
  410. }
  411. /**
  412. * Determines the number of {@link Record Records} in the database.
  413. *
  414. * @return The number of {@link Record Records} in the database.
  415. */
  416. public int getRecordCount() {
  417. String countQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME;
  418. SQLiteDatabase db = this.getReadableDatabase();
  419. Cursor cursor = db.rawQuery(countQuery, null);
  420. int result = cursor.getCount();
  421. cursor.close();
  422. // return count
  423. db.close();
  424. return result;
  425. }
  426. //TODO ADD AGAIN ?
  427. /**
  428. * Returns the {@link AttackRecord} with the given attack id from the database.
  429. *
  430. * @param attack_id
  431. * The attack id of the {@link Record};
  432. * @return The {@link Record}.
  433. */
  434. /*
  435. public AttackRecord getRecordOfAttackId(long attack_id) {
  436. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " WHERE " + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id;
  437. SQLiteDatabase db = this.getReadableDatabase();
  438. Cursor cursor = db.rawQuery(selectQuery, null);
  439. AttackRecord record = null;
  440. if (cursor.moveToFirst()) {
  441. record = createAttackRecord(cursor);
  442. }
  443. cursor.close();
  444. // return record list
  445. db.close();
  446. return record;
  447. } */
  448. /**
  449. * Gets a {@link AttackRecord} for every attack identified by its attack id.
  450. *
  451. * @return A ArrayList with a {@link AttackRecord} for each attack id in the Database.
  452. */
  453. public ArrayList<AttackRecord> getRecordOfEachAttack() {
  454. ArrayList<AttackRecord> recordList = new ArrayList<AttackRecord>();
  455. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME;
  456. SQLiteDatabase db = this.getReadableDatabase();
  457. Cursor cursor = db.rawQuery(selectQuery, null);
  458. // looping through all rows and adding to list
  459. if (cursor.moveToFirst()) {
  460. do {
  461. AttackRecord record = createAttackRecord(cursor);
  462. // Adding record to list
  463. recordList.add(record);
  464. } while (cursor.moveToNext());
  465. }
  466. cursor.close();
  467. // return record list
  468. db.close();
  469. return recordList;
  470. }
  471. /**
  472. * Gets a AttackRecord for every attack with a higher attack id than the specified.
  473. *
  474. * @param attack_id
  475. * The attack id to match the query against.
  476. * @return A ArrayList with one {@link AttackRecord} for each attack id
  477. * higher than the given.
  478. */
  479. public ArrayList<AttackRecord> getRecordOfEachAttack(long attack_id) {
  480. ArrayList<AttackRecord> recordList = new ArrayList<AttackRecord>();
  481. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " WHERE " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > " + attack_id;
  482. SQLiteDatabase db = this.getReadableDatabase();
  483. Cursor cursor = db.rawQuery(selectQuery, null);
  484. // looping through all rows and adding to list
  485. if (cursor.moveToFirst()) {
  486. do {
  487. AttackRecord record = createAttackRecord(cursor);
  488. // Adding record to list
  489. recordList.add(record);
  490. } while (cursor.moveToNext());
  491. }
  492. cursor.close();
  493. // return count
  494. db.close();
  495. return recordList;
  496. }
  497. /**
  498. * Determines the highest attack id stored in the database.
  499. *
  500. * @return The highest attack id stored in the database.
  501. */
  502. public long getHighestAttackId() {
  503. String selectQuery = "SELECT MAX(" + AttackEntry.COLUMN_NAME_ATTACK_ID + ") FROM " + AttackEntry.TABLE_NAME;
  504. SQLiteDatabase db = this.getReadableDatabase();
  505. Cursor cursor = db.rawQuery(selectQuery, null);
  506. int result;
  507. if (cursor.moveToFirst()) {
  508. result = cursor.getInt(0);
  509. } else {
  510. result = -1;
  511. }
  512. cursor.close();
  513. db.close();
  514. return result;
  515. }
  516. /**
  517. * Determines the smallest attack id stored in the database.
  518. *
  519. * @return The smallest attack id stored in the database.
  520. */
  521. public long getSmallestAttackId() {
  522. String selectQuery = "SELECT MIN(" + AttackEntry.COLUMN_NAME_ATTACK_ID + ") FROM " + AttackEntry.TABLE_NAME;
  523. SQLiteDatabase db = this.getReadableDatabase();
  524. Cursor cursor = db.rawQuery(selectQuery, null);
  525. int result;
  526. if (cursor.moveToFirst()) {
  527. result = cursor.getInt(0);
  528. } else {
  529. result = -1;
  530. }
  531. cursor.close();
  532. db.close();
  533. return result;
  534. }
  535. /**
  536. * Gets the last recorded SSID to a given BSSID.
  537. *
  538. * @param bssid
  539. * The BSSID to match against.
  540. * @return A String of the last SSID or null if the BSSID is not in the
  541. * database.
  542. */
  543. public String getSSID(String bssid) {
  544. String selectQuery = "SELECT " + NetworkEntry.COLUMN_NAME_SSID + " FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID
  545. + " = " + "'" + bssid + "'";
  546. SQLiteDatabase db = this.getReadableDatabase();
  547. Cursor cursor = db.rawQuery(selectQuery, null);
  548. String ssid = null;
  549. if (cursor.moveToFirst()) {
  550. ssid = cursor.getString(0);
  551. }
  552. cursor.close();
  553. db.close();
  554. return ssid;
  555. }
  556. public ArrayList<NetworkRecord> getNetworkInformation() {
  557. String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME;
  558. SQLiteDatabase db = this.getReadableDatabase();
  559. Cursor cursor = db.rawQuery(selectQuery, null);
  560. ArrayList<NetworkRecord> networkInformation = new ArrayList<NetworkRecord>();
  561. // looping through all rows and adding to list
  562. if (cursor.moveToFirst()) {
  563. do {
  564. NetworkRecord record = new NetworkRecord();
  565. record.setBssid(cursor.getString(0));
  566. record.setSsid(cursor.getString(1));
  567. record.setLatitude(Double.parseDouble(cursor.getString(2)));
  568. record.setLongitude(Double.parseDouble(cursor.getString(3)));
  569. record.setAccuracy(Float.parseFloat(cursor.getString(4)));
  570. record.setTimestampLocation(cursor.getLong(5));
  571. networkInformation.add(record);
  572. } while (cursor.moveToNext());
  573. }
  574. cursor.close();
  575. db.close();
  576. return networkInformation;
  577. }
  578. public void updateNetworkInformation(ArrayList<NetworkRecord> networkInformation) {;
  579. for (NetworkRecord record : networkInformation) {
  580. updateNetworkInformation(record);
  581. }
  582. }
  583. public void updateNetworkInformation(NetworkRecord record) {
  584. SQLiteDatabase db = this.getReadableDatabase();
  585. String bssid = record.getBssid();
  586. String bssidQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
  587. Cursor cursor = db.rawQuery(bssidQuery, new String[] {bssid});
  588. if (!cursor.moveToFirst() || cursor.getLong(5) < record.getTimestampLocation()){
  589. ContentValues bssidValues = new ContentValues();
  590. bssidValues.put(NetworkEntry.COLUMN_NAME_BSSID, bssid);
  591. bssidValues.put(NetworkEntry.COLUMN_NAME_SSID, record.getSsid());
  592. bssidValues.put(NetworkEntry.COLUMN_NAME_LATITUDE, record.getLatitude());
  593. bssidValues.put(NetworkEntry.COLUMN_NAME_LONGITUDE, record.getLongitude());
  594. bssidValues.put(NetworkEntry.COLUMN_NAME_ACCURACY, record.getAccuracy());
  595. bssidValues.put(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP, record.getTimestampLocation());
  596. db.insertWithOnConflict(NetworkEntry.TABLE_NAME, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
  597. }
  598. cursor.close();
  599. db.close();
  600. }
  601. /**
  602. * Updates the the timestamp of a single device id
  603. * @param devices The Device id
  604. * @param timestamp The synchronization timestamp
  605. */
  606. public void updateSyncDevice(String devices, long timestamp){
  607. SQLiteDatabase db = this.getReadableDatabase();
  608. ContentValues deviceValues = new ContentValues();
  609. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, devices);
  610. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, timestamp);
  611. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
  612. db.close();
  613. }
  614. /**
  615. * Updates the Timestamps of synchronization devices from a HashMap.
  616. * @param devices HashMap of device ids and their synchronization timestamps.
  617. */
  618. public void updateSyncDevices(HashMap<String, Long> devices){
  619. SQLiteDatabase db = this.getReadableDatabase();
  620. for(String key : devices.keySet()){
  621. ContentValues deviceValues = new ContentValues();
  622. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, key);
  623. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, devices.get(key));
  624. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
  625. }
  626. db.close();
  627. }
  628. /**
  629. * Returns a HashMap of all devices that were previously synchronized with.
  630. * @return HashMap containing device id's and the last synchronization timestamp.
  631. */
  632. public HashMap<String, Long> getSyncDevices(){
  633. SQLiteDatabase db = this.getReadableDatabase();
  634. HashMap<String, Long> devices = new HashMap<String, Long>();
  635. String query = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
  636. Cursor cursor = db.rawQuery(query, null);
  637. if (cursor.moveToFirst()) {
  638. do {
  639. devices.put(cursor.getString(0), cursor.getLong(1));
  640. } while (cursor.moveToNext());
  641. }
  642. cursor.close();
  643. db.close();
  644. return devices;
  645. }
  646. /**
  647. * Returns a ArrayList containing all information stored in the SyncInfo table.
  648. * @return ArrayList<SyncInfo>
  649. */
  650. public ArrayList<SyncInfoRecord> getSyncInfo(){
  651. SQLiteDatabase db = this.getReadableDatabase();
  652. ArrayList<SyncInfoRecord> syncInfo = new ArrayList<SyncInfoRecord>();
  653. String query = "SELECT * FROM " + SyncInfoEntry.TABLE_NAME;
  654. Cursor cursor = db.rawQuery(query, null);
  655. if (cursor.moveToFirst()) {
  656. do {
  657. SyncInfoRecord info = new SyncInfoRecord();
  658. info.setDeviceID(cursor.getString(0));
  659. info.setBSSID(cursor.getString(1));
  660. info.setNumber_of_attacks(cursor.getLong(2));
  661. info.setNumber_of_portscans(cursor.getLong(3));
  662. syncInfo.add(info);
  663. } while (cursor.moveToNext());
  664. }
  665. cursor.close();
  666. db.close();
  667. return syncInfo;
  668. }
  669. public void updateSyncInfo(ArrayList<SyncInfoRecord> syncInfo){
  670. for(SyncInfoRecord info : syncInfo){
  671. updateSyncInfo(info);
  672. }
  673. }
  674. public void updateSyncInfo(SyncInfoRecord syncInfo){
  675. SQLiteDatabase db = this.getReadableDatabase();
  676. ContentValues syncValues = new ContentValues();
  677. syncValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, syncInfo.getBSSID());
  678. syncValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, syncInfo.getDeviceID());
  679. syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, syncInfo.getNumber_of_attacks());
  680. syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, syncInfo.getNumber_of_portscans());
  681. db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, syncValues, SQLiteDatabase.CONFLICT_REPLACE);
  682. db.close();
  683. }
  684. /**
  685. * 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}
  686. * @param device_id The id of the device that is to be deleted.
  687. */
  688. public void clearSyncInfos(){
  689. SQLiteDatabase db = this.getReadableDatabase();
  690. db.delete(SyncDeviceEntry.TABLE_NAME, null, null);
  691. db.delete(SyncInfoEntry.TABLE_NAME, null, null);
  692. db.close();
  693. }
  694. /**
  695. * Deletes all records from {@link #PacketEntry.TABLE_NAME}.
  696. */
  697. public void clearData() {
  698. SQLiteDatabase db = this.getReadableDatabase();
  699. db.delete(PacketEntry.TABLE_NAME, null, null);
  700. db.delete(AttackEntry.TABLE_NAME, null, null);
  701. db.close();
  702. }
  703. /**
  704. * Deletes all records from {@link #PacketEntry.TABLE_NAME} with a specific BSSID.
  705. *
  706. * @param bssid
  707. * The BSSID to match against.
  708. */
  709. public void deleteByBSSID(String bssid) {
  710. SQLiteDatabase db = this.getReadableDatabase();
  711. db.delete(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID + " = ?", new String[] { bssid });
  712. db.delete(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_BSSID + " = ?", new String[] { bssid });
  713. db.close();
  714. }
  715. /**
  716. * Deletes all records from {@link #PacketEntry.TABLE_NAME} with a time stamp smaller
  717. * then the given
  718. *
  719. * @param date
  720. * A Date represented in milliseconds.
  721. */
  722. public void deleteByDate(long date) {
  723. SQLiteDatabase db = this.getReadableDatabase();
  724. String deleteQuery = "DELETE FROM " + PacketEntry.TABLE_NAME + " WHERE " + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP + " < " + date;
  725. db.execSQL(deleteQuery);
  726. db.close();
  727. }
  728. /**
  729. * Creates a {@link Record} from a Cursor. If the cursor does not show to a
  730. * valid data structure a runtime exception is thrown.
  731. *
  732. * @param cursor
  733. * @return Returns the created {@link Record} .
  734. */
  735. private MessageRecord createMessageRecord(Cursor cursor) {
  736. MessageRecord record = new MessageRecord();
  737. record.setId(Integer.parseInt(cursor.getString(0)));
  738. record.setAttack_id(cursor.getLong(1));
  739. record.setType(MessageRecord.TYPE.valueOf(cursor.getString(2)));
  740. record.setTimestamp(cursor.getLong(3));
  741. record.setPacket(cursor.getString(4));
  742. return record;
  743. }
  744. /**
  745. * Creates a {@link AttackRecord} from a Cursor. If the cursor does not show to a
  746. * valid data structure a runtime exception is thrown.
  747. *
  748. * @param cursor
  749. * @return Returns the created {@link Record} .
  750. */
  751. private AttackRecord createAttackRecord(Cursor cursor) {
  752. AttackRecord record = new AttackRecord();
  753. record.setAttack_id(cursor.getLong(0));
  754. record.setProtocol(cursor.getString(1));
  755. record.setExternalIP(cursor.getString(2));
  756. record.setLocalIP(cursor.getString(3));
  757. record.setLocalPort(Integer.parseInt(cursor.getString(4)));
  758. record.setRemoteIP(cursor.getString(5));
  759. record.setRemotePort(Integer.parseInt(cursor.getString(6)));
  760. record.setBssid(cursor.getString(7));
  761. return record;
  762. }
  763. /**
  764. * Creates a {@link Record} from a Cursor. If the cursor does not show to a
  765. * valid data structure a runtime exception is thrown.
  766. *
  767. * @param cursor
  768. * @return Returns the created {@link Record} .
  769. */
  770. private Record createRecord(Cursor cursor) {
  771. Record record = new Record();
  772. record.setId(Integer.parseInt(cursor.getString(0)));
  773. record.setAttack_id(cursor.getLong(1));
  774. record.setType(TYPE.valueOf(cursor.getString(2)));
  775. record.setTimestamp(cursor.getLong(3));
  776. record.setPacket(cursor.getString(4));
  777. record.setProtocol(cursor.getString(5));
  778. record.setExternalIP(cursor.getString(6));
  779. record.setLocalIP(cursor.getString(7));
  780. record.setLocalPort(Integer.parseInt(cursor.getString(8)));
  781. record.setRemoteIP(cursor.getString(9));
  782. record.setRemotePort(Integer.parseInt(cursor.getString(10)));
  783. record.setBssid(cursor.getString(11));
  784. record.setSsid(cursor.getString(12));
  785. record.setLatitude(Double.parseDouble(cursor.getString(13)));
  786. record.setLongitude(Double.parseDouble(cursor.getString(14)));
  787. record.setAccuracy(Float.parseFloat(cursor.getString(15)));
  788. record.setTimestampLocation(cursor.getLong(16));
  789. return record;
  790. }
  791. /**
  792. * Gets all received {@link Record Records} for the specified information in
  793. * the LogFilter ordered by date.
  794. *
  795. * @return A ArrayList with all received {@link Record Records} for the
  796. * LogFilter.
  797. */
  798. public ArrayList<Record> getRecordsForFilter(LogFilter filter) {
  799. ArrayList<Record> recordList = new ArrayList<Record>();
  800. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  801. + ")";
  802. // TIMESTAMPS
  803. selectQuery = selectQuery + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  804. selectQuery = selectQuery + " < " + filter.getBelowTimestamp();
  805. selectQuery = selectQuery + " AND " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  806. selectQuery = selectQuery + " > " + filter.getAboveTimestamp();
  807. if (filter.getBSSIDs() != null && filter.getBSSIDs().size() > 0) {
  808. selectQuery = selectQuery + " AND ";
  809. selectQuery = selectQuery + filter.getBSSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID);
  810. }
  811. if (filter.getESSIDs() != null && filter.getESSIDs().size() > 0) {
  812. selectQuery = selectQuery + " AND ";
  813. selectQuery = selectQuery + filter.getESSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_SSID);
  814. }
  815. if (filter.getProtocols() != null && filter.getProtocols().size() > 0) {
  816. selectQuery = selectQuery + " AND ";
  817. selectQuery = selectQuery + filter.getProtocolsQueryStatement(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_PROTOCOL);
  818. }
  819. selectQuery = selectQuery + " GROUP BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID;
  820. if (filter.getSorttype() == LogFilter.SortType.packet_timestamp) {
  821. // DESC
  822. selectQuery = selectQuery + " ORDER BY " + filter.getSorttype() + " DESC";
  823. } else {
  824. selectQuery = selectQuery + " ORDER BY " + filter.getSorttype();
  825. }
  826. System.out.println(selectQuery);
  827. SQLiteDatabase db = this.getReadableDatabase();
  828. Cursor cursor = db.rawQuery(selectQuery, null);
  829. // looping through all rows and adding to list
  830. if (cursor.moveToFirst()) {
  831. do {
  832. Record record = createRecord(cursor);
  833. // Adding record to list
  834. recordList.add(record);
  835. } while (cursor.moveToNext());
  836. }
  837. cursor.close();
  838. // return record list
  839. db.close();
  840. return recordList;
  841. }
  842. /*
  843. * Returns the Conversation of a specific attack id
  844. *
  845. * @param attack_id Tha attack id to match the query against.
  846. *
  847. * @return A arraylist with all {@link Record Records}s for an attack id.
  848. */
  849. public ArrayList<Record> getConversationForAttackID(long attack_id) {
  850. ArrayList<Record> recordList = new ArrayList<Record>();
  851. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  852. + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id;
  853. SQLiteDatabase db = this.getReadableDatabase();
  854. Cursor cursor = db.rawQuery(selectQuery, null);
  855. if (cursor.moveToFirst()) {
  856. do {
  857. Record record = createRecord(cursor);
  858. recordList.add(record);
  859. } while (cursor.moveToNext());
  860. }
  861. cursor.close();
  862. db.close();
  863. return recordList;
  864. }
  865. /**
  866. * Gets a single {@link Record} with the given attack id from the database.
  867. *
  868. * @param attack_id
  869. * The attack id of the {@link Record};
  870. * @return The {@link Record}.
  871. */
  872. public Record getRecordOfAttackId(long attack_id) {
  873. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  874. + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id + " GROUP BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ID;
  875. SQLiteDatabase db = this.getReadableDatabase();
  876. Cursor cursor = db.rawQuery(selectQuery, null);
  877. Record record = null;
  878. if (cursor.moveToFirst()) {
  879. record = createRecord(cursor);
  880. }
  881. cursor.close();
  882. // return record list
  883. db.close();
  884. return record;
  885. }
  886. /**
  887. * Gets a single {@link Record} with the given ID from the database.
  888. *
  889. * @param id
  890. * The ID of the {@link Record};
  891. * @return The {@link Record}.
  892. */
  893. public Record getRecord(int id) {
  894. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + PacketEntry.COLUMN_NAME_ATTACK_ID
  895. + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ID + " = " + id;
  896. SQLiteDatabase db = this.getReadableDatabase();
  897. Cursor cursor = db.rawQuery(selectQuery, null);
  898. Record record = null;
  899. if (cursor.moveToFirst()) {
  900. record = createRecord(cursor);
  901. }
  902. cursor.close();
  903. db.close();
  904. // return contact
  905. return record;
  906. }
  907. /**
  908. * Gets all {@link Record Records} saved in the database.
  909. *
  910. * @return A ArrayList of all the {@link Record Records} in the Database.
  911. */
  912. public ArrayList<Record> getAllRecords() {
  913. ArrayList<Record> recordList = new ArrayList<Record>();
  914. // Select All Query
  915. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  916. + ")";
  917. SQLiteDatabase db = this.getWritableDatabase();
  918. Cursor cursor = db.rawQuery(selectQuery, null);
  919. // looping through all rows and adding to list
  920. if (cursor.moveToFirst()) {
  921. do {
  922. Record record = createRecord(cursor);
  923. // Adding record to list
  924. recordList.add(record);
  925. } while (cursor.moveToNext());
  926. }
  927. cursor.close();
  928. db.close();
  929. // return record list
  930. return recordList;
  931. }
  932. /**
  933. * Gets all non duplicate Records For the key BSSID.
  934. *
  935. * @return A ArrayList with received Records.
  936. */
  937. public ArrayList<String> getUniqueBSSIDRecords() {
  938. return this.getUniqueDataEntryForKeyType(NetworkEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME);
  939. }
  940. /**
  941. * Gets all non duplicate Records For the key ESSID.
  942. *
  943. * @return A ArrayList with received Records.
  944. */
  945. public ArrayList<String> getUniqueESSIDRecords() {
  946. return this.getUniqueDataEntryForKeyType(NetworkEntry.COLUMN_NAME_SSID, NetworkEntry.TABLE_NAME);
  947. }
  948. public ArrayList<String> getUniqueESSIDRecordsForProtocol(String protocol) {
  949. return this.getUniqueIDForProtocol(NetworkEntry.COLUMN_NAME_SSID, protocol);
  950. }
  951. public ArrayList<String> getUniqueBSSIDRecordsForProtocol(String protocol) {
  952. return this.getUniqueIDForProtocol(NetworkEntry.COLUMN_NAME_BSSID, protocol);
  953. }
  954. private ArrayList<String> getUniqueIDForProtocol(String id, String protocol) {
  955. ArrayList<String> recordList = new ArrayList<String>();
  956. String selectQuery = "SELECT DISTINCT " + id + " FROM " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID + ") " + " WHERE "
  957. + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_PROTOCOL + " = " + "'" + protocol + "'" + " ORDER BY " + id; // " NATURAL JOIN "
  958. // +
  959. // TABLE_ATTACK_INFO
  960. // +
  961. // " NATURAL JOIN "
  962. // +
  963. // TABLE_BSSIDS
  964. // +
  965. // " NATURAL JOIN "
  966. // +
  967. // TABLE_PORTS
  968. // +
  969. // ORDERED BY TIME
  970. System.out.println(selectQuery);
  971. SQLiteDatabase db = this.getReadableDatabase();
  972. Cursor cursor = db.rawQuery(selectQuery, null);
  973. // looping through all rows and adding to list
  974. if (cursor.moveToFirst()) {
  975. do {
  976. String record = cursor.getString(0);
  977. recordList.add(record);
  978. } while (cursor.moveToNext());
  979. }
  980. cursor.close();
  981. // return record list
  982. db.close();
  983. return recordList;
  984. }
  985. /**
  986. * Gets all non duplicate Data Entry For a specific KeyType ( e.g. BSSIDs).
  987. *
  988. * @return A ArrayList with received Records.
  989. */
  990. public ArrayList<String> getUniqueDataEntryForKeyType(String keyType, String table) {
  991. ArrayList<String> recordList = new ArrayList<String>();
  992. // String selectQuery = "SELECT * FROM " + TABLE_RECORDS +
  993. // " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " +
  994. // TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS;
  995. String selectQuery = "SELECT DISTINCT " + keyType + " FROM " + table + " ORDER BY " + keyType; // " NATURAL JOIN "
  996. // +
  997. // TABLE_ATTACK_INFO
  998. // +
  999. // " NATURAL JOIN "
  1000. // +
  1001. // TABLE_BSSIDS
  1002. // +
  1003. // " NATURAL JOIN "
  1004. // +
  1005. // TABLE_PORTS
  1006. // +
  1007. // ORDERED BY TIME
  1008. System.out.println(selectQuery);
  1009. SQLiteDatabase db = this.getReadableDatabase();
  1010. Cursor cursor = db.rawQuery(selectQuery, null);
  1011. // looping through all rows and adding to list
  1012. if (cursor.moveToFirst()) {
  1013. do {
  1014. String record = cursor.getString(0);
  1015. recordList.add(record);
  1016. } while (cursor.moveToNext());
  1017. }
  1018. cursor.close();
  1019. // return record list
  1020. db.close();
  1021. return recordList;
  1022. }
  1023. //TODO PROFILE DATABASE QUERIES - STILL NEEDED?
  1024. /**
  1025. * Retrieves all the profiles from the database
  1026. *
  1027. * @return list of profiles
  1028. */
  1029. public List<Profile> getAllProfiles() {
  1030. List<Profile> profiles = new LinkedList<Profile>();
  1031. // Select All Query
  1032. String selectQuery = "SELECT * FROM " + ProfileEntry.TABLE_NAME;
  1033. SQLiteDatabase db = this.getWritableDatabase();
  1034. Cursor cursor = db.rawQuery(selectQuery, null);
  1035. // looping through all rows and adding to list
  1036. if (cursor.moveToFirst()) {
  1037. do {
  1038. Profile profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
  1039. if (cursor.getInt(6) == 1) {
  1040. profile.mActivated = true;
  1041. }
  1042. profile.mIconName = cursor.getString(4);
  1043. // Adding record to list
  1044. profiles.add(profile);
  1045. } while (cursor.moveToNext());
  1046. }
  1047. cursor.close();
  1048. db.close();
  1049. // return record list
  1050. return profiles;
  1051. }
  1052. /**
  1053. * Persists the given profile into the database
  1054. *
  1055. * @param profile
  1056. * the profile which should be persisted
  1057. *
  1058. * @return
  1059. */
  1060. public long persistProfile(Profile profile) {
  1061. SQLiteDatabase db = this.getReadableDatabase();
  1062. ContentValues values = new ContentValues();
  1063. if (profile.mId != -1) {
  1064. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ID, profile.mId);
  1065. }
  1066. values.put(ProfileEntry.COLUMN_NAME_PROFILE_NAME, profile.mLabel);
  1067. values.put(ProfileEntry.COLUMN_NAME_PROFILE_DESCRIPTION, profile.mText);
  1068. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ICON, profile.mIconPath);
  1069. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ICON_NAME, profile.mIconName);
  1070. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ACTIVE, profile.mActivated);
  1071. values.put(ProfileEntry.COLUMN_NAME_PROFILE_EDITABLE, profile.mEditable);
  1072. return db.replace(ProfileEntry.TABLE_NAME, null, values);
  1073. }
  1074. /**
  1075. * private static final String CREATE_PROFILE_TABLE = "CREATE TABLE " +
  1076. * TABLE_PROFILES + "(" + KEY_PROFILE_ID +
  1077. * " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_PROFILE_NAME + " TEXT," +
  1078. * KEY_PROFILE_DESCRIPTION + " TEXT," + KEY_PROFILE_ICON + " TEXT," +
  1079. * KEY_PROFILE_ICON_ID + " INTEGER," + KEY_PROFILE_EDITABLE + " INTEGER," +
  1080. * KEY_PROFILE_ACTIVE + " INTEGER" + ")";
  1081. */
  1082. public Profile getProfile(int id) {
  1083. String selectQuery = "SELECT * FROM " + ProfileEntry.TABLE_NAME + " WHERE " + ProfileEntry.TABLE_NAME + "." + ProfileEntry.COLUMN_NAME_PROFILE_ID + " = " + id;
  1084. SQLiteDatabase db = this.getReadableDatabase();
  1085. Cursor cursor = db.rawQuery(selectQuery, null);
  1086. Profile profile = null;
  1087. if (cursor.moveToFirst()) {
  1088. profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
  1089. if (cursor.getInt(6) == 1) {
  1090. profile.mActivated = true;
  1091. }
  1092. profile.mIconName = cursor.getString(5);
  1093. }
  1094. cursor.close();
  1095. db.close();
  1096. // return contact
  1097. return profile;
  1098. }
  1099. public void deleteProfile(int id) {
  1100. SQLiteDatabase db = this.getReadableDatabase();
  1101. db.delete(ProfileEntry.TABLE_NAME, ProfileEntry.COLUMN_NAME_PROFILE_ID + "=?", new String[] { String.valueOf(id) });
  1102. }
  1103. /**
  1104. * Gets all received {@link Record Records} for every attack identified by
  1105. * its attack id and ordered by date.
  1106. *
  1107. * @return A ArrayList with all received {@link Record Records} for each
  1108. * attack id in the Database.
  1109. */
  1110. public ArrayList<Record> getAllReceivedRecordsOfEachAttack() {
  1111. ArrayList<Record> recordList = new ArrayList<Record>();
  1112. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  1113. + ")" + " WHERE " + PacketEntry.COLUMN_NAME_TYPE + "='RECEIVE'" + " ORDER BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  1114. SQLiteDatabase db = this.getReadableDatabase();
  1115. Cursor cursor = db.rawQuery(selectQuery, null);
  1116. // looping through all rows and adding to list
  1117. if (cursor.moveToFirst()) {
  1118. do {
  1119. Record record = createRecord(cursor);
  1120. // Adding record to list
  1121. recordList.add(record);
  1122. } while (cursor.moveToNext());
  1123. }
  1124. cursor.close();
  1125. // return record list
  1126. db.close();
  1127. return recordList;
  1128. }
  1129. }