HostageDBOpenHelper.java 64 KB

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