Browse Source

uninstall and install porthack from settings

Fabio Arnold 9 years ago
parent
commit
6d7c3ae0cd

+ 27 - 4
res/layout/fragment_settings.xml

@@ -14,35 +14,58 @@
 				android:textAppearance="?android:attr/textAppearanceMedium"
 				android:text="@string/device_rooted"
 				android:id="@+id/record_details_text_ssid" android:textStyle="bold"/>
+		<TextView
+				android:layout_width="wrap_content"
+				android:layout_height="wrap_content"
+				android:textAppearance="?android:attr/textAppearanceMedium"
+				android:text="@string/iptables_available"
+				android:id="@+id/iptables_available"
+				android:layout_below="@+id/record_details_text_ssid" android:layout_alignParentLeft="true"
+				android:layout_alignParentStart="true" android:textStyle="bold"/>
 		<TextView
 				android:layout_width="wrap_content"
 				android:layout_height="wrap_content"
 				android:textAppearance="?android:attr/textAppearanceMedium"
 				android:text="@string/porthack_installed"
 				android:id="@+id/txtP2PSubheader"
-				android:layout_below="@+id/record_details_text_ssid" android:layout_alignParentLeft="true"
+				android:layout_below="@+id/iptables_available" android:layout_alignParentLeft="true"
 				android:layout_alignParentStart="true" android:textStyle="bold"/>
 		<TextView
 				android:layout_width="wrap_content"
 				android:layout_height="wrap_content"
 				android:textAppearance="?android:attr/textAppearanceMedium"
 				android:text="@string/yes"
-				android:id="@+id/settings_device_rooted" android:layout_above="@+id/txtP2PSubheader"
+				android:id="@+id/settings_device_rooted" android:layout_above="@+id/iptables_available"
+				android:layout_alignParentRight="true" android:layout_alignParentEnd="true"/>
+		<TextView
+				android:layout_width="wrap_content"
+				android:layout_height="wrap_content"
+				android:textAppearance="?android:attr/textAppearanceMedium"
+				android:text="@string/yes"
+				android:id="@+id/settings_iptables_available" android:layout_below="@+id/settings_device_rooted"
 				android:layout_alignParentRight="true" android:layout_alignParentEnd="true"/>
 		<TextView
 				android:layout_width="wrap_content"
 				android:layout_height="wrap_content"
 				android:textAppearance="?android:attr/textAppearanceMedium"
 				android:text="@string/yes"
-				android:id="@+id/settings_porthack_installed" android:layout_below="@+id/settings_device_rooted"
+				android:id="@+id/settings_porthack_installed" android:layout_below="@+id/iptables_available"
 				android:layout_alignParentRight="true" android:layout_alignParentEnd="true"/>
 		<Button
 				android:layout_width="wrap_content"
 				android:layout_height="wrap_content"
-				android:text="Deploy Porthack"
+				android:text="@string/install_porthack"
 				android:id="@+id/settings_deploy_porthack"
 				android:layout_below="@+id/txtP2PSubheader" android:layout_alignParentLeft="true"
 				android:layout_alignParentStart="false" android:layout_alignParentEnd="true"/>
+		<Button
+				android:layout_width="wrap_content"
+				android:layout_height="wrap_content"
+				android:text="@string/uninstall_porthack"
+				android:id="@+id/settings_uninstall_porthack"
+				android:layout_below="@+id/settings_deploy_porthack" android:layout_alignParentLeft="true"
+				android:layout_alignParentStart="false" android:layout_alignParentEnd="true"
+				android:background="@color/holo_red" android:textColor="@android:color/white"/>
 	</RelativeLayout>
 	<FrameLayout
 			android:layout_width="fill_parent"

+ 3 - 0
res/values/strings.xml

@@ -105,6 +105,9 @@
 
     <string name="device_rooted">Device rooted</string>
     <string name="porthack_installed">Porthack installed</string>
+	<string name="iptables_available">IPTables available</string>
+	<string name="install_porthack">Install Porthack</string>
+	<string name="uninstall_porthack">Uninstall Porthack</string>
     <string name="yes">Yes</string>
     <string name="no">No</string>
 

+ 11 - 0
src/de/tudarmstadt/informatik/hostage/commons/HelperUtils.java

@@ -576,6 +576,17 @@ public final class HelperUtils {
 		return true; // SUCCESS!
 	}
 
+	public static void uninstallPorthack() {try {
+		Process p = new ProcessBuilder("su", "-c", "rm "+getPorthackFilepath()).start();
+		if (p.waitFor() != 0) {
+			logError(p.getErrorStream());
+		}
+		logOutput(p.getInputStream());
+		} catch (IOException e) {
+		} catch (InterruptedException e) {
+		}
+	}
+
 	// copied from PrivilegedPort.java
 	private static void logOutput(InputStream stdout) throws IOException {
 		BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));

+ 47 - 30
src/de/tudarmstadt/informatik/hostage/system/Device.java

@@ -10,38 +10,60 @@ import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
 public class Device {
 	private static String porthackFilepath = "/data/local/bind";
 	private static boolean initialized = false;
-	private static boolean root = false;
-	private static boolean pp = false;
+	private static boolean root = false; // device is rooted
+	private static boolean porthack = false; // porthack installed
+	private static boolean iptables = false; // iptables redirection confirmed working
 
-    private static void checkPorthack() {
+    public static void checkCapabilities() {
+		// assume worst case
 		initialized = false;
+		root = false;
+		porthack = false;
+		iptables = false;
+
 		porthackFilepath = HelperUtils.getPorthackFilepath();
-		Log.i("FILEPATH", porthackFilepath);
-		String portBinder = "[ -e "+porthackFilepath+" ]";
+		String porthackExists = "[ -e "+porthackFilepath+" ]"; // checks existence of porthack
 
 		try {
-			Process su = new ProcessBuilder("su", "-c", portBinder).start();
-			switch (su.waitFor()) {
-			case 0:
-				root = true;
-				pp = true;
-				break;
-			case 1:
-				root = true;
-				pp = false;
+			Process p = new ProcessBuilder("su", "-c", porthackExists).start();
+			switch (p.waitFor()) {
+			case 0: porthack = true;
+			// fall through and don't break
+			case 1: root = true; // 0 and 1 are valid return values of the porthack
 				break;
-			case 127:
+
+			case 127: // command not found or executable
 				root = false;
-				pp = false;
+				porthack = false;
 				break;
 			}
+		} catch (IOException e) {
+			e.printStackTrace();
 		} catch (InterruptedException e) {
+			e.printStackTrace();
+		}
+
+		final String ipTablesList = "iptables -L -n -t nat"; // list all rules in NAT table
+		try {
+			Process p = new ProcessBuilder("su", "-c", ipTablesList).start();
+			switch (p.waitFor()) {
+				case 0: // everything is fine
+					iptables = true; // iptables available and working
+					break;
+
+				case 3: // no such table
+				case 127: // command not found
+				default: // unexpected return code
+					// while testing code 3 has been returned when table NAT is not available
+					iptables = false;
+			}
 		} catch (IOException e) {
-		} finally {
-			initialized = true;
-			Log.d("hostage", "Root: " + Boolean.toString(root));
-			Log.d("hostage", "PP: " + Boolean.toString(pp));
+			e.printStackTrace();
+		} catch (InterruptedException e) {
+			e.printStackTrace();
 		}
+
+		initialized = true;
 	}
 
 	public static boolean isRooted() {
@@ -51,16 +73,11 @@ public class Device {
 
 	public static boolean isPorthackInstalled() {
 		assert(initialized);
-		return pp;
+		return porthack;
 	}
 
-    /**
-     * Called after auto-loading porthack. To update the local variables.
-     */
-
-    public static boolean updatePorthack(){
-		checkPorthack();
-        return (pp && root);
-    }
-
+	public static boolean isPortRedirectionAvailable() { // using iptables
+		assert(initialized);
+		return iptables;
+	}
 }

+ 2 - 2
src/de/tudarmstadt/informatik/hostage/ui/activity/MainActivity.java

@@ -217,8 +217,8 @@ public class MainActivity extends Activity {
 		setContentView(R.layout.activity_drawer_main);
 		mProfileManager = ProfileManager.getInstance();
 
-		// check for the porthack
-		Device.updatePorthack();
+		// check for the porthack and iptables
+		Device.checkCapabilities();
 
 		// init threat indicator animation
 		ThreatIndicatorGLRenderer.assets = getAssets();

+ 31 - 4
src/de/tudarmstadt/informatik/hostage/ui/fragment/SettingsFragment.java

@@ -17,6 +17,8 @@ import de.tudarmstadt.informatik.hostage.system.Device;
  */
 public class SettingsFragment extends UpNavigatibleFragment {
 	private TextView mPorthackText;
+	private Button mPorthackInstallButton;
+	private Button mPorthackUninstallButton;
 
 	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 		super.onCreateView(inflater, container, savedInstanceState);
@@ -24,8 +26,10 @@ public class SettingsFragment extends UpNavigatibleFragment {
 		View v = inflater.inflate(R.layout.fragment_settings, container, false);
 
 		TextView rootedText = (TextView) v.findViewById(R.id.settings_device_rooted);
+		TextView iptablesText = (TextView) v.findViewById(R.id.settings_iptables_available);
 		mPorthackText = (TextView) v.findViewById(R.id.settings_porthack_installed);
-		Button porthackButton = (Button) v.findViewById(R.id.settings_deploy_porthack);
+		mPorthackInstallButton = (Button) v.findViewById(R.id.settings_deploy_porthack);
+		mPorthackUninstallButton = (Button) v.findViewById(R.id.settings_uninstall_porthack);
 
 		if (Device.isRooted()) {
 			rootedText.setText(R.string.yes);
@@ -35,29 +39,52 @@ public class SettingsFragment extends UpNavigatibleFragment {
 			rootedText.setTextColor(getResources().getColor(R.color.holo_red));
 		}
 
+		if (Device.isPortRedirectionAvailable()) {
+			iptablesText.setText(R.string.yes);
+			iptablesText.setTextColor(getResources().getColor(R.color.holo_dark_green));
+		} else {
+			iptablesText.setText(R.string.no);
+			iptablesText.setTextColor(getResources().getColor(R.color.holo_red));
+		}
+
 		updatePorthackStatus();
 
-		porthackButton.setOnClickListener(new View.OnClickListener() {
+		mPorthackInstallButton.setOnClickListener(new View.OnClickListener() {
 			@Override
 			public void onClick(View v) {
 				HelperUtils.deployPorthack();
 				updatePorthackStatus();
 			}
 		});
-		porthackButton.setEnabled(Device.isRooted()); // we're only able to deploy if the device is rooted
+		mPorthackUninstallButton.setOnClickListener(new View.OnClickListener() {
+			@Override
+		public void onClick(View v) {
+				HelperUtils.uninstallPorthack();
+				updatePorthackStatus();
+			}
+		});
 
 		return v;
 	}
 
 	private void updatePorthackStatus() {
-		Device.updatePorthack(); // get current situation
+		Device.checkCapabilities(); // get current situation
 
 		if (Device.isPorthackInstalled()) {
 			mPorthackText.setText(R.string.yes);
 			mPorthackText.setTextColor(getResources().getColor(R.color.holo_dark_green));
+			mPorthackInstallButton.setEnabled(false); // we're only able to deploy if the device is rooted
+			mPorthackInstallButton.setVisibility(View.GONE);
+			mPorthackUninstallButton.setEnabled(true);
+			mPorthackUninstallButton.setVisibility(View.VISIBLE);
 		} else {
 			mPorthackText.setText(R.string.no);
 			mPorthackText.setTextColor(getResources().getColor(R.color.holo_red));
+			// we're only able to deploy if the device is rooted
+			mPorthackInstallButton.setEnabled(Device.isRooted());
+			mPorthackInstallButton.setVisibility(Device.isRooted() ? View.VISIBLE : View.GONE);
+			mPorthackUninstallButton.setEnabled(false);
+			mPorthackUninstallButton.setVisibility(View.GONE);
 		}
 	}