Browse Source

Root and Porthack check rewritten

Mihai Plasoianu 10 years ago
parent
commit
071fc220af

BIN
assets/p


+ 3 - 3
native/Makefile

@@ -3,9 +3,9 @@ x86: CC = i686-linux-android-gcc
 mips: CC = mipsel-linux-android-gcc
 CFLAGS = -Wall -g
 LDFLAGS = -llog
-SRC = p.c
+SRC = pp.c
 OBJ = $(SRC:.c=.o)
-EXE = p
+EXE = pp
 
 arm x86 mips: $(SRC) $(EXE)
 
@@ -19,4 +19,4 @@ clean:
 	rm -f *.o $(EXE)
 
 install:
-	adb push p /data/local
+	adb push pp /data/local

+ 1 - 1
native/p.c → native/pp.c

@@ -9,7 +9,7 @@
 #include <sys/un.h>
 #include <unistd.h>
 
-#define  LOG_TAG "hostage: p"
+#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__)
 

+ 13 - 45
src/de/tudarmstadt/informatik/hostage/net/MyServerSocketFactory.java

@@ -10,21 +10,11 @@ import java.net.SocketImpl;
 
 import javax.net.ServerSocketFactory;
 
-import de.tudarmstadt.informatik.hostage.system.P;
-import de.tudarmstadt.informatik.hostage.ui.MainActivity;
+import de.tudarmstadt.informatik.hostage.system.Device;
+import de.tudarmstadt.informatik.hostage.system.PrivilegedPort;
 
-/**
- * Server Socket Factory using file descriptors.
- * 
- * @author Mihai Plasoianu
- * @author Lars Pandikow
- */
 public class MyServerSocketFactory extends ServerSocketFactory {
 
-	/**
-	 * This method creates and returns a ServerSocket. A custom SocketImpl is
-	 * injected into the ServerSocket.
-	 */
 	@Override
 	public ServerSocket createServerSocket(int port) throws IOException {
 		ServerSocket socket = null;
@@ -32,75 +22,53 @@ public class MyServerSocketFactory extends ServerSocketFactory {
 			socket = new ServerSocket();
 			socket.setReuseAddress(true);
 			socket.bind(new InetSocketAddress(port));
-		} else if (MainActivity.porthackInstalled) {
-			FileDescriptor fd = new P(port).bindAndGetFD();
+		} else if (Device.isPPInstalled()) {
+			FileDescriptor fd = new PrivilegedPort(port).getFD();
 			socket = new ServerSocket();
 			try {
 				SocketImpl impl = getImpl(socket);
 				injectFD(fd, impl);
 				injectImpl(impl, socket);
 				setBound(socket);
-			} catch (Exception e) {
-				e.printStackTrace();
+			} catch (ReflectiveOperationException e) {
+			} catch (IllegalArgumentException e) {
 			}
 		}
 		return socket;
 	}
 
-	/**
-	 * Must override.
-	 */
 	@Override
-	public ServerSocket createServerSocket(int port, int backlog)
-			throws IOException {
+	public ServerSocket createServerSocket(int port, int backlog) throws IOException {
 		return createServerSocket(port);
 	}
 
-	/**
-	 * Must override.
-	 */
 	@Override
-	public ServerSocket createServerSocket(int port, int backlog,
-			InetAddress iAddress) throws IOException {
+	public ServerSocket createServerSocket(int port, int backlog, InetAddress iAddress) throws IOException {
 		return createServerSocket(port);
 	}
 
-	/**
-	 * Extracts the SocketImpl out of a ServerSocket.
-	 */
-	private SocketImpl getImpl(ServerSocket socket) throws Exception {
+	private SocketImpl getImpl(ServerSocket socket) throws ReflectiveOperationException, IllegalArgumentException {
 		Field implField = socket.getClass().getDeclaredField("impl");
 		implField.setAccessible(true);
 		return (SocketImpl) implField.get(socket);
 	}
 
-	/**
-	 * Injects a FileDescriptor into a SocketImpl.
-	 */
-	private void injectFD(FileDescriptor fd, SocketImpl impl) throws Exception {
+	private void injectFD(FileDescriptor fd, SocketImpl impl) throws ReflectiveOperationException, IllegalArgumentException {
 		Class<?> plainServerSocketImplClazz = impl.getClass();
-		Class<?> plainSocketImplClazz = plainServerSocketImplClazz
-				.getSuperclass();
+		Class<?> plainSocketImplClazz = plainServerSocketImplClazz.getSuperclass();
 		Class<?> socketImplClazz = plainSocketImplClazz.getSuperclass();
 		Field fdField = socketImplClazz.getDeclaredField("fd");
 		fdField.setAccessible(true);
 		fdField.set(impl, fd);
 	}
 
-	/**
-	 * Injects a SocketImpl into a ServerSocket.
-	 */
-	private void injectImpl(SocketImpl impl, ServerSocket socket)
-			throws Exception {
+	private void injectImpl(SocketImpl impl, ServerSocket socket) throws ReflectiveOperationException, IllegalArgumentException {
 		Field implField = socket.getClass().getDeclaredField("impl");
 		implField.setAccessible(true);
 		implField.set(socket, impl);
 	}
 
-	/**
-	 * Sets the isBound Field of a ServerSocket to true.
-	 */
-	private void setBound(ServerSocket socket) throws Exception {
+	private void setBound(ServerSocket socket) throws ReflectiveOperationException, IllegalArgumentException {
 		Field boundField = socket.getClass().getDeclaredField("isBound");
 		boundField.setAccessible(true);
 		boundField.set(socket, true);

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

@@ -0,0 +1,53 @@
+package de.tudarmstadt.informatik.hostage.system;
+
+import java.io.IOException;
+
+public class Device {
+
+	private static boolean initialized = false;
+	private static boolean root = false;
+	private static boolean pp = false;
+
+	private Device() {
+		new Thread(new Runnable() {
+			@Override
+			public void run() {
+				try {
+					String test = "[ -e /data/local/pp ]";
+					Process su = new ProcessBuilder("su", "-c", test).start();
+					switch (su.waitFor()) {
+					case 0:
+						root = true;
+						pp = true;
+						break;
+					case 1:
+						root = true;
+						pp = false;
+						break;
+					case 127:
+						root = false;
+						pp = false;
+						break;
+					}
+				} catch (InterruptedException e) {
+				} catch (IOException e) {
+				} finally {
+					initialized = true;
+				}
+			}
+		}).start();
+	}
+
+	public static boolean isRooted() {
+		while (!initialized)
+			;
+		return root;
+	}
+
+	public static boolean isPPInstalled() {
+		while (!initialized)
+			;
+		return pp;
+	}
+
+}

+ 0 - 68
src/de/tudarmstadt/informatik/hostage/system/P.java

@@ -1,68 +0,0 @@
-package de.tudarmstadt.informatik.hostage.system;
-
-import java.io.FileDescriptor;
-
-import android.net.LocalServerSocket;
-import android.net.LocalSocket;
-
-/**
- * Receives a file descriptor through a unix domain socket.
- * 
- * @author Mihai Plasoianu
- */
-public class P implements Runnable {
-
-	/**
-	 * Path for UDS in abstract namespace.
-	 */
-	private final static String NAME = "hostage";
-
-	/**
-	 * Port to bind.
-	 */
-	private int port;
-
-	public P(int port) {
-		this.port = port;
-	}
-
-	/**
-	 * Start p, wait for connection and receive a file descriptor.
-	 */
-	public FileDescriptor bindAndGetFD() {
-		FileDescriptor fd = null;
-		try {
-			new Thread(this).start();
-			LocalServerSocket localServer = new LocalServerSocket(NAME);
-			LocalSocket localSocket = localServer.accept();
-			while (localSocket.getInputStream().read() != -1)
-				;
-			FileDescriptor[] fdArray;
-			fdArray = localSocket.getAncillaryFileDescriptors();
-			if (fdArray != null) {
-				fd = fdArray[0];
-			}
-			localSocket.close();
-			localServer.close();
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-		return fd;
-	}
-
-	/**
-	 * Run p in separate Thread.
-	 */
-	@Override
-	public void run() {
-		Process p;
-		try {
-			String command = String.format("/data/local/p %d", port);
-			p = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
-			p.waitFor();
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-	}
-
-}

+ 48 - 0
src/de/tudarmstadt/informatik/hostage/system/PrivilegedPort.java

@@ -0,0 +1,48 @@
+package de.tudarmstadt.informatik.hostage.system;
+
+import java.io.FileDescriptor;
+import java.io.IOException;
+
+import android.net.LocalServerSocket;
+import android.net.LocalSocket;
+
+public class PrivilegedPort implements Runnable {
+
+	private final static String UNIX_PATH = "hostage";
+
+	private int port;
+	private FileDescriptor fd;
+
+	public PrivilegedPort(int port) {
+		this.port = port;
+		try {
+			new Thread(this).start();
+			LocalServerSocket localServer = new LocalServerSocket(UNIX_PATH);
+			LocalSocket localSocket = localServer.accept();
+			while (localSocket.getInputStream().read() != -1)
+				;
+			FileDescriptor[] fdArray;
+			fdArray = localSocket.getAncillaryFileDescriptors();
+			if (fdArray != null) {
+				this.fd = fdArray[0];
+			}
+			localSocket.close();
+			localServer.close();
+		} catch (IOException e) {
+		}
+	}
+
+	public FileDescriptor getFD() {
+		return fd;
+	}
+
+	@Override
+	public void run() {
+		String command = String.format("/data/local/pp %d", port);
+		try {
+			new ProcessBuilder("su", "-c", command).start();
+		} catch (IOException e) {
+		}
+	}
+
+}

+ 0 - 43
src/de/tudarmstadt/informatik/hostage/ui/MainActivity.java

@@ -1,7 +1,5 @@
 package de.tudarmstadt.informatik.hostage.ui;
 
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.HashMap;
 
@@ -19,7 +17,6 @@ import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
 import android.support.v4.content.LocalBroadcastManager;
-import android.util.Log;
 import android.view.GestureDetector;
 import android.view.GestureDetector.SimpleOnGestureListener;
 import android.view.Menu;
@@ -60,14 +57,6 @@ import de.tudarmstadt.informatik.hostage.logging.Logger;
  * @author Wulf Pfeiffer
  */
 public class MainActivity extends Activity implements Receiver {
-	/**
-	 * Flag for root acces. True if phone has root acces, else false.
-	 */
-	public static boolean isRooted = false;
-	/**
-	 * Flag for porthack. True if porthack is installed, else false.
-	 */
-	public static boolean porthackInstalled = false;
 
 	/**
 	 * Integer representing a grey light.
@@ -245,37 +234,6 @@ public class MainActivity extends Activity implements Receiver {
 		bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
 	}
 
-	/**
-	 * Checks if the phone ist rooted and if porthack is installed. Sets flags
-	 * {@link isRooted} and {@link porthackInstalled}
-	 */
-	private void checkRootAndPorthack() {
-		isRooted = false;
-		porthackInstalled = false;
-		Process p;
-		try {
-			String found = "Found";
-			String notFound = "Not found";
-			String command = "[ -f /data/local/p ] && echo " + found + " || echo " + notFound;
-			p = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
-			BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
-			/*
-			 * int av = byte[] b = new byte[av]; if (av != 0) { in.read(b); }
-			 */
-			String echoResponse = in.readLine();
-			Log.i("MainAc", echoResponse);
-			if (echoResponse.equals(found)) {
-				isRooted = true;
-				porthackInstalled = true;
-			} else if (echoResponse.equals(notFound)) {
-				isRooted = true;
-			}
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-		Log.i("MainAc", "Rooted: " + isRooted + " Porthack: " + porthackInstalled);
-	}
-
 	/**
 	 * Returns an intent to start HoneyService.
 	 * 
@@ -631,7 +589,6 @@ public class MainActivity extends Activity implements Receiver {
 		initViewAnimator();
 		initListView();
 		// Initialize Class variables
-		checkRootAndPorthack();
 		startAndBind();
 	}