HostageDBOpenHelper.java 59 KB

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