netcat 命令詳解

簡介

netcat 是一款調試 TCP/UDP 網絡連接的利器,常被稱作網絡調試的瑞士軍刀,可見其功能強大。
netcat 在 Linux, Windows 等各大操作系統上都有對應等發行版,以下以 Linux(Ubuntu 16.04) 爲例介紹其幾個強大的用法。

netcat 在 Linux 中一般通過命令 nc 調用。我們先來看下它的幫助文檔

# 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 等發行版。不同發行版的
基本功能相同,只是有細微的差別。

簡單來說, nc 有以下功能:

  • 模擬 TCP 服務端
  • 模擬 TCP 客戶端
  • 模擬 UDP 服務端
  • 模擬 UDP 客戶端
  • 模擬 UNIX socket 服務端
  • 模擬 UNIX socket 客戶端
  • 端口掃描
  • 傳輸文件
  • 將服務器 bash 暴露給遠程客戶端
  • 內網穿透,反向獲取防火牆後的機器的 bash

以下分別舉例說明。

實例

環境設定

假設

  • 服務器 A 有外網 IP 202.118.69.40
  • 服務器 B 沒有外網 IP
  • 客戶端 C 有外網 IP 202.119.70.41

三臺主機上均爲 Ubuntu 16.04 操作系統。

TODO 網絡拓撲圖

1 模擬 TCP 服務端

nc -lk 9090

在服務器 A 執行以上命令,將會把 nc 綁定到 9090 端口,並開始監聽請求。
-l 代表 netcat 將以監聽模式運行;
-k 表示 nc 在接收完一個請求後不會立即退出,而是會繼續監聽其他請求。
這時就可以請求該接口了, nc 會把請求報文輸出到標準輸出。

例如在客戶端 C 執行 curl 202.118.69.40
nc 將會將 HTTP 請求的報文輸出到標準輸出

GET / HTTP/1.1
Host: 192.168.0.71:9090
User-Agent: curl/7.54.0
Accept: */*

2 模擬 TCP 客戶端

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 請求。

3 模擬 UDP 服務端

在 A 執行

nc -lk -u 9090

4 模擬 UDP 客戶端

在 C 執行

nc -u 202.118.69.40 9090

此時在客戶端終端中輸入任意字符,將在 A 的終端中輸出同樣的字符,證明 UDP 服務端和客戶端模擬成功。

5 模擬 UNIX socket 服務端

在 A 執行

nc -Ul /tmp/mync.sock

6 模擬 UNIX socket 客戶端

在 A 執行(UNIX 默認不能跨服務器)

nc -U /tmp/mync.sock

此時在該終端中輸入任意字符,將在第5步的終端中輸出同樣的字符,證明 Unix socket 服務端和客戶端模擬成功。

7 端口掃描

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!

8 傳輸文件

8.1 向服務器上傳圖片

服務器 A 監聽 9090 端口

nc -l 9090 | base64 -d > WechatIMG88.jpeg

客戶端上傳圖片

base64 WechatIMG88.jpeg | nc 202.118.69.40 9090

注:因爲需要傳輸圖片,所以先 base64 編碼,然後下載完再解碼避免終端錯亂。

8.2 從服務器下載圖片

服務器 A 監聽 9090 端口,並將要下載的圖片輸出到 nc

base64 WechatIMG88.jpeg | nc -l 9090

客戶端下載

nc -t 202.118.69.40 9090|base64 -D > w.jpeg

9 將服務器 bash 暴露給遠程客戶端

與 7 類似,只不過服務端將接收到到內容管道給 /bin/bash
然後在客戶端輸入要敲的命令

nc -l 9090 | /bin/bash

10 內網穿透,反向獲取防火牆後的機器的 bash

與 8 類似,只不過服務器 B 將內容管道給 /bin/bash
在客戶端 A 打開監聽

nc -l 202.119.70.41 90

在服務器 C 上執行以下代碼反向接受命令

nc -t 202.119.70.41 9090 | /bin/bash

然後在客戶端 A 輸入要執行的命令即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章