Learning the socat Command

Introduction

socat is a powerful and easy-to-use tool that can implement conversions between any types of sockets.

Usage

socat [options] <address> <address>

You can check the help documentation for options.

Address can take several forms:

  • - STDIN STDOUT: Represents standard input/output, can be replaced with just a dash
  • /var/log/syslog: Can also be any path (use ./ for relative paths), opens a file as a data stream
  • TCP:127.0.0.1:1080: Establishes a TCP connection as a data stream, TCP can also be replaced with UDP
  • TCP-LISTEN:12345: Creates a TCP listening port, TCP can also be replaced with UDP
  • EXEC:/bin/bash: Executes a program as a data stream

Scenario 1

I have a local file that I want to display in the terminal

socat - /etc/sysctl.conf

Scenario 2

A TCP connection will connect, and I want to see what data will be received socat TCP-LISTEN:12345 -

Scenario 3

I’m a hacker and I want to create a shell proxy on the target machine socat TCP-LISTEN:12345 EXEC:/bin/bash

Scenario 4

I have a UNIX DOMAIN socket locally, and I want to convert it to a TCP SOCKET for use by machines in the local network. How do I do it?

socat TCP-LISTEN:12345,reuseaddr,fork UNIX-CONNECT:/data/deCOREIDPS/unix.domain

When multiple TCP connections come in, it forks one to connect to the domain socket

Scenario 5

Forward local port 80 to a remote destination

socat TCP-LISTEN:80,fork TCP:www.baidu.com:80

Reference: http://brieflyx.me/2015/linux-tools/socat-introduction/

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy