Browse Source

portscan detection implemented (only logger output)

Mihai Plasoianu 10 years ago
parent
commit
8e38f676eb

+ 1 - 1
project.properties

@@ -15,4 +15,4 @@ target=android-19
 android.library=false
 
 
-android.library.reference.1=../../development/Android/HosTaGe/google-play-services_lib
+android.library.reference.1=../google-play-services_lib

+ 25 - 0
src/de/tudarmstadt/informatik/hostage/ConnectionGuard.java

@@ -0,0 +1,25 @@
+package de.tudarmstadt.informatik.hostage;
+
+import android.util.Log;
+
+public class ConnectionGuard {
+
+	private final static ConnectionGuard INSTANCE = new ConnectionGuard();
+
+	private ConnectionGuard() {
+	}
+
+	private final static long ONE_SECOND_IN_NANOSECONDS = 1000000000;
+
+	private static long lastTimestamp = 0;
+
+	public static void registerConnection() {
+		long timestamp = System.nanoTime();
+		boolean firstConnection = (lastTimestamp == 0);
+		boolean belowThreshold = ((timestamp - lastTimestamp) < ONE_SECOND_IN_NANOSECONDS);
+		if (!firstConnection && belowThreshold) {
+			Log.d("ConnectionGuard", "PORTSCAN DETECTED");
+		}
+		lastTimestamp = timestamp;
+	}
+}

+ 8 - 8
src/de/tudarmstadt/informatik/hostage/Listener.java

@@ -127,8 +127,10 @@ public class Listener implements Runnable {
 			server = new MyServerSocketFactory().createServerSocket(port);
 			if (server == null)
 				return false;
-			if(protocol.toString().equals("SMB")) {
-				((SMB) protocol).setIP(Hostage.getContext().getSharedPreferences(Hostage.getContext().getString(R.string.connection_info), Hostage.MODE_PRIVATE).getString(Hostage.getContext().getString(R.string.connection_info_internal_ip), ""));
+			if (protocol.toString().equals("SMB")) {
+				((SMB) protocol).setIP(Hostage.getContext()
+						.getSharedPreferences(Hostage.getContext().getString(R.string.connection_info), Hostage.MODE_PRIVATE)
+						.getString(Hostage.getContext().getString(R.string.connection_info_internal_ip), ""));
 			}
 			(this.thread = new Thread(this)).start();
 			running = true;
@@ -158,13 +160,13 @@ public class Listener implements Runnable {
 	}
 
 	/**
-	 * Waits for an incoming connection, accepts it and starts a
-	 * {@link Handler}
+	 * Waits for an incoming connection, accepts it and starts a {@link Handler}
 	 */
 	private void addHandler() {
 		if (conReg.isConnectionFree()) {
 			try {
 				Socket client = server.accept();
+				ConnectionGuard.registerConnection();
 				conReg.newOpenConnection();
 				if (protocol.isSecure()) {
 					startSecureHandler(client);
@@ -188,8 +190,7 @@ public class Listener implements Runnable {
 	 *            The Protocol the handler will run on
 	 * @param client
 	 *            The Socket the handler uses
-	 * @return A Instance of a {@link Handler} with the specified
-	 *         parameter.
+	 * @return A Instance of a {@link Handler} with the specified parameter.
 	 */
 	private Handler newInstance(Hostage service, Listener listener, Protocol protocol, Socket client) {
 		return new Handler(service, listener, protocol, client);
@@ -207,8 +208,7 @@ public class Listener implements Runnable {
 	}
 
 	/**
-	 * Creates a SSLSocket out of the given socket and starts a
-	 * {@link Handler}.
+	 * Creates a SSLSocket out of the given socket and starts a {@link Handler}.
 	 * 
 	 * @param client
 	 *            The socket with the accepted connection.