HostageDBOpenHelper.java 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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 = 2;
  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. * Deletes all records from {@link #TABLE_RECORDS} with a specific Attack ID.
  717. *
  718. * @param attackID
  719. * The Attack ID to match against.
  720. */
  721. public void deleteByAttackID(long attackID) {
  722. SQLiteDatabase db = this.getReadableDatabase();
  723. db.delete(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_ATTACK_ID + " = ?", new String[] { String.valueOf(attackID) });
  724. db.delete(PacketEntry.TABLE_NAME, PacketEntry.COLUMN_NAME_ATTACK_ID + " = ?", new String[] { String.valueOf(attackID) });
  725. db.close();
  726. }
  727. /**
  728. * Creates a {@link Record} from a Cursor. If the cursor does not show to a
  729. * valid data structure a runtime exception is thrown.
  730. *
  731. * @param cursor
  732. * @return Returns the created {@link Record} .
  733. */
  734. private MessageRecord createMessageRecord(Cursor cursor) {
  735. MessageRecord record = new MessageRecord();
  736. record.setId(Integer.parseInt(cursor.getString(0)));
  737. record.setAttack_id(cursor.getLong(1));
  738. record.setType(MessageRecord.TYPE.valueOf(cursor.getString(2)));
  739. record.setTimestamp(cursor.getLong(3));
  740. record.setPacket(cursor.getString(4));
  741. return record;
  742. }
  743. /**
  744. * Creates a {@link AttackRecord} from a Cursor. If the cursor does not show to a
  745. * valid data structure a runtime exception is thrown.
  746. *
  747. * @param cursor
  748. * @return Returns the created {@link Record} .
  749. */
  750. private AttackRecord createAttackRecord(Cursor cursor) {
  751. AttackRecord record = new AttackRecord();
  752. record.setAttack_id(cursor.getLong(0));
  753. record.setProtocol(cursor.getString(1));
  754. record.setExternalIP(cursor.getString(2));
  755. record.setLocalIP(cursor.getString(3));
  756. record.setLocalPort(Integer.parseInt(cursor.getString(4)));
  757. record.setRemoteIP(cursor.getString(5));
  758. record.setRemotePort(Integer.parseInt(cursor.getString(6)));
  759. record.setBssid(cursor.getString(7));
  760. return record;
  761. }
  762. /**
  763. * Creates a {@link Record} from a Cursor. If the cursor does not show to a
  764. * valid data structure a runtime exception is thrown.
  765. *
  766. * @param cursor
  767. * @return Returns the created {@link Record} .
  768. */
  769. private Record createRecord(Cursor cursor) {
  770. Record record = new Record();
  771. record.setId(Integer.parseInt(cursor.getString(0)));
  772. record.setAttack_id(cursor.getLong(1));
  773. record.setType(TYPE.valueOf(cursor.getString(2)));
  774. record.setTimestamp(cursor.getLong(3));
  775. record.setPacket(cursor.getString(4));
  776. record.setProtocol(cursor.getString(5));
  777. record.setExternalIP(cursor.getString(6));
  778. record.setLocalIP(cursor.getString(7));
  779. record.setLocalPort(Integer.parseInt(cursor.getString(8)));
  780. record.setRemoteIP(cursor.getString(9));
  781. record.setRemotePort(Integer.parseInt(cursor.getString(10)));
  782. record.setBssid(cursor.getString(11));
  783. record.setSsid(cursor.getString(12));
  784. record.setLatitude(Double.parseDouble(cursor.getString(13)));
  785. record.setLongitude(Double.parseDouble(cursor.getString(14)));
  786. record.setAccuracy(Float.parseFloat(cursor.getString(15)));
  787. record.setTimestampLocation(cursor.getLong(16));
  788. return record;
  789. }
  790. /**
  791. * Gets all received {@link Record Records} for the specified information in
  792. * the LogFilter ordered by date.
  793. *
  794. * @return A ArrayList with all received {@link Record Records} for the
  795. * LogFilter.
  796. */
  797. public ArrayList<Record> getRecordsForFilter(LogFilter filter) {
  798. ArrayList<Record> recordList = new ArrayList<Record>();
  799. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  800. + ")";
  801. // TIMESTAMPS
  802. selectQuery = selectQuery + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  803. selectQuery = selectQuery + " < " + filter.getBelowTimestamp();
  804. selectQuery = selectQuery + " AND " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  805. selectQuery = selectQuery + " > " + filter.getAboveTimestamp();
  806. if (filter.getBSSIDs() != null && filter.getBSSIDs().size() > 0) {
  807. selectQuery = selectQuery + " AND ";
  808. selectQuery = selectQuery + filter.getBSSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID);
  809. }
  810. if (filter.getESSIDs() != null && filter.getESSIDs().size() > 0) {
  811. selectQuery = selectQuery + " AND ";
  812. selectQuery = selectQuery + filter.getESSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_SSID);
  813. }
  814. if (filter.getProtocols() != null && filter.getProtocols().size() > 0) {
  815. selectQuery = selectQuery + " AND ";
  816. selectQuery = selectQuery + filter.getProtocolsQueryStatement(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_PROTOCOL);
  817. }
  818. selectQuery = selectQuery + " GROUP BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID;
  819. if (filter.getSorttype() == LogFilter.SortType.packet_timestamp) {
  820. // DESC
  821. selectQuery = selectQuery + " ORDER BY " + filter.getSorttype() + " DESC";
  822. } else {
  823. selectQuery = selectQuery + " ORDER BY " + filter.getSorttype();
  824. }
  825. System.out.println(selectQuery);
  826. SQLiteDatabase db = this.getReadableDatabase();
  827. Cursor cursor = db.rawQuery(selectQuery, null);
  828. // looping through all rows and adding to list
  829. if (cursor.moveToFirst()) {
  830. do {
  831. Record record = createRecord(cursor);
  832. // Adding record to list
  833. recordList.add(record);
  834. } while (cursor.moveToNext());
  835. }
  836. cursor.close();
  837. // return record list
  838. db.close();
  839. return recordList;
  840. }
  841. /*
  842. * Returns the Conversation of a specific attack id
  843. *
  844. * @param attack_id Tha attack id to match the query against.
  845. *
  846. * @return A arraylist with all {@link Record Records}s for an attack id.
  847. */
  848. public ArrayList<Record> getConversationForAttackID(long attack_id) {
  849. ArrayList<Record> recordList = new ArrayList<Record>();
  850. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  851. + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id;
  852. SQLiteDatabase db = this.getReadableDatabase();
  853. Cursor cursor = db.rawQuery(selectQuery, null);
  854. if (cursor.moveToFirst()) {
  855. do {
  856. Record record = createRecord(cursor);
  857. recordList.add(record);
  858. } while (cursor.moveToNext());
  859. }
  860. cursor.close();
  861. db.close();
  862. return recordList;
  863. }
  864. /**
  865. * Gets a single {@link Record} with the given attack id from the database.
  866. *
  867. * @param attack_id
  868. * The attack id of the {@link Record};
  869. * @return The {@link Record}.
  870. */
  871. public Record getRecordOfAttackId(long attack_id) {
  872. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  873. + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id + " GROUP BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ID;
  874. SQLiteDatabase db = this.getReadableDatabase();
  875. Cursor cursor = db.rawQuery(selectQuery, null);
  876. Record record = null;
  877. if (cursor.moveToFirst()) {
  878. record = createRecord(cursor);
  879. }
  880. cursor.close();
  881. // return record list
  882. db.close();
  883. return record;
  884. }
  885. /**
  886. * Gets a single {@link Record} with the given ID from the database.
  887. *
  888. * @param id
  889. * The ID of the {@link Record};
  890. * @return The {@link Record}.
  891. */
  892. public Record getRecord(int id) {
  893. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + PacketEntry.COLUMN_NAME_ATTACK_ID
  894. + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ID + " = " + id;
  895. SQLiteDatabase db = this.getReadableDatabase();
  896. Cursor cursor = db.rawQuery(selectQuery, null);
  897. Record record = null;
  898. if (cursor.moveToFirst()) {
  899. record = createRecord(cursor);
  900. }
  901. cursor.close();
  902. db.close();
  903. // return contact
  904. return record;
  905. }
  906. /**
  907. * Gets all {@link Record Records} saved in the database.
  908. *
  909. * @return A ArrayList of all the {@link Record Records} in the Database.
  910. */
  911. public ArrayList<Record> getAllRecords() {
  912. ArrayList<Record> recordList = new ArrayList<Record>();
  913. // Select All Query
  914. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  915. + ")";
  916. SQLiteDatabase db = this.getWritableDatabase();
  917. Cursor cursor = db.rawQuery(selectQuery, null);
  918. Log.i("Database", "Start loop");
  919. // looping through all rows and adding to list
  920. if (cursor.moveToFirst()) {
  921. do {
  922. Log.i("Database", "Add Record");
  923. Record record = createRecord(cursor);
  924. // Adding record to list
  925. recordList.add(record);
  926. } while (cursor.moveToNext());
  927. }
  928. cursor.close();
  929. db.close();
  930. // return record list
  931. return recordList;
  932. }
  933. /**
  934. * Gets all non duplicate Records For the key BSSID.
  935. *
  936. * @return A ArrayList with received Records.
  937. */
  938. public ArrayList<String> getUniqueBSSIDRecords() {
  939. return this.getUniqueDataEntryForKeyType(NetworkEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME);
  940. }
  941. /**
  942. * Gets all non duplicate Records For the key ESSID.
  943. *
  944. * @return A ArrayList with received Records.
  945. */
  946. public ArrayList<String> getUniqueESSIDRecords() {
  947. return this.getUniqueDataEntryForKeyType(NetworkEntry.COLUMN_NAME_SSID, NetworkEntry.TABLE_NAME);
  948. }
  949. public ArrayList<String> getUniqueESSIDRecordsForProtocol(String protocol) {
  950. return this.getUniqueIDForProtocol(NetworkEntry.COLUMN_NAME_SSID, protocol);
  951. }
  952. public ArrayList<String> getUniqueBSSIDRecordsForProtocol(String protocol) {
  953. return this.getUniqueIDForProtocol(NetworkEntry.COLUMN_NAME_BSSID, protocol);
  954. }
  955. private ArrayList<String> getUniqueIDForProtocol(String id, String protocol) {
  956. ArrayList<String> recordList = new ArrayList<String>();
  957. String selectQuery = "SELECT DISTINCT " + id + " FROM " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID + ") " + " WHERE "
  958. + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_PROTOCOL + " = " + "'" + protocol + "'" + " ORDER BY " + id; // " NATURAL JOIN "
  959. // +
  960. // TABLE_ATTACK_INFO
  961. // +
  962. // " NATURAL JOIN "
  963. // +
  964. // TABLE_BSSIDS
  965. // +
  966. // " NATURAL JOIN "
  967. // +
  968. // TABLE_PORTS
  969. // +
  970. // ORDERED BY TIME
  971. System.out.println(selectQuery);
  972. SQLiteDatabase db = this.getReadableDatabase();
  973. Cursor cursor = db.rawQuery(selectQuery, null);
  974. // looping through all rows and adding to list
  975. if (cursor.moveToFirst()) {
  976. do {
  977. String record = cursor.getString(0);
  978. recordList.add(record);
  979. } while (cursor.moveToNext());
  980. }
  981. cursor.close();
  982. // return record list
  983. db.close();
  984. return recordList;
  985. }
  986. /**
  987. * Gets all non duplicate Data Entry For a specific KeyType ( e.g. BSSIDs).
  988. *
  989. * @return A ArrayList with received Records.
  990. */
  991. public ArrayList<String> getUniqueDataEntryForKeyType(String keyType, String table) {
  992. ArrayList<String> recordList = new ArrayList<String>();
  993. // String selectQuery = "SELECT * FROM " + TABLE_RECORDS +
  994. // " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " +
  995. // TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS;
  996. String selectQuery = "SELECT DISTINCT " + keyType + " FROM " + table + " ORDER BY " + keyType; // " NATURAL JOIN "
  997. // +
  998. // TABLE_ATTACK_INFO
  999. // +
  1000. // " NATURAL JOIN "
  1001. // +
  1002. // TABLE_BSSIDS
  1003. // +
  1004. // " NATURAL JOIN "
  1005. // +
  1006. // TABLE_PORTS
  1007. // +
  1008. // ORDERED BY TIME
  1009. System.out.println(selectQuery);
  1010. SQLiteDatabase db = this.getReadableDatabase();
  1011. Cursor cursor = db.rawQuery(selectQuery, null);
  1012. // looping through all rows and adding to list
  1013. if (cursor.moveToFirst()) {
  1014. do {
  1015. String record = cursor.getString(0);
  1016. recordList.add(record);
  1017. } while (cursor.moveToNext());
  1018. }
  1019. cursor.close();
  1020. // return record list
  1021. db.close();
  1022. return recordList;
  1023. }
  1024. //TODO PROFILE DATABASE QUERIES - STILL NEEDED?
  1025. /**
  1026. * Retrieves all the profiles from the database
  1027. *
  1028. * @return list of profiles
  1029. */
  1030. public List<Profile> getAllProfiles() {
  1031. List<Profile> profiles = new LinkedList<Profile>();
  1032. // Select All Query
  1033. String selectQuery = "SELECT * FROM " + ProfileEntry.TABLE_NAME;
  1034. SQLiteDatabase db = this.getWritableDatabase();
  1035. Cursor cursor = db.rawQuery(selectQuery, null);
  1036. // looping through all rows and adding to list
  1037. if (cursor.moveToFirst()) {
  1038. do {
  1039. Profile profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
  1040. if (cursor.getInt(6) == 1) {
  1041. profile.mActivated = true;
  1042. }
  1043. profile.mIconName = cursor.getString(4);
  1044. // Adding record to list
  1045. profiles.add(profile);
  1046. } while (cursor.moveToNext());
  1047. }
  1048. cursor.close();
  1049. db.close();
  1050. // return record list
  1051. return profiles;
  1052. }
  1053. /**
  1054. * Persists the given profile into the database
  1055. *
  1056. * @param profile
  1057. * the profile which should be persisted
  1058. *
  1059. * @return
  1060. */
  1061. public long persistProfile(Profile profile) {
  1062. SQLiteDatabase db = this.getReadableDatabase();
  1063. ContentValues values = new ContentValues();
  1064. if (profile.mId != -1) {
  1065. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ID, profile.mId);
  1066. }
  1067. values.put(ProfileEntry.COLUMN_NAME_PROFILE_NAME, profile.mLabel);
  1068. values.put(ProfileEntry.COLUMN_NAME_PROFILE_DESCRIPTION, profile.mText);
  1069. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ICON, profile.mIconPath);
  1070. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ICON_NAME, profile.mIconName);
  1071. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ACTIVE, profile.mActivated);
  1072. values.put(ProfileEntry.COLUMN_NAME_PROFILE_EDITABLE, profile.mEditable);
  1073. return db.replace(ProfileEntry.TABLE_NAME, null, values);
  1074. }
  1075. /**
  1076. * private static final String CREATE_PROFILE_TABLE = "CREATE TABLE " +
  1077. * TABLE_PROFILES + "(" + KEY_PROFILE_ID +
  1078. * " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_PROFILE_NAME + " TEXT," +
  1079. * KEY_PROFILE_DESCRIPTION + " TEXT," + KEY_PROFILE_ICON + " TEXT," +
  1080. * KEY_PROFILE_ICON_ID + " INTEGER," + KEY_PROFILE_EDITABLE + " INTEGER," +
  1081. * KEY_PROFILE_ACTIVE + " INTEGER" + ")";
  1082. */
  1083. public Profile getProfile(int id) {
  1084. String selectQuery = "SELECT * FROM " + ProfileEntry.TABLE_NAME + " WHERE " + ProfileEntry.TABLE_NAME + "." + ProfileEntry.COLUMN_NAME_PROFILE_ID + " = " + id;
  1085. SQLiteDatabase db = this.getReadableDatabase();
  1086. Cursor cursor = db.rawQuery(selectQuery, null);
  1087. Profile profile = null;
  1088. if (cursor.moveToFirst()) {
  1089. profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
  1090. if (cursor.getInt(6) == 1) {
  1091. profile.mActivated = true;
  1092. }
  1093. profile.mIconName = cursor.getString(5);
  1094. }
  1095. cursor.close();
  1096. db.close();
  1097. // return contact
  1098. return profile;
  1099. }
  1100. public void deleteProfile(int id) {
  1101. SQLiteDatabase db = this.getReadableDatabase();
  1102. db.delete(ProfileEntry.TABLE_NAME, ProfileEntry.COLUMN_NAME_PROFILE_ID + "=?", new String[] { String.valueOf(id) });
  1103. }
  1104. /**
  1105. * Gets all received {@link Record Records} for every attack identified by
  1106. * its attack id and ordered by date.
  1107. *
  1108. * @return A ArrayList with all received {@link Record Records} for each
  1109. * attack id in the Database.
  1110. */
  1111. public ArrayList<Record> getAllReceivedRecordsOfEachAttack() {
  1112. ArrayList<Record> recordList = new ArrayList<Record>();
  1113. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  1114. + ")" + " WHERE " + PacketEntry.COLUMN_NAME_TYPE + "='RECEIVE'" + " ORDER BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  1115. SQLiteDatabase db = this.getReadableDatabase();
  1116. Cursor cursor = db.rawQuery(selectQuery, null);
  1117. // looping through all rows and adding to list
  1118. if (cursor.moveToFirst()) {
  1119. do {
  1120. Record record = createRecord(cursor);
  1121. // Adding record to list
  1122. recordList.add(record);
  1123. } while (cursor.moveToNext());
  1124. }
  1125. cursor.close();
  1126. // return record list
  1127. db.close();
  1128. return recordList;
  1129. }
  1130. }