HostageDBOpenHelper.java 47 KB

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