Browse Source

Added SO_REUSEADDR option to Socket in porthack, to fix restart bug.
Minor Changes for saving states in preferences

Lars 10 years ago
parent
commit
8bcee26e63

+ 1 - 1
native/Makefile

@@ -17,4 +17,4 @@ clean:
 	rm -f *.o $(EXE)
 
 install:
-	cp $(EXE) ../assets
+	adb push p /data/local

BIN
native/p


+ 7 - 1
native/p.c

@@ -12,7 +12,7 @@
 
 #include <android/log.h>
 
-#define  LOG_TAG "hostage: bindport"
+#define  LOG_TAG "hostage: p"
 #define  LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
 #define  LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)
 #define  LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
@@ -45,6 +45,7 @@ int ipc_sock() {
 
 int net_sock(int port) {
 	int fd;
+	int reuse = 1;
 	struct sockaddr_in addr;
 
 	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
@@ -57,6 +58,11 @@ int net_sock(int port) {
 	addr.sin_addr.s_addr = INADDR_ANY;
 	addr.sin_port = htons(port);
 
+	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1){ 
+		LOGE("Unable to set socketopt: %d", errno); 
+		return -1; 
+	}
+
 	if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
 		LOGE("Unable to bind net socket: %d", errno);
 		return -1;

BIN
native/p.o


+ 10 - 24
src/de/tudarmstadt/informatik/hostage/HoneyHandler.java

@@ -10,6 +10,7 @@ import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.preference.PreferenceManager;
+import android.util.Log;
 import de.tudarmstadt.informatik.hostage.io.ByteArrayReaderWriter;
 import de.tudarmstadt.informatik.hostage.io.ReaderWriter;
 import de.tudarmstadt.informatik.hostage.io.StringReaderWriter;
@@ -43,7 +44,7 @@ public class HoneyHandler implements Runnable {
 	protected Thread thread;
 
 	private int attack_id;
-	private int message_id;
+	private int message_id = 0;
 	private String externalIP;
 	private String BSSID;
 	private String SSID;
@@ -69,15 +70,12 @@ public class HoneyHandler implements Runnable {
 		this.protocol = protocol;
 		this.client = client;
 		this.thread = new Thread(this);
-		SharedPreferences pref = PreferenceManager
-				.getDefaultSharedPreferences(service);
+		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(service);
 		SLEEPTIME = pref.getInt("sleeptime", 1); // 1 ms already removes ressource leak
 		TIMEOUT = pref.getInt("timeout", 30) * 1000;
 		// TODO ThreadSicher?
-		attack_id = getAndIncrementAttackID(pref);
-		message_id = 0;
-		SharedPreferences sessionPref = service.getSharedPreferences(
-				MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
+		getAndIncrementAttackID(pref);
+		SharedPreferences sessionPref = service.getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
 		BSSID = sessionPref.getString(MainActivity.BSSID, null);
 		SSID = sessionPref.getString(MainActivity.SSID, null);
 		externalIP = sessionPref.getString(MainActivity.EXTERNAL_IP, null);
@@ -116,6 +114,7 @@ public class HoneyHandler implements Runnable {
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		Log.i("HoneyHandler", "Connection closed, close Socket and kill Thread");
 		kill();
 	}
 
@@ -126,6 +125,7 @@ public class HoneyHandler implements Runnable {
 		thread.interrupt();
 		try {
 			client.close();
+			Log.i("HoneyHandler", "Socket closed: " + client.isClosed());
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
@@ -197,7 +197,7 @@ public class HoneyHandler implements Runnable {
 	 */
 	protected Record createRecord(TYPE type, String packet) {
 		Record record = new Record();
-		record.setId(getAndIncrementMessageCounter());
+		record.setId(message_id++);
 		record.setAttack_id(attack_id);
 		record.setProtocol(protocol.toString());
 		record.setType(type);
@@ -238,24 +238,10 @@ public class HoneyHandler implements Runnable {
 	 *            The default SharedPreference of the application
 	 * @return Unique integer attack ID
 	 */
-	private synchronized int getAndIncrementAttackID(SharedPreferences pref) {
+	private synchronized void getAndIncrementAttackID(SharedPreferences pref) {
 		Editor editor = pref.edit();
-		int attackID = pref.getInt("ATTACK_ID_COUNTER", 0);
+		attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
 		editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
 		editor.commit();
-		return attackID;
 	}
-
-	/**
-	 * Gets current message ID for the record. Also increases the message ID
-	 * counter by one. Method is synchronized for thread safety.
-	 * 
-	 * @return Unique integer message ID
-	 */
-	private synchronized int getAndIncrementMessageCounter() {
-		int msgID = message_id;
-		message_id++;
-		return msgID;
-	}
-
 }

+ 15 - 17
src/de/tudarmstadt/informatik/hostage/HoneyListener.java

@@ -12,7 +12,6 @@ import javax.net.ssl.SSLSocketFactory;
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
-import android.util.Log;
 import de.tudarmstadt.informatik.hostage.net.MyServerSocketFactory;
 import de.tudarmstadt.informatik.hostage.protocol.Protocol;
 import de.tudarmstadt.informatik.hostage.protocol.SSLProtocol;
@@ -47,11 +46,11 @@ public class HoneyListener implements Runnable {
 
 	private HoneyService service;
 	// Shared Preferences
-	private SharedPreferences pref;
+	private SharedPreferences sessionPref;
 	private ConnectionRegister conReg;
 
 	// Editor for Shared preferences
-	private Editor editor;
+	private Editor sessionEditor;
 
 	private boolean running = false;
 
@@ -75,9 +74,8 @@ public class HoneyListener implements Runnable {
 	public HoneyListener(HoneyService service, Protocol protocol) {
 		this.service = service;
 		this.protocol = protocol;
-		pref = service.getApplicationContext().getSharedPreferences(
-				MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
-		editor = pref.edit();
+		sessionPref = service.getApplicationContext().getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
+		sessionEditor = sessionPref.edit();
 		conReg = new ConnectionRegister(service);
 	}
 
@@ -99,9 +97,8 @@ public class HoneyListener implements Runnable {
 			server = new MyServerSocketFactory().createServerSocket(protocol
 					.getPort());
 			(this.thread = new Thread(this)).start();
-			Log.i("BugSearch", "Open ServerSocket - ServerSocket is: " + ((server == null) ? "null" : "not null"));
-			editor.putBoolean(protocol + MainActivity.LISTENER, true);
-			editor.commit();
+			sessionEditor.putBoolean(protocol + MainActivity.LISTENER, true);
+			sessionEditor.commit();
 			service.notifyUI(protocol.toString(), MainActivity.LISTENER);
 			running = true;
 		} catch (Exception e) {
@@ -115,11 +112,10 @@ public class HoneyListener implements Runnable {
 	 */
 	public void stop() {
 		try {
-			Log.i("BugSearch", "Closing ServerSocket - ServerSocket is: " + ((server == null) ? "null" : "not null"));
 			server.close();
 			thread.interrupt();
-			editor.putBoolean(protocol + MainActivity.LISTENER, false);
-			editor.commit();
+			sessionEditor.putBoolean(protocol + MainActivity.LISTENER, false);
+			sessionEditor.commit();
 			service.notifyUI(protocol.toString(), MainActivity.LISTENER);
 			running = false;
 		} catch (Exception e) {
@@ -146,6 +142,10 @@ public class HoneyListener implements Runnable {
 			if (handler.isTerminated()) {
 				conReg.closeConnection();
 				iterator.remove();
+				int handlerCount = sessionPref.getInt(protocol + MainActivity.HANDLER_COUNT, 0);
+				sessionEditor.putInt(protocol + MainActivity.HANDLER_COUNT, handlerCount - 1);
+				sessionEditor.commit();
+				service.notifyUI(protocol.toString(), MainActivity.HANDLER_COUNT);
 			}
 		}
 	}
@@ -164,11 +164,9 @@ public class HoneyListener implements Runnable {
 				} else {
 					startHandler(client);
 				}
-				int handlerCount = pref.getInt(protocol
-						+ MainActivity.HANDLER_COUNT, 0);
-				editor.putInt(protocol + MainActivity.HANDLER_COUNT,
-						handlerCount + 1);
-				editor.commit();
+				int handlerCount = sessionPref.getInt(protocol + MainActivity.HANDLER_COUNT, 0);
+				sessionEditor.putInt(protocol + MainActivity.HANDLER_COUNT, handlerCount + 1);
+				sessionEditor.commit();
 				service.notifyUI(protocol.toString(),
 						MainActivity.HANDLER_COUNT);
 			} catch (Exception e) {

+ 6 - 14
src/de/tudarmstadt/informatik/hostage/HoneyService.java

@@ -83,9 +83,9 @@ public class HoneyService extends Service {
 	public void onCreate() {
 		super.onCreate();
 		HoneyService.context = getApplicationContext();
-		sessionPref = getSharedPreferences(MainActivity.SESSION_DATA,
-				Context.MODE_PRIVATE);
+		sessionPref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
 		editor = sessionPref.edit();
+		deleteSessionData();
 		createNotification();
 		for (Protocol protocol : getProtocolArray()) {
 			listeners.add(new HoneyListener(this, protocol));
@@ -105,8 +105,9 @@ public class HoneyService extends Service {
 	@Override
 	public void onDestroy() {
 		cancelNotification();
-		super.onDestroy();
 		unregisterNetReceiver();
+		deleteSessionData();
+		super.onDestroy();
 	}
 
 	/**
@@ -154,15 +155,8 @@ public class HoneyService extends Service {
 		public void onReceive(Context context, Intent intent) {
 			String bssid_old = sessionPref.getString(MainActivity.BSSID, "");
 			String bssid_new = HelperUtils.getBSSID(context);
-			// TODO INFORM UI
-			// TODO CHECK IF OTHER NETWORKS WORK TOO
 			if (bssid_new == null || !bssid_new.equals(bssid_old)) {
 				getLocationData();
-				String[] protocols = getResources().getStringArray(
-						R.array.protocols);
-				for (String protocol : protocols) {
-					editor.remove(protocol + MainActivity.HANDLER_COUNT);
-				}
 				notifyUI("SERVICE", "CONNECTIVITY_CHANGE");
 			}
 		}
@@ -298,14 +292,12 @@ public class HoneyService extends Service {
 		// Send Notification
 		if (key.equals(MainActivity.HANDLER_COUNT)) {
 			updateNotification();
-		} else if (key.equals("CONNECTIVITY_CHANGE")) {
-			createNotification();
 		}
-		Log.i("HoneyService", key);
+		Log.i("HoneyService", sender + key);
 		// Inform UI of Preference Change
 		Intent intent = new Intent(MainActivity.BROADCAST);
 		intent.putExtra("SENDER", sender);
-		intent.putExtra("SENDER", key);
+		intent.putExtra("KEY", key);
 		LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
 	}
 

+ 1 - 5
src/de/tudarmstadt/informatik/hostage/net/MyServerSocketFactory.java

@@ -9,8 +9,6 @@ import java.net.SocketImpl;
 
 import javax.net.ServerSocketFactory;
 
-import android.util.Log;
-
 import de.tudarmstadt.informatik.hostage.system.PrivilegedPort;
 
 /**
@@ -26,12 +24,10 @@ public class MyServerSocketFactory extends ServerSocketFactory {
 	@Override
 	public ServerSocket createServerSocket(int port) throws IOException {
 		FileDescriptor fd = new PrivilegedPort(port).bindAndGetFD();
-
 		ServerSocket socket = new ServerSocket();
-		Log.i("BugSearch", "Creating ServerSocket - ServerSocket is: " + ((socket == null) ? "null" : "not null"));
+		
 		try {
 			SocketImpl impl = getImpl(socket);
-			Log.i("BugSearch", "Creating SocketImpl - SocketImpl is: " + ((impl == null) ? "null" : "not null"));
 			injectFD(fd, impl);
 			injectImpl(impl, socket);
 			setBound(socket);

+ 1 - 11
src/de/tudarmstadt/informatik/hostage/ui/MainActivity.java

@@ -149,8 +149,7 @@ public class MainActivity extends Activity implements Receiver {
 		initViewAnimator();
 		initListView();
 		// Initialize Class variables
-		sessionPref = getSharedPreferences(MainActivity.SESSION_DATA,
-				Context.MODE_PRIVATE);
+		sessionPref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
 		sessionEditor = sessionPref.edit();
 		checkRootAndPorthack();
 	}
@@ -512,15 +511,6 @@ public class MainActivity extends Activity implements Receiver {
 	private BroadcastReceiver netReceiver = new BroadcastReceiver() {
 		@Override
 		public void onReceive(Context context, Intent intent) {
-			/*
-			 * String bssid_old = sessionPref.getString(BSSID, ""); String
-			 * bssid_new = HelperUtils.getBSSID(context);
-			 * 
-			 * if ((bssid_new == null || !bssid_new.equals(bssid_old)) &&
-			 * serviceBound) { Toast.makeText(getApplicationContext(),
-			 * "Connection changed! Services stopped!",
-			 * Toast.LENGTH_LONG).show(); unbindService(); }
-			 */
 			updateConnectionInfText();
 		}
 	};