You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

364 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // Examples for using socat (and filan)
  2. //"$" means normal user, "#" requires privileges, "//" starts a comment
  3. ///////////////////////////////////////////////////////////////////////////////
  4. // similar to netcat
  5. // connect to 10.1.1.1 on port 80 and relay to and from stdio
  6. $ socat - TCP:10.1.1.1:80 # similar to "netcat 10.1.1.1 80"
  7. // listen on port 25, wait for an incoming connection, use CR+NL on this
  8. // connection, relay data to and from stdio;
  9. // then emulate a mailserver by hand :-)
  10. # socat - TCP-LISTEN:25,crlf
  11. // listen on port 25, wait for an incoming connection, use CR+NL on this
  12. // connection, relay data to and from stdio, but have line editing and history;
  13. // then emulate a mailserver by hand :-)
  14. # socat readline TCP-LISTEN:25,crlf
  15. // provide a transient history enabled front end to stupid line based
  16. // interactive programs
  17. $ socat readline exec:"nslookup",pty,ctty,setsid,echo=0
  18. // same works for ftp (but password is not hidden)
  19. // you may also use a file based history list
  20. $ socat readline,history=.nslookup_hist exec:"nslookup",pty,ctty,setsid,echo=0
  21. // using ~ as abbreviation for $HOME does not work!
  22. // poor mans 'telnetd' replacement
  23. # socat tcp-l:2023,reuseaddr,fork exec:/bin/login,pty,setsid,setpgid,stderr,ctty
  24. // and here an appropriate client:
  25. $ socat -,raw,echo=0 tcp:172.16.181.130:2023
  26. // use ssl with client and server certificate for improved security;
  27. // replace /bin/login by /bin/bash when using SSL client authentication, can be
  28. // run without root then
  29. // this is a cool trick, proposed by Christophe Lohr, to dump communications to
  30. // two files; it would also work for other manipulations (recode, compress...)
  31. // and it might also work with netcat ;-)
  32. $ socat TCP-LISTEN:5555 SYSTEM:'tee l2r | socat - "TCP:remote:5555" | tee r2l'
  33. ///////////////////////////////////////////////////////////////////////////////
  34. // emergence solution because usleep(1) is not always available
  35. // this will "sleep" for 0.1s
  36. $ socat -T 0.1 pipe pipe
  37. ///////////////////////////////////////////////////////////////////////////////
  38. // a very primitive HTTP/1.0 echo server (problems: sends reply headers before
  39. // request; hangs if client does not shutdown - HTTP keep-alive)
  40. // wait for a connection on port 8000; do not wait for request, but immediately
  41. // start a shell that sends reply headers and an empty line; then echo all
  42. // incoming data back to client
  43. $ socat TCP-LISTEN:8000,crlf SYSTEM:"echo HTTP/1.0 200; echo Content-Type\: text/plain; echo; cat"
  44. // a less primitive HTTP echo server that sends back not only the reqest but
  45. // also server and client address and port. Might have portability issues with
  46. // echo
  47. ./socat -T 1 -d -d tcp-l:10081,reuseaddr,fork,crlf system:"echo -e \"\\\"HTTP/1.0 200 OK\\\nDocumentType: text/html\\\n\\\n<html>date: \$\(date\)<br>server:\$SOCAT_SOCKADDR:\$SOCAT_SOCKPORT<br>client: \$SOCAT_PEERADDR:\$SOCAT_PEERPORT\\\n<pre>\\\"\"; cat; echo -e \"\\\"\\\n</pre></html>\\\"\""
  48. ///////////////////////////////////////////////////////////////////////////////
  49. // for communicating with an attached modem, I had reasonable results with
  50. // following command line. Required privileges depend on device mode.
  51. // after leaving socat, type "sane".
  52. // replace /dev/ttyS0 by the correct serial line or with /dev/modem
  53. $ socat readline /dev/ttyS0,raw,echo=0,crlf
  54. // or
  55. $ socat readline /dev/ttyS0,raw,echo=0,crlf,nonblock
  56. // then enter "at$"
  57. ///////////////////////////////////////////////////////////////////////////////
  58. // relay TCP port 80 from everywhere (internet, intranet, dmz) through your
  59. // firewall to your DMZ webserver (like plug-gw)
  60. // listen on port 80; whenever a connection is made, fork a new process (parent
  61. // process keeps accepting connections), su to nobody, and connect to
  62. // www.dmz.mydomain.org on port 80.
  63. // attention: this is a substitute for a reverse proxy without providing
  64. // application level security.
  65. # socat TCP-LISTEN:80,reuseaddr,fork,su=nobody TCP:www.dmz.mydomain.org:80
  66. // Note: parent process keeps running as root, su after forking
  67. ///////////////////////////////////////////////////////////////////////////////
  68. // relay mail from your DMZ server through your firewall.
  69. // accept connections only on dmz interface and allow connections only from
  70. // smtp.dmz.mydomain.org.
  71. // the advantages over plug-gw and other relays are:
  72. // * you can bind to an IP address (even an alias), therefore enhance security
  73. // * in your OS you can create several IP aliases and bind another socat daemon
  74. // to each, making several application servers addressable
  75. // * lots of options, like switching user, chroot, IP performance tuning
  76. // * no need for inetd
  77. # socat -lm -d -d TCP-LISTEN:25,bind=fw.dmz.mydomain.org,fork,su=nobody,range=smtp.dmz.mydomain.org/32 TCP:smtp.intra.mydomain.org:25
  78. ///////////////////////////////////////////////////////////////////////////////
  79. // convert line terminator in ascii streams, stdin to stdout
  80. // use unidirectional mode, convert nl to crnl
  81. $ socat -u - -,crlf
  82. // or cr to nl
  83. $ socat -u -,cr -
  84. // save piped data similar to 'tee':
  85. // copies stdin to stdout, but writes everything to the file too
  86. $ socat -,echo=0 open:/tmp/myfile,create,trunc,ignoreeof!!/tmp/myfile
  87. ///////////////////////////////////////////////////////////////////////////////
  88. // intrusion testing
  89. // found an XWindow Server behind IP filters with FTP data hole? (you are
  90. // lucky!)
  91. // prepare your host:
  92. # rm -f /tmp/.X11-unix/X1
  93. // relay a pseudo display :1 on your machine to victim:0
  94. # socat UNIX-LISTEN:/tmp/.X11-unix/X1,fork TCP:host.victim.org:6000,sp=20 &
  95. // and try to take a screendump (must be very lucky - when server has not even
  96. // host based authentication!)
  97. # xwd -root -display :1 -silent >victim.xwd
  98. // you sit behind a socks firewall that has IP filters but lazily allows socks
  99. // connections to loopback and has only host based X11 security.
  100. // like above, but from your inside client:
  101. # socat UNIX-LISTEN:/tmp/.X11-unix/X1,fork SOCKS4:firewall:loopback:6000
  102. // or for the HTTP proxy:
  103. # socat UNIX-LISTEN:/tmp/.X11-unix/X1,fork PROXY:firewall:loopback:6000
  104. ///////////////////////////////////////////////////////////////////////////////
  105. // forms of stdin with stdout, all equivalent
  106. $ socat echo -
  107. $ socat echo STDIO
  108. $ socat echo STDIN!!STDOUT
  109. $ socat echo STDIO!!STDIO
  110. $ socat echo -!!-
  111. $ socat echo FD:0!!FD:1
  112. $ socat echo 0!!1
  113. $ socat echo /dev/stdin!!/dev/stdout // if your OS provides these
  114. ///////////////////////////////////////////////////////////////////////////////
  115. // some echo address examples
  116. $ socat - PIPE
  117. $ socat - PIPE:/tmp/pipi // other version of echo
  118. $ socat - PIPE:/tmp/pipi,nonblock!!/tmp/pipi // other version of echo
  119. $ socat - EXEC:/bin/cat // another echo
  120. $ socat - SYSTEM:/bin/cat // another echo
  121. $ socat - TCP:loopback:7 // if inetd echo/TCP service activated
  122. $ socat - UDP:loopback:7 // if inetd echo/UDP service activated
  123. $ socat - /tmp/hugo,trunc,ignoreeof!!/tmp/hugo // with delay
  124. $ socat - UDP:loopback:2000,bind=:2000 // self "connection"
  125. $ socat - TCP:loopback:2000,bind=:2000 // Linux bug?
  126. # socat - IP:loopback:222 // raw protocol, self "connected" (attention,
  127. // Linux might drop packets with less than 8 bytes payload)
  128. ///////////////////////////////////////////////////////////////////////////////
  129. // unidirectional data transfer
  130. $ socat -u - -
  131. // like "tail -f", but start with showing all file contents
  132. $ socat -u FILE:/var/log/syslog.debug,ignoreeof -
  133. // like "tail -f", but do not show existing file contents
  134. $ socat -u FILE:/var/log/syslog.debug,ignoreeof,seek-end -
  135. // write to new file, create with given permission and group (must be member) - race condition with group!!!
  136. $ socat -u - CREATE:/tmp/outfile1,group=floppy,perm=0640
  137. //
  138. // for an existing file /tmp/outfile1
  139. # socat -u - FILE:/tmp/outfile1,group=floppy,perm=0700,user=4321
  140. ///////////////////////////////////////////////////////////////////////////////
  141. // file handling
  142. $ socat - FILE:/tmp/outfile1,ignoreeof!!FILE:/tmp/outfile1,append // prints outfile1, then echoes input and protocols into file (appends to old data)
  143. ///////////////////////////////////////////////////////////////////////////////
  144. // unix socket handling
  145. // create a listening unix socket
  146. $ rm -f /tmp/mysocket; socat UNIX-LISTEN:/tmp/mysocket -
  147. // from another terminal, connect to this socket
  148. $ socat UNIX:/tmp/mysocket -
  149. // then transfer data bidirectionally
  150. ///////////////////////////////////////////////////////////////////////////////
  151. // transport examples
  152. // socks relay (externally socksify applications);
  153. // your ssh client and OS are not socksified, but you want to pass a socks
  154. // server with ssh:
  155. $ socat TCP-LISTEN:10022,fork SOCKS4:socks.mydomain.org:ssh-serv:22
  156. $ ssh -p 10022 loopback
  157. // or better define a ProxyCommand in ~/.ssh/config:
  158. ProxyCommand socat - SOCKS:socks.mydomain.org:%h:%p
  159. // and with proxy:
  160. ProxyCommand socat - PROXY:proxy.mydomain.org:%h:%p,proxyport=8000
  161. ///////////////////////////////////////////////////////////////////////////////
  162. // application examples
  163. // run sendmail daemon with your favorite network options
  164. # socat TCP-LISTEN:25,fork,ip-ttl=4,ip-tos=7,tcp-maxseg=576 EXEC:"/usr/sbin/sendmail -bs",nofork
  165. // local mail delivery over UNIX socket - no SUID program required
  166. # socat UNIX-LISTEN:/tmp/postoffice,fork,perm-early=0666 EXEC:"/usr/sbin/sendmail -bs"
  167. $ socat - /tmp/postoffice
  168. ///////////////////////////////////////////////////////////////////////////////
  169. // uses of filan
  170. // see what your operating system opens for you
  171. $ filan
  172. // or if that was too detailled
  173. $ filan -s
  174. // see what file descriptors are passed via exec function
  175. $ socat - EXEC:filan,nofork
  176. $ socat - EXEC:filan
  177. $ socat - EXEC:filan,pipes,stderr
  178. $ socat - EXEC:filan,pipes
  179. $ socat - EXEC:filan,pty
  180. // see what's done by your shell and with option "pipes"
  181. $ socat - SYSTEM:filan,pipes
  182. // see if gdb gives you an equivalent environment or opens some files for your program
  183. $ gdb ./filan
  184. (gdb) r
  185. (gdb) r -s
  186. ///////////////////////////////////////////////////////////////////////////////
  187. // want to use chat from the ppp package?
  188. // note: some OS's do not need "-e" for echo to print control characters
  189. // note: chat might send bytes one by one
  190. // with AIX, a similar program is available under the name "pppdial"
  191. $ socat -d -d tcp:localhost:25,crlf,nodelay exec:'/usr/sbin/chat -v -s "\"220 \"" "\"HELO loopback\"" "\"250 \"" "\"MAIL FROM: <hugo@localhost>\"" "\"250 \"" "\"RCPT TO: root\"" "\"250 \"" "\"DATA\"" "\"354 \"" "\"test1'$(echo -e "\r.")'\"" "\"250 \"" "\"QUIT\"" "\"221 \""',pty,echo=0,cr
  192. //////////////////////////////////////////////////////////////////////////////
  193. // IP6
  194. # socat readline TCP6:[::1]:21 # if your inetd/ftp is listening on ip6
  195. ///////////////////////////////////////////////////////////////////////////////
  196. // application server solutions
  197. // run a program (here: /bin/sh) chrooted, unprivileged;
  198. // parent process stays in real / running as root
  199. # socat -d -d - EXEC:/bin/sh,chroot=/home/sandbox,su=sandbox,pty
  200. // make a program available on the network chrooted, unprivileged;
  201. // parent process stays in / running as root
  202. // script path is already chrooted
  203. # ./socat -lm -d -d TCP-LISTEN:5555,fork EXEC:/bin/myscript,chroot=/home/sandbox,su=sandbox,pty,stderr
  204. // to avoid terminal problems, you might - instead of telnet - connect using
  205. $ socat -,icanon=0,echo=0 tcp:target:5555; reset
  206. // access local display from ssh server, when ssh port forwarding is disabled
  207. // socat must be installed on ssh server host
  208. // might have to use xauth...
  209. // this example is one-shot because ssh can handle only one channel
  210. xterm1$ socat -d -d exec:"ssh www.dest-unreach.org rm -f /tmp/.X11-unix/X9; ~/bin/socat -d -d unix-l\:/tmp/.X11-unix/X9\,fork -" unix:/tmp/.X11-unix/X0
  211. xterm2$ ssh target
  212. target$ DISPLAY=:9 myxapplication
  213. // touch with perms:
  214. // no race condition for perms (applied with creat() call)
  215. $ socat -u /dev/null creat:/tmp/tempfile,perm=0600
  216. // touch with owner and perms:
  217. // race condition before changing owner, but who cares - only root may access
  218. # socat -u /dev/null creat:/tmp/tempfile,user=user1,perm=0600
  219. // invoke an interactive ssh with exec
  220. // first example passes control chars (^C etc.) to remote server as usual
  221. socat -,echo=0,raw exec:'ssh server',pty,setsid,ctty
  222. // second example interprets control chars on local command line
  223. socat -,echo=0,icanon=0 exec:'ssh server',pty,setsid,ctty
  224. // afterwards, type "reset"!
  225. // convince ssh to provide an "interactive" shell to your script
  226. // three main versions for entering password:
  227. // 1) from your TTY; have 10 seconds to enter password:
  228. (sleep 10; echo "ls"; sleep 1) |socat - exec:'ssh server',pty
  229. // 2) from XWindows (DISPLAY !); again 10 seconds
  230. (sleep 10; echo "ls"; sleep 1) |socat - exec:'ssh server',pty,setsid
  231. // 3) from script
  232. (sleep 5; echo PASSWORD; echo ls; sleep 1) |./socat - exec:'ssh server',pty,setsid,ctty
  233. // download with proxy CONNECT
  234. // use echo -e if required for \n
  235. $ (echo -e "CONNECT 128.129.130.131:80 HTTP/1.0\n"; sleep 5; echo -e "GET
  236. /download/file HTTP/1.0\n"; sleep 10) |socat -d -d -t 3600 - tcp:proxy:8080,crlf
  237. // retrieve a file from an sshd site with sourceforge style entry menu;
  238. // fill in your personal values; cat lets you enter your password (will be
  239. // visible on screen)
  240. $ (sleep 10; read pass; echo $pass; sleep 10; echo M; sleep 5; echo cat FILENAME; sleep 10) |./socat -d -d -ly - EXEC:'ssh -c 3des -l USER cf.sourceforge.net',pty,setsid,ctty |tee FILENAME
  241. // multicast community on local network: start the following command on all
  242. // participating hosts; like a conference call:
  243. # socat -d -d -d -d - udp-datagram:224.0.0.2:6666,bind=:6666,ip-add-membership=224.0.0.2:eth0,bindtodevice=eth0
  244. // or
  245. $ socat -d -d -d -d - udp-datagram:224.0.0.2:6666,bind=:6666,ip-add-membership=224.0.0.2:eth0
  246. // possible reasons for failure:
  247. // iptables or other filters (open your filters as required)
  248. // packets leave via wrong interface (set route: ...)
  249. // socket bound to specific address
  250. //=============================================================================
  251. // GENERIC FUNCTION CALLS
  252. // ioctl(): open CD drive (given value valid on Linux)
  253. // on my Linux system I find in /usr/include/linux/cdrom.h the define:
  254. // #define CDROMEJECT 0x5309 /* Ejects the cdrom media */
  255. // the following command makes something like ioctl(fd, CDROMEJECT, NULL)
  256. // (don't care about the read error):
  257. $ socat /dev/cdrom,o-nonblock,ioctl-void=0x5309 -
  258. // setsockopt(): SO_REUSEADDR
  259. // the following command performs - beyond lots of overhead - something like:
  260. // myint=1; setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &myint, sizeof(myint))
  261. $ socat -u udp-recv:7777,setsockopt-int=1:2:1 -
  262. // setsockopt(): SO_BINDTODEVICE
  263. // ways to apply SO_BINDTODEVICE without using the special socat address option
  264. // so-bindtodevice:
  265. // with string argument:
  266. $ sudo ./socat tcp-l:7777,setsockopt-string=1:25:eth0 pipe
  267. // with binary argument:
  268. $ sudo ./socat tcp-l:7777,setsockopt-bin=1:25:x6574683000 pipe
  269. ===============================================================================
  270. // not tested, just ideas, or have problems
  271. // traverse firewall for making internal telnet server accessible for outside
  272. // telnet client, when only outbound traffic (syn-filter) is allowed:
  273. // on external client run "double server". this process waits for a
  274. // connection from localhost on port 10023, and, when it is established, waits
  275. // for a connection from anywhere to port 20023:
  276. ext$ socat -d TCP-LISTEN:10023,range=localhost TCP-LISTEN:20023
  277. // on internal server run double client:
  278. int$ socat -d TCP:localhost:23 TCP:extclient:10023
  279. // or, with socks firewall:
  280. int$ socat -d TCP:localhost:23 SOCKS:socksserver:extclient:10023
  281. // login with:
  282. ext$ telnet localhost 20023
  283. // you can make a double server capable of handling multiple instances:
  284. ext$ socat -d TCP-LISTEN:10023,range=localhost,fork TCP-LISTEN:20023,reuseaddr
  285. // access remote display via ssh, when ssh port forwarding is disabled
  286. $ socat -d -d EXEC:"ssh target socat - UNIX:/tmp/.X11-unix/X0" TCP-LISTEN:6030
  287. $ xclock -display localhost:30
  288. // relay multiple webserver addresses through your firewall into your DMZ:
  289. // make IP aliases on your firewall, and then:
  290. # socat -d -d TCP-L:80,bind=fw-addr1,fork TCP:dmz-www1:80
  291. # socat -d -d TCP-L:80,bind=fw-addr2,fork TCP:dmz-www2:80
  292. // and for improved security:
  293. # socat -d -d TCP-L:80,bind=fw-addr3,su=nobody,fork TCP:dmz-www3:80
  294. // proxy an arbitrary IP protocol over your firewall (answers won't work)
  295. # socat -d -d IP:0.0.0.0:150,bind=fwnonsec IP:sec-host:150,bind=fwsec
  296. // proxy an unsupported IP protocol over your firewall, point to point
  297. // end points see firewall interfaces as IP peers!
  298. # socat -d -d IP:nonsec-host:150,bind=fwnonsec IP:sec-host:150,bind=fwsec
  299. // note that, for IPsec, you might face problems that are known with NAT