Browse Source

porthack refactored

Mihai Plasoianu 10 years ago
parent
commit
979e60078e

+ 11 - 15
native/pp.c

@@ -1,6 +1,6 @@
-#include <android/log.h>
 #include <errno.h>
 #include <netinet/in.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/socket.h>
@@ -9,10 +9,6 @@
 #include <sys/un.h>
 #include <unistd.h>
 
-#define  LOG_TAG "hostage: pp"
-#define  LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
-#define  LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
-
 #define CONTROLLEN CMSG_LEN(sizeof(int))
 
 #define UNIX_PATH "hostage"
@@ -22,7 +18,7 @@ int ipc_sock() {
 	struct sockaddr_un addr;
 
 	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
-		LOGE("Unable to create local socket: %d", errno);
+		perror("Unable to create local socket");
 		return -1;
 	}
 
@@ -32,7 +28,7 @@ int ipc_sock() {
 
 	if (connect(fd, (struct sockaddr*) &addr,
 			sizeof(sa_family_t) + strlen(UNIX_PATH) + 1) == -1) {
-		LOGE("Unable to connect local socket: %d", errno);
+		perror("Unable to connect local socket");
 		return -1;
 	}
 
@@ -45,7 +41,7 @@ int net_sock(int port) {
 	struct sockaddr_in addr;
 
 	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
-		LOGE("Unable to create net socket: %d", errno);
+		perror("Unable to create net socket");
 		return -1;
 	}
 
@@ -55,17 +51,17 @@ int net_sock(int port) {
 	addr.sin_port = htons(port);
 
 	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) {
-		LOGE("Unable to set socket options: %d", errno);
+		perror("Unable to set socket options");
 		return -1;
 	}
 
 	if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
-		LOGE("Unable to bind net socket: %d", errno);
+		perror("Unable to bind net socket");
 		return -1;
 	}
 
 	if (listen(fd, 5) == -1) {
-		LOGE("Unable to listen net socket: %d", errno);
+		perror("Unable to listen net socket");
 		return -1;
 	}
 
@@ -95,7 +91,7 @@ int send_fd(int fd, int fd_to_send) {
 	*(int *) CMSG_DATA(cmptr) = fd_to_send;
 
 	if (sendmsg(fd, &msg, 0) == -1) {
-		LOGE("sendmsg failed: %d", errno);
+		perror("sendmsg failed");
 	}
 
 	return 0;
@@ -117,18 +113,18 @@ int main(int argc, char *argv[]) {
 		close(ipc_fd);
 		exit(EXIT_FAILURE);
 	}
-	LOGD("ipc_fd: %d", ipc_fd);
+	printf("ipc_fd: %d\n", ipc_fd);
 
 	if ((net_fd = net_sock(port)) == -1) {
 		close(ipc_fd);
 		close(net_fd);
 		exit(EXIT_FAILURE);
 	}
-	LOGD("net_fd: %d", net_fd);
+	printf("net_fd: %d\n", net_fd);
 
 	int status;
 	status = send_fd(ipc_fd, net_fd);
-	LOGD("send_fd: %d", status);
+	printf("send_fd: %d\n", status);
 
 	close(ipc_fd);
 	close(net_fd);

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

@@ -30,7 +30,8 @@ public class MyServerSocketFactory extends ServerSocketFactory {
 				injectFD(fd, impl);
 				injectImpl(impl, socket);
 				setBound(socket);
-			} catch (ReflectiveOperationException e) {
+			} catch (NoSuchFieldException e) {
+			} catch (IllegalAccessException e) {
 			} catch (IllegalArgumentException e) {
 			}
 		}
@@ -47,13 +48,13 @@ public class MyServerSocketFactory extends ServerSocketFactory {
 		return createServerSocket(port);
 	}
 
-	private SocketImpl getImpl(ServerSocket socket) throws ReflectiveOperationException, IllegalArgumentException {
+	private SocketImpl getImpl(ServerSocket socket) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
 		Field implField = socket.getClass().getDeclaredField("impl");
 		implField.setAccessible(true);
 		return (SocketImpl) implField.get(socket);
 	}
 
-	private void injectFD(FileDescriptor fd, SocketImpl impl) throws ReflectiveOperationException, IllegalArgumentException {
+	private void injectFD(FileDescriptor fd, SocketImpl impl) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
 		Class<?> plainServerSocketImplClazz = impl.getClass();
 		Class<?> plainSocketImplClazz = plainServerSocketImplClazz.getSuperclass();
 		Class<?> socketImplClazz = plainSocketImplClazz.getSuperclass();
@@ -62,13 +63,13 @@ public class MyServerSocketFactory extends ServerSocketFactory {
 		fdField.set(impl, fd);
 	}
 
-	private void injectImpl(SocketImpl impl, ServerSocket socket) throws ReflectiveOperationException, IllegalArgumentException {
+	private void injectImpl(SocketImpl impl, ServerSocket socket) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
 		Field implField = socket.getClass().getDeclaredField("impl");
 		implField.setAccessible(true);
 		implField.set(socket, impl);
 	}
 
-	private void setBound(ServerSocket socket) throws ReflectiveOperationException, IllegalArgumentException {
+	private void setBound(ServerSocket socket) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
 		Field boundField = socket.getClass().getDeclaredField("isBound");
 		boundField.setAccessible(true);
 		boundField.set(socket, true);

+ 8 - 0
src/de/tudarmstadt/informatik/hostage/system/Device.java

@@ -2,8 +2,14 @@ package de.tudarmstadt.informatik.hostage.system;
 
 import java.io.IOException;
 
+import android.util.Log;
+
 public class Device {
 
+	@SuppressWarnings("unused")
+	// DO NOT REMOVE, NEEDED FOR SINGLETON INITIALIZATION
+	private static final Device INSTANCE = new Device();
+
 	private static boolean initialized = false;
 	private static boolean root = false;
 	private static boolean pp = false;
@@ -33,6 +39,8 @@ public class Device {
 				} catch (IOException e) {
 				} finally {
 					initialized = true;
+					Log.d("hostage", "Root: " + Boolean.toString(root));
+					Log.d("hostage", "PP: " + Boolean.toString(pp));
 				}
 			}
 		}).start();

+ 26 - 1
src/de/tudarmstadt/informatik/hostage/system/PrivilegedPort.java

@@ -1,19 +1,26 @@
 package de.tudarmstadt.informatik.hostage.system;
 
+import java.io.BufferedReader;
 import java.io.FileDescriptor;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 
 import android.net.LocalServerSocket;
 import android.net.LocalSocket;
+import android.util.Log;
 
 public class PrivilegedPort implements Runnable {
 
 	private final static String UNIX_PATH = "hostage";
 
+	private final String LOG_TAG;
+
 	private int port;
 	private FileDescriptor fd;
 
 	public PrivilegedPort(int port) {
+		LOG_TAG = String.format("hostage (pp %d)", port);
 		this.port = port;
 		try {
 			new Thread(this).start();
@@ -40,9 +47,27 @@ public class PrivilegedPort implements Runnable {
 	public void run() {
 		String command = String.format("/data/local/pp %d", port);
 		try {
-			new ProcessBuilder("su", "-c", command).start();
+			Process p = new ProcessBuilder("su", "-c", command).start();
+			if (p.waitFor() != 0) {
+				logError(p.getErrorStream());
+			}
+			logOutput(p.getInputStream());
 		} catch (IOException e) {
+		} catch (InterruptedException e) {
 		}
 	}
 
+	private void logOutput(InputStream stdout) throws IOException {
+		BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
+		String line;
+		while ((line = reader.readLine()) != null) {
+			Log.d(LOG_TAG, line);
+		}
+	}
+
+	private void logError(InputStream stderr) throws IOException {
+		BufferedReader reader = new BufferedReader(new InputStreamReader(stderr));
+		Log.e(LOG_TAG, reader.readLine());
+	}
+
 }