http://technostuff.blogspot.com/2008/10/some-useful-socat-commands.html

 

MONDAY, OCTOBER 6, 2008

Some useful socat commands

 


socat tcp-l:7777,reuseaddr,fork system:filan -i 0 -s >&2,nofork

listens for incoming TCP connections on port 7777. For each accepted connection, invokes a shell. This shell has its stdin and stdout directly connected to the TCP socket (nofork). The shell starts filan and lets it print the socket addresses to stderr (your terminal window).

echo -e

functions as primitive binary editor: it writes the 4 bytes 000 014 000 000 to the executable /usr/bin/squid at offset 0x00074420 (this is a real world patch to make the squid executable from Cygwin run under Windows, actual per May 2004).


socat - tcp:www.blackhat.org:31337,readbytes=1000

connect to an unknown service and prevent being flooded.
 
http://ly50247.diandian.com/post/2012-10-22/40040136408
 

socat用法

 

socat是类似nc(netcat)的软件,很久之前就听过,最近才开始使用。

之前不用它的原因是同样的功能,socat要比nc要多打一些字符,实际上nc我也很少有,以至于记不全它的参数。最近想调试使用unixsock文件,发现gnu netcat实现不了,而openbsd netcat可以实现,而gnu netcat的一些选项openbsd netcat没有,而且服务器上只有gnu netcat,于是想还是用socat比较清净,而且功能更强大。

socat的基本用法还是比较简单的:

监听端口:

socat tcp-l:12345 -

发送到端口:

echo xxx | socat - tcp:127.0.0.1:12345

其中tcp-l是tcp-listen的简写,tcp是tcp-connect的简写,简写后就不比nc长多少了。

udp: udp-listen, udp-connect

unix socket: unix-listen, unix-connect

unix datagram socket: unix-recvfrom, unix-sendto

发送文件:

ip1 # socat -u open:a.txt tcp-listen:8888

ip2 # socat -u tcp:ip1:8888 open:a.txt,create

开telnet:

ip1 # socat tcp-listen:8888 exec:bash,pty,stderr

ip2 # socat readline tcp:ip1:888

端口转发(好像是这样,没用过):

ip1(外网机器的内网地址) # socat tcp-listen:1234 tcp-listen:3389

ip2(内网机器) # socat tcp:ip1的外网地址:1234 tcp:ip1:3389

socat 使用方法与一点点心得

socat是一個netcat(nc)的替代產品,可以稱得上nc++。socat的特點就是在兩個流之間建立一個雙向的 通道。socat的地址類型很 多,有ip, tcp, udp, ipv6, pipe,exec,system,open,proxy,openssl,等等。看一個例子:

c:\>socat - tcp:192.168.1.18:80

這個命令等同於 nc 192.168.1.18 80。 socat裡面,必須有兩個流,所以第一個參數-代表標準的輸入輸出,第二個流連接到192.168.1.18的80端口。再看一個反向telnet的例子:

on server:
c:\>socat tcp-listen:23 exec:cmd,pty,stderr

這個命名把cmd綁定到端口23,同時把cmd的Stderr重定向到stdout。

on client:
c:\>socat readline tcp:server:23

連接到服務器的23端口,即可獲得一個cmd shell。readline是gnu的命令行編輯器,具有歷史功能。

再看文件傳遞的例子。nc也經常用來傳遞文件,但是nc有一個缺點,就是不知道文件什麼時候傳完了,一般要用Ctrl+c來終止,或者估計一個時間,用-w參數來讓他自動終止。用socat就不用這麼麻煩了:

on host 1:
c:\>socat -u open:myfile.exe,binary tcp-listen:999

on host 2:
c:\>socat -u tcp:host1:999 open:myfile.exe,create,binary

這個命令把文件myfile.exe用二進制的方式,從host 1 傳到host 2。-u 表示數據單向流動,從第一個參數到第二個參數,-U表示從第二個到第一個。文件傳完了,自動退出。

再來一個大家喜歡用的例子。在一個NAT環境,如何從外部連接到內部的一個端口呢?只要能夠在內部運行socat就可以了。

外部:
c:\>socat tcp-listen:1234 tcp-listen:3389

內部:
c:\>socat tcp:outerhost:1234 tcp:192.168.12.34:3389

這樣,你外部機器上的3389就影射在內部網192.168.12.34的3389端口上。

socat還具有一個獨特的讀寫分流功能,比如:

c:\>socat open:read.txt!!open:write.txt,create,append tcp-listen:80,reuseaddr,fork

這個命令實現一個假的web server,客戶端連過來之後,就把read.txt裡面的內容發過去,同時把客戶的數據保存到write.txt裡面。”!!”符號用戶合併讀寫流,前面的用於讀,後面的用於寫。

 

上面的全是抄的别人的,自己试用发现了一些问题。

 

1、socat是支持cmdshell反连的,这点最重要的我在google和百度了一堆都没找到,自己研究了半天出来了。

   server:socat readline udp-listen:port  -------socat支持udp、tcp、https等,我测试了tcp和udp

   client:socat udp:serverip:port exec:cmd,pty,stderr

2、端口转发功能:上文写的是错误的,根本无法实现。大家测试过就知道了。

   正确的应该如下:

   外部:socat tcp-listen:11161,reuseaddr,fork tcp-listen:9833

   内部:socat tcp:外部ip:11161,reuseaddr,fork tcp:127.0.0.1:3389

不加reuseaddr,fork是无法成功连接的,具体原因大家可以再百度下,有详细介绍。

 

 

相关文章:

  • 2022-01-19
  • 2022-01-15
  • 2021-06-06
  • 2021-07-25
  • 2021-09-27
  • 2021-08-06
  • 2022-12-23
  • 2022-02-04
猜你喜欢
  • 2021-07-29
  • 2021-09-05
  • 2021-05-28
  • 2022-12-23
  • 2021-07-26
  • 2021-08-23
  • 2021-11-23
相关资源
相似解决方案