|
@@ -18,13 +18,22 @@ import org.apache.http.params.HttpParams;
|
|
|
import org.apache.http.params.HttpProtocolParams;
|
|
|
import org.apache.http.protocol.HTTP;
|
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.OutputStream;
|
|
|
import java.net.InetAddress;
|
|
|
import java.net.UnknownHostException;
|
|
|
import java.security.KeyStore;
|
|
|
import java.security.SecureRandom;
|
|
|
|
|
|
+import android.app.Activity;
|
|
|
import android.content.Context;
|
|
|
+import android.content.res.AssetManager;
|
|
|
import android.net.ConnectivityManager;
|
|
|
import android.net.NetworkInfo;
|
|
|
import android.net.wifi.WifiInfo;
|
|
@@ -32,11 +41,13 @@ import android.net.wifi.WifiManager;
|
|
|
import android.os.Environment;
|
|
|
import android.preference.PreferenceManager;
|
|
|
import android.text.TextUtils;
|
|
|
+import android.util.Log;
|
|
|
|
|
|
import de.tudarmstadt.informatik.hostage.logging.Record;
|
|
|
import de.tudarmstadt.informatik.hostage.logging.formatter.TraCINgFormatter;
|
|
|
import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
|
|
|
import de.tudarmstadt.informatik.hostage.system.Device;
|
|
|
+import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
|
|
|
|
|
|
/**
|
|
|
* Helper class with some static methods for general usage.
|
|
@@ -463,4 +474,119 @@ public final class HelperUtils {
|
|
|
public static int getRedirectedPort(int port){
|
|
|
return port + 1024 + 27113;
|
|
|
}
|
|
|
-}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * copies an asset to the local filesystem for later usage
|
|
|
+ * (used for port hack and shell scripts)
|
|
|
+ * @param assetFilePath
|
|
|
+ * @param destFilePath
|
|
|
+ * @return true on success
|
|
|
+ */
|
|
|
+ private static boolean deployAsset(String assetFilePath, String destFilePath) {
|
|
|
+ Activity activity = MainActivity.getInstance();
|
|
|
+ File file = new File(activity.getFilesDir(), destFilePath);
|
|
|
+ try {
|
|
|
+ OutputStream os = new FileOutputStream(file);
|
|
|
+ try {
|
|
|
+ InputStream is = activity.getAssets().open(assetFilePath);
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ int bytesRead;
|
|
|
+ while ((bytesRead = is.read(buffer)) != -1) {
|
|
|
+ os.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+ is.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ os.close();
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ //Log.i("FILEPATH", file.getAbsolutePath());
|
|
|
+ return true; // SUCCESS!
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean deployPortRedirectionScript() {
|
|
|
+ return deployAsset("payload/redirect-ports.sh", "redirect-ports.sh");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return name of porthack binary for device architecture
|
|
|
+ */
|
|
|
+ private static String getPorthackName() {
|
|
|
+ String porthack = "bind";
|
|
|
+
|
|
|
+ // determine system architecture to return the correct binary
|
|
|
+ String arch = System.getProperty("os.arch");
|
|
|
+ // TODO: handle more architectures
|
|
|
+ if (arch.equals("i686")) { // this is what genymotion reports
|
|
|
+ porthack += ".x86";
|
|
|
+ } else if (arch.startsWith("arm")) {
|
|
|
+ /*
|
|
|
+ possible values:
|
|
|
+ armv4
|
|
|
+ armv4t
|
|
|
+ armv5t
|
|
|
+ armv5te
|
|
|
+ armv5tej
|
|
|
+ armv6
|
|
|
+ armv7
|
|
|
+ */
|
|
|
+ porthack += ".arm";
|
|
|
+ } else if (arch.equals("mips")) {
|
|
|
+ porthack += ".mips";
|
|
|
+ }
|
|
|
+
|
|
|
+ return porthack;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return filepath to deployed porthack binary
|
|
|
+ */
|
|
|
+ public static String getPorthackFilepath() {
|
|
|
+ File file = new File(MainActivity.getInstance().getFilesDir(), getPorthackName());
|
|
|
+ return file.getAbsolutePath();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean deployPorthack() {
|
|
|
+ String porthack = getPorthackName();
|
|
|
+ if (!deployAsset("payload/"+porthack, porthack)) {
|
|
|
+ return false; // :(
|
|
|
+ }
|
|
|
+
|
|
|
+ // make port hack executable
|
|
|
+ try {
|
|
|
+ Process p = new ProcessBuilder("su", "-c", "chmod 700 "+getPorthackFilepath()).start();
|
|
|
+ if (p.waitFor() != 0) {
|
|
|
+ logError(p.getErrorStream());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ logOutput(p.getInputStream());
|
|
|
+ } catch (IOException e) {
|
|
|
+ return false;
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true; // SUCCESS!
|
|
|
+ }
|
|
|
+
|
|
|
+ // copied from PrivilegedPort.java
|
|
|
+ private static void logOutput(InputStream stdout) throws IOException {
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ Log.i("HelperUtils", line);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void logError(InputStream stderr) throws IOException {
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(stderr));
|
|
|
+ Log.e("HelperUtils", reader.readLine());
|
|
|
+ }
|
|
|
+}
|