Skip to content

Commit

Permalink
Removed port hack dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
irinil committed Jul 31, 2020
1 parent 6f08d28 commit fdc45c6
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 223 deletions.
Expand Up @@ -172,14 +172,7 @@ public boolean start() {
*/
return false;
}
if (Device.isPorthackInstalled()) {
/*
Currently the port binder is the preferred method for creating sockets.
If it installed, we can't use iptables to create UDP sockets.
@see MyServerSocketFactory
*/
return false;
}

((SMB) protocol).initialize(this);
}

Expand Down
Expand Up @@ -11,24 +11,24 @@
import de.tudarmstadt.informatik.hostage.system.PrivilegedPort;


@Deprecated
public class MyDatagramSocketFactory {

//TODO Check validity of this method
public DatagramSocket createDatagramSocket(int port) throws IOException {
DatagramSocket socket = null;
//port = 1024;
if (port > 1023) {
socket = new DatagramSocket(port);
} else if (Device.isPorthackInstalled()) {
} else if (false) {
FileDescriptor fd = new PrivilegedPort(PrivilegedPort.TYPE.UDP, port).getFD();
socket = new DatagramSocket();
try {
DatagramSocketImpl impl = getImpl(socket);
injectFD(fd, impl);
injectLocalPort(port, impl);
setBound(socket);
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException e) {
} catch (IllegalArgumentException e) {
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
}
}
return socket;
Expand Down
116 changes: 0 additions & 116 deletions src/main/java/de/tudarmstadt/informatik/hostage/system/Device.java
Expand Up @@ -18,22 +18,16 @@


public class Device {
private static String porthackFilepath = "/data/local/bind";
private static boolean initialized = 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

// TODO: do asynchronous
public static void checkCapabilities() {
// assume worst case
initialized = false;
root = false;
porthack = false;
iptables = false;

portHackCheck();

final String ipTablesList = "iptables -L -n -t nat"; // list all rules in NAT table

try {
Expand All @@ -59,37 +53,10 @@ public static void checkCapabilities() {
}


private static void portHackCheck(){
porthackFilepath = getPorthackFilepath();
String porthackExists = "[ -e "+porthackFilepath+" ]"; // checks existence of porthack

try {
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: // command not found or executable
root = false;
porthack = false;
break;
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}

}

public static boolean isRooted() {
return root;
}

public static boolean isPorthackInstalled() {
return porthack;
}

public static boolean isPortRedirectionAvailable() { // using iptables
return iptables;
}
Expand Down Expand Up @@ -144,87 +111,4 @@ public static void executePortRedirectionScript() {
}
}

/**
* @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 | InterruptedException e) {
return false;
}

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 | InterruptedException e) {
e.printStackTrace();
}
}

// 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());
}
}
Expand Up @@ -9,7 +9,7 @@
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.util.Log;

@Deprecated
public class PrivilegedPort implements Runnable {

public enum TYPE {
Expand Down Expand Up @@ -52,19 +52,18 @@ public FileDescriptor getFD() {

@Override
public void run() {
String porthack = Device.getPorthackFilepath();
Log.i("privileged port", porthack);
String command = String.format(porthack+" %s %d", type.toString(), port);

try {
Process p = new ProcessBuilder("su", "-c", command).start();
if (p.waitFor() != 0) {
logError(p.getErrorStream());
}
logOutput(p.getInputStream());
} catch (IOException e) {
} catch (InterruptedException e) {
}
// String porthack = Device.getPorthackFilepath();
// Log.i("privileged port", porthack);
// String command = String.format(porthack+" %s %d", type.toString(), port);
//
// try {
// Process p = new ProcessBuilder("su", "-c", command).start();
// if (p.waitFor() != 0) {
// logError(p.getErrorStream());
// }
// logOutput(p.getInputStream());
// } catch (IOException | InterruptedException e) {
// }
}

private void logOutput(InputStream stdout) throws IOException {
Expand Down
@@ -1,31 +1,23 @@
package de.tudarmstadt.informatik.hostage.ui.fragment;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import de.tudarmstadt.informatik.hostage.R;
import de.tudarmstadt.informatik.hostage.system.Device;
import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;


/**
* @author Alexander Brakowski
* @created 24.02.14 23:37
* @modified Shreyas Srinivasa
*/
public class SettingsFragment extends UpNavigatibleFragment {
private TextView mPorthackText;
private Button mPorthackInstallButton;
private Button mPorthackUninstallButton;
private View v;
private LayoutInflater inflater;
private ViewGroup container;
Expand All @@ -42,9 +34,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa

TextView rootedText = v.findViewById(R.id.settings_device_rooted);
TextView iptablesText = v.findViewById(R.id.settings_iptables_available);
mPorthackText = v.findViewById(R.id.settings_porthack_installed);
mPorthackInstallButton = v.findViewById(R.id.settings_deploy_porthack);
mPorthackUninstallButton = v.findViewById(R.id.settings_uninstall_porthack);

if (Device.isRooted()) {
rootedText.setText(R.string.yes);
Expand All @@ -62,46 +51,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
iptablesText.setTextColor(getResources().getColor(R.color.holo_red));
}

updatePorthackStatus();

mPorthackInstallButton.setOnClickListener(v -> {
Device.deployPorthack();
updatePorthackStatus();
});
mPorthackUninstallButton.setOnClickListener(v -> {
Device.uninstallPorthack();
updatePorthackStatus();
});

v.findViewById(R.id.porthack_info_button).setOnClickListener(v -> {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.getInstance());
builder.setMessage(Html.fromHtml(getString(R.string.porthack_explanation)));
AlertDialog alert = builder.create();
alert.show();
});

return v;
}

private void updatePorthackStatus() {
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);
}
}

public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

Expand Down
34 changes: 1 addition & 33 deletions src/main/res/layout/fragment_settings.xml
Expand Up @@ -22,14 +22,6 @@
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/iptables_available" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -44,31 +36,7 @@
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/iptables_available"
android:layout_alignParentRight="true" android:layout_alignParentEnd="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/install_porthack"
android:id="@+id/settings_deploy_porthack"
android:layout_below="@+id/txtP2PSubheader" android:layout_centerHorizontal="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/txtP2PSubheader" android:layout_centerHorizontal="true"/>
<ImageView android:id="@+id/porthack_info_button" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_info_details"
android:layout_below="@+id/settings_porthack_installed"
android:layout_alignRight="@+id/settings_porthack_installed"
android:layout_alignEnd="@+id/settings_porthack_installed"/>

</RelativeLayout>
<FrameLayout
android:layout_width="fill_parent"
Expand Down

0 comments on commit fdc45c6

Please sign in to comment.