socat使用指南:3:5种常见的使用方法
這篇文章繼續(xù)介紹一下socat常見(jiàn)的5種使用方法。
使用方法1:當(dāng)作cat來(lái)使用
liumiaocn:~ liumiao$ socat - `pwd`/greetings hello liumiao liumiaocn:~ liumiao$但是需要注意的是相對(duì)路徑或者絕對(duì)路徑寫(xiě)完整,畢竟還是socket方式的實(shí)現(xiàn),直接使用文件名可能會(huì)出現(xiàn)下面類(lèi)似的錯(cuò)誤。
liumiaocn:~ liumiao$ socat - greetings 2020/03/01 09:28:08 socat[26732] E unknown device/address "greetings" liumiaocn:~ liumiao$相對(duì)路徑也可以使用
liumiaocn:~ liumiao$ socat - ./greetings hello liumiao liumiaocn:~ liumiao$使用方法2: 寫(xiě)文件
通過(guò)管道將內(nèi)容傳遞給指定名稱(chēng)的文件
liumiaocn:~ liumiao$ echo "hi, liumiao" |socat - ./hellomsg liumiaocn:~ liumiao$ cat hellomsg hi, liumiao liumiaocn:~ liumiao$這種方式缺省情況下是append的方式追加內(nèi)容的,再次執(zhí)行即可確認(rèn)
liumiaocn:~ liumiao$ echo "greetings " |socat - ./hellomsg liumiaocn:~ liumiao$ cat ./hellomsg hi, liumiao greetings liumiaocn:~ liumiao$使用方法3: 指定端口和類(lèi)型進(jìn)行監(jiān)聽(tīng)
比如在8088端口指定TCP方式的監(jiān)聽(tīng),客戶端連接此端口發(fā)送的數(shù)據(jù)都可以被socat確認(rèn)到。
步驟1: 使用socat啟動(dòng)監(jiān)聽(tīng)
執(zhí)行命令:socat tcp-listen:8088 -
步驟2: 使用nc發(fā)送數(shù)據(jù)
執(zhí)行命令:nc 127.0.0.1 8088
另啟一個(gè)終端執(zhí)行上述命令之后,輸入的信息都可以在socat終端看到,實(shí)際上是socket通信方式的數(shù)據(jù)傳輸。
liumiaocn:~ liumiao$ nc 127.0.0.1 8088 hello, this is greetings from liumiao而此時(shí)在socat監(jiān)聽(tīng)的終端即可確認(rèn)到信息的傳遞
liumiaocn:~ liumiao$ socat tcp-listen:8088 - hello, this is greetings from liumiao使用方法4: 活用EXEC當(dāng)作shell代理
使用者可以使用如下命令即可在目標(biāo)機(jī)器上設(shè)立一個(gè)shell代理,結(jié)合進(jìn)程隱藏可能更難以被發(fā)現(xiàn),自己使用自己的資源還可以,在別人的機(jī)器上獲取權(quán)限和端口來(lái)做這個(gè)事情不就太好了。
執(zhí)行命令:socat TCP-LISTEN:8848 EXEC:/bin/bash
比如在本地機(jī)器上進(jìn)行驗(yàn)證,通過(guò)nc進(jìn)行連接,可以看到shell命令就可以正常執(zhí)行了,這里我們生成一個(gè)文件,然后執(zhí)行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$進(jìn)行結(jié)果確認(rèn)發(fā)現(xiàn),正常的動(dòng)作都可以執(zhí)行
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進(jìn)行回顯
在很多demo的示例中,對(duì)輸入進(jìn)行回復(fù)能夠更加清晰地確認(rèn)到執(zhí)行的正確性,比如這里使用exec結(jié)合cat命令,即可實(shí)現(xiàn)輸出和輸入一樣內(nèi)容的回顯功能。
執(zhí)行命令:socat -v tcp-l:8181 exec:"/bin/cat"
執(zhí)行上述命令之后通過(guò)nc進(jìn)行連接,然后輸入一行消息,回車(chē)之后,信息會(huì)立即回顯
liumiaocn:~ liumiao$ nc 127.0.0.1 8181 hello, this is greetings from liumiao hello, this is greetings from liumiao而確認(rèn)socat的終端信息由于使用了-v參數(shù),也可以看到詳細(xì)信息
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總結(jié)
socat使用非常方便,這里總結(jié)了常見(jiàn)的5種使用方法,但是只是其中非常小的使用方法的一部分,還有關(guān)于轉(zhuǎn)發(fā)、SSL支持等很多特性都非常好用。
總結(jié)
以上是生活随笔為你收集整理的socat使用指南:3:5种常见的使用方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 转 php 观察者模式
- 下一篇: Hamcrest 总结