|
@@ -3,6 +3,7 @@ package de.tudarmstadt.informatik.hostage.logging;
|
|
|
import java.net.InetAddress;
|
|
|
import java.net.UnknownHostException;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
|
|
|
import de.tudarmstadt.informatik.hostage.R;
|
|
|
import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
|
|
@@ -37,24 +38,24 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
private static final String TABLE_PORTS = "ports";
|
|
|
|
|
|
// Contacts Table Columns names
|
|
|
- private static final String KEY_ID = "_id";
|
|
|
- private static final String KEY_ATTACK_ID = "_attack_id";
|
|
|
- private static final String KEY_TYPE = "type";
|
|
|
- private static final String KEY_TIME = "timestamp";
|
|
|
- private static final String KEY_PACKET = "packet";
|
|
|
- private static final String KEY_PROTOCOL = "protocol";
|
|
|
- private static final String KEY_EXTERNAL_IP ="externalIP";
|
|
|
- private static final String KEY_LOCAL_IP = "localIP";
|
|
|
- private static final String KEY_LOCAL_HOSTNAME = "localHostName";
|
|
|
- private static final String KEY_LOCAL_PORT = "localPort";
|
|
|
- private static final String KEY_REMOTE_IP = "remoteIP";
|
|
|
- private static final String KEY_REMOTE_HOSTNAME = "remoteHostName";
|
|
|
- private static final String KEY_REMOTE_PORT = "remotePort";
|
|
|
- private static final String KEY_BSSID = "_bssid";
|
|
|
- private static final String KEY_SSID = "ssid";
|
|
|
- private static final String KEY_LATITUDE = "latitude";
|
|
|
- private static final String KEY_LONGITUDE = "longitude";
|
|
|
- private static final String KEY_ACCURACY = "accuracy";
|
|
|
+ public static final String KEY_ID = "_id";
|
|
|
+ public static final String KEY_ATTACK_ID = "_attack_id";
|
|
|
+ public static final String KEY_TYPE = "type";
|
|
|
+ public static final String KEY_TIME = "timestamp";
|
|
|
+ public static final String KEY_PACKET = "packet";
|
|
|
+ public static final String KEY_PROTOCOL = "protocol";
|
|
|
+ public static final String KEY_EXTERNAL_IP ="externalIP";
|
|
|
+ public static final String KEY_LOCAL_IP = "localIP";
|
|
|
+ public static final String KEY_LOCAL_HOSTNAME = "localHostName";
|
|
|
+ public static final String KEY_LOCAL_PORT = "localPort";
|
|
|
+ public static final String KEY_REMOTE_IP = "remoteIP";
|
|
|
+ public static final String KEY_REMOTE_HOSTNAME = "remoteHostName";
|
|
|
+ public static final String KEY_REMOTE_PORT = "remotePort";
|
|
|
+ public static final String KEY_BSSID = "_bssid";
|
|
|
+ public static final String KEY_SSID = "ssid";
|
|
|
+ public static final String KEY_LATITUDE = "latitude";
|
|
|
+ public static final String KEY_LONGITUDE = "longitude";
|
|
|
+ public static final String KEY_ACCURACY = "accuracy";
|
|
|
|
|
|
|
|
|
// Database sql create statements
|
|
@@ -75,7 +76,7 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
|
|
|
private static final String CREATE_BSSID_TABLE = "CREATE TABLE " + TABLE_BSSIDS + "("
|
|
|
+ KEY_BSSID + " TEXT PRIMARY KEY," + KEY_SSID + " TEXT," + KEY_LATITUDE + " INTEGER,"
|
|
|
- + KEY_LONGITUDE + " INTEGER," + KEY_ACCURACY + " INTEGER"
|
|
|
+ + KEY_LONGITUDE + " INTEGER," + KEY_ACCURACY + " INTEGER," + KEY_TIME + " INTEGER"
|
|
|
+ ")";
|
|
|
|
|
|
private static final String CREATE_PORT_TABLE = "CREATE TABLE " + TABLE_PORTS + "("
|
|
@@ -131,12 +132,13 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
public void addRecord(Record record) {
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
|
- ContentValues bssidValues = new ContentValues();
|
|
|
+ HashMap<String, Object> bssidValues = new HashMap<String, Object>();
|
|
|
bssidValues.put(KEY_BSSID, record.getBSSID());
|
|
|
bssidValues.put(KEY_SSID, record.getSSID());
|
|
|
bssidValues.put(KEY_LATITUDE, record.getLatitude());
|
|
|
bssidValues.put(KEY_LONGITUDE, record.getLongitude());
|
|
|
bssidValues.put(KEY_ACCURACY, record.getAccuracy());
|
|
|
+ bssidValues.put(KEY_TIME, record.getTimestampLocation());
|
|
|
|
|
|
ContentValues attackValues = new ContentValues();
|
|
|
attackValues.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
|
|
@@ -157,11 +159,14 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
recordValues.put(KEY_PACKET, record.getPacket()); // Log Packet
|
|
|
|
|
|
|
|
|
- // Inserting Rows
|
|
|
- db.insertWithOnConflict(TABLE_BSSIDS, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
+
|
|
|
+
|
|
|
+ // Inserting Rows
|
|
|
db.insertWithOnConflict(TABLE_ATTACK_INFO, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
db.insert(TABLE_RECORDS, null, recordValues);
|
|
|
db.close(); // Closing database connection
|
|
|
+ // Update Network Information
|
|
|
+ updateNetworkInformation(bssidValues);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -187,7 +192,8 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
record.setLatitude(Double.parseDouble(cursor.getString(14)));
|
|
|
record.setLongitude(Double.parseDouble(cursor.getString(15)));
|
|
|
record.setAccuracy(Float.parseFloat(cursor.getString(16)));
|
|
|
- record.setLocalPort(Integer.parseInt(cursor.getString(17)));
|
|
|
+ record.setTimestampLocation(cursor.getLong(17));
|
|
|
+ record.setLocalPort(Integer.parseInt(cursor.getString(18)));
|
|
|
} catch (UnknownHostException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -429,6 +435,22 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Determines if a network with given BSSID has already been recorded as malicious.
|
|
|
+ * @param BSSID The BSSID of the network.
|
|
|
+ * @return True if an attack has been recorded in a network with the given BSSID, else false.
|
|
|
+ */
|
|
|
+ public boolean bssidSeen(String BSSID){
|
|
|
+ String countQuery = "SELECT * FROM " + TABLE_BSSIDS + " WHERE " + KEY_BSSID + " = " + "'" + BSSID + "'";
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
+ Cursor cursor = db.rawQuery(countQuery, null);
|
|
|
+ int result = cursor.getCount();
|
|
|
+ cursor.close();
|
|
|
+ db.close();
|
|
|
+ return result > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 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.
|
|
@@ -496,7 +518,7 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
db.close();
|
|
|
}
|
|
|
|
|
|
- //TODO Delete statement überarbeiten
|
|
|
+ //TODO Delete statement �berarbeiten
|
|
|
/**
|
|
|
* Deletes all records from {@link #TABLE_RECORDS} with a time stamp smaller then the given
|
|
|
* @param date A Date represented in milliseconds.
|
|
@@ -504,7 +526,7 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
public void deleteByDate(long date){
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
String deleteQuery = "DELETE FROM " + TABLE_RECORDS + " WHERE " + KEY_TIME + " < " + date;
|
|
|
- //TODO Delete statement überarbeiten
|
|
|
+ //TODO Delete statement �berarbeiten
|
|
|
// String deleteQuery2 = "DELETE "
|
|
|
db.execSQL(deleteQuery);
|
|
|
db.close();
|
|
@@ -519,4 +541,58 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
db.delete(TABLE_ATTACK_INFO, null, null);
|
|
|
db.close();
|
|
|
}
|
|
|
+
|
|
|
+ public ArrayList<HashMap<String, Object>> getNetworkInformation(){
|
|
|
+ String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
+ Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
+
|
|
|
+ ArrayList<HashMap<String, Object>> networkInformation = new ArrayList<HashMap<String, Object>>();
|
|
|
+
|
|
|
+ // looping through all rows and adding to list
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
+ do {
|
|
|
+ HashMap<String, Object> values = new HashMap<String, Object>();
|
|
|
+ values.put(KEY_BSSID, cursor.getString(0));
|
|
|
+ values.put(KEY_SSID, cursor.getString(1));
|
|
|
+ values.put(KEY_LATITUDE, Double.parseDouble(cursor.getString(2)));
|
|
|
+ values.put(KEY_LONGITUDE, Double.parseDouble(cursor.getString(3)));
|
|
|
+ values.put(KEY_ACCURACY, Float.parseFloat(cursor.getString(4)));
|
|
|
+ values.put(KEY_TIME, cursor.getLong(5));
|
|
|
+ networkInformation.add(values);
|
|
|
+ } while (cursor.moveToNext());
|
|
|
+ }
|
|
|
+
|
|
|
+ cursor.close();
|
|
|
+ db.close();
|
|
|
+ return networkInformation;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void updateNetworkInformation(HashMap<String, Object> networkInformation){
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
+ String bssid = (String) networkInformation.get(KEY_BSSID);
|
|
|
+ Log.i("DatabaseHandler", "Füge " + bssid + " hinzu");
|
|
|
+ String bssidQuery = "SELECT * FROM " + TABLE_BSSIDS + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
|
|
|
+ Cursor cursor = db.rawQuery(bssidQuery, null);
|
|
|
+ int result = cursor.getCount();
|
|
|
+ if( cursor != null && cursor.moveToFirst() && (result <= 0 || cursor.getLong(5) < (Long) networkInformation.get(KEY_TIME)));{
|
|
|
+ ContentValues bssidValues = new ContentValues();
|
|
|
+ bssidValues.put(KEY_BSSID, bssid);
|
|
|
+ bssidValues.put(KEY_SSID, (String) networkInformation.get(KEY_SSID));
|
|
|
+ bssidValues.put(KEY_LATITUDE, (double)(Double) networkInformation.get(KEY_LATITUDE));
|
|
|
+ bssidValues.put(KEY_LONGITUDE, (double)(Double) networkInformation.get(KEY_LONGITUDE));
|
|
|
+ bssidValues.put(KEY_ACCURACY, (float)(Float) networkInformation.get(KEY_ACCURACY));
|
|
|
+ bssidValues.put(KEY_TIME, (Long) networkInformation.get(KEY_TIME));
|
|
|
+ db.insertWithOnConflict(TABLE_BSSIDS, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
+ }
|
|
|
+ cursor.close();
|
|
|
+ db.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void updateNetworkInformation(ArrayList<HashMap<String, Object>> networkInformation){
|
|
|
+ Log.i("DatabaseHandler", "Starte updating");
|
|
|
+ for(HashMap<String, Object> values : networkInformation){
|
|
|
+ updateNetworkInformation(values);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|