|
@@ -1,8 +1,11 @@
|
|
package de.tudarmstadt.informatik.hostage.logging;
|
|
package de.tudarmstadt.informatik.hostage.logging;
|
|
|
|
|
|
|
|
+import java.net.InetAddress;
|
|
|
|
+import java.net.UnknownHostException;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
+import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
|
|
import android.content.ContentValues;
|
|
import android.content.ContentValues;
|
|
import android.content.Context;
|
|
import android.content.Context;
|
|
import android.database.Cursor;
|
|
import android.database.Cursor;
|
|
@@ -28,10 +31,22 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
private static final String KEY_TYPE = "type";
|
|
private static final String KEY_TYPE = "type";
|
|
private static final String KEY_TIME = "timestamp";
|
|
private static final String KEY_TIME = "timestamp";
|
|
private static final String KEY_LOCAL_IP = "localIP";
|
|
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_LOCAL_PORT = "localPort";
|
|
private static final String KEY_REMOTE_IP = "remoteIP";
|
|
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_REMOTE_PORT = "remotePort";
|
|
|
|
+ private static final String KEY_BSSID = "bssid";
|
|
|
|
+ private static final String KEY_SSID = "ssid";
|
|
private static final String KEY_PACKET = "packet";
|
|
private static final String KEY_PACKET = "packet";
|
|
|
|
+
|
|
|
|
+ // Database sql create statement
|
|
|
|
+ private static final String CREATE_RECORD_TABLE = "CREATE TABLE " + TABLE_RECORDS + "(" + KEY_ID
|
|
|
|
+ + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ATTACK_ID + " INTEGER," + KEY_PROTOCOL + " TEXT,"
|
|
|
|
+ + KEY_TYPE + " TEXT," + KEY_TIME + " INTEGER," + KEY_LOCAL_IP
|
|
|
|
+ + " BLOB," + KEY_LOCAL_HOSTNAME + " TEXT," + KEY_LOCAL_PORT + " INTEGER," + KEY_REMOTE_IP
|
|
|
|
+ + " BLOB," + KEY_REMOTE_HOSTNAME + " TEXT," + KEY_REMOTE_PORT + " INTEGER,"
|
|
|
|
+ + KEY_BSSID + " TEXT," + KEY_SSID + " TEXT," + KEY_PACKET + " TEXT" + ")";
|
|
|
|
|
|
public DatabaseHandler(Context context) {
|
|
public DatabaseHandler(Context context) {
|
|
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
|
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
|
@@ -40,12 +55,6 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
// Creating Tables
|
|
// Creating Tables
|
|
@Override
|
|
@Override
|
|
public void onCreate(SQLiteDatabase db) {
|
|
public void onCreate(SQLiteDatabase db) {
|
|
- String CREATE_RECORD_TABLE = "CREATE TABLE " + TABLE_RECORDS + "(" + KEY_ID
|
|
|
|
- + " INTEGER PRIMARY KEY," + KEY_ATTACK_ID + " INTEGER," + KEY_PROTOCOL + " TEXT,"
|
|
|
|
- + KEY_TYPE + " TEXT," + KEY_TIME + " TEXT," + KEY_LOCAL_IP
|
|
|
|
- + " Text," + KEY_LOCAL_PORT + " INTEGER," + KEY_REMOTE_IP
|
|
|
|
- + " TEXT," + KEY_REMOTE_PORT + " INTEGER," + KEY_PACKET
|
|
|
|
- + " TEXT" + ")";
|
|
|
|
db.execSQL(CREATE_RECORD_TABLE);
|
|
db.execSQL(CREATE_RECORD_TABLE);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -60,18 +69,22 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
}
|
|
}
|
|
|
|
|
|
// Adding new record
|
|
// Adding new record
|
|
- public void addRecord(SQLRecord record) {
|
|
|
|
|
|
+ public void addRecord(Record record) {
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
ContentValues values = new ContentValues();
|
|
values.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
|
|
values.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
|
|
- values.put(KEY_PROTOCOL, record.getProtocol());
|
|
|
|
- values.put(KEY_TYPE, record.getType()); // Log Type
|
|
|
|
|
|
+ values.put(KEY_PROTOCOL, record.getProtocol().toString());
|
|
|
|
+ values.put(KEY_TYPE, record.getType().name()); // Log Type
|
|
values.put(KEY_TIME, record.getTimestamp()); // Log Timestamp
|
|
values.put(KEY_TIME, record.getTimestamp()); // Log Timestamp
|
|
- values.put(KEY_LOCAL_IP, record.getLocalIP()); // Log Local IP
|
|
|
|
|
|
+ values.put(KEY_LOCAL_IP, record.getLocalIP().getAddress()); // Log Local IP
|
|
|
|
+ values.put(KEY_LOCAL_HOSTNAME, record.getLocalIP().getHostName());
|
|
values.put(KEY_LOCAL_PORT, record.getLocalPort()); // Log Local Port
|
|
values.put(KEY_LOCAL_PORT, record.getLocalPort()); // Log Local Port
|
|
- values.put(KEY_REMOTE_IP, record.getRemoteIP()); // Log Remote IP
|
|
|
|
|
|
+ values.put(KEY_REMOTE_IP, record.getRemoteIP().getAddress()); // Log Remote IP
|
|
|
|
+ values.put(KEY_REMOTE_HOSTNAME, record.getRemoteIP().getHostName());
|
|
values.put(KEY_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
|
|
values.put(KEY_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
|
|
|
|
+ values.put(KEY_BSSID, record.getBSSID());
|
|
|
|
+ values.put(KEY_SSID, record.getSSID());
|
|
values.put(KEY_PACKET, record.getPacket()); // Log Packet
|
|
values.put(KEY_PACKET, record.getPacket()); // Log Packet
|
|
|
|
|
|
// Inserting Row
|
|
// Inserting Row
|
|
@@ -80,22 +93,34 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
}
|
|
}
|
|
|
|
|
|
// Getting single record
|
|
// Getting single record
|
|
- public SQLRecord getRecord(int id) {
|
|
|
|
|
|
+ public Record getRecord(int id) {
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
|
- Cursor cursor = db.query(TABLE_RECORDS, new String[] { KEY_ID,
|
|
|
|
- KEY_ATTACK_ID, KEY_PROTOCOL, KEY_TYPE, KEY_TIME, KEY_LOCAL_IP,
|
|
|
|
- KEY_LOCAL_PORT, KEY_REMOTE_IP, KEY_REMOTE_PORT, KEY_PACKET },
|
|
|
|
|
|
+ Cursor cursor = db.query(TABLE_RECORDS, null,
|
|
KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null,
|
|
KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null,
|
|
null, null);
|
|
null, null);
|
|
if (cursor != null)
|
|
if (cursor != null)
|
|
cursor.moveToFirst();
|
|
cursor.moveToFirst();
|
|
|
|
|
|
- SQLRecord record = new SQLRecord(Integer.parseInt(cursor.getString(0)),
|
|
|
|
- Integer.parseInt(cursor.getString(1)), cursor.getString(2), cursor.getString(3),
|
|
|
|
- cursor.getString(4), cursor.getString(5),
|
|
|
|
- Integer.parseInt(cursor.getString(6)), cursor.getString(7),
|
|
|
|
- Integer.parseInt(cursor.getString(8)), cursor.getString(9));
|
|
|
|
|
|
+
|
|
|
|
+ Record record = new Record();
|
|
|
|
+ try {
|
|
|
|
+ record.setID(Integer.parseInt(cursor.getString(0)));
|
|
|
|
+ record.setAttack_id(cursor.getLong(1));
|
|
|
|
+ record.setProtocol(cursor.getString(2));
|
|
|
|
+ record.setType(cursor.getString(3).equals("SEND") ? TYPE.SEND : TYPE.RECEIVE);
|
|
|
|
+ record.setTimestamp(cursor.getLong(4));
|
|
|
|
+ record.setLocalIP(InetAddress.getByAddress(cursor.getString(6), cursor.getBlob(5)));
|
|
|
|
+ record.setLocalPort(Integer.parseInt(cursor.getString(7)));
|
|
|
|
+ record.setRemoteIP(InetAddress.getByAddress(cursor.getString(9), cursor.getBlob(8)));
|
|
|
|
+ record.setRemotePort(Integer.parseInt(cursor.getString(10)));
|
|
|
|
+ record.setBSSID(cursor.getString(11));
|
|
|
|
+ record.setSSID(cursor.getString(12));
|
|
|
|
+ record.setPacket(cursor.getString(13));
|
|
|
|
+ } catch (UnknownHostException e) {
|
|
|
|
+ // TODO Auto-generated catch block
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
|
|
cursor.close();
|
|
cursor.close();
|
|
db.close();
|
|
db.close();
|
|
@@ -104,8 +129,8 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
}
|
|
}
|
|
|
|
|
|
// Getting All Records
|
|
// Getting All Records
|
|
- public ArrayList<SQLRecord> getAllRecords() {
|
|
|
|
- ArrayList<SQLRecord> contactList = new ArrayList<SQLRecord>();
|
|
|
|
|
|
+ public ArrayList<Record> getAllRecords() {
|
|
|
|
+ ArrayList<Record> recordList = new ArrayList<Record>();
|
|
// Select All Query
|
|
// Select All Query
|
|
String selectQuery = "SELECT * FROM " + TABLE_RECORDS;
|
|
String selectQuery = "SELECT * FROM " + TABLE_RECORDS;
|
|
|
|
|
|
@@ -115,26 +140,33 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
// looping through all rows and adding to list
|
|
// looping through all rows and adding to list
|
|
if (cursor.moveToFirst()) {
|
|
if (cursor.moveToFirst()) {
|
|
do {
|
|
do {
|
|
- SQLRecord record = new SQLRecord();
|
|
|
|
|
|
+ Record record = new Record();
|
|
|
|
+ try {
|
|
record.setID(Integer.parseInt(cursor.getString(0)));
|
|
record.setID(Integer.parseInt(cursor.getString(0)));
|
|
- record.setAttack_id(Integer.parseInt(cursor.getString(1)));
|
|
|
|
|
|
+ record.setAttack_id(cursor.getLong(1));
|
|
record.setProtocol(cursor.getString(2));
|
|
record.setProtocol(cursor.getString(2));
|
|
- record.setType(cursor.getString(3));
|
|
|
|
- record.setTimestamp(cursor.getString(4));
|
|
|
|
- record.setLocalIP(cursor.getString(5));
|
|
|
|
- record.setLocalPort(Integer.parseInt(cursor.getString(6)));
|
|
|
|
- record.setRemoteIP(cursor.getString(7));
|
|
|
|
- record.setRemotePort(Integer.parseInt(cursor.getString(8)));
|
|
|
|
- record.setPacket(cursor.getString(9));
|
|
|
|
|
|
+ record.setType(cursor.getString(3).equals("SEND") ? TYPE.SEND : TYPE.RECEIVE);
|
|
|
|
+ record.setTimestamp(cursor.getLong(4));
|
|
|
|
+ record.setLocalIP(InetAddress.getByAddress(cursor.getString(6), cursor.getBlob(5)));
|
|
|
|
+ record.setLocalPort(Integer.parseInt(cursor.getString(7)));
|
|
|
|
+ record.setRemoteIP(InetAddress.getByAddress(cursor.getString(9), cursor.getBlob(8)));
|
|
|
|
+ record.setRemotePort(Integer.parseInt(cursor.getString(10)));
|
|
|
|
+ record.setBSSID(cursor.getString(11));
|
|
|
|
+ record.setSSID(cursor.getString(12));
|
|
|
|
+ record.setPacket(cursor.getString(13));
|
|
|
|
+ } catch (UnknownHostException e) {
|
|
|
|
+ // TODO Auto-generated catch block
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
|
|
// Adding record to list
|
|
// Adding record to list
|
|
- contactList.add(record);
|
|
|
|
|
|
+ recordList.add(record);
|
|
} while (cursor.moveToNext());
|
|
} while (cursor.moveToNext());
|
|
}
|
|
}
|
|
cursor.close();
|
|
cursor.close();
|
|
db.close();
|
|
db.close();
|
|
// return record list
|
|
// return record list
|
|
- return contactList;
|
|
|
|
|
|
+ return recordList;
|
|
}
|
|
}
|
|
|
|
|
|
// Getting record Count
|
|
// Getting record Count
|
|
@@ -165,7 +197,7 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
|
|
|
|
// Getting record Count
|
|
// Getting record Count
|
|
public int getAttackPerProtokolCount(String protocol) {
|
|
public int getAttackPerProtokolCount(String protocol) {
|
|
- String countQuery = "SELECT * FROM " + TABLE_RECORDS + " WHERE " + KEY_PROTOCOL + " = " + "'" + protocol + "'" + " GROUP BY " + KEY_ATTACK_ID;
|
|
|
|
|
|
+ String countQuery = "SELECT * FROM " + TABLE_RECORDS + " WHERE " + KEY_PROTOCOL + " = " + "'" + protocol + "'" + " GROUP BY " + KEY_ATTACK_ID;
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
Cursor cursor = db.rawQuery(countQuery, null);
|
|
Cursor cursor = db.rawQuery(countQuery, null);
|
|
int result = cursor.getCount();
|
|
int result = cursor.getCount();
|
|
@@ -176,25 +208,54 @@ public class DatabaseHandler extends SQLiteOpenHelper {
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
- public SQLRecord getFirstEntry(){
|
|
|
|
- //TODO Fixen oder löschen
|
|
|
|
- SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
- Cursor cursor = db.query(TABLE_RECORDS, new String[] { KEY_ID,
|
|
|
|
- KEY_ATTACK_ID, KEY_PROTOCOL, KEY_TYPE, KEY_TIME, KEY_LOCAL_IP,
|
|
|
|
- KEY_LOCAL_PORT, KEY_REMOTE_IP, KEY_REMOTE_PORT, KEY_PACKET },
|
|
|
|
- KEY_ID + "=?", new String[] { "min(" + KEY_ID + ")" }, null,
|
|
|
|
- null, null, null);
|
|
|
|
|
|
+ // Getting first Record for each AttackId
|
|
|
|
+ public ArrayList<Record> getRecordOfEachAtack() {
|
|
|
|
+ ArrayList<Record> recordList = new ArrayList<Record>();
|
|
|
|
+ String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " WHERE " + " GROUP BY " + KEY_ATTACK_ID;
|
|
|
|
+ SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
+ Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
|
|
|
- if (cursor != null)
|
|
|
|
- cursor.moveToFirst();
|
|
|
|
|
|
+ // looping through all rows and adding to list
|
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
|
+ do {
|
|
|
|
+ Record record = new Record();
|
|
|
|
+ try {
|
|
|
|
+ record.setID(Integer.parseInt(cursor.getString(0)));
|
|
|
|
+ record.setAttack_id(cursor.getLong(1));
|
|
|
|
+ record.setProtocol(cursor.getString(2));
|
|
|
|
+ record.setType(cursor.getString(3).equals("SEND") ? TYPE.SEND : TYPE.RECEIVE);
|
|
|
|
+ record.setTimestamp(cursor.getLong(4));
|
|
|
|
+ record.setLocalIP(InetAddress.getByAddress(cursor.getString(6), cursor.getBlob(5)));
|
|
|
|
+ record.setLocalPort(Integer.parseInt(cursor.getString(7)));
|
|
|
|
+ record.setRemoteIP(InetAddress.getByAddress(cursor.getString(9), cursor.getBlob(8)));
|
|
|
|
+ record.setRemotePort(Integer.parseInt(cursor.getString(10)));
|
|
|
|
+ record.setBSSID(cursor.getString(11));
|
|
|
|
+ record.setSSID(cursor.getString(12));
|
|
|
|
+ record.setPacket(cursor.getString(13));
|
|
|
|
+ } catch (UnknownHostException e) {
|
|
|
|
+ // TODO Auto-generated catch block
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Adding record to list
|
|
|
|
+ recordList.add(record);
|
|
|
|
+ } while (cursor.moveToNext());
|
|
|
|
+ }
|
|
|
|
+ cursor.close();
|
|
|
|
|
|
- SQLRecord record = new SQLRecord(Integer.parseInt(cursor.getString(0)),
|
|
|
|
- Integer.parseInt(cursor.getString(1)), cursor.getString(2), cursor.getString(3),
|
|
|
|
- cursor.getString(4), cursor.getString(5),
|
|
|
|
- Integer.parseInt(cursor.getString(6)), cursor.getString(7),
|
|
|
|
- Integer.parseInt(cursor.getString(8)), cursor.getString(9));
|
|
|
|
- return record;
|
|
|
|
|
|
+ // return count
|
|
|
|
+ db.close();
|
|
|
|
+ return recordList;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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();
|
|
|
|
+ Cursor cursor = db.rawQuery(countQuery, null);
|
|
|
|
+ int result = cursor.getCount();
|
|
|
|
+ cursor.close();
|
|
|
|
+
|
|
|
|
+ return result > 0;
|
|
}
|
|
}
|
|
|
|
|
|
//Delete all Data from Database
|
|
//Delete all Data from Database
|