当前位置: 移动技术网 > 科技>软件教程>网络通讯 > linux下网络扫描工具nmap使用教程

linux下网络扫描工具nmap使用教程

2019年07月28日  | 移动技术网科技  | 我要评论
linux下网络扫描工具nmap使用教程

本文原创地址:博客园骏马金龙https://www.cnblogs.com/f-ck-need-u/p/7064323.html

1.1 选项说明

nmap需要自行安装。

shell> yum -y install nmap

使用nmap -h可以查看选项和用法。选项非常多,这是功能强大的工具带来的必然结果,但简单使用并用不到几个选项。

usage: nmap [scan type(s)] [options] {target specification}

target specification:

  can pass hostnames, ip addresses, networks, etc.

  ex: scanme.nmap.org, microsoft.com/24, 192.168.0.1; 10.0.0-255.1-254

  -il <inputfilename>: input from list of hosts/networks

  -ir <num hosts>: choose random targets

  --exclude <host1[,host2][,host3],...>: exclude hosts/networks

  --excludefile <exclude_file>: exclude list from file

host discovery:

  -sl: list scan - simply list targets to scan

  -sn: ping scan - disable port scan

  -pn: treat all hosts as online -- skip host discovery

  -ps/pa/pu/py[portlist]: tcp syn/ack, udp or sctp discovery to given ports

  -pe/pp/pm: icmp echo, timestamp, and netmask request discovery probes

  -po[protocol list]: ip protocol ping

  -pr: arp ping - does not need hw address -> ip translation

  -n/-r: never do dns resolution/always resolve [default: sometimes]

  --dns-servers <serv1[,serv2],...>: specify custom dns servers

  --system-dns: use os's dns resolver

  --traceroute: trace hop path to each host

scan techniques:

  -ss/st/sa/sw/sm: tcp syn/connect()/ack/window/maimon scans

  -su: udp scan

  -sn/sf/sx: tcp null, fin, and xmas scans

  --scanflags <flags>: customize tcp scan flags

  -si <zombie host[:probeport]>: idle scan

  -sy/sz: sctp init/cookie-echo scans

  -so: ip protocol scan

  -b <ftp relay host>: ftp bounce scan

port specification and scan order:

  -p <port ranges>: only scan specified ports

    ex: -p22; -p1-65535; -p u:53,111,137,t:21-25,80,139,8080,s:9

  -f: fast mode - scan fewer ports than the default scan

  -r: scan ports consecutively - don't randomize

  --top-ports <number>: scan <number> most common ports

  --port-ratio <ratio>: scan ports more common than <ratio>

service/version detection:

  -sv: probe open ports to determine service/version info

  -sr: check what service uses opened ports using rpc scan

  --version-intensity <level>: set from 0 (light) to 9 (try all probes)

  --version-light: limit to most likely probes (intensity 2)

  --version-all: try every single probe (intensity 9)

  --version-trace: show detailed version scan activity (for debugging)

script scan:

  -sc: equivalent to --script=default

  --script=<lua scripts>: <lua scripts> is a comma separated list of directories, script-files or script-categories

  --script-args=<n1=v1,[n2=v2,...]>: provide arguments to scripts

  --script-trace: show all data sent and received

  --script-updatedb: update the script database.

os detection:

  -o: enable os detection

  --osscan-limit: limit os detection to promising targets

  --osscan-guess: guess os more aggressively

timing and performance:

  options which take <time> are in seconds, or append 'ms' (milliseconds),

  's' (seconds), 'm' (minutes), or 'h' (hours) to the value (e.g. 30m).

  -t<0-5>: set timing template (higher is faster)

  --min-hostgroup/max-hostgroup <size>: parallel host scan group sizes

  --min-parallelism/max-parallelism <numprobes>: probe parallelization

  --min-rtt-timeout/max-rtt-timeout/initial-rtt-timeout <time>: specifies

      probe round trip time.

  --max-retries <tries>: caps number of port scan probe retransmissions.

  --host-timeout <time>: give up on target after this long

  --scan-delay/--max-scan-delay <time>: adjust delay between probes

  --min-rate <number>: send packets no slower than <number> per second

  --max-rate <number>: send packets no faster than <number> per second

firewall/ids evasion and spoofing:

  -f; --mtu <val>: fragment packets (optionally w/given mtu)

  -d <decoy1,decoy2[,me],...>: cloak a scan with decoys

  -s <ip_address>: spoof source address

  -e <iface>: use specified interface

  -g/--source-port <portnum>: use given port number

  --data-length <num>: append random data to sent packets

  --ip-options <options>: send packets with specified ip options

  --ttl <val>: set ip time-to-live field

  --spoof-mac <mac address/prefix/vendor name>: spoof your mac address

  --badsum: send packets with a bogus tcp/udp/sctp checksum

output:

  -on/-ox/-os/-og <file>: output scan in normal, xml, s|<ript kiddi3,

     and grepable format, respectively, to the given filename.

  -oa <basename>: output in the three major formats at once

  -v: increase verbosity level (use -vv or more for greater effect)

  -d: increase debugging level (use -dd or more for greater effect)

  --reason: display the reason a port is in a particular state

  --open: only show open (or possibly open) ports

  --packet-trace: show all packets sent and received

  --iflist: print host interfaces and routes (for debugging)

  --log-errors: log errors/warnings to the normal-format output file

  --append-output: append to rather than clobber specified output files

  --resume <filename>: resume an aborted scan

  --stylesheet <path/url>: xsl stylesheet to transform xml output to html

  --webxml: reference stylesheet from nmap.org for more portable xml

  --no-stylesheet: prevent associating of xsl stylesheet w/xml output

misc:

  -6: enable ipv6 scanning

  -a: enable os detection, version detection, script scanning, and traceroute

  --datadir <dirname>: specify custom nmap data file location

  --send-eth/--send-ip: send using raw ethernet frames or ip packets

  --privileged: assume that the user is fully privileged

  --unprivileged: assume the user lacks raw socket privileges

  -v: print version number

  -h: print this help summary page.

examples:

  nmap -v -a scanme.nmap.org

  nmap -v -sn 192.168.0.0/16 10.0.0.0/8

  nmap -v -ir 10000 -pn -p 80

常用的就上面标红的几个。

下面是解释:

-il <inputfilename>:从输入文件中读取主机或者ip列表作为探测目标
-sn: ping扫描,但是禁止端口扫描。默认总是会扫描端口。禁用端口扫描可以加速扫描主机
-n/-r: 永远不要/总是进行dns解析,默认情况下有时会解析
-pe/pp/pm:分别是基于echo/timestamp/netmask的icmp探测报文方式。使用echo最快
-ss/st/sa/sw:tcp syn/connect()/ack/window,其中st扫描表示tcp扫描
-su:udp扫描
-so:ip扫描
-p <port ranges>: 指定扫描端口
--min-hostgroup/max-hostgroup <size>: 对目标主机进行分组然后组之间并行扫描
--min-parallelism/max-parallelism <numprobes>: 设置并行扫描的探针数量
-on/-ox/ <file>: 输出扫描结果到普通文件或xml文件中。输入到xml文件中的结果是格式化的结果
-v:显示详细信息,使用-vv或者更多的v显示更详细的信息

1.2 尝试一次扫描

nmap扫描一般会比较慢,特别是扫描非本机的时候。

[root@server2 ~]# nmap 127.0.0.1

starting nmap 6.40 ( http://nmap.org ) at 2017-06-20 13:03 cst
nmap scan report for localhost (127.0.0.1)
host is up (0.0000010s latency).
not shown: 998 closed ports
port state service
22/tcp open ssh
25/tcp open smtp

只扫描出了两个端口,但是不代表真的只开了两个端口,这样不加任何参数的nmap将自动决定扫描1000个高危端口,但哪些是高危端口由nmap决定。从结果中也能看出来,"not shown:998 closed ports"表示998个关闭的端口未显示出来,随后又显示了2个open端口,正好1000个。虽说默认只扫描1000个,但常见的端口都能扫描出来。

从虚拟机扫描win主机看看。可以感受到,扫描速度明显降低了。

[root@server2 ~]# nmap 192.168.0.122
 
starting nmap 6.40 ( http://nmap.org ) at 2017-06-20 13:11 cst
nmap scan report for 192.168.0.122
host is up (1.2s latency).
not shown: 990 closed ports
port     state    service
21/tcp   open     ftp
135/tcp  open     msrpc
139/tcp  open     netbios-ssn
443/tcp  open     https
445/tcp  open     microsoft-ds
514/tcp  filtered shell
902/tcp  open     iss-realsecure
912/tcp  open     apex-mesh
1583/tcp open     simbaexpress
5357/tcp open     wsdapi
 
nmap done: 1 ip address (1 host up) scanned in 8.38 seconds

可以指定"-p [1-65535]"来扫描所有端口,或者使用"-p-"选项也是全面扫描。

[root@xuexi ~]# nmap -p- 127.0.0.1

nmap默认总是会扫描端口,可以使用-sn选项禁止扫描端口,以加速扫描主机是否存活。

1.3 扫描目标说明

nmap支持cidr风格的地址,nmap将会扫描所有和该参考ip地址具有相同cidr位数的所有ip地址或主机。

例如192.168.10.0/24将扫描192.168.10.0和192.168.10.255之间的256台主机,192.168.10.40/24会做同样的事情。假设主机scanme.nmap.org的ip地址是205.217.153.62,scanme.nmap.org/16将扫描205.217.0.0和205.217.255.255之间的65536个ip地址。掩码位所允许的最小值是/1,这将会扫描半个互联网,最大值是/32,这将会扫描该主机或ip地址,因为所有主机位都固定了。

cidr标志位很简洁但有时候不够灵活。例如也许想要扫描192.168.0.0/16,但略过任何以".0"或者".255"结束的ip地址,因为它们通常是网段地址或广播地址。可以用逗号分开的数字或范围列表为ip地址指定它的范围。例如"192.168.0-255.1-254"将略过该范围内以".0"和".255"结束的地址。范围不必限于最后的8位:"0-255.0-255.13.37"将在整个互联网范围内扫描所有以"13.37"结束的地址。

nmap命令行接受多个主机说明,它们不必是相同类型。如:

nmap www.hostname.com 192.168.0.0/8 10.0.0,1,3-7.0-255

虽然目标通常在命令行指定,下列选项也可用来控制目标的选择:

-il <inputfilename> (从列表中输入)
从<inputfilename>中读取目标说明。在命令行输入一堆主机名显得很笨拙,然而经常需要这样。例如dhcp服务器可能导出10000个当前租约列表。列表中的项可以是nmap在命令行上接受的任何格式(ip地址,主机名,cidr,ipv6,或者八位字节范围)。每一项必须以一个或多个空格、制表符或换行符分开。如果希望nmap从标准输入读取列表,则使用"-"作为表示/dev/stdin。

--exclude <host1[,host2][,host3],...> (排除主机/网络)
--excludefile <excludefile> (排除文件中的列表),这和--exclude的功能一样,只是所排除的目标是用<excludefile>提供的。

1.3.1 范围扫描示例
指定一个ip地址然后加一个cidr的掩码位,如192.168.100.22/24,当然写成192.168.100.0/24也是一样的,因为nmap需要的是参考ip。如果扫描的是范围地址,可以192.168.100.1-254这样的书写方式。

[root@xuexi ~]# nmap 192.168.100.1/24
 
starting nmap 6.40 ( http://nmap.org ) at 2017-06-20 13:22 cst
nmap scan report for 192.168.100.1
host is up (0.00053s latency).
not shown: 992 filtered ports
port     state service
21/tcp   open  ftp
135/tcp  open  msrpc
139/tcp  open  netbios-ssn
443/tcp  open  https
445/tcp  open  microsoft-ds
902/tcp  open  iss-realsecure
912/tcp  open  apex-mesh
5357/tcp open  wsdapi
mac address: 00:50:56:c0:00:08 (vmware)
 
nmap scan report for 192.168.100.2
host is up (0.000018s latency).
not shown: 999 closed ports
port   state service
53/tcp open  domain
mac address: 00:50:56:e2:16:04 (vmware)

nmap scan report for 192.168.100.70
host is up (0.00014s latency).
not shown: 999 closed ports
port   state service
22/tcp open  ssh
mac address: 00:0c:29:71:81:64 (vmware)

nmap scan report for 192.168.100.254
host is up (0.000095s latency).
all 1000 scanned ports on 192.168.100.254 are filtered
mac address: 00:50:56:ed:a1:04 (vmware)

nmap scan report for 192.168.100.62
host is up (0.0000030s latency).
not shown: 999 closed ports
port   state service
22/tcp open  ssh

nmap done: 256 ip addresses (5 hosts up) scanned in 7.96 seconds

一般来说,端口全部关闭的很可能不是计算机,而可能是路由器、虚拟网卡等设备。

1.4 端口状态说明

nmap功能越来越多,但它赖以成名的是它的核心功能——端口扫描。

nmap把端口分成六个状态:open(开放的),closed(关闭的),filtered(被过滤的),unfiltered(未被过滤的),open|filtered(开放或者被过滤的),或者closed|filtered(关闭或者被过滤的)。

这些状态并非端口本身的性质,而是描述nmap怎样看待它们。例如,对于同样的目标机器的135/tcp端口,从同网络扫描显示它是开放的,而跨网络做完全相同的扫描则可能显示它是filtered(被过滤的)。

  • 1.open:(开放的)用程序正在该端口接收tcp或者udp报文。它常常是端口扫描的主要目标。
  • 2.closed:(关闭的)关闭的端口对于nmap也是可访问的(它接受nmap的探测报文并作出响应),但没有应用程序在其上监听。
  • 3.filtered:(被过滤的)由于目标上设置了包过滤(如防火墙设备),使得探测报文被阻止到达端口,nmap无法确定该端口是否开放。过滤可能来自专业的防火墙设备,路由器规则或者主机上的软件防火墙。
  • 4.unfiltered:(未被过滤的)未被过滤状态意味着端口可访问,但nmap不能确定它是开放还是关闭。用其它类型的扫描如窗口扫描、syn扫描、fin扫描来扫描这些未被过滤的端口可以帮助确定端口是否开放。
  • 5.open|filtered:(开放或被过滤的):当无法确定端口是开放还是被过滤的,nmap就把该端口划分成这种状态。开放的端口不响应就是一个例子。没有响应也可能意味着目标主机上报文过滤器丢弃了探测报文或者它引发的任何响应。因此nmap无法确定该端口是开放的还是被过滤的。
  • 6.closed|filtered:(关闭或被过滤的)该状态用于nmap不能确定端口是关闭的还是被过滤的。它只可能出现在ipid idle扫描中。

1.5 时间参数优化

改善扫描时间的技术有:忽略非关键的检测、升级最新版本的nmap(文档中说nmap版本越高性能越好)等。此外,优化时间参数也会带来实质性的优化,这些参数如下:

timing and performance:

 -t<0-5>: set timing template (higher is faster)

  --min-hostgroup/max-hostgroup <size>: parallel host scan group sizes
  --min-parallelism/max-parallelism <numprobes>: probe parallelization

  --min-rtt-timeout/max-rtt-timeout/initial-rtt-timeout <time>: specifies probe round trip time.
  --max-retries <tries>: caps number of port scan probe retransmissions.
  --host-timeout <time>: give up on target after this long
  --scan-delay/--max-scan-delay <time>: adjust delay between probes
  --min-rate <number>: send packets no slower than <number> per second
  --max-rate <number>: send packets no faster than <number> per second

其中最主要的是前3种:

1.-t<0-5>:这表示直接使用namp提供的扫描模板,不同的模板适用于不同的环境下,默认的模板为"-t 3",具体的看man文档,其实用的很少。

2.--min-hostgroup <milliseconds>; --max-hostgroup <milliseconds> (调整并行扫描组的大小)

nmap具有并行扫描多主机端口的能力,实现方法是将所有给定的目标ip按空间分成组,然后一次扫描一个组。通常组分的越大效率越高,但分组的缺点是只有当整个组扫描结束后才会返回该组中主机扫描结果。例如,组的大小定义为50,则只有前50个主机扫描结束后才能得到这50个ip内的结果。

默认方式下,nmap采取折衷的方法。开始扫描时的组较小,默认值为5,这样便于尽快产生结果,随后增长组的大小,默认最大为1024。但最小和最大确切的值则依赖于所给定的选项。

--max-hostgroup选项用于说明使用最大的组,nmap不会超出这个大小。--min-hostgroup选项说明最小的组,nmap会保持组大于这个值。如果在指定的接口上没有足够的目标主机来满足所指定的最小值,nmap可能会采用比所指定的值小的组。

这些选项的主要用途是说明一个最小组的大小,使得整个扫描更加快速。通常选择256来扫描c类网段,对于端口数较多的扫描,超出该值没有意义,因为它只是分组了,但是cpu资源是有限的。对于端口数较少的扫描,2048或更大的组大小是有帮助的。

3.--min-parallelism <milliseconds>; --max-parallelism <milliseconds> (调整探测报文的并行度,即探针数)

这些选项用于控制主机组的探测报文数量,可用于端口扫描和主机发现。默认状态下,nmap基于网络性能计算一个理想的并行度,这个值经常改变。如果报文被丢弃,nmap降低速度,探测报文数量减少。随着网络性能的改善,理想的探测报文数量会缓慢增加。默认状态下,当网络不可靠时,理想的并行度值可能为1,在好的条件下,可能会增长至几百。

最常见的应用是--min-parallelism值大于1,以加快性能不佳的主机或网络的扫描。这个选项具有风险,如果过高则影响准确度,同时也会降低nmap基于网络条件动态控制并行度的能力。

一般说来,这个值要设置的和--min-hostgroup的值相等或大于它性能才会提升。

1.6 扫描操作系统类型

扫描操作系统。操作系统的扫描有可能会出现误报

c:\windows\system32>nmap -o 127.0.0.1
 
starting nmap 7.40 ( https://nmap.org ) at 2017-03-09 13:18 cst
nmap scan report for lmlicenses.wip4.adobe.com (127.0.0.1)
host is up (0.000046s latency).
not shown: 990 closed ports
port      state service
21/tcp    open  ftp
135/tcp   open  msrpc
443/tcp   open  https
445/tcp   open  microsoft-ds
902/tcp   open  iss-realsecure
912/tcp   open  apex-mesh
5357/tcp  open  wsdapi
5678/tcp  open  rrac
10000/tcp open  snet-sensor-mgmt
65000/tcp open  unknown
device type: general purpose
running: microsoft windows 10
os cpe: cpe:/o:microsoft:windows_10
os details: microsoft windows 10 1511
network distance: 0 hops
 
os detection performed. please report any incorrect results at https://nmap.org/submit/ .
nmap done: 1 ip address (1 host up) scanned in 2.33 seconds

1.7 快速扫描存活的主机

要快速扫描存活的主机,需要使用的几个重要选项是:

-n:永远不要dns解析。这个不管是给定地址扫描还是给定网址扫描,加上它速度都会极速提升

-sn:禁止端口扫描

-pe:只根据echo回显判断主机在线,这种类型的选项使用越多,速度越慢,如-pm -pp选项都是类似的,但他们速度要慢的多的多,pe有个缺点,不能穿透防火墙

--min-hostgroup n:当ip太多时,nmap需要分组,然后并扫描,使用该选项可以指定多少个ip一组

--min-parallelism n:这个参数非常关键,为了充分利用系统和网络资源,设置好合理的探针数。一般来说,设置的越大速度越快,且和min-hostgroup的值相等或大于它性能才会提升

示例一:扫描192.168.100.0/24网段存活的机器

[root@server2 ~]# nmap -sn -n -pe --min-hostgroup 1024 --min-parallelism 1024 192.168.100.1/24

warning: you specified a highly aggressive --min-hostgroup.
warning: your --min-parallelism option is pretty high!  this can hurt reliability.

starting nmap 6.40 ( http://nmap.org ) at 2017-06-20 14:30 cst
nmap scan report for 192.168.100.1
host is up (0.00036s latency).
mac address: 00:50:56:c0:00:08 (vmware)
nmap scan report for 192.168.100.2
host is up (0.000051s latency).
mac address: 00:50:56:e2:16:04 (vmware)
nmap scan report for 192.168.100.70
host is up (0.000060s latency).
mac address: 00:0c:29:71:81:64 (vmware)
nmap scan report for 192.168.100.254
host is up (0.000069s latency).
mac address: 00:50:56:ed:a1:04 (vmware)
nmap scan report for 192.168.100.62
host is up.
nmap done: 256 ip addresses (5 hosts up) scanned in 0.26 seconds

255个局域网地址只用了半秒钟。可谓是极速。

再测试扫描下以www.baidu.com作为参考地址的地址空间。

[root@server2 ~]# nmap -sn -pe -n --min-hostgroup 1024 --min-parallelism 1024 -ox nmap_output.xml www.baidu.com/16

…….省略部分结果

nmap scan report for 163.177.81.145
host is up (0.072s latency).
nmap done: 65536 ip addresses (144 hosts up) scanned in 19.15 seconds

可以看到,65535个地址只需19秒就扫描完成了。速度是相当的快。

1.8 快速扫描端口

既然是扫描端口,就不能使用-sn选项,也不能使用-pe,否则不会返回端口状态,只会返回哪些主机。

[root@server2 ~]# nmap -n -p 20-2000 --min-hostgroup 1024 --min-parallelism 1024 192.168.100.70/24

warning: you specified a highly aggressive --min-hostgroup.
warning: your --min-parallelism option is pretty high!  this can hurt reliability.

starting nmap 6.40 ( http://nmap.org ) at 2017-06-20 14:52 cst
nmap scan report for 192.168.100.1
host is up (0.00084s latency).
not shown: 1980 filtered ports
port   state service
21/tcp open  ftp
mac address: 00:50:56:c0:00:08 (vmware)

nmap scan report for 192.168.100.2
host is up (0.000018s latency).
not shown: 1980 closed ports
port   state service
53/tcp open  domain
mac address: 00:50:56:e2:16:04 (vmware)
 
nmap scan report for 192.168.100.70
host is up (0.000041s latency).
not shown: 1980 closed ports
port   state service
22/tcp open  ssh
mac address: 00:0c:29:71:81:64 (vmware)

nmap scan report for 192.168.100.254
host is up (0.000035s latency).
all 1981 scanned ports on 192.168.100.254 are filtered
mac address: 00:50:56:ed:a1:04 (vmware)

nmap scan report for 192.168.100.62
host is up (0.0000020s latency).
not shown: 1980 closed ports
port   state service
22/tcp open  ssh

nmap done: 256 ip addresses (5 hosts up) scanned in 2.38 seconds

姊妹篇:抓包工具tcpdump用法说明

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网