HostageDBOpenHelper.java 51 KB

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