|
@@ -1,5 +1,7 @@
|
|
package de.tudarmstadt.informatik.hostage;
|
|
package de.tudarmstadt.informatik.hostage;
|
|
|
|
|
|
|
|
+import android.content.SharedPreferences;
|
|
|
|
+import android.preference.PreferenceManager;
|
|
import android.util.Log;
|
|
import android.util.Log;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -14,16 +16,23 @@ public class ConnectionGuard {
|
|
private ConnectionGuard() {
|
|
private ConnectionGuard() {
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * Intervall between 2 connection in wich we assume a port scan
|
|
|
|
- */
|
|
|
|
- public final static long TIMESTAMP_THRESHOLD_MS = 60000;
|
|
|
|
-
|
|
|
|
private static long lastConnectionTimestamp = 0;
|
|
private static long lastConnectionTimestamp = 0;
|
|
private static long lastPortscanTimestamp = 0;
|
|
private static long lastPortscanTimestamp = 0;
|
|
private static String lastIP = "";
|
|
private static String lastIP = "";
|
|
private static int lastPort = 0;
|
|
private static int lastPort = 0;
|
|
|
|
|
|
|
|
+ private static long getPortscanTimeout() {
|
|
|
|
+ // this might be called in a time critical context so maybe
|
|
|
|
+ // we don't want to do this each time and instead just once
|
|
|
|
+ SharedPreferences defaultPref = PreferenceManager
|
|
|
|
+ .getDefaultSharedPreferences(Hostage.getContext());
|
|
|
|
+ // the pref value is in seconds
|
|
|
|
+ int portscanTimeoutS = Integer.parseInt(defaultPref.getString("pref_timeout", "60"));
|
|
|
|
+ long portscanTimeoutMS = 1000 * portscanTimeoutS;
|
|
|
|
+
|
|
|
|
+ return portscanTimeoutMS;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Register a connection for port scan detection. Stores information about the last connection.
|
|
* Register a connection for port scan detection. Stores information about the last connection.
|
|
* @param port The local port used for communication.
|
|
* @param port The local port used for communication.
|
|
@@ -44,7 +53,7 @@ public class ConnectionGuard {
|
|
}
|
|
}
|
|
|
|
|
|
public synchronized static boolean portscanInProgress() {
|
|
public synchronized static boolean portscanInProgress() {
|
|
- return (System.currentTimeMillis() - lastPortscanTimestamp) < TIMESTAMP_THRESHOLD_MS;
|
|
|
|
|
|
+ return (System.currentTimeMillis() - lastPortscanTimestamp) < getPortscanTimeout();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -58,7 +67,7 @@ public class ConnectionGuard {
|
|
Log.i("Alte Werte:", "LastTime: " + lastConnectionTimestamp + " ,LastIP: " + lastIP + ", lastPort:" + port);
|
|
Log.i("Alte Werte:", "LastTime: " + lastConnectionTimestamp + " ,LastIP: " + lastIP + ", lastPort:" + port);
|
|
Log.i("Alte Werte:", "Time: " + timestamp + " ,IP: " + ip + ", Port:" + port);
|
|
Log.i("Alte Werte:", "Time: " + timestamp + " ,IP: " + ip + ", Port:" + port);
|
|
boolean result = false;
|
|
boolean result = false;
|
|
- boolean belowThreshold = ((timestamp - lastConnectionTimestamp) < TIMESTAMP_THRESHOLD_MS);
|
|
|
|
|
|
+ boolean belowThreshold = ((timestamp - lastConnectionTimestamp) < getPortscanTimeout());
|
|
boolean sameIP = (lastIP.equals(ip));
|
|
boolean sameIP = (lastIP.equals(ip));
|
|
boolean samePort = (lastPort == port);
|
|
boolean samePort = (lastPort == port);
|
|
if (sameIP && belowThreshold && !samePort) {
|
|
if (sameIP && belowThreshold && !samePort) {
|