NetCat 强大的网络小工具

2013/01/28 15:27
阅读数 378

NetCat

by Jian Lee

简介

使用

最简单的使用: ”nc 主机名/IP 端口号/服务名",只要甲主机打开了端口 (任何对外服务的端口),乙主机就可以使用nc命令连接这个端口。比 如nmap查看到172.16.70.140开了22端口,通常这是sshd的服务端口,我 们nc上去看看:

# nc 172.16.70.140 22
SSH-2.0-OpenSSH_4.3

ssh的版本号都显示了,不过一般我们做不了什么事情,但是现在对方的 sshd真在等待我们的输入,如果你能正确和sshd“交流”,那什么都可以 做了 ^_^。

使用主机名也可以,这次我们加一个 -v 参数:

# nc -v g.cn 80
Connection to g.cn 80 port [tcp/http] succeeded!

端口号我们可以不用数字,用 /etc/services 里面的服务名来代替知名 端口号,好像也不是所有的系统自带nc都支持:

# nc dev3-140  ssh
SSH-2.0-OpenSSH_4.3

隐藏命令行参数

如果没有提供命令行参数,NetCat会提示你从标准输入来输入命令参 数,然后NetCat会在内部解析输入。用这种办法输入命令式参数,可以 用来防止借助“ps”来查看你的命令行参数。下面是一个示例:

# nc
Cmd line: -l 1234

此时我们在系统上用ps命令查看nc的进程,是看不到nc的参数的。如果 我们直接在命令行写上参数,ps就能看到命令行参数了。

# nc -l 1234
// 上面命令我们一般能用ps命令查看到类似下面的信息。
# ps aux|grep nc
root      7332  0.0  0.0   1772   536 pts/4    S+   10:38   0:00 nc -l 1234

正/反向域名解析

主机参数可以是一个名字或一个IP地址。如果-n出现,则它接受IP地 址,而不再对计算机的名字或域名进行解析。如果没有-n,但加上-v, 则 NetCat可进行正/反向域名解析,并警告the all-too-common problem of mismatched name in DNS。这会耗费稍多一点时间,但在某 些情况下会有用处。如,你想知道某个IP的主机名,NetCat可省却你手 工查找的时间。

// 甲机器使用参数运行nc:
# /data/bin/nc -l 1234
... 此处是等待

// 乙机器连接:
# nc -v dev3-168 1234
Connection to dev3-168 1234 port [tcp/search-agent] succeeded!
... 此处是等待

// 接着在乙机器输入任何字符串并按Enter键,甲机器都能收到。

上面dev3-168是我的域中一个机器名,-v参数和-n参数都对主机名解析。 都是-v参数可以通过ip反向解析出主机名。示例:

// 很多系统自带nc的-v参数不能做到这一点。
# nc -v 172.16.70.140 1234
dev3-140.dev.cn.tlan [172.16.70.140] 1234 (?) open

参数详解

显示详细连接信息

-v 参数可以将一些关于连接建立信息输出到标准错误。-v参数多出现 几次,则显示的信息会更多一些。如果-v参数没有出现,则NetCat将默 默地工作,至到出现错误为止。

# nc -vvv g.cn 80
Connection to g.cn 80 port [tcp/http] succeeded!

设定建立链接时间

-w

[root@dev3-140 opt]# nc -vvv g.cn 80 -w 1   //1秒后断开连接
Connection to g.cn 80 port [tcp/http] succeeded!
[root@dev3-140 opt]# nc -vvv g.cn 80 -w 2 //2秒
Connection to g.cn 80 port [tcp/http] succeeded!
[root@dev3-140 opt]# nc -vvv g.cn 80 -w 2 -w 3   // 3秒,覆盖前面的2秒设置
Connection to g.cn 80 port [tcp/http] succeeded!

绑定端口

-p 端口 " 来绑定本地端口。除了因权限限制或端口已经使用外,-p可 以绑定任何端口。Root用户可以绑定保留的1024以内的端口。如果不 用-p指定端口,则使用系统给定的未使用的端口。(-p功能在客户端状 态也可以使用,-s功能并不是在所有的平台上都可用)

作为服务器

-l 参数可以使NetCat以服务器状态运行。 ”nc -l -p 1234 [remote hostname] [remote port]” 可以用来指定入连的主机和端口,如果申 请连接的主机或端口不符指定,则会断开连接。

// 172.16.70.168上运行nc监听本地80端口
// 远程client机器指定ip为172.16.70.140,使用的链接端口是35
# nc -l -p 80 172.16.70.140 35

// 只有172.16.70.140上运行下面命令才能链接到168机器上
# nc 172.16.70.168 80 -p 35

执行指定程序

-e 参数后面跟一可执行程序的名称,当一个连接(入或出)被建立 时,这个程序被运行。尤其当NetCat以服务器端运行时,-e参数使其有 点象inetd了,只是只能运行一个进行而已。需要说明的是,-e后的程 序不能从NetCat的命令行接收参数,如果有参数要传递,可能需要一个 脚本。通常linux的发行版是不带这个参数支持的,可以自己编译,编 译时置-DGAPING_SECURITY_HOLE,则-e参数被NetCat支持。

// 172.16.70.168作为一个服务器,当172.16.70.140的端口35链接此140时
// 执行 /bin/sh 命令
# nc
Cmd line: -l -p 80 172.16.70.140 35 -e /bin/sh

// 172.16.70.140上执行下面命令链接上去
# nc 172.16.70.168 80 -p 35
ls   ---> 这是传递给172.16.70.168上/bin/sh的指令
Adobe
nc_httpd.log

上面 -e 参数用法是在指定主机链接进来执行一个程序,相对于服务器 的作用,我们也可以在链接的时候使用 -e 参数,相对于client端:

// 172.16.70.140 上执行
# nc -l 80

// 172.16.70.168 上执行
# nc 172.16.70.140 80 -e /bin/dmesg

其实 -e 参数在哪一个机器都可以执行,这里面有一个基本原 理,netcat工具只是在网络两端建立一个 TCP/UDP 同路。两边的关系 都是对等的。

// 172.16.70.24 上执行
# nc -l 1234

// 172.16.70.168 上执行
# nc -e /bin/bash 172.16.70.24 1234

//这样在172.16.70.24上输入的没一个串字符都是
//172.16.70.168 上的 /bin/bash 的输入。也就是执行bash命令了。

当做telnetd服务器

-t 参数通常在linux发行版自带的nc都是支持的,此时NetCat可以登录 到一个telnetd服务器,并提供相关的握手应答,至到出现登录提示符。

// 172.16.70.140 上
# nc -l 80 -t

// 172.16.70.168 上可以
# nc 172.16.70.140 80

// 也可以
# telnet 172.16.70.140 80

案例

监听端口(制作蜜罐)

很多时候我们可以让nc监听一个端口来达到制作蜜罐的作用。例如我们 在 172.16.70.140 机器上启动nc监听80端口,然后在 172.16.70.168 上用firefox打开地址: http://172.16.70.140 ,这样140机器的nc端可 以得到168试图连接的信息:

# nc -l 80
GET / HTTP/1.1
Host: 172.16.70.140
User-Agent: Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.0.5) Gecko/2008121622 Ubuntu/8.10 (intrepid) Firefox/3.0.5
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

有写nc版本可以使用 -o 参数保存信息到一个文件中。不过我们可以用 shell重定向来做。只是建立的是文本文件而已。

// 172.16.70.140 上运行
# nc -l 80 &> nc_httpd.log &

// 172.16.70.168 上运行
# w3m http://dev3-140

// 172.16.70.140 上可以查看 nc_httpd.log
# cat nc_httpd.log
GET / HTTP/1.0
User-Agent: w3m/0.5.2
Accept: text/html, text/*;q=0.5, image/*, application/*, multipart/*, message/*, audio/*
Accept-Encoding: gzip, compress, bzip, bzip2, deflate
Accept-Language: en;q=1.0
Host: dev3-140

不过注意,上面参数后台运行nc的时候,进程用手动kill掉。

端口扫描

// 扫描172.16.70.24的20至30端口
#  nc -z  -v -v 172.16.70.24 20-30

ftp 服务器

前面的nc建立链接除了 -e 参数指定运行程序,一般都是从标准输入获 取输入。被链接的主机将收到的信息输出到标准输出。nc可以接受重定 向数据,如果使用重定向技术,就可以把nc当做服务器使用了:

// 172.16.70.168 上监听链接(相对于server)
# nc -l -p 80 > passwd

// 172.16.70.140 上链接(相对于client)
# nc 172.16.70.168 80 < /etc/passwd
// 赶快去168看看,passwd是不是已经传过去了?

可以打包压缩传输,用条管道而已:

// 172.16.70.168 接收nc的输入给tar程序
# nc -l -p 80 | tar xzf -

// 172.16.70.140 用tar打包压缩e1000e目录传递给nc
# tar cjf - e1000e |  nc 172.16.70.168 80

如果客户端没有nc怎么办?下面一个示例:

// 172.16.70.24 的机器上运行
# nc -l http < /var/log/messages

上面 172.16.70.24 运行了nc,监听80(http)端口的请求,把 /var/log/messages 作为标准输入。现在我们可以在 172.16.70.168 上 使用firefox打开网址: http://172.16.70.24 就可以看到24机器上的 messages 日志了。

两台服务器文件校验

// 172.16.70.216
# find /etc/rc.d/ -type f|xargs md5sum|nc -l 1234

// 172.16.70.24
# nc 172.16.70.216 1234|md5sum -c -|grep -v OK

这样可比较的东西就多了,例如下面命令检查机器上的sid位:

find / -xdev -type f -user root \( -perm -4000 -o -perm -2000 \)

使用注意

netcat 一般的linux发行版本都自带,通常都叫 nc 。不过自带的 netcat 功能不完全,比如 -e 的参数不支持,而且一些命令的使用也 不一样。GNU的nc功能比较全面,可以自己编译。

网上介绍 netcat 的使用文章也很多,虽然这个工具原理是如此的简 单,但是在 Unix/Linux 环境中它能发挥的作用远远超过我们的想像。 网上的很多方法并不一定能在你的当前系统中运行,主要是 netcat 的 版本不太一样。也有些经过多次转贴,已经有错误了。这时,你应该仔 细查看你的 nc 支持选项。使用 "nc -h" 查看,下面两个版本的 nc 支持选项差别还是很大的:

下面是我的ubuntu系统上 netcat-openbsd 包中的 nc 用法:

# nc -h
OpenBSD netcat (Debian patchlevel 1.89-3ubuntu1)
usage: nc [-46DdhklnrStUuvzC] [-i interval] [-P proxy_username] [-p source_port]
          [-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_protocol]
          [-x proxy_address[:port]] [hostname] [port[s]]
        Command Summary:
                -4              Use IPv4
                -6              Use IPv6
                -D              Enable the debug socket option
                -d              Detach from stdin
                -h              This help text
                -i secs         Delay interval for lines sent, ports scanned
                -k              Keep inbound sockets open for multiple connects
                -l              Listen mode, for inbound connects
                -n              Suppress name/port resolutions
                -P proxyuser    Username for proxy authentication
                -p port         Specify local port for remote connects
                -q secs         quit after EOF on stdin and delay of secs (-1 to not quit)
                -r              Randomize remote ports
                -S              Enable the TCP MD5 signature option
                -s addr         Local source address
                -T ToS          Set IP Type of Service
                -C              Send CRLF as line-ending
                -t              Answer TELNET negotiation
                -U              Use UNIX domain socket
                -u              UDP mode
                -v              Verbose
                -w secs         Timeout for connects and final net reads
                -X proto        Proxy protocol: "4", "5" (SOCKS) or "connect"
                -x addr[:port]  Specify proxy address and port
                -z              Zero-I/O mode [used for scanning]
        Port numbers can be individual or ranges: lo-hi [inclusive]

RHEL 中的 nc 也是 openbsd版本的:

# nc -h
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]]
        Command Summary:
                -4              Use IPv4
                -6              Use IPv6
                -D              Enable the debug socket option
                -d              Detach from stdin
                -h              This help text
                -i secs         Delay interval for lines sent, ports scanned
                -k              Keep inbound sockets open for multiple connects
                -l              Listen mode, for inbound connects
                -n              Suppress name/port resolutions
                -p port         Specify local port for remote connects
                -r              Randomize remote ports
                -s addr         Local source address
                -T ToS          Set IP Type of Service
                -C              Send CRLF as line-ending
                -t              Answer TELNET negotiation
                -U              Use UNIX domain socket
                -u              UDP mode
                -v              Verbose
                -w secs         Timeout for connects and final net reads
                -X proto        Proxy protocol: "4", "5" (SOCKS) or "connect"
                -x addr[:port]  Specify proxy address and port
                -z              Zero-I/O mode [used for scanning]
        Port numbers can be individual or ranges: lo-hi [inclusive]

ubuntu 中还有一个 netcat 版本,功能很强大,上面例子实际都是使 用这个版本的nc:

# nc.traditional -h
[v1.10-38]
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
        -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').
您是第  统计 位访问者 
© 2009  YLinux 
最后更新 : 2009年9月3日 
51.la 专业、免费、强健的访问统计 
展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
4 收藏
1
分享
返回顶部
顶部