NetCat,在网络工具中有“瑞士军刀”美誉,其有Windows和Linux的版本。因为它短小精悍,功能实用,被设计为一个简单、可靠的网络工具,可通过TCP或UDP协议传输读写数据。同时,它还是一个网络应用Debug分析器,因为它可以根据需要创建各种不同类型的网络连接。
先来看下它的基本用法:
root@10.1.1.43:~# nc -h
[v1.10-38]
connect to somewhere: nc [-options] hostname port[s] [ports] …
listen for inbound: nc -l -p port [-options] [hostname] [port]
options:
-c shell commands as `-e’; use /bin/sh to exec [dangerous!!]
-e filename program to exec after connect [dangerous!!]
-b allow broadcasts
-g gateway source-routing hop point[s], up to 8 设置路由器跃程通信网关,最高可设置8个。
-G num source-routing pointer: 4, 8, 12, … 设置来源路由指向器,其数值为4的倍数。
-h this cruft
-i secs delay interval for lines sent, ports sca 延时的间隔
-k set keepalive option on socket
-l listen mode, for inbound connects 监听模式,入站连接
-n numeric-only IP addresses, no DNS 直接使用ip地址,而不用域名服务器
-o file hex dump of traffic 指定文件名称,把往来传输的数据以16进制字码倾倒成该文件保存。
-p port local port number 本地端口
-r randomize local and remote ports 随机本地和远程端口
-q secs quit after EOF on stdin and delay of secs
-s addr local source address
-T tos set Type Of Service
-t answer TELNET negotiation
-u UDP mode udp 模式
-v verbose [use twice to be more verbose] 显示过程,vv 更多
-w secs timeout for connects and final net reads 等待连接超时
-z zero-I/O mode [used for scanning] 使用输入/输出模式,只在扫描通信端口时使用。
1.监听本地端口
root@10.1.1.43:~# nc -l -p 1234
root@10.1.1.43:~# netstat -tunlp | grep 1234
tcp 0 0 0.0.0.0:1234 0.0.0.0:* LISTEN 15543/nc
2.端口扫描
root@10.1.1.43:~# nc -v -w 10 10.1.1.180 80
(UNKNOWN) [10.1.1.180] 80 (www) open
root@10.1.1.43:~# nc -v -w 10 10.1.1.180 -z 80-30000
(UNKNOWN) [10.1.1.180] 22000 (?) open
(UNKNOWN) [10.1.1.180] 80 (www) open
3.文件传出
源10.1.1.43 text.txt
目的 10.1.1.180
root@10:~# nc -l -p 1234 > test.txt #开10.1.1.180:1234端口监听,并将socket传输过来的数据重定向到test.txt文件 test 43 nc
root@10.1.1.43:~#cat test.txt
root@10.1.1.43:~# nc 10.1.1.180 1234 < test.txt #连接远程的10.1.1.180,从test.txt的路径从定向到socket,从而将文件传输到远方。
root@10:~# cat test.txt
test 43 nc
4.目录传输
源10.1.1.43 python_program
目的 10.1.1.180
root@10:~# nc -l -p 1234 | tar xzvf -
root@10.1.1.43:~# tar czvf - python_program | nc 10.1.1.180 1234
python_program/
python_program/1.py
python_program/4.py
python_program/3.py
5.测试UDP端口
root@172.16.211.34:web# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1111/sshd
udp 0 0 0.0.0.0:68 0.0.0.0:* 887/dhclient
root@172.16.211.35:~# nc -vuz 172.16.211.34 68
Connection to 172.16.211.34 68 port [udp/bootpc] succeeded!
简单来说, 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>
<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">
…
Example Domain
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.