Socat – A utility for data transfer between two addresses (2018)
copyconstruct.medium.com
copyconstruct.medium.com
We exchanged emails back and forth about a use case I had, which was to split a serial device into two PTYs, allowing a process to control one while I monitor the other with picocom. We couldn't get it to work, so I gave up, but then Gerhard wrote back a few months later saying he added it as a feature.
It can be frustrating to figure out the right incantation for some advanced use cases, but this is more a complaint about esoteric corners of the operating system than socat which exposes the full complexity to you.
One of the invocations I recently worked out was to wrap an interactive process with a PTY, so that I could make a mock serial device that emulates a piece of hardware.
socat PTY,link=/tmp/dev,rawer,wait-slave \
EXEC:"./emulator",pty,setsid,ctty,echo=0Nearly all of the microservices we develop have a way to be run locally with all dependent services using docker-compose.
The problem is most teams don't provide a way to do efficient local development for their particular service. Some do remote debugging into the built/running container (slow and requires rebuilding docker image for every change). Some inspect logs to debug (yikes!).
When I want to debug a particular service in IntelliJ, I simply remove it from the docker network and replace it with a container running socat, forwarding the appropriate port to host.docker.internal (the host machine). This is on a MAC, of course, where Docker runs in a VM.
This allows me to do in-IDE debugging of any service that runs in the docker network without having to rebuild the image every time I make a change.
I know there are IntelliJ plugins that provide a similar workflow, but I believe they all build an image and deploy a container to the network for every change. I could be wrong.
Do you just have a service defined for each, and only start one of them?
Would you happen to have a fully worked example of this? We to have some services that we can and do run via docker compose - but it's often a cumbersome process to move one and one service out of the docker compose setup for debug/devel.
socat -v TCP-LISTEN:666,forever,reuseaddr,fork TCP:example.com:8080
socat -v TCP-LISTEN:8080,forever,reuseaddr,fork ssl:example.com:443I’ve tried to grok socat a few times but gave up quickly upon being faced with its screenfuls of dense help text.
This post sheds light on the core concepts behind the tool in a way that the man page and help text just didn’t do for me.
Now that I ‘get’ it, I’m actually excited to find an opportunity to use socat! I can think of so many interesting use cases.
But your question about an audit is still a good one! Tarsnap has bug bounties, but the top hit for “spiped security audit” led to a sort of amusing flame war on HN [1].
It's also considerably simpler than wireguard, which reduces the potential for vulnerabilities; and operates on a per-connection basis, which means you can do things like establishing a secure connection to another host but have a socket endpoint which can only be accessed by certain users.
I would absolutely love to get some deliberate code review from experts. I know tedu has looked at some of the code -- he sent me some minor corrections -- and I would certainly count him as an expert, but I don't know if his review was systematic or if he just glanced at a handful of files.
[1] https://github.com/kubernetes/kubernetes/blob/770d3f181c5d7e...
For example, I have a database with no docker port mappings (only a web application container in the same docker network accesses it directly). I can use a socat container with port mappings to proxy network traffic to the database.
Using the socat container allows me to access a port on my original container that I wouldn't be able to access unless I recreated that container.
As for why the database didn't have port mappings originally, it is best not to allow network access to things unless it's necessary. It's the principle of least privilege.
During that migration, we tried to minimize downtime, so we made sure that requests made to the old infrastructure were aptly forwarded to the new infrastructure wherever possible, until DNS properly propagated. I found out about socat and we went for it. It worked great, and I marveled at the simplicity of the solution!
Ever since then, I've used socat both as an emergency saviour and as an actual service. I seem to recall using it once to go around some docker bug or shenanigans.
I’ll have to remember it for the next time I migrate.
https://nc110.sourceforge.io/ (original release: https://seclists.org/bugtraq/1995/Oct/28)
Although it is "freely given away to the Internet community" with "an obligation to give credit where due", at least OpenBSD and GNU have seen the need to write their own versions under their project licenses:
GNU netcat: http://netcat.sourceforge.net/
OpenBSD nc: https://man.openbsd.org/nc.1
(The OpenBSD version has been ported to at least FreeBSD and Apple Macintosh OS.)
All of them have the same basic telnet's `host port` syntax for outbound TCP connections, but annoyingly the syntax for opening a local listening TCP socket varies. Say, you want to open a TCP socket listening on port 1234 (local), and a confirmation when it is ready:
The original and GNU netcats: netcat -v -l -p 1234
BSD netcat: nc -v -l 1234
https://nmap.org/book/ncat-man-examples.html
Means half of implemations use `-l -p 1234` and half `-l 1234`.
And this is indeed enforced, see line 424: http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/nc/netc...
However, at least Debian ships a ported version of BSD's netcat. They actually apply a patch to allow using -p and -s with -l for consistency with traditional netcat: https://sources.debian.org/patches/netcat-openbsd/1.217-3/us...
Similarly, you can also do this with KeepassXC
Edit: These directions are very similar to those linked in the comment this is in reply to and either will work.
https://gist.github.com/duebbert/4298b5f4eb7cc064b09e9d865dd...
https://github.com/ndbeals/winssh-pageant
I use both this and rupor's wsl-ssh-agent from the other thread extensively.
I was in need of a solution to this issue for a long time before ndbeals thankfully released this about 9mo ago. And I was checking around the web regularly! I nearly wrote my own as well.
The use case for me was to allow WinSCP and Sourcetree to be used with the Windows ssh agent.
And with history and all that stuff, too! I guess it can be used to forward serial port over network too.
I can also do port redirects with iptables that can assist with solving the problem.
I need to host a microservice that uses UDP which won’t connect out on most corporate networks due to firewalls. I need to get the connection out via a standard TCP port.
Nothing malicious. It’s a basic corporate client/server app, but it’s bound to a UDP port that I can’t change.
It's also an CLI IMAPS client, with history: socat READLINE,history=$HOME/.imaps_history EXEC:'"openssl s_client -connect mailserver:993"'
or a CLI web browser with history: socat -d -d READLINE,history=$HOME/.http_history TCP4:www.domain.com:www,crnl
or, if you have to interact with something that has no readline, say sendmail: socat READLINE EXEC:"sendmail -bt"
See also https://github.com/hanslub42/rlwrap
So if you're commonly doing a socat process like: socat -x OPEN:/dev/ttyS0,b115200,echo=0,icanon=0 PTY,link=/dev/ttyO0,rawer 2> socat_log.log
then it may be of interest to you.
When used to establish a persistent connection to a remote listening (TCP) port, it has NO WAY to detect that the remote end has dropped the connect, and automatically reconnect. It just does not work.
Yes, I know all about all the keep-alive and retry options in the manual. Try it yourself.
It's one of those horrible tools that you can get up and running quickly, but can never make work reliably.
https://stackoverflow.com/questions/9341254/reconnect-socat-...