ncat详细介绍
ncat簡介
ncat即Netcat。Netcat用于從TCP/UDP連接中讀取或發(fā)送網(wǎng)絡(luò)數(shù)據(jù)。cat是Linux中查看或連接文件的命令,所以netcat本意為從網(wǎng)絡(luò)上查看文件內(nèi)容。而Netcat的作者Hobbit為它添加了非常豐富的功能,使它幾乎能夠完成網(wǎng)絡(luò)操作中各式各樣的操作,所以Netcat在網(wǎng)絡(luò)安全領(lǐng)域被稱作“TCPIP的瑞士軍刀”(“Swiss-army knife forTCP/IP”)。
Netcat穩(wěn)定版1.10由Hobbit在1996年3月發(fā)布(開源軟件),之后作者沒有再對其進行維護,但該工具十多年來依然在被廣泛地使用,而且基于Netcat的各種衍生工具也層出不窮,他們在很多方面增強或擴展了Netcat的功能。
Nmap團隊開發(fā)了Ncat作為Netcat的升級版本,增加了更多的功能(如ssl加密、代理連接通過socks4 獲取http),讓其更能適應(yīng)現(xiàn)代網(wǎng)絡(luò)環(huán)境的需求。
安裝
yum install -y nc安裝完后可以通過nc或者ncat來使用
參數(shù)說明
命令格式:
ncat [options] [hostname] [port] -4 Use IPv4 only-6 Use IPv6 only-U, --unixsock Use Unix domain sockets only-C, --crlf Use CRLF for EOL sequence-c, --sh-exec <command> Executes the given command via /bin/sh-e, --exec <command> Executes the given command--lua-exec <filename> Executes the given Lua script-g hop1[,hop2,...] Loose source routing hop points (8 max)-G <n> Loose source routing hop pointer (4, 8, 12, ...)-m, --max-conns <n> Maximum <n> simultaneous connections-h, --help Display this help screen-d, --delay <time> Wait between read/writes-o, --output <filename> Dump session data to a file-x, --hex-dump <filename> Dump session data as hex to a file-i, --idle-timeout <time> Idle read/write timeout-p, --source-port port Specify source port to use-s, --source addr Specify source address to use (doesn't affect -l)-l, --listen Bind and listen for incoming connections-k, --keep-open Accept multiple connections in listen mode-n, --nodns Do not resolve hostnames via DNS-t, --telnet Answer Telnet negotiations-u, --udp Use UDP instead of default TCP--sctp Use SCTP instead of default TCP-v, --verbose Set verbosity level (can be used several times)-w, --wait <time> Connect timeout-z Zero-I/O mode, report connection status only--append-output Append rather than clobber specified output files--send-only Only send data, ignoring received; quit on EOF--recv-only Only receive data, never send anything--allow Allow only given hosts to connect to Ncat--allowfile A file of hosts allowed to connect to Ncat--deny Deny given hosts from connecting to Ncat--denyfile A file of hosts denied from connecting to Ncat--broker Enable Ncat's connection brokering mode--chat Start a simple Ncat chat server--proxy <addr[:port]> Specify address of host to proxy through--proxy-type <type> Specify proxy type ("http" or "socks4" or "socks5")--proxy-auth <auth> Authenticate with HTTP or SOCKS proxy server--ssl Connect or listen with SSL--ssl-cert Specify SSL certificate file (PEM) for listening--ssl-key Specify SSL private key (PEM) for listening--ssl-verify Verify trust and domain name of certificates--ssl-trustfile PEM file containing trusted SSL certificates--ssl-ciphers Cipherlist containing SSL ciphers to use--version Display Ncat's version information and exit使用場景
監(jiān)聽入站連接
通過 -l 選項,ncat 可以進入監(jiān)聽模式,使我們可以在指定端口監(jiān)聽入站連接。
ncat -l port_number連接遠程系統(tǒng)
使用下面命令可以用 nc 來連接遠程系統(tǒng),類似于telnet ip port ,建立一個與服務(wù)器的連接。可以發(fā)送命令
ncat IP_address port_number連接 UDP 端口
默認情況下,nc 創(chuàng)建連接時只會連接 TCP 端口。 不過我們可以使用 -u 選項來連接到 UDP 端口
ncat -l -u 1234假設(shè)我們想發(fā)送或者說測試某個遠程主機 UDP 端口的連通性,我們可以使用下面命令
ncat -v -u {host-ip} {udp-port}比如
ncat -v -u 192.168.105.150 53 Ncat: Version 6.40 ( http://nmap.org/ncat ) Ncat: Connected to 192.168.105.150:53將 nc 作為聊天工具
nc 也可以作為聊天工具來用,我們可以配置服務(wù)器監(jiān)聽某個端口,然后從遠程主機上連接到服務(wù)器的這個端口,就可以開始發(fā)送消息了。 在服務(wù)器這端運行:
ncat -l 8080在遠程客戶端主機上運行:
ncat 192.168.1.100 8080之后開始發(fā)送消息,這些消息會在服務(wù)器終端上顯示出來。
將 nc 作為代理
nc 也可以用來做代理。比如下面這個例子
ncat -l 8080 | ncat 192.168.1.200 80所有發(fā)往我們服務(wù)器 8080 端口的連接都會自動轉(zhuǎn)發(fā)到 192.168.1.200 上的 80 端口。 不過由于我們使用了管道,數(shù)據(jù)只能被單向傳輸。 要同時能夠接受返回的數(shù)據(jù),我們需要創(chuàng)建一個雙向管道。 使用下面命令可以做到這點
mkfifo 2way ncat -l 8080 0<2way | ncat 192.168.1.200 80 1>2way現(xiàn)在你可以通過 nc 代理來收發(fā)數(shù)據(jù)了
使用 nc 拷貝文件
nc 還能用來在系統(tǒng)間拷貝文件,雖然這么做并不推薦,因為絕大多數(shù)系統(tǒng)默認都安裝了 ssh/scp。 不過如果你恰好遇見個沒有 ssh/scp 的系統(tǒng)的話, 你可以用 nc 來作最后的努力。
在要接受數(shù)據(jù)的機器上啟動 nc 并讓它進入監(jiān)聽模式:
ncat -l 8080 > file.txt現(xiàn)在去要被拷貝數(shù)據(jù)的機器上運行下面命令:
ncat 192.168.1.100 8080 --send-only < data.txt這里,data.txt 是要發(fā)送的文件。 -–send-only 選項會在文件拷貝完后立即關(guān)閉連接。 如果不加該選項, 我們需要手工按下 ctrl+c 來關(guān)閉連接。
我們也可以用這種方法拷貝整個磁盤分區(qū),不過請一定要小心
通過 nc 創(chuàng)建后門
nc 命令還可以用來在系統(tǒng)中創(chuàng)建后門,并且這種技術(shù)也確實被黑客大量使用。 為了保護我們的系統(tǒng),我們需要知道它是怎么做的。 創(chuàng)建后門的命令為:
ncat -l 10000 -e /bin/bash-e 標志將一個 bash 與端口 10000 相連。現(xiàn)在客戶端只要連接到服務(wù)器上的 10000 端口就能通過 bash 獲取我們系統(tǒng)的完整訪問權(quán)限:
ncat 192.168.1.100 10000通過 nc 進行端口轉(zhuǎn)發(fā)
我們通過選項 -c 來用 nc 進行端口轉(zhuǎn)發(fā),實現(xiàn)端口轉(zhuǎn)發(fā)的語法為:
ncat -u -l 80 -c 'ncat -u -l 8080'這樣,所有連接到 80 端口的連接都會轉(zhuǎn)發(fā)到 8080 端口
設(shè)置連接超時
nc 的監(jiān)聽模式會一直運行,直到手工終止。 不過我們可以通過選項 -w 設(shè)置超時時間:
ncat -w 10 192.168.1.100 8080這會導致連接 10 秒后終止,不過這個選項只能用于客戶端而不是服務(wù)端。
使用 -k 選項強制 nc 待命
當客戶端從服務(wù)端斷開連接后,過一段時間服務(wù)端也會停止監(jiān)聽。 但通過選項 -k 我們可以強制服務(wù)器保持連接并繼續(xù)監(jiān)聽端口。 命令如下:
ncat -l -k 8080現(xiàn)在即使來自客戶端的連接斷了也依然會處于待命狀態(tài)
總結(jié)
- 上一篇: RAP2工具的应用
- 下一篇: gitlab-ci详细说明