NetCat,在网络工具中有“瑞士军刀”美誉,其有Windows和Linux的版本。因为它短小精悍(1.84版本也不过25k,旧版本或缩减版甚至更小)、功能实用,被设计为一个简单、可靠的网络工具,可通过TCP或UDP协议传输读写数据。同时,它还是一个网络应用Debug分析器,因为它可以根据需要创建各种不同类型的网络连接。
一、版本
通常的Linux发行版中都带有NetCat(简称nc),甚至在拯救模式光盘中也由busybox提供了简版的nc工具。但不同的版本,其参数的使用略有差异。
NetCat 官方地址:http://netcat.sourceforge.net/
1
2
3
4
5
6
7
8
|
引用[root@hatest1 ~] # cat /etc/asianux-release
Asianux release 2.0 (Trinity SP2) [root@hatest1 ~] # cat /etc/redflag-release
Red Flag DC Server release 5.0 (Trinity SP2) [root@hatest1 ~] # type -a nc
nc is /usr/bin/nc
[root@hatest1 ~] # rpm -q nc
nc-1.10-22 |
建议在使用前,先用man nc看看帮助。这里以红旗DC Server 5.0上的1.10版本进行简单说明。
假设两服务器信息:
1
2
|
引用server1: 192.168.228.221 server2: 192.168.228.222 |
二、常见使用
1、远程拷贝文件
从server1拷贝文件到server2上。需要先在server2上,用nc激活监听,server2上运行:
1
2
3
4
5
|
引用[root@hatest2 tmp] # nc -lp 1234 > install.log
server1上运行: 引用[root@hatest1 ~] # ll install.log
-rw-r--r-- 1 root root 39693 12月 20 2007 install .log
[root@hatest1 ~] # nc -w 1 192.168.228.222 1234 < install.log
|
2、克隆硬盘或分区
操作与上面的拷贝是雷同的,只需要由dd获得硬盘或分区的数据,然后传输即可。
克隆硬盘或分区的操作,不应在已经mount的的系统上进行。所以,需要使用安装光盘引导后,进入拯救模式(或使用Knoppix工具光盘)启动系统后,在server2上进行类似的监听动作:
1
2
3
|
# nc -l -p 1234 | dd of=/dev/sda server1上执行传输,即可完成从server1克隆sda硬盘到server2的任务: # dd if=/dev/sda | nc 192.168.228.222 1234 |
※ 完成上述工作的前提,是需要落实光盘的拯救模式支持服务器上的网卡,并正确配置IP。
3、端口扫描
可以执行:
1
2
|
引用 # nc -v -w 1 192.168.228.222 -z 1-1000
hatest2 [192.168.228.222] 22 ( ssh ) open
|
4、保存Web页面
1
|
# while true; do nc -l -p 80 -q 1 < somepage.html; done |
5、模拟HTTP Headers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
引用[root@hatest1 ~] # nc www.linuxfly.org 80
GET / HTTP /1 .1
Host: ispconfig.org Referrer: mypage.com User-Agent: my-browser HTTP /1 .1 200 OK
Date: Tue, 16 Dec 2008 07:23:24 GMT Server: Apache /2 .2.6 (Unix) DAV /2 mod_mono /1 .2.1 mod_python /3 .2.8 Python /2 .4.3 mod_perl /2 .0.2 Perl /v5 .8.8
Set-Cookie: PHPSESSID=bbadorbvie1gn037iih6lrdg50; path=/ Expires: 0 Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Cache-Control: private, post-check=0, pre-check=0, max-age=0 Set-Cookie: oWn_sid=xRutAY; expires=Tue, 23-Dec-2008 07:23:24 GMT; path=/ Vary: Accept-Encoding Transfer-Encoding: chunked Content-Type: text /html
[......] |
在nc命令后,输入红色部分的内容,然后按两次回车,即可从对方获得HTTP Headers内容。
6、聊天
nc还可以作为简单的字符下聊天工具使用,同样的,server2上需要启动监听:
1
2
3
4
|
root@hatest2 tmp] # nc -lp 1234
server1上传输: [root@hatest1 ~] # nc 192.168.228.222 1234
|
这样,双方就可以相互交流了。使用Ctrl+D正常退出。
7、传输目录
从server1拷贝nginx-0.6.34目录内容到server2上。需要先在server2上,用nc激活监听,server2上运行:
1
2
3
4
5
|
引用[root@hatest2 tmp] # nc -l 1234 |tar xzvf -
server1上运行: 引用[root@hatest1 ~] # ll -d nginx-0.6.34
drwxr-xr-x 8 1000 1000 4096 12-23 17:25 nginx-0.6.34 [root@hatest1 ~] # tar czvf - nginx-0.6.34|nc 192.168.228.222 1234
|
8、参数简介
这仅是一个1.10版本的简单说明,详细的参数使用还是需要看man:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
引用想要连接到某处: nc [-options] hostname port[s] [ports] ...
绑定端口等待连接: nc -l -p port [-options] [ hostname ] [port]
参数: -g gateway source -routing hop point[s], up to 8
-G num source -routing pointer: 4, 8, 12, ...
-h 帮助信息 -i secs 延时的间隔 -l 监听模式,用于入站连接 -n 指定数字的IP地址,不能用 hostname -o file 记录16进制的传输
-p port 本地端口号 -r 任意指定本地及远程端口 -s addr 本地源地址 -u UDP模式 - v 详细输出——用两个- v 可得到更详细的内容
-w secs timeout的时间 -z 将输入输出关掉——用于扫描时,其中端口号可以指定一个或者用lo-hi式的指定范围。 |
9、1.84版本参数简介
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
1. nc [-46DdhklnrStUuvzC] [-i interval] [-p source_port] 2. [-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_protocol] [-x 3. proxy_address[:port]] [ hostname ] [port[s]]
1. -4 强制使用ipv4 2. -6 强制使用ipv6 3. -D 允许socket通信返回debug信息 4. -d 不允许从标准输入中读取 5. -h 显示nc帮助文档 6. -i interval 7. 指定每行之间内容延时发送和接受,也可以使多个端口之间的连接延时 8. -k 当一个连接结束时,强制nc监听另一个连接。必须和-l一起使用 9. -l 用于监听传入的数据链接,不能与-p -z -s一起使用。-w 参数的超时也会被忽略 10. -n 不执行任何地址,主机名,端口或DNS查询 11. -p 指定nc使用的源端口,受权限限制且不能余-l一起使用 12. -r 指定nc使用的源端口和目的端口,不能使用系统原来就指定的那些端口 13. -S 允许在RFC 2385的TCP MD5签名选项 14. -s source_ip_address 15. 指定用于发包的接口的IP地址,不能和-l一起使用 16. -T ToS 17. 指定链接的IP服务类型(TOS) 18. -C 自动换行 19. -t 使nc能够与telnet交互 20. -U 使用UNIX域socket 21. -u 使用udp代替默认的tcp选项 22. - v 输出详细报告
23. -w timeout 24. 一个链接一段时间无操作,则自动断开,默认无超时 25. -X proxy_version 26. 指定nc使用代理时所采用的协议,可选的有socksv4,socks5以及https。默认socks5 27. -x proxy_address[:port] 28. 指定nc使用的代理地址和端口。默认设置:1080(SOCKS),3128(HTTPS) 29. -z 只监听不发送任何包 |
三、版本差异
不用系统上提供的nc版本会有说不同,其提供的参数使用方法也略有差异。
例如,红旗Asianux 3.0 SP1拯救光盘上的版本是供使用的参数仅有一部分:
引用# nc -h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
BusyBox v1.2.0 (2008.04.14-01:35+0000) multi-call binary Usage: nc [OPTIONS] [IP] [port] Netcat opens a pipe to IP:port Options: -l listen mode, for inbound connects
-p PORT local port number
-i SECS delay interval for lines sent
-e PROG program to exec after connect (dangerous!)
-w SECS timeout for connects and final net reads
而在Asianux 3.0 SP1系统中提供的nc版本则是1.84的,按上面的参数用法写会执行不了: 引用[root@ftpserver ~] # rpm -q nc
nc-1.84-10 [root@ftpserver ~] # nc -lp 1234
usage: nc [-46DdhklnrStUuvzC] [-i interval] [-p source_port] [-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_version]
[-x proxy_address[:port]] [ hostname ] [port[s]]
|
讲查看man文档,可见在这个版本中,-l是不能与-s、-p、-z一起使用的,-w参数也会被忽略,所以,正确的用法是:
1
|
[root@ftpserver tmp] # nc -l 1234
|
四、用在脚本中
nc每次启动监听后,都会在客户端连接完成并退出的同时,服务端一同退出。所以,如果需要不断的使用nc进行数据传输,需要在脚本中使用循环。利用nc实现更多的功能,可参考其rpm提供的参考脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
引用 # rpm -qd nc
/usr/share/doc/nc-1 .10 /Changelog
/usr/share/doc/nc-1 .10 /README
/usr/share/doc/nc-1 .10 /scripts/README
/usr/share/doc/nc-1 .10 /scripts/alta
/usr/share/doc/nc-1 .10 /scripts/bsh
/usr/share/doc/nc-1 .10 /scripts/dist .sh
/usr/share/doc/nc-1 .10 /scripts/irc
/usr/share/doc/nc-1 .10 /scripts/iscan
/usr/share/doc/nc-1 .10 /scripts/ncp
/usr/share/doc/nc-1 .10 /scripts/probe
/usr/share/doc/nc-1 .10 /scripts/web
/usr/share/doc/nc-1 .10 /scripts/webrelay
/usr/share/doc/nc-1 .10 /scripts/websearch
/usr/share/man/man1/nc .1.gz
|