|
@@ -10,7 +10,15 @@ import android.database.Cursor;
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
import android.util.Log;
|
|
|
-
|
|
|
+/**
|
|
|
+ * This class creates SQL tables and handles all access to the database.<br>
|
|
|
+ * It contains several methods with predefined queries to extract different kinds of information from the database.<br>
|
|
|
+ * The database contains two tables: {@link #TABLE_RECORDS} and {@link #TABLE_BSSIDS}:<br>
|
|
|
+ * {@link #TABLE_RECORDS} contains all logging information of a single message record except the SSID.<br>
|
|
|
+ * {@link #TABLE_BSSIDS} contains the BSSID of all recorded Networks and the corresponding SSID.<br>
|
|
|
+ * @author Lars Pandikow
|
|
|
+ *
|
|
|
+ */
|
|
|
public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
|
|
|
// All Static variables
|
|
@@ -74,7 +82,10 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
onCreate(db);
|
|
|
}
|
|
|
|
|
|
- // Adding new record
|
|
|
+ /**
|
|
|
+ * Adds a given record to the database.
|
|
|
+ * @param record The added record.
|
|
|
+ */
|
|
|
public void addRecord(Record record) {
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
@@ -95,8 +106,6 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
recordValues.put(KEY_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
|
|
|
recordValues.put(KEY_BSSID, record.getBSSID());
|
|
|
recordValues.put(KEY_PACKET, record.getPacket()); // Log Packet
|
|
|
-
|
|
|
-
|
|
|
|
|
|
// Inserting Rows
|
|
|
db.insertWithOnConflict(TABLE_BSSIDS, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
@@ -104,6 +113,11 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
db.close(); // Closing database connection
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates a {@link Record} from a {@link Cursor}. If the cursor does not show to a valid data structure a runtime exception is thrown.
|
|
|
+ * @param cursor
|
|
|
+ * @return Returns the created Record.
|
|
|
+ */
|
|
|
private Record createRecord(Cursor cursor){
|
|
|
Record record = new Record();
|
|
|
try {
|
|
@@ -126,7 +140,11 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
return record;
|
|
|
}
|
|
|
|
|
|
- // Getting single record
|
|
|
+ /**
|
|
|
+ * Gets a single {@link Record} with the given ID from the database.
|
|
|
+ * @param id The ID of the {@link Record};
|
|
|
+ * @return The {@link Record}.
|
|
|
+ */
|
|
|
public Record getRecord(int id) {
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_BSSIDS + " WHERE " + KEY_ID + " = " + id;
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
@@ -135,7 +153,6 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
Record record = null;
|
|
|
if (cursor.moveToFirst()){
|
|
|
record = createRecord(cursor);
|
|
|
- Log.i("DatabaseHandler", record == null ? "null" : "not null");
|
|
|
}
|
|
|
|
|
|
cursor.close();
|
|
@@ -144,7 +161,10 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
return record;
|
|
|
}
|
|
|
|
|
|
- // Getting All Records
|
|
|
+ /**
|
|
|
+ * Gets all {@link Record Records} saved in the database.
|
|
|
+ * @return A ArrayList of all the {@link Record Records} in the Database.
|
|
|
+ */
|
|
|
public ArrayList<Record> getAllRecords() {
|
|
|
ArrayList<Record> recordList = new ArrayList<Record>();
|
|
|
// Select All Query
|
|
@@ -167,7 +187,11 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
return recordList;
|
|
|
}
|
|
|
|
|
|
- // Getting first Record for each AttackId greater than a given id
|
|
|
+ /**
|
|
|
+ * Gets a single {@link Record} with the given attack id from the database.
|
|
|
+ * @param attack_id The attack id of the {@link Record};
|
|
|
+ * @return The {@link Record}.
|
|
|
+ */
|
|
|
public Record getRecordOfAttackId(long attack_id) {
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_BSSIDS + " WHERE " + KEY_ATTACK_ID + " = " + attack_id + " GROUP BY " + KEY_ATTACK_ID;
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
@@ -179,12 +203,15 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
}
|
|
|
cursor.close();
|
|
|
|
|
|
- // return count
|
|
|
+ // return record list
|
|
|
db.close();
|
|
|
return record;
|
|
|
}
|
|
|
|
|
|
- // Getting first Record for each AttackId
|
|
|
+ /**
|
|
|
+ * Gets a representative {@link Record} for every attack identified by its attack id.
|
|
|
+ * @return A ArrayList with one {@link Record Records} for each attack id in the Database.
|
|
|
+ */
|
|
|
public ArrayList<Record> getRecordOfEachAttack() {
|
|
|
ArrayList<Record> recordList = new ArrayList<Record>();
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_BSSIDS + " GROUP BY " + KEY_ATTACK_ID;
|
|
@@ -201,12 +228,16 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
}
|
|
|
cursor.close();
|
|
|
|
|
|
- // return count
|
|
|
+ // return record list
|
|
|
db.close();
|
|
|
return recordList;
|
|
|
}
|
|
|
|
|
|
- // Getting first Record for each AttackId greater than a given id
|
|
|
+ /**
|
|
|
+ * Gets a representative {@link Record} for every attack with a higher attack id than the specified.
|
|
|
+ * @param attack_id The attack id to match the query against.
|
|
|
+ * @return A ArrayList with one {@link Record Records} for each attack id higher than the given.
|
|
|
+ */
|
|
|
public ArrayList<Record> getRecordOfEachAttack(long attack_id) {
|
|
|
ArrayList<Record> recordList = new ArrayList<Record>();
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_BSSIDS + " WHERE " + KEY_ATTACK_ID + " > " + attack_id + " GROUP BY " + KEY_ATTACK_ID;
|
|
@@ -228,7 +259,10 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
return recordList;
|
|
|
}
|
|
|
|
|
|
- // Getting record Count
|
|
|
+ /**
|
|
|
+ * Determines the number of {@link Record Records} in the database.
|
|
|
+ * @return The number of {@link Record Records} in the database.
|
|
|
+ */
|
|
|
public int getRecordCount() {
|
|
|
String countQuery = "SELECT * FROM " + TABLE_RECORDS;
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
@@ -241,7 +275,10 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- // Getting record Count
|
|
|
+ /**
|
|
|
+ * Determines the number of different attack_ids in the database.
|
|
|
+ * @return The number of different attack_ids in the database.
|
|
|
+ */
|
|
|
public int getAttackCount() {
|
|
|
String countQuery = "SELECT * FROM " + TABLE_RECORDS + " GROUP BY " + KEY_ATTACK_ID;
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
@@ -254,7 +291,11 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- // Getting record Count
|
|
|
+ /**
|
|
|
+ * Determines the number of different attack_ids for a specific protocol in the database.
|
|
|
+ * @param protocol The String representation of the {@link de.tudarmstadt.informatik.hostage.protocol.Protocol Protocol}
|
|
|
+ * @return The number of different attack_ids in the database.
|
|
|
+ */
|
|
|
public int getAttackPerProtokolCount(String protocol) {
|
|
|
String countQuery = "SELECT * FROM " + TABLE_RECORDS + " WHERE " + KEY_PROTOCOL + " = " + "'" + protocol + "'" + " GROUP BY " + KEY_ATTACK_ID;
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
@@ -267,7 +308,10 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- //Get the smallest AttackId
|
|
|
+ /**
|
|
|
+ * Determines the smallest attack id stored in the database.
|
|
|
+ * @return The smallest attack id stored in the database.
|
|
|
+ */
|
|
|
public long getSmallestAttackId(){
|
|
|
String selectQuery = "SELECT MIN(" + KEY_ATTACK_ID +") FROM " + TABLE_RECORDS;
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
@@ -284,7 +328,10 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- //Get the smallest AttackId
|
|
|
+ /**
|
|
|
+ * Determines the highest attack id stored in the database.
|
|
|
+ * @return The highest attack id stored in the database.
|
|
|
+ */
|
|
|
public long getHighestAttackId(){
|
|
|
String selectQuery = "SELECT MAX(" + KEY_ATTACK_ID +") FROM " + TABLE_RECORDS;
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
@@ -301,6 +348,12 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Determines if an attack has been recorded on a specific protocol in a network with a given BSSID.
|
|
|
+ * @param protocol The {@link de.tudarmstadt.informatik.hostage.protocol.Protocol Protocol} to inspect.
|
|
|
+ * @param BSSID The BSSID of the network.
|
|
|
+ * @return True if an attack on the given protocol has been recorded in a network with the given BSSID, else false.
|
|
|
+ */
|
|
|
public boolean bssidSeen(String protocol, String BSSID){
|
|
|
String countQuery = "SELECT * FROM " + TABLE_RECORDS + " WHERE " + KEY_PROTOCOL + " = " + "'" + protocol + "'" + " AND " + KEY_BSSID + " = " + "'" + BSSID + "'";
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
@@ -311,6 +364,10 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
return result > 0;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a String array with all BSSIDs stored in the database.
|
|
|
+ * @return String[] of all recorded BSSIDs.
|
|
|
+ */
|
|
|
public String[] getAllBSSIDS(){
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
@@ -329,6 +386,11 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
return bssidList;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets the last recorded SSID to a given BSSID.
|
|
|
+ * @param bssid The BSSID to match against.
|
|
|
+ * @return A String of the last SSID or null if the BSSID is not in the database.
|
|
|
+ */
|
|
|
public String getSSID(String bssid){
|
|
|
String selectQuery = "SELECT "+ KEY_SSID +" FROM " + TABLE_BSSIDS + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
@@ -342,12 +404,20 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
return ssid;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Deletes all records from {@link TABLE_RECORDS} with a specific BSSID.
|
|
|
+ * @param bssid The BSSID to match against.
|
|
|
+ */
|
|
|
public void deleteByBSSID(String bssid){
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
db.delete(TABLE_RECORDS, KEY_BSSID + " = ?", new String[]{bssid});
|
|
|
db.close();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Deletes all records from {@link TABLE_RECORDS} with a time stamp smaller then the given
|
|
|
+ * @param date A Date represented in milliseconds.
|
|
|
+ */
|
|
|
public void deleteByDate(long date){
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
String deleteQuery = "DELETE FROM " + TABLE_RECORDS + " WHERE " + KEY_TIME + " < " + date;
|
|
@@ -355,7 +425,9 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
db.close();
|
|
|
}
|
|
|
|
|
|
- //Delete all Data from Database
|
|
|
+ /**
|
|
|
+ * Deletes all records from {@link TABLE_RECORDS}.
|
|
|
+ */
|
|
|
public void clearData(){
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
db.delete(TABLE_RECORDS, null, null);
|