socat使用指南:3:5种常见的使用方法
這篇文章繼續介紹一下socat常見的5種使用方法。
使用方法1:當作cat來使用
liumiaocn:~ liumiao$ socat - `pwd`/greetings hello liumiao liumiaocn:~ liumiao$但是需要注意的是相對路徑或者絕對路徑寫完整,畢竟還是socket方式的實現,直接使用文件名可能會出現下面類似的錯誤。
liumiaocn:~ liumiao$ socat - greetings 2020/03/01 09:28:08 socat[26732] E unknown device/address "greetings" liumiaocn:~ liumiao$相對路徑也可以使用
liumiaocn:~ liumiao$ socat - ./greetings hello liumiao liumiaocn:~ liumiao$使用方法2: 寫文件
通過管道將內容傳遞給指定名稱的文件
liumiaocn:~ liumiao$ echo "hi, liumiao" |socat - ./hellomsg liumiaocn:~ liumiao$ cat hellomsg hi, liumiao liumiaocn:~ liumiao$這種方式缺省情況下是append的方式追加內容的,再次執行即可確認
liumiaocn:~ liumiao$ echo "greetings " |socat - ./hellomsg liumiaocn:~ liumiao$ cat ./hellomsg hi, liumiao greetings liumiaocn:~ liumiao$使用方法3: 指定端口和類型進行監聽
比如在8088端口指定TCP方式的監聽,客戶端連接此端口發送的數據都可以被socat確認到。
步驟1: 使用socat啟動監聽
執行命令:socat tcp-listen:8088 -
步驟2: 使用nc發送數據
執行命令:nc 127.0.0.1 8088
另啟一個終端執行上述命令之后,輸入的信息都可以在socat終端看到,實際上是socket通信方式的數據傳輸。
liumiaocn:~ liumiao$ nc 127.0.0.1 8088 hello, this is greetings from liumiao而此時在socat監聽的終端即可確認到信息的傳遞
liumiaocn:~ liumiao$ socat tcp-listen:8088 - hello, this is greetings from liumiao使用方法4: 活用EXEC當作shell代理
使用者可以使用如下命令即可在目標機器上設立一個shell代理,結合進程隱藏可能更難以被發現,自己使用自己的資源還可以,在別人的機器上獲取權限和端口來做這個事情不就太好了。
執行命令:socat TCP-LISTEN:8848 EXEC:/bin/bash
比如在本地機器上進行驗證,通過nc進行連接,可以看到shell命令就可以正常執行了,這里我們生成一個文件,然后執行copy命令,然后刪掉生成的文件
liumiaocn:~ liumiao$ nc 127.0.0.1 8848 pwd /Users/liumiao hostname liumiaocn echo "hello, nice to see you..." >create_new_file.txt cp create_new_file.txt copy_file.txt rm create_new_file.txt ^C liumiaocn:~ liumiao$進行結果確認發現,正常的動作都可以執行
liumiaocn:~ liumiao$ ls create_new_file.txt ls: create_new_file.txt: No such file or directory liumiaocn:~ liumiao$ cat copy_file.txt hello, nice to see you... liumiaocn:~ liumiao$使用方法5: 活用exec進行回顯
在很多demo的示例中,對輸入進行回復能夠更加清晰地確認到執行的正確性,比如這里使用exec結合cat命令,即可實現輸出和輸入一樣內容的回顯功能。
執行命令:socat -v tcp-l:8181 exec:"/bin/cat"
執行上述命令之后通過nc進行連接,然后輸入一行消息,回車之后,信息會立即回顯
liumiaocn:~ liumiao$ nc 127.0.0.1 8181 hello, this is greetings from liumiao hello, this is greetings from liumiao而確認socat的終端信息由于使用了-v參數,也可以看到詳細信息
liumiaocn:~ liumiao$ socat -v tcp-l:8181 exec:"/bin/cat" > 2020/03/01 10:17:41.144841 length=38 from=0 to=37 hello, this is greetings from liumiao < 2020/03/01 10:17:41.145345 length=38 from=0 to=37 hello, this is greetings from liumiao總結
socat使用非常方便,這里總結了常見的5種使用方法,但是只是其中非常小的使用方法的一部分,還有關于轉發、SSL支持等很多特性都非常好用。
總結
以上是生活随笔為你收集整理的socat使用指南:3:5种常见的使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 转 php 观察者模式
- 下一篇: Hamcrest 总结