nmap命令 基础用法附官方手册
nmap系统漏洞扫描之王,也就是network mapper,是linux下常用的网络扫描和嗅探工具包,现在支持多个平台。
其基本功能有三个:
(1)是扫描主机端口,嗅探所提供的网络服务
(2)是探测一组主机是否在线
(3)还可以推断主机所用的操作系统,到达主机经过的路由,系统已开放端口的软件版本
nmap端口状态解析
open : 应用程序在该端口接收 tcp 连接或者 udp 报文。
closed :关闭的端口对于nmap也是可访问的, 它接收nmap探测报文并作出响应。但没有应用程序在其上监听。
filtered :由于包过滤阻止探测报文到达端口,nmap无法确定该端口是否开放。过滤可能来自专业的防火墙设备,路由规则 或者主机上的软件防火墙。
unfiltered :未被过滤状态意味着端口可访问,但是nmap无法确定它是开放还是关闭。 只有用于映射防火墙规则集的 ack 扫描才会把端口分类到这个状态。
open | filtered :无法确定端口是开放还是被过滤, 开放的端口不响应就是一个例子。没有响应也可能意味着报文过滤器丢弃了探测报文或者它引发的任何反应。udp,ip协议,fin, null 等扫描会引起。
closed|filtered:(关闭或者被过滤的):无法确定端口是关闭的还是被过滤的
nmap有windows和linux
nmap是一款网络扫描和主机检测的非常有用的工具。nmap是不局限于仅仅收集信息和枚举,同时可以用来作为一个漏洞探测器或安全扫描器。它可以适用于winodws,linux,mac等操作系统
从下面官网可以下载exe程序包和zip包
https://nmap.org/download.html#windows
nmap常用参数
nmap扫描速度要比nc快
面是一些基本的命令和它们的用法的例子:扫描单一的一个主机,命令如下:
前期准备
准备两台机器
主机a:ip地址 10.0.1.161
主机b:ip地址 10.0.1.162
b机器安装nmap的包(这个工具比较强大,习惯上每台机器都安装)
yum install nmap -y
端口扫描部分
前期准备
b机器使用nmap去扫描a机器,扫描之前,a机器先查看自己上面有哪些端口在被占用
a机器上查看本地ipv4的监听端口
netstat参数解释:
-l (listen) 仅列出 listen (监听) 的服务
-t (tcp) 仅显示tcp相关内容
-n (numeric) 直接显示ip地址以及端口,不解析为服务名或者主机名
-p (pid) 显示出socket所属的进程pid 以及进程名字
--inet 显示ipv4相关协议的监听
查看ipv4端口上的tcp的监听
netstat -lntp --inet
[root@a ~]# netstat -lntp --inet
active internet connections (only servers)
proto recv-q send-q local address foreign address state pid/program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* listen 2157/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* listen 1930/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* listen 2365/master
tcp 0 0 0.0.0.0:13306 0.0.0.0:* listen 21699/mysqld
tcp 0 0 0.0.0.0:873 0.0.0.0:* listen 2640/rsync
tcp 0 0 0.0.0.0:111 0.0.0.0:* listen 21505/rpcbind
[root@a ~]#
过滤掉监控在127.0.0.1的端口
[root@a ~]# netstat -lntp --inet | grep -v 127.0.0.1
active internet connections (only servers)
proto recv-q send-q local address foreign address state pid/program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* listen 2157/sshd
tcp 0 0 0.0.0.0:13306 0.0.0.0:* listen 21699/mysqld
tcp 0 0 0.0.0.0:873 0.0.0.0:* listen 2640/rsync
tcp 0 0 0.0.0.0:111 0.0.0.0:* listen 21505/rpcbind
[root@a ~]#
扫描tcp端口
b机器上使用nmap扫描a机器所有端口(-p后面也可以跟空格)
下面表示扫描a机器的1到65535所有在监听的tcp端口。
nmap 10.0.1.161 -p1-65535
指定端口范围使用-p参数,如果不指定要扫描的端口,nmap默认扫描从1到1024再加上nmap-services列出的端口
nmap-services是一个包含大约2200个著名的服务的数据库,nmap通过查询该数据库可以报告那些端口可能对应于什么服务器,但不一定正确。
所以正确扫描一个机器开放端口的方法是上面命令。-p1-65535
注意,nmap有自己的库,存放一些已知的服务和对应端口号,假如有的服务不在nmap-services,可能nmap就不会去扫描,这就是明明一些端口已经是处于监听状态,nmap默认没扫描出来的原因,需要加入-p参数让其扫描所有端口。
虽然直接使用nmap 10.0.1.161也可以扫描出开放的端口,但是使用-p1-65535 能显示出最多的端口
区别在于不加-p 时,显示的都是已知协议的端口,对于未知协议的端口没显示
[root@b ~]# nmap 10.0.1.161 -p1-65535
starting nmap 5.51 ( http://nmap.org ) at 2016-12-29 10:11 cst
nmap scan report for 10.0.1.161
host is up (0.00017s latency).
not shown: 65531 closed ports
port state service
22/tcp open ssh
111/tcp open rpcbind
873/tcp open rsync
13306/tcp open unknown
mac address: 00:0c:29:56:de:46 (vmware)
nmap done: 1 ip address (1 host up) scanned in 2.49 seconds
[root@b ~]#
如果不加-p1-65535,对于未知服务的端口(a机器的13306端口)就没法扫描到
[root@b ~]# nmap 10.0.1.161
starting nmap 5.51 ( http://nmap.org ) at 2016-12-29 10:12 cst
nmap scan report for 10.0.1.161
host is up (0.000089s latency).
not shown: 997 closed ports
port state service
22/tcp open ssh
111/tcp open rpcbind
873/tcp open rsync
mac address: 00:0c:29:56:de:46 (vmware)
nmap done: 1 ip address (1 host up) scanned in 0.43 seconds
[root@b ~]#
扫描一个ip的多个端口
连续的端口可以使用横线连起来,端口之间可以使用逗号隔开
a机器上再启动两个tcp的监听,分别占用7777和8888端口,用于测试,加入&符号可以放入后台
[root@a ~]# nc -l 7777&
[1] 21779
[root@a ~]# nc -l 8888&
[2] 21780
[root@a ~]#
[root@b ~]# nmap 10.0.1.161 -p20-200,7777,8888
starting nmap 5.51 ( http://nmap.org ) at 2016-12-29 10:32 cst
nmap scan report for 10.0.1.161
host is up (0.00038s latency).
not shown: 179 closed ports
port state service
22/tcp open ssh
111/tcp open rpcbind
7777/tcp open cbt
8888/tcp open sun-answerbook
mac address: 00:0c:29:56:de:46 (vmware)
nmap done: 1 ip address (1 host up) scanned in 0.17 seconds
[root@b ~]#
扫描udp端口
先查看哪些ipv4的监听,使用grep -v排除回环接口上的监听
netstat -lnup --inet |grep -v 127.0.0.1
[root@a ~]# netstat -lnup --inet |grep -v 127.0.0.1
active internet connections (only servers)
proto recv-q send-q local address foreign address state pid/program name
udp 0 0 0.0.0.0:111 0.0.0.0:* 21505/rpcbind
udp 0 0 0.0.0.0:631 0.0.0.0:* 1930/cupsd
udp 0 0 10.0.1.161:123 0.0.0.0:* 2261/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 2261/ntpd
udp 0 0 0.0.0.0:904 0.0.0.0:* 21505/rpcbind
[root@a ~]#
-su:表示udp scan , udp端口扫描
-pn:不对目标进行ping探测(不判断主机是否在线)(直接扫描端口)
对于udp端口扫描比较慢,扫描完6万多个端口需要20分钟左右
[root@b ~]# nmap -su 10.0.1.161 -pn
starting nmap 5.51 ( http://nmap.org ) at 2016-12-29 10:16 cst
stats: 0:12:54 elapsed; 0 hosts completed (1 up), 1 undergoing udp scan
udp scan timing: about 75.19% done; etc: 10:33 (0:04:16 remaining)
stats: 0:12:55 elapsed; 0 hosts completed (1 up), 1 undergoing udp scan
udp scan timing: about 75.29% done; etc: 10:33 (0:04:15 remaining)
nmap scan report for 10.0.1.161
host is up (0.0011s latency).
not shown: 997 closed ports
port state service
111/udp open rpcbind
123/udp open ntp
631/udp open|filtered ipp
mac address: 00:0c:29:56:de:46 (vmware)
nmap done: 1 ip address (1 host up) scanned in 1081.27 seconds
[root@b ~]#
扫描多个ip用法
中间用空格分开
[root@b ~]# nmap 10.0.1.161 10.0.1.162
starting nmap 5.51 ( http://nmap.org ) at 2016-12-29 10:18 cst
nmap scan report for 10.0.1.161
host is up (0.000060s latency).
not shown: 997 closed ports
port state service
22/tcp open ssh
111/tcp open rpcbind
873/tcp open rsync
mac address: 00:0c:29:56:de:46 (vmware)
nmap scan report for 10.0.1.162
host is up (0.0000070s latency).
not shown: 998 closed ports
port state service
22/tcp open ssh
111/tcp open rpcbind
nmap done: 2 ip addresses (2 hosts up) scanned in 0.26 seconds
[root@b ~]#
也可以采用下面方式逗号隔开
nmap 10.0.1.161,162
[root@b ~]# nmap 10.0.1.161,162
starting nmap 5.51 ( http://nmap.org ) at 2016-12-29 10:19 cst
nmap scan report for 10.0.1.161
host is up (0.00025s latency).
not shown: 997 closed ports
port state service
22/tcp open ssh
111/tcp open rpcbind
873/tcp open rsync
mac address: 00:0c:29:56:de:46 (vmware)
nmap scan report for 10.0.1.162
host is up (0.0000080s latency).
not shown: 998 closed ports
port state service
22/tcp open ssh
111/tcp open rpcbind
nmap done: 2 ip addresses (2 hosts up) scanned in 0.81 seconds
[root@b ~]#
扫描连续的ip地址
[root@b ~]# nmap 10.0.1.161-162
starting nmap 5.51 ( http://nmap.org ) at 2016-12-29 10:20 cst
nmap scan report for 10.0.1.161
host is up (0.00011s latency).
not shown: 997 closed ports
port state service
22/tcp open ssh
111/tcp open rpcbind
873/tcp open rsync
mac address: 00:0c:29:56:de:46 (vmware)
nmap scan report for 10.0.1.162
host is up (0.0000030s latency).
not shown: 998 closed ports
port state service
22/tcp open ssh
111/tcp open rpcbind
nmap done: 2 ip addresses (2 hosts up) scanned in 0.25 seconds
[root@b ~]#
扫描一个子网网段所有ip
[root@b ~]# nmap 10.0.3.0/24
starting nmap 5.51 ( http://nmap.org ) at 2016-12-29 10:21 cst
nmap scan report for 10.0.3.1
host is up (0.020s latency).
not shown: 997 closed ports
port state service
23/tcp open telnet
6666/tcp open irc
8888/tcp open sun-answerbook
nmap scan report for 10.0.3.2
host is up (0.012s latency).
not shown: 997 closed ports
port state service
21/tcp filtered ftp
22/tcp filtered ssh
23/tcp open telnet
nmap scan report for 10.0.3.3
host is up (0.018s latency).
not shown: 997 closed ports
port state service
21/tcp filtered ftp
22/tcp filtered ssh
23/tcp open telnet
nmap done: 256 ip addresses (3 hosts up) scanned in 14.91 seconds
[root@b ~]#
扫描文件里的ip
如果你有一个ip地址列表,将这个保存为一个txt文件,和namp在同一目录下,扫描这个txt内的所有主机,用法如下
[root@b ~]# cat ip.txt
10.0.1.161
10.0.1.162
[root@b ~]# nmap -il ip.txt
starting nmap 5.51 ( http://nmap.org ) at 2016-12-29 10:23 cst
nmap scan report for 10.0.1.161
host is up (0.00030s latency).
not shown: 997 closed ports
port state service
22/tcp open ssh
111/tcp open rpcbind
873/tcp open rsync
mac address: 00:0c:29:56:de:46 (vmware)
nmap scan report for 10.0.1.162
host is up (0.0000070s latency).
not shown: 998 closed ports
port state service
22/tcp open ssh
111/tcp open rpcbind
nmap done: 2 ip addresses (2 hosts up) scanned in 0.68 seconds
[root@b ~]#
扫描地址段是排除某个ip地址
nmap 10.0.1.161-162 --exclude 10.0.1.162
用法如下
[root@b ~]# nmap 10.0.1.161-162 --exclude 10.0.1.162
starting nmap 5.51 ( http://nmap.org ) at 2016-12-29 10:24 cst
nmap scan report for 10.0.1.161
host is up (0.0022s latency).
not shown: 997 closed ports
port state service
22/tcp open ssh
111/tcp open rpcbind
873/tcp open rsync
mac address: 00:0c:29:56:de:46 (vmware)
nmap done: 1 ip address (1 host up) scanned in 0.53 seconds
[root@b ~]#
扫描时排除多个ip地址
排除连续的,可以使用横线连接起来
nmap 10.0.1.161-163 --exclude 10.0.1.162-163
[root@b ~]# nmap 10.0.1.161-163 --exclude 10.0.1.162-163
starting nmap 5.51 ( http://nmap.org ) at 2016-12-29 10:25 cst
nmap scan report for 10.0.1.161
host is up (0.00023s latency).
not shown: 997 closed ports
port state service
22/tcp open ssh
111/tcp open rpcbind
873/tcp open rsync
mac address: 00:0c:29:56:de:46 (vmware)
nmap done: 1 ip address (1 host up) scanned in 0.56 seconds
[root@b ~]#
排除分散的,使用逗号隔开
nmap 10.0.1.161-163 --exclude 10.0.1.161,10.0.1.163
[root@b ~]# nmap 10.0.1.161-163 --exclude 10.0.1.161,10.0.1.163
starting nmap 5.51 ( http://nmap.org ) at 2016-12-29 10:27 cst
nmap scan report for 10.0.1.162
host is up (0.0000030s latency).
not shown: 998 closed ports
port state service
22/tcp open ssh
111/tcp open rpcbind
nmap done: 1 ip address (1 host up) scanned in 0.12 seconds
[root@b ~]#
扫描多个地址时排除文件里的ip地址
(可以用来排除不连续的ip地址)
把10.0.1.161和10.0.1.163添加到一个文件里,文件名可以随意取
下面扫描10.0.1.161到10.0.1.163 这3个ip地址,排除10.0.1.161和10.0.1.163这两个ip
nmap 10.0.1.161-163 --excludefile ex.txt
[root@b ~]# cat ex.txt
10.0.1.161
10.0.1.163
[root@b ~]# nmap 10.0.1.161-163 --excludefile ex.txt
starting nmap 5.51 ( http://nmap.org ) at 2016-12-29 10:29 cst
nmap scan report for 10.0.1.162
host is up (0.0000050s latency).
not shown: 998 closed ports
port state service
22/tcp open ssh
111/tcp open rpcbind
nmap done: 1 ip address (1 host up) scanned in 0.18 seconds
[root@b ~]#
好了,这篇文章就介绍到这,这里推荐大家下载这个,帮助大家更加深入的了解。
上一篇: 软文推广的实际效果如何监测