HostageDBOpenHelper.java 54 KB

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