HostageDBOpenHelper.java 50 KB

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