简介
nc全称netcat,是一个功能强大的网络工具,被网络界称赞为瑞士军刀。主要被用来端口监听,端口扫描、传输文件、网络测速。
详解
使用nc -h
查看帮助文档。
┌──(root㉿kali)-[~]
└─# nc -h
[v1.10-47]
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
-G num source-routing pointer: 4, 8, 12, ...
-h this cruft
-i secs delay interval for lines sent, ports scanned
-k set keepalive option on socket
-l listen mode, for inbound connects
-n numeric-only IP addresses, no DNS
-o file hex dump of traffic
-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
-v verbose [use twice to be more verbose]
-w secs timeout for connects and final net reads
-C Send CRLF as line-ending
-z zero-I/O mode [used for scanning]
port numbers can be individual or ranges: lo-hi [inclusive];
hyphens in port names must be backslash escaped (e.g. 'ftp\-data').
参数讲解
-c shell commands | 为“-e”;使用/bin/sh执行[危险!!] |
-e filename | 连接后要执行的程序[危险!!] |
-b | 允许广播 |
-g gateway | 源路由跃点,最多8个 |
-G num | 源路由指针:4、8、12 |
-i secs | 发送线路、扫描端口的延迟间隔 |
-k | 在套接字上设置keepalive选项 |
-l | 侦听模式,用于入站连接 |
-n | 只有数字IP地址,不用DNS解析 |
-o file | 十六进制流量转储 |
-p port | 指定nc使用的端口号 |
-r | 随机化本地和远程端口 |
-q secs | 在stdin上的EOF后退出,延迟秒 |
-s addr | 本地源地址 |
-T tops | 设置服务类型 |
-t | 回答TELNET协商 |
-u | UDP模式 |
-v | 输出详细执行过程[使用两次更详细] |
-w secs | 连接和最终网络读取超时 |
-C | 以行结尾发送CRLF |
-z | 零I/O模式,就是不发送任何数据包[用于扫描] |
实例
端口扫描
nc -zv 192.168.0.103 80-90
监听服务
# Step 1: 开启本地HTTP服务器
python -m SimpleHTTPServer 8080
# Step 2: 建立本地服务器地址到目标地址(8080->8001)的监听器,并输出连接成功信息
nc -l 8080 | nc localhost 8001 -q 0
你也可以通过以下方式直接将客户端的网络流量重定向到目标主机上,达到相应的端口转发效果,参考如下代码:
nc -p [侦听端口号] [目标IP] [目标端口号]
通过执行上述命令之后即可建立连接并进行数据传输。其中,-p选项指定当前客户端使用的端口号(同样也是 UNIX 中 socket 的原理),而[目标IP] 和[目标端口]则需要指定想要与之通信的远程主机地址及其端口号。
数据传输
从远程主机到本地主机下载文件:nc -w 30 [远程IP] [远程端口] > [保存文件路径及名称]
将本地文件传输给远程主机:nc -w 30 [目标IP] [目标端口] < [本地文件位置]