Browse Source

UI improved

mip-it 11 năm trước cách đây
mục cha
commit
dd73d85973

BIN
bin/classes.dex


BIN
bin/classes/de/tudarmstadt/informatik/hostage/HoneyService$LocalBinder.class


BIN
bin/classes/de/tudarmstadt/informatik/hostage/HoneyService.class


BIN
bin/classes/de/tudarmstadt/informatik/hostage/ui/MainActivity$1.class


BIN
bin/classes/de/tudarmstadt/informatik/hostage/ui/MainActivity$2.class


BIN
bin/classes/de/tudarmstadt/informatik/hostage/ui/MainActivity$3.class


BIN
bin/classes/de/tudarmstadt/informatik/hostage/ui/MainActivity$4.class


BIN
bin/classes/de/tudarmstadt/informatik/hostage/ui/MainActivity$5.class


BIN
bin/classes/de/tudarmstadt/informatik/hostage/ui/MainActivity.class


BIN
bin/hostage.apk


+ 23 - 0
native/Makefile

@@ -0,0 +1,23 @@
+CC = arm-linux-androideabi-gcc
+CFLAGS = -Wall -g
+LDFLAGS = -llog
+SRC = porthack.c
+OBJ = $(SRC:.c=.o)
+EXE = p
+ 
+all: $(SRC) $(EXE)
+
+$(EXE): $(OBJ)
+	$(CC) -o $@ $^ $(LDFLAGS)
+ 
+%.o: %.c
+	$(CC) -o $@ -c $< $(CFLAGS)
+
+clean:
+	rm -f *.o $(EXE)
+
+install:
+	adb push p /data/local
+
+run:
+	adb shell /data/local/p 21

+ 139 - 0
native/porthack.c

@@ -0,0 +1,139 @@
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/time.h>
+#include <sys/uio.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include <android/log.h>
+
+#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__)
+
+#define CONTROLLEN CMSG_LEN(sizeof(int))
+
+char *socket_path = "\0hostage";
+
+int ipc_sock() {
+	int fd;
+	struct sockaddr_un addr;
+
+	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+		LOGE("Unable to create local socket: %d", errno);
+		return -1;
+	}
+
+	memset(&addr, 0, sizeof(addr));
+	addr.sun_family = AF_UNIX;
+	strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
+
+	if (connect(fd, (struct sockaddr*) &addr, sizeof(addr)) == -1) {
+		LOGE("Unable to connect local socket: %d", errno);
+		return -1;
+	}
+
+	return fd;
+}
+
+int net_sock(int port) {
+	int fd;
+	struct sockaddr_in addr;
+
+	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
+		LOGE("Unable to create net socket: %d", errno);
+		return -1;
+	}
+
+	memset(&addr, 0, sizeof(addr));
+	addr.sin_family = AF_INET;
+	addr.sin_addr.s_addr = INADDR_ANY;
+	addr.sin_port = htons(port);
+
+	if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
+		LOGE("Unable to bind net socket: %d", errno);
+		return -1;
+	}
+
+	if (listen(fd, 5) == -1) {
+		LOGE("Unable to listen net socket: %d", errno);
+		return -1;
+	}
+
+	return fd;
+}
+
+int send_fd(int fd, int fd_to_send) {
+	struct iovec iov[1];
+	struct cmsghdr *cmptr;
+	struct msghdr msg;
+	char buf[2] = "FD";
+
+	iov[0].iov_base = buf;
+	iov[0].iov_len = 2;
+
+	cmptr = malloc(CONTROLLEN);
+	cmptr->cmsg_level = SOL_SOCKET;
+	cmptr->cmsg_type = SCM_RIGHTS;
+	cmptr->cmsg_len = CONTROLLEN;
+
+	msg.msg_iov = iov;
+	msg.msg_iovlen = 1;
+	msg.msg_name = NULL;
+	msg.msg_namelen = 0;
+	msg.msg_control = cmptr;
+	msg.msg_controllen = CONTROLLEN;
+	*(int *) CMSG_DATA(cmptr) = fd_to_send;
+
+	if (sendmsg(fd, &msg, 0) == -1) {
+		LOGE("sendmsg failed: %d", errno);
+	}
+
+	return 0;
+}
+
+int main(int argc, char *argv[]) {
+	int port;
+	int ipc_fd, net_fd;
+
+	if (argc < 2) {
+		exit(EXIT_FAILURE);
+	}
+
+	if ((port = atoi(argv[1])) < 1 || (port = atoi(argv[1])) > 65535) {
+		exit(EXIT_FAILURE);
+	}
+
+	if ((ipc_fd = ipc_sock()) == -1) {
+		close(ipc_fd);
+		exit(EXIT_FAILURE);
+	}
+	LOGI("ipc_fd: %d", ipc_fd);
+
+	if ((net_fd = net_sock(port)) == -1) {
+		close(ipc_fd);
+		close(net_fd);
+		exit(EXIT_FAILURE);
+	}
+	LOGI("net_fd: %d", net_fd);
+
+	int status;
+	status = send_fd(ipc_fd, net_fd);
+	LOGI("send_fd: %d", status);
+
+	close(ipc_fd);
+	close(net_fd);
+
+	if (status == -1) {
+		return (EXIT_FAILURE);
+	}
+
+	return EXIT_SUCCESS;
+}

+ 27 - 0
src/de/tudarmstadt/informatik/hostage/HoneyService.java

@@ -3,10 +3,15 @@ package de.tudarmstadt.informatik.hostage;
 import java.util.ArrayList;
 import java.util.List;
 
+import android.app.NotificationManager;
+import android.app.PendingIntent;
 import android.app.Service;
+import android.content.Context;
 import android.content.Intent;
 import android.os.Binder;
 import android.os.IBinder;
+import android.support.v4.app.NotificationCompat;
+import android.support.v4.app.TaskStackBuilder;
 import android.support.v4.content.LocalBroadcastManager;
 import de.tudarmstadt.informatik.hostage.logging.FileLogger;
 import de.tudarmstadt.informatik.hostage.logging.Logger;
@@ -44,6 +49,7 @@ public class HoneyService extends Service {
 	public void onCreate() {
 		super.onCreate();
 		log = new FileLogger(getApplicationContext());
+		createNotification();
 		for (Protocol protocol : getProtocolArray()) {
 			listeners.add(new HoneyListener(this, protocol));
 		}
@@ -51,10 +57,31 @@ public class HoneyService extends Service {
 
 	@Override
 	public void onDestroy() {
+		cancelNotification();
 		log.close();
 		super.onDestroy();
 	}
 
+	private void createNotification() {
+		NotificationCompat.Builder builder = new NotificationCompat.Builder(
+				this).setSmallIcon(R.drawable.ic_launcher)
+				.setContentTitle(getString(R.string.app_name))
+				.setContentText("Honeypot up and running!");
+		TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
+		stackBuilder.addParentStack(MainActivity.class);
+		stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
+		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
+				PendingIntent.FLAG_UPDATE_CURRENT);
+		builder.setContentIntent(resultPendingIntent);
+		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+		mNotificationManager.notify(1, builder.build());
+	}
+
+	private void cancelNotification() {
+		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+		mNotificationManager.cancel(1);
+	}
+
 	private ArrayList<Protocol> getProtocolArray() {
 		String[] protocols = getResources().getStringArray(R.array.protocols);
 		String packageName = Protocol.class.getPackage().getName();

+ 38 - 13
src/de/tudarmstadt/informatik/hostage/ui/MainActivity.java

@@ -1,5 +1,6 @@
 package de.tudarmstadt.informatik.hostage.ui;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 
@@ -12,6 +13,8 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.ServiceConnection;
+import android.net.LocalSocket;
+import android.net.LocalSocketAddress;
 import android.os.Bundle;
 import android.os.IBinder;
 import android.support.v4.content.LocalBroadcastManager;
@@ -57,13 +60,13 @@ public class MainActivity extends Activity {
 	private ListView listView;
 	private ListViewAdapter adapter;
 
+	private boolean running;
+
 	@Override
 	protected void onCreate(Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
 		setContentView(R.layout.activity_main);
 
-		startService(getServiceIntent());
-
 		initViewAnimator();
 		initListView();
 	}
@@ -78,13 +81,17 @@ public class MainActivity extends Activity {
 	protected void onStart() {
 		super.onStart();
 		registerReceiver();
-		bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
+		if (isServiceRunning()) {
+			bindService(getServiceIntent(), mConnection, BIND_ABOVE_CLIENT);
+		}
 	}
 
 	@Override
 	protected void onStop() {
 		super.onStop();
-		unbindService(mConnection);
+		if (isServiceRunning()) {
+			unbindService(mConnection);
+		}
 		unregisterReceiver();
 	}
 
@@ -95,18 +102,24 @@ public class MainActivity extends Activity {
 
 	public void buttonOnOffClick(View view) {
 		if (((ToggleButton) view).isChecked()) {
-			if (isParanoid()) {
-				mService.startListeners();
-			} else {
-				mService.startListener("SMB");
-			}
-			findViewById(R.id.checkBoxParanoid).setEnabled(false);
+			startAndBind();
 		} else {
-			mService.stopListeners();
-			findViewById(R.id.checkBoxParanoid).setEnabled(true);
+			stopAndUnbind();
+			running = false;
 		}
 	}
 
+	private void startAndBind() {
+		startService(getServiceIntent());
+		bindService(getServiceIntent(), mConnection, BIND_ABOVE_CLIENT);
+	}
+
+	private void stopAndUnbind() {
+		mService.stopListeners();
+		unbindService(mConnection);
+		stopService(getServiceIntent());
+	}
+
 	private boolean isParanoid() {
 		return ((CheckBox) findViewById(R.id.checkBoxParanoid)).isChecked();
 	}
@@ -152,7 +165,9 @@ public class MainActivity extends Activity {
 					int position, long id) {
 				String protocolName = ((HashMap<String, String>) adapter
 						.getItem(position)).get("protocol");
-				mService.toggleListener(protocolName);
+				if (isServiceRunning()) {
+					mService.toggleListener(protocolName);
+				}
 			}
 
 		});
@@ -210,6 +225,15 @@ public class MainActivity extends Activity {
 		@Override
 		public void onServiceConnected(ComponentName name, IBinder service) {
 			mService = ((LocalBinder) service).getService();
+			if (!running) {
+				if (isParanoid()) {
+					mService.startListeners();
+				} else {
+					mService.startListener("SMB");
+				}
+			}
+			running = true;
+			updateUI();
 		}
 
 		@Override
@@ -234,6 +258,7 @@ public class MainActivity extends Activity {
 		public void onReceive(Context context, Intent intent) {
 			updateUI();
 		}
+
 	};
 
 	private void updateUI() {