轉載註明出處,原文在此 https://segmentfault.com/a/11...
netcat 是一款調試 TCP/UDP 網絡鏈接的利器,常被稱做網絡調試的瑞士軍刀,可見其功能強大。
netcat 在 Linux, Windows 等各大操做系統上都有對應等發行版,如下以 Linux(Ubuntu 16.04) 爲例介紹其幾個強大的用法。css
netcat 在 Linux 中通常經過命令 nc 調用。咱們先來看下它的幫助文檔html
# nc -h OpenBSD netcat (Debian patchlevel 1.105-7ubuntu1) This is nc from the netcat-openbsd package. An alternative nc is available in the netcat-traditional package. usage: nc [-46bCDdhjklnrStUuvZz] [-I length] [-i interval] [-O length] [-P proxy_username] [-p source_port] [-q seconds] [-s source] [-T toskeyword] [-V rtable] [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]] [destination] [port] Command Summary: -4 Use IPv4 -6 Use IPv6 -b Allow broadcast -C Send CRLF as line-ending -D Enable the debug socket option -d Detach from stdin -h This help text -I length TCP receive buffer length -i secs Delay interval for lines sent, ports scanned -j Use jumbo frame -k Keep inbound sockets open for multiple connects -l Listen mode, for inbound connects -n Suppress name/port resolutions -O length TCP send buffer length -P proxyuser Username for proxy authentication -p port Specify local port for remote connects -q secs quit after EOF on stdin and delay of secs -r Randomize remote ports -S Enable the TCP MD5 signature option -s addr Local source address -T toskeyword Set IP Type of Service -t Answer TELNET negotiation -U Use UNIX domain socket -u UDP mode -V rtable Specify alternate routing table -v Verbose -w secs Timeout for connects and final net reads -X proto Proxy protocol: "4", "5" (SOCKS) or "connect" -x addr[:port] Specify proxy address and port -Z DCCP mode -z Zero-I/O mode [used for scanning] Port numbers can be individual or ranges: lo-hi [inclusive]
能夠看到咱們使用的是 netcat-openbsd
這個發行版的 netcat, 除此以外,還有 netcat-traditional
等發行版。不一樣發行版的
基本功能相同,只是有細微的差異。ubuntu
簡單來講, nc 有如下功能:segmentfault
如下分別舉例說明。bash
假設服務器
三臺主機上均爲 Ubuntu 16.04 操做系統。網絡
TODO 網絡拓撲圖
dom
nc -lk 9090
在服務器 A 執行以上命令,將會把 nc 綁定到 9090 端口,並開始監聽請求。-l
表明 netcat 將以監聽模式運行; -k
表示 nc 在接收完一個請求後不會當即退出,而是會繼續監聽其餘請求。
這時就能夠請求該接口了, nc 會把請求報文輸出到標準輸出。ssh
例如在客戶端 C 執行 curl 202.118.69.40
nc 將會將 HTTP 請求的報文輸出到標準輸出curl
GET / HTTP/1.1 Host: 192.168.0.71:9090 User-Agent: curl/7.54.0 Accept: */*
printf "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n" | nc example.com 80
在客戶端 C 執行上述代碼,
C 的輸出以下
Connection to example.com port 80 [tcp/http] succeeded! HTTP/1.1 200 OK Accept-Ranges: bytes Cache-Control: max-age=604800 Content-Type: text/html; charset=UTF-8 Date: Tue, 09 Oct 2018 07:08:38 GMT Etag: "1541025663+gzip" Expires: Tue, 16 Oct 2018 07:08:38 GMT Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT Server: ECS (sjc/4E52) Vary: Accept-Encoding X-Cache: HIT Content-Length: 1270 <!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> ... </head> <body> <div> <h1>Example Domain</h1> <p>This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission.</p> <p><a href="http://www.iana.org/domains/example">More information...</a></p> </div> </body> </html>
證實客戶端模擬成功,給 example.com 發送了 HTTP Method 爲 GET 的 HTTP 請求。
在 A 執行
nc -lk -u 9090
在 C 執行
nc -u 202.118.69.40 9090
此時在客戶端終端中輸入任意字符,將在 A 的終端中輸出一樣的字符,證實 UDP 服務端和客戶端模擬成功。
在 A 執行
nc -Ul /tmp/mync.sock
在 A 執行(UNIX 默認不能跨服務器)
nc -U /tmp/mync.sock
此時在該終端中輸入任意字符,將在第5步的終端中輸出一樣的字符,證實 Unix socket 服務端和客戶端模擬成功。
nc -vz 202.118.69.40 1-81 2>&1|grep succeed
-z
指 Zero-I/O mode,即鏈接的過程當中禁用輸入輸出,僅用與端口掃描。2>&1|grep succeed
默認狀況下掃描過程當中,不論成功與失敗,掃描結果都被輸出到了「標準錯誤輸出」,該命令用來過濾,僅顯示出打開到端口。
上述指令輸出結果以下:
Connection to 202.118.69.40 22 port [tcp/ssh] succeeded! Connection to 202.118.69.40 53 port [tcp/domain] succeeded! Connection to 202.118.69.40 80 port [tcp/http] succeeded!
服務器 A 監聽 9090 端口
nc -l 9090 | base64 -d > WechatIMG88.jpeg
客戶端上傳圖片
base64 WechatIMG88.jpeg | nc 202.118.69.40 9090
注:由於須要傳輸圖片,因此先 base64 編碼,而後下載完再解碼避免終端錯亂。
服務器 A 監聽 9090 端口,並將要下載的圖片輸出到 nc
base64 WechatIMG88.jpeg | nc -l 9090
客戶端下載
nc -t 202.118.69.40 9090|base64 -D > w.jpeg
與 7 相似,只不過服務端將接收到到內容管道給 /bin/bash
而後在客戶端輸入要敲的命令
nc -l 9090 | /bin/bash
與 8 相似,只不過服務器 B 將內容管道給 /bin/bash
在客戶端 A 打開監聽
nc -l 9090
在服務器 C 上執行如下代碼反向接受命令
nc -t 202.119.70.41 9090 | /bin/bash
而後在客戶端 A 輸入要執行的命令便可。
須要注意的是,使用上述命令遠程執行命令時在客戶端沒法看到命令的返回結果。
經過建立命名管道的方式,可將 bash 執行的結果回傳給 netcat,
具體命令以下(在服務器 C 執行代碼):
mkfifo ncpipe nc -t 202.119.70.41 9090 0<ncpipe| /bin/bash 1>ncpipe