HostageDBOpenHelper.java 65 KB

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