NMAP:网络探索和安全审计手册 Chapter3 侦察任务

本章包括以下内容:

  • 执行 IP 地址地理定位

  • 从 WHOIS 记录中获取信息

  • 获取 traceroute 地理定位信息

  • 查询 Shodan 以获取目标信息

  • 检查主机是否被 Google 安全浏览标记为恶意活动

  • 从网络服务器收集有效的电子邮件帐户和 IP 地址

  • 发现指向同一 IP 地址的主机名

  • 通过暴力破解 DNS 记录发现主机名

  • 从谷歌人员 API 获取个人资料信息

  • 将服务与公共漏洞公告相匹配

引言

渗透测试中最重要的过程是信息收集阶段。在这一阶段,我们调查目标,目的是了解有关目标的一切信息。我们应尝试收集信息,如用户名、可能的密码、其他主机和服务(包括版本标语)以及许多其他有趣的数据。我们发现的信息在渗透测试的后续阶段可能非常宝贵。

有几种工具依赖于许多不同的外部数据源和技术,可以帮助我们成功完成这一阶段。这一阶段的有效性取决于我们是否能利用一切可用资源。如果胆敢忽视或忽略其中的任何一项,就有可能错失彻底入侵目标所需的信息。

Nmap 以其信息收集能力(如操作系统指纹、端口枚举和服务发现)而闻名,但由于 Nmap 脚本引擎的出现。现在,Nmap 可以执行多种新的信息收集任务,如获取额外的 IP 地址信息、检查主机是否存在恶意活动、暴力破解 DNS 记录、收集有效的电子邮件帐户等。

在本章中,我将介绍如何结合 Nmap 选项和 NSE 脚本来查询 WHOIS 服务器、获取远程目标的地理位置信息,以及收集渗透测试期间有用的各种信息,例如发现新目标,甚至匹配服务与公共安全漏洞。

穿上长袍,戴上巫师帽,让我们来侦察一些目标吧。

执行 IP 地址地理定位

识别 IP 地址的位置可以帮助系统管理员确定网络连接的来源。Nmap 随附的几个 NSE 脚本可以帮助我们执行远程 IP 地址的地理定位: ip-geolocation-maxmind , ip-geolocation-ipinfodb , 和 ip-geolocation-geoplugin 。

本食谱将向您演示如何设置和使用 Nmap NSE 附带的地理位置脚本。

准备就绪

ip-geolocation-maxmind 脚本依赖于 Nmap 默认不包含的数据库。从 http://dev.maxmind.com/geoip/legacy/geolite/ 下载二进制格式的 Maxmind GeoLite City 数据库,并将其放入安装目录内的本地 Nmap 数据文件夹 (/nselib/data/)。

ip-geolocation-ipinfodb 脚本需要一个 API 密钥来查询外部服务。该服务是免费的,您只需在 http://ipinfodb.com/register.php 注册即可获得一个。该服务不限制查询次数,但只处理来自一个 IP 地址的连接,您需要在注册过程中注册该 IP 地址。

如何操作...

  1. 打开终端,输入以下命令:

    $nmap -sn --script ip-geolocation-* <target> 
    
  2. 例如,让我们定位主机 scanme.nmap.org:

    $nmap -sn --script ip-geolocation-* scanme.nmap.org 
    
  3. 数据库中的地理位置信息将显示在每个目标上:

     Nmap scan report for scanme.nmap.org (45.33.32.156) 
      Host is up (0.059s latency). 
     Other addresses for scanme.nmap.org (not scanned):  
     2600:3c01::f03c:91ff:fe18:bb2f 
     Host script results: 
     | ip-geolocation-geoplugin: 
     | 45.33.32.156 (scanme.nmap.org) 
     | coordinates (lat,lon): 39.4899,-74.4773 
     |_ state: New Jersey, United States 
     | ip-geolocation-ipinfodb: 
     | 45.33.32.156 (scanme.nmap.org) 
     | coordinates (lat,lon): 39.4201,-74.4998 
     |_ city: Pomona, New Jersey, United States 
     | ip-geolocation-maxmind: 
     | 45.33.32.156 (scanme.nmap.org) 
     | coordinates (lat,lon): 37.567,-121.9829 
     |_ city: Fremont, San Francisco, CA, United States 
     Nmap done: 1 IP address (1 host up) scanned in 1.10 seconds 
    

How it works...

参数 --script ip-geolocation-* 会告诉 Nmap 启动所有以 ip-geolocation- 开头的脚本。在撰写本文时,有三个地理定位脚本可用:

  • ip-geolocation-geoplugin

  • ip-geolocation-maxmind

  • ip-geolocation-ipinfodb

有时,这些服务提供商不会返回特定 IP 地址的任何信息,因此建议您尝试比较所有脚本的结果。这些脚本返回的信息包括经纬度坐标、国家、州和城市(如有)。

There's more...

NSE 脚本 ip-geolocation-geoplugin 通过查询免费公共服务来工作。请考虑您需要发送的查询次数,并考虑周全;否则,提供商会像过去其他提供商那样限制服务。

一个常见的误解是,IP-to-geolocation 服务能百分之百地提供计算机或设备的位置。定位精度在很大程度上取决于数据库,而且每个服务提供商可能使用不同的数据收集方法。在解释这些 NSE 脚本的结果时,请记住这一点。

提交新的地理位置提供商

如果您知道更好的 IP 转地理位置提供商,请毫不犹豫地向官方邮件列表提交您自己的地理位置脚本。不要忘记记录脚本是否需要外部 API 或数据库。如果您没有为 Nmap 开发的经验,可以将您的想法添加到位于 https://secwiki.org/w/Nmap/Script_Ideas 的 NSE 脚本愿望列表。

从 WHOIS 记录中获取信息

WHOIS 记录通常包含有用的信息,如注册商/组织名称、创建和到期日期、地理位置和其他联系信息,如报告滥用的电子邮件地址。系统管理员使用 WHOIS 已经有很多年了,虽然有很多工具可以查询这些信息,但 Nmap 可以将 IP 范围或目标列表作为输入,在一个地方获取 IP 地址和域名信息。

本食谱将向您展示如何使用 Nmap 检索 IP 地址或域名的 WHOIS 记录。

How to do it...

打开终端,输入以下命令

$nmap -sn --script whois-* <target> 

输出结果类似于下面的内容:

$nmap -sn --script whois-* websec.mx 
 Host script results: 
 | whois-domain: 
 | 
 | Domain name record found at whois.mx 
 | 
 | Domain Name: websec.mx 
 | 
 | Created On: 2010-04-14 
 | Expiration Date: 2018-04-13 
 | Last Updated On: 2014-10-07 
 | Registrar: Akky (Una division de NICMexico) 
 | URL: http://www.akky.mx 
 | Whois TCP URI: whois.akky.mx 
 | Whois Web URL: http://www.akky.mx/jsf/whois/whois.jsf 
 | 
 | Registrant: 
 | Name: Pedro Vapo Rub 
 | City: Cozumel 
 | State: Quintana Roo 
 | Country: Mexico 
 | 
 | 
 | Name Servers: 
 | DNS: dora.ns.cloudflare.com 
 | DNS: rick.ns.cloudflare.com 
 | 
 |_ 
 | whois-ip: Record found at whois.arin.net 
 | netrange: 54.210.0.0 - 54.211.255.255 
 | netname: AMAZO-ZIAD5 
 | orgname: Amazon.com, Inc. 
 | orgid: AMAZO-4 
 | country: US stateprov: WA 
 | orgtechname: Amazon EC2 Network Operations 
 |_orgteche-mail: amzn-noc-contact@amazon.com 

How it works...

-sn --script whois-* 命令告诉 Nmap 跳过端口扫描 (-sn),执行与文件名模式 whois-* 匹配的 NSE 脚本。目前,有两个脚本与此表达式匹配:whois-ip 和 whois-domain。

whois-ip 脚本查询区域互联网注册机构 WHOIS 数据库,whois-domain 脚本查询 http://www.iana.org/whois 以获取转介记录,直到找到相关信息。

There's more...

NSE 脚本 whois-ip 的行为可配置为启用或禁用缓存。选择服务提供商并忽略推荐记录。让我们看看如何使用这些选项。

选择服务提供商

whois-ip 脚本使用 IANA 的分配数据来选择 RIR,并将结果缓存在本地。或者,你也可以覆盖这一行为,在参数 whodb 中选择要使用的服务提供商的顺序:

$nmap --script whois-ip --script-args whois.whodb=arin+ripe+afrinic <target> 


忽视转介记录

whois-ip 脚本将按顺序查询 WHOIS 提供商列表,直到找到该记录或该记录的转介。要忽略转介记录,请使用 nofollow 值:

$nmap --script whois-ip --script-args whois.whodb=nofollow <target> 

禁用缓存

有时,缓存响应会优先于查询 WHOIS 服务,这可能会妨碍发现 IP 地址分配。要禁用缓存,可以将脚本参数 whodb 设置为 nocache:

$nmap -sn --script whois-ip --script-args whois.whodb=nocache <target> 

与所有免费服务一样,我们需要考虑我们需要查询的次数,以避免达到每日上限而被禁用。

获取 traceroute 地理位置信息

Nmap 可以通过追踪起点和终点之间的跳数来映射网络路径。在追踪事件时,地理信息非常有用,我们可以在 NSE 脚本 traceroute-geolocation 的帮助下,将地理信息与 Nmap 的 traceroute 功能结合起来。

在本食谱中,我们将使用 Nmap 获取远程目标的 traceroute 地理位置信息。

How to do it...

要获取远程节点的 traceroute 地理位置信息,请使用以下命令:

# nmap --traceroute --script traceroute-geolocation <target> 

远程节点将在结果中的主机名和 IP 地址旁显示 GPS 坐标和位置:

# nmap --traceroute --script traceroute-geolocation scanme.nmap.org 
 Nmap scan report for scanme.nmap.org (45.33.32.156) 
 Host is up (0.057s latency). 
 Other addresses for scanme.nmap.org (not scanned):   2600:3c01::f03c:91ff:fe18:bb2f 
 Host script results: 
 | traceroute-geolocation: 
 | HOP RTT ADDRESS  GEOLOCATION 
 | 1 3.87 192.168.1.1 - ,- 
 | 2 8.08 192.168.0.1 - ,- 
 | 3 5.84 192.168.222.254 - ,- 
 | 4 24.67 dsl-servicio-l200.uninet.net.mx (200.38.193.226)  19.430,-99.130 Mexico () 
 | 5 67.54 bb-la-grand-8-tge0-13-0-7.uninet.net.mx  (189.246.189.118) 19.430,-99.130 Mexico () 
 | 6 57.56 10ge5-3.core1.lax1.he.net (64.62.205.33)  37.516,-121.896 United States (California) 
 | 7 68.79 100ge14-1.core1.sjc2.he.net (184.105.223.249)   37.516,-121.896 United States (California) 
 | 8 83.34 10ge3-2.core3.fmt2.he.net (184.105.222.13)  37.516,-121.896 United States (California) 
 | 9 ... 
 | 10 68.85 173.230.159.3  39.490,-74.477 United States (New Jersey) 
 |_ 11 59.70 scanme.nmap.org (45.33.32.156)  39.490,-74.477 United States (New Jersey) 
 TRACEROUTE (using port 443/tcp) 
 HOP RTT ADDRESS 
 1 3.87 ms 192.168.1.1 
 2 8.08 ms 192.168.0.1 
 3 5.84 ms 192.168.222.254 
 4 24.67 ms dsl-servicio-l200.uninet.net.mx (200.38.193.226) 
 5 67.54 ms bb-la-grand-8-tge0-13-0-7.uninet.net.mx  (189.246.189.118) 
 6 57.56 ms10ge5-3.core1.lax1.he.net (64.62.205.33) 
 7 68.79 ms100ge14-1.core1.sjc2.he.net (184.105.223.249) 
 8 83.34 ms10ge3-2.core3.fmt2.he.net (184.105.222.13) 
 9 ... 
 10 68.85 ms 173.230.159.3 
 11 59.70 msscanme.nmap.org (45.33.32.156) 
 Nmap done: 1 IP address (1 host up) scanned in 19.51 seconds 

How it works...

NSE 脚本 traceroute-geolocation 显示跟踪路由中每一跳的地理位置。它依赖于 http://www.geoplugin.com/ 提供的服务,不需要 API 密钥,也没有查询限制。该脚本必须与 --traceroute 一起运行,因为 Nmap 实际上负责生成该脚本使用的 traceroute 信息。

There's more...

您可以通过设置脚本参数 traceroute-geolocation.kmlfile,将结果保存为 KML 格式,然后在谷歌地图或地球上绘制:

$nmap --traceroute --script traceroute-geolocation --script-args traceroute-geolocation.kmlfile=<output file> <target> 

1697594453395

查询 Shodan 以获取目标信息

Shodan 是一个互联网连接设备搜索引擎。它是一个有用的信息源,我们可以通过它找到远程目标的端口和横幅信息。它的优点之一是,我们甚至不需要直接向目标发送一个数据包,就能获取丰富的主机信息,包括端口号、协议和服务横幅。

在本教程中,您将学习如何使用 Shodan 通过 Nmap 从远程主机获取端口和版本信息。

Getting ready

NSE 脚本 shodan-api 在使用前需要一个 API 密钥。Shodan 提供免费的开发人员 API,您可以从

https://developer.shodan.io/ 获取。

在继续之前,请确保您手头有 Shodan API 密钥。

How to do it...

要从 Shodan 获取远程目标的主机信息,请使用以下命令:

$nmap -sn -Pn -n --script shodan-api --script-args shodan-api.apikey=<ShodanAPI KEY> <target> 

结果将包含 Shodan 中可用的所有主机信息,包括端口号、协议、生产和版本信息:

$nmap -sn -Pn -n --script shodan-api --script-args shodanapi.apikey=<ShodanAPI KEY> scanme.nmap.org
 Nmap scan report for scanme.nmap.org (45.33.32.156)
 Host is up.
 Other addresses for scanme.nmap.org (not scanned): 
 2600:3c01::f03c:91ff:fe18:bb2f
 Host script results:
 | shodan-api: Report for 45.33.32.156 (scanme.nmap.org)
 | PORT PROTO PRODUCT VERSION
 | 22 tcp
 |_80 tcp Apache httpd 2.4.7
 Post-scan script results:
 |_shodan-api: Shodan done: 1 hosts up.
 Nmap done: 1 IP address (1 host up) scanned in 15.95 seconds

How it works...

在上一条命令中,我们查询 Shodan 以获取与版本检测引擎 (-sV) 返回的信息类似的信息,而无需与目标进行任何联系。ShodanHQ (https://www.shodan.io/) 会定期扫描互联网以收集端口和服务信息,我们可以在扫描远程目标时使用这些有价值的信息。

如果您需要执行大量扫描,我建议您考虑购买适合您需要的许可证:

https://developer.shodan.io/pricing

There's more...

NSE 脚本 shodan-api 支持配置选项,允许我们以其他格式保存结果或设置不同的目标。让我们简要回顾一下可用的脚本参数。

将结果保存为 CSV 文件

您可以通过设置脚本参数 shodan-api.outfile,将结果保存为 CSV 格式:

$nmap -sn -Pn -n --script shodan-api --script-args shodan-api.apikey='<ShodanAPI KEY>',shodan-api.outfile=results.csv <target> 

指定单一目标

使用脚本参数 shodan-api.target,设置要扫描的单个目标。请记住使用 IP 地址作为目标,因为 DNS 解析 (-n) 已被禁用:

$nmap -sn -Pn -n --script shodan-api --script-args shodan-api.apikey='<ShodanAPI KEY>',shodan-api.target=<IP> 

检查主机是否被谷歌安全浏览标记为恶意活动

托管用户的系统管理员通常都在努力监控服务器,防止恶意软件的传播。借助 Google Safe Browsing API,Nmap 可以让我们系统地检查主机是否因传播恶意软件或被用于网络钓鱼攻击而闻名。

本食谱向系统管理员展示了如何检查主机是否被 Google 安全浏览服务标记为用于网络钓鱼攻击或传播恶意软件。

准备工作

http-google-malware 脚本依赖于谷歌的安全浏览服务,需要注册才能获得 API 密钥。请登录 https://developers.google.com/safe-browsing/?csw=1 注册。

怎么做...

打开您喜欢的终端,键入以下内容:

$nmap -p80 --script http-google-malware --script-args http-google-malware.api=<API> <target> 

该脚本将返回一条信息,说明谷歌安全浏览是否发现该服务器传播恶意软件或被用于网络钓鱼攻击。

 Nmap scan report for mertsssooopa.in (203.170.193.102) 
 Host is up (0.60s latency). 
 PORT STATE SERVICE 
 80/tcp open http 
 |_http-google-malware: Host is known for distributing malware. 

How it works...

http-google-malware 脚本会查询谷歌安全浏览服务,以确定主机是否涉嫌恶意。Mozilla Firefox 和谷歌 Chrome 浏览器等网络浏览器都使用这项服务来保护用户,而且名单会经常更新。

There's more...

如果不想每次启动脚本时都使用参数 http-google-malware.api,可以编辑 http-google-malware.nse 文件,将 API 密钥硬编码到脚本中。请查找以下部分,并将你的密钥存储在变量 APIKEY 中:

---######################### 
--ENTER YOUR API KEY HERE # 
---######################### 
local APIKEY = "" 
---######################### 

如需完整文档,请访问 https://nmap.org/nsedoc/scripts/http-google-malware.html。

从网络服务器上收集有效的电子邮件账户和 IP 地址

有效的电子邮件账户在渗透测试中非常有用,因为它们可用于利用网络钓鱼攻击中的信任关系、邮件服务器的密码审计以及包括 Active Directory 服务在内的许多 IT 系统中的用户名。

本教程说明如何使用 Nmap 获取有效的公共电子邮件帐户列表。

How to do it...

打开终端,输入以下命令

# nmap -p <Port> --script http-grep <target> 

Nmap 将抓取网络应用程序,并返回发现的任何有趣信息,如下所示:

 # nmap -p443 --script http-grep nmap.org
 Nmap scan report for nmap.org (45.33.49.119)
 Host is up, received syn-ack (0.072s latency).
 Other addresses for nmap.org (not scanned): 
 2600:3c01::f03c:91ff:fe98:ff4e
 rDNS record for 45.33.49.119: ack.nmap.org
 Scanned at 2016-08-22 10:44:08 CDT for 9s
 PORT STATE SERVICE REASON
 443/tcp open https syn-ack
 | http-grep:
 | (1) https://nmap.org/movies/#elysium:
 | (1) e-mail:
 | + fyodor@nmap.org
 | (2) https://nmap.org/mailman/listinfo/dev:
 | (2) e-mail:
 | + dev@nmap.org
 | + dev-owner@nmap.org
 | (6) https://nmap.org/5/:
 | (6) ip:
 | + 207.68.200.30
 | + 64.13.134.52
 | + 4.68.105.6
 | + 209.245.176.2
 | + 69.63.179.23
 | + 69.63.180.12
 | (16) https://nmap.org/changelog.html:
 | (9) e-mail:
 | + d1n@inbox.com
 | + fyodor@insecure.org
 | + uce@ftc.gov
 | + rhundt@fcc.gov
 | + jquello@fcc.gov
 | + sness@fcc.gov
 | + president@whitehouse.gov
 | + haesslich@loyalty.org
 | + rchong@fcc.gov
 | (7) ip:
 | + 255.255.255.255
 | + 10.99.24.140
 | + 74.125.53.103
 | + 64.147.188.3
 | + 203.65.42.255
 | + 192.31.33.7
 | + 168.0.40.135
 | (1) https://nmap.org/book/man.html:
 | (1) ip:
 |_ + 74.207.244.221

How it works...

http-grep 脚本会抓取网络应用程序,并通过匹配模式从所有页面中提取有趣的信息。该脚本默认搜索电子邮件和 IP 地址,但也有其他内置模式,如社会保险号或信用卡号。搜索结果按 URL 分组。

通过设置脚本参数 http-grep.match,脚本还能匹配自定义模式:

$nmap -p 80 <target> --script http-grep --script-args='match="[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?"' 

There's more...

脚本 http-grep 可以通过设置脚本参数 http-grep.buildins,选择不同的提取模式。内置模式有

  • E-mail

  • Phone

  • Mastercard

  • Discover

  • VISA

  • Amex

  • SSN

  • IP address

向 http-grep.builtins 传递模式表,以选择任何内置模式:

$nmap -p 80 <target> --script http-grep --script-args 'http-grep.builtins ={"mastercard", "discover"} 

只需设置 http-grep.buildins,就能启用所有模式:

$nmap -p80 --script http-grep --script-args http-grep.builtins <target> 

NSE http 库具有高度可配置性。阅读附录 A "HTTP、HTTP 流水线和网络抓取配置选项",了解更多可用高级选项。

发现指向同一 IP 地址的主机名

网络服务器会根据 HTTP 请求中使用的主机名返回不同的内容。通过发现新的主机名,渗透测试人员可以访问使用服务器 IP 无法访问的新目标网络应用程序。

本教程展示了如何枚举指向同一 IP 地址的所有主机名,以发现新目标。

How to do it...

要发现指向同一 IP 地址的主机名,请打开终端并输入以下命令:

$nmap -sn --script hostmap-* <target> 

hostmap-robtex、hostmap-bfk 和 hostmap-ip2hosts 脚本将返回与给定 IP 地址匹配的所有记录:

 Nmap scan report for nmap.org (45.33.49.119)
 Host is up (0.057s latency).
 Other addresses for nmap.org (not scanned): 
 2600:3c01::f03c:91ff:fe98:ff4e
 rDNS record for 45.33.49.119: ack.nmap.org
 Host script results:
 | hostmap-bfk:
 | hosts:
 | sectools.org
 | svn.nmap.org
 | www.secwiki.org
 | mail.seclists.org
 | www.nmap.com
 | seclists.org
 | www.insecure.org
 | nmap.org
 | cgi.insecure.org
 | www.sectools.org
 | insecure.org
 |_ www.nmap.org
 | hostmap-ip2hosts:
 | hosts:
 | nmap.org
 | svn.nmap.org
 | sectools.org
 | insecure.org
 | seclists.org
 |_ secwiki.org
 | hostmap-robtex:
 | hosts:
 | insecure.com
 | nmap.com
 | nmap.org
 | seclists.org
 | sectools.org
 | secwiki.org
 | www.nmap.com
 |_ www.sectools.org
 Nmap done: 1 IP address (1 host up) scanned in 2.84 seconds

How it works...

-sn --script hostmap-* 命令会告诉 Nmap 运行与文件名 hostmap-* 匹配的所有 NSE 脚本,即 hostmap-ip2hosts、hostmap-bfk 和 hostmap-robtex。所有这些脚本都依赖于使用不同技术获取信息的外部服务。http://ip2hosts.com/ 是我自己维护的一项网络服务,它基于必应的搜索 API 和其他数据源。

所有这些服务都是免费的,滥用这些服务很可能会被禁用。

There's more...

hostmap-bfk 和 hostmap-ip2hosts 脚本可以保存扫描到的每个 IP 的主机名列表。使用参数 prefix 在工作目录中创建名为 的文件:

$nmap -sn --script hostmap-ip2hosts --script-args hostmap-ip2hosts.prefix=HOSTSFILE <target> 

通过暴力破解 DNS 记录发现主机名

DNS 记录中包含的主机信息量惊人,通过暴力破解这些记录,我们可以发现更多目标。DNS 条目通常会泄露信息;例如,名为 mail 的 DNS 记录类型 A 显然表明我们正在处理邮件服务器,或者 Cloudflare 的默认 DNS 条目 direct 在大多数情况下都会指向他们试图保护的 IP。

本食谱展示了如何使用 Nmap 强行获取 DNS 记录。

How to do it...

要暴力破解 DNS 条目,请运行以下命令:

$nmap --script dns-brute <target> 
Nmap scan report for websec.mx (54.210.89.118)
 Host is up (0.099s latency).
 rDNS record for 54.210.89.118: ec2-54-210-89-118.compute- 
 1.amazonaws.com
 Host script results:
 | dns-brute:
 | DNS Brute-force hostnames:
 | ipv6.websec.mx - 54.210.89.118
 | beta.websec.mx - 54.210.89.118
 | web.websec.mx - 198.58.106.134
 |_ www.websec.mx - 54.210.89.118

How it works...

参数 --script dns-brute 会启动 NSE 脚本 dns-brute。

dns-brute 由 Cirrus 开发,它试图通过暴力破解目标的 DNS 记录来发现新的主机名。该脚本基本上是遍历主机名列表,检查 DNS 条目是否存在,以找到有效记录。

这种暴力破解攻击很容易被监控 NXDOMAIN 响应的安全机制检测到。

There's more...

NSE 脚本 dns-brute 的行为可以使用一些 NSE 脚本参数进行自定义。现在,让我们回顾一下脚本和 NSE 库的可用配置选项。

自定义字典

dns-brute 使用的默认字典是在本地脚本文件夹 /scripts/dns-brute.nse 中的 NSE 文件中硬编码的。要使用自己的字典文件,请使用参数 dns-brute.hostlist:

$nmap --script dns-brute --script-args dns-brute.hostlist=words.txt <target> 

调整线程数

要设置线程数,请使用参数 dns-brute.threads:

$nmap --script dns-brute --script-args dns-brute.threads=8 <target> 

指定 DNS 服务器

您可以使用 --dns-servers <serv1[,serv2],...> 设置不同的 DNS 服务器:

$nmap --dns-servers 8.8.8.8,8.8.4.4 scanme.nmap.org 

使用 NSE 目标库

NSE 库目标可帮助我们添加扫描过程中发现的新目标。这在 dns-brute 等脚本组合中特别有用。有关详细信息,请参阅附录 C "NSE 调试"。

从 Google People API 获取个人资料信息

在侦察阶段利用公共 API 可以提供大量有关目标的信息。可以通过谷歌的人员 API 获取使用 Gmail 的组织中人员的个人资料信息。这项服务从注册的联系人信息中提供姓名详情和个人照片,任何拥有有效 Gmail 账户的人都可以查询。这使得 API 非常适合于枚举和获取组织中有效用户的信息。

本教程展示了如何使用 Gmail 和 Nmap 从组织中获取个人资料信息。

Getting ready

在此任务中,我们将使用 NSE 脚本,该脚本未包含在 Nmap 官方软件库中。请在继续之前手动安装它。您可以从 https://raw.githubusercontent.com/cldrn/nmap-nse-scripts/master/scripts/google-people-enum.nse 下载 google-people-enum.nse。

How to do it...

运行以下 Nmap 命令检查 users.txt 中包含的电子邮件地址是否存在:

$nmap -sn --script google-people-enum --script-args='username=<username>,password=<password>,userdb=users.txt' <domain> 

对于域中找到的任何有效电子邮件地址,它都会返回包括全名和照片在内的联系信息:

 Host script results: 
 | google-people-enum: 
 | users: 
 | 
 | user1@example.com: 
 | photo:  
 https://lh3.googleusercontent.com/XXXXXXXXXXXXX/photo.jpg 
 | name: User 1 
 | 
 | user2@example.com: 
 |_ photo:  
 https://lh3.googleusercontent.com/XXXXXXXXXXXXXXX/photo.jpg 

How it works...

NSE 脚本 google-people-enum 可查询 Google 的 People API 以获取联系人信息。该脚本由 Websec 的 Aaron Velasco 编写,可用于枚举有效用户并检索全名和照片等信息。脚本可独立于端口扫描执行(-sn 跳过端口扫描),并使用 Nmap 的 upwdb 库管理用户名列表(--script-args userdb=users.txt)。

除了用户列表(如果未设置 userdb,Nmap 将使用默认列表),该脚本还需要一个有效的 Gmail 账户来查询 Google 的 People API。免费账户也可用于此目的。脚本将获取用户列表中的每个条目,并根据 API 检查该用户是否有效,然后尝试检索联系人信息,其中可包括全名和照片。

There's more...

暴力脚本或任何其他依赖于列表的脚本的有效性取决于我们使用的单词列表。每种情况都是独一无二的;您应该定制它们,如果您还没有开始,请开始收集这些单词表。OWASP 的 SecList 项目 (https://github.com/danielmiessler/SecLists) 就是一个不错的开始,该项目包含不同类型的列表,其中包括用户名、密码、URL、攻击有效载荷等。用户名单词表可在 https://github.com/danielmiessler/SecLists/tree/master/Usernames 上找到。

将服务与公共漏洞咨询相匹配

版本发现对渗透测试人员至关重要,因为他们可以使用版本字符串来查找影响扫描服务的公共安全漏洞。Nmap 脚本引擎允许我们将流行的漏洞数据库与扫描获得的服务版本相匹配。

本食谱展示了如何列出可能影响 Nmap 发现的服务的公共安全公告。

Getting ready

为了完成这项任务,我们使用 NSE 脚本 vulscan。这个脚本不包含在 Nmap 官方软件仓库中,因此在继续之前需要手动安装。

要安装它,请从我的 GitHub 仓库下载最新版本的 vulscan:

https://github.com/cldrn/nmap-nse-scripts/blob/master/scripts/vulscan.nse

将脚本 vulscan.nse 复制到本地脚本文件夹($NMAP_INSTALLATION/scripts/)中。然后,在数据目录($NMAP_INSTALLATION/nselib/data)中创建 cve.csv、scipvuldb.csv 和 exploitdb.csv 文件。

现在执行命令下载数据库:

$nmap -p80 -sV --script vulscan --script-args vulscan.updatedb <target> 

如果数据库更新正确,您应该会看到以下信息:

 Nmap scan report for scanme.nmap.org (45.33.32.156) 
 Host is up, received reset ttl 54 (0.077s latency). 
 Other addresses for scanme.nmap.org (not scanned):  
 2600:3c01::f03c:91ff:fe18:bb2f 
 Scanned at 2016-08-28 17:52:43 CDT for 614s 
 PORT STATE SERVICE REASON VERSION 
 80/tcp open http syn-ackttl 54 Apache httpd 2.4.7 ((Ubuntu)) 
 |_http-server-header: Apache/2.4.7 (Ubuntu) 
 |_vulscan: Vulnerability databases updated. 

How to do it...

要将安全公告与从版本检测引擎获取的服务版本相匹配,请使用以下命令:

# nmap -sV --script vulscan <target> 

NSE 脚本 vulscan 将返回与服务版本相匹配的所有安全公告:

# nmap -sV --script vulscan scanme.nmap.org
 Nmap scan report for scanme.nmap.org (45.33.32.156)
 Host is up, received reset ttl 54 (0.068s latency).
 Other addresses for scanme.nmap.org (not scanned):
 2600:3c01::f03c:91ff:fe18:bb2f
 Scanned at 2016-08-28 18:10:13 CDT for 8s
 PORT STATE SERVICE REASON VERSION
 80/tcp open http syn-ackttl 54 Apache httpd 2.4.7 ((Ubuntu))
 |_http-server-header: Apache/2.4.7 (Ubuntu)
 | vulscan: MITRE CVE - http://cve.mitre.org:
 | [CVE-2014-8109] mod_lua.c in the mod_lua module in the Apache HTTP
 Server 2.3.x and 2.4.x through 2.4.10 does not support an httpd
 configuration in which the same Lua authorization provider is used 
 with different arguments within different contexts, which allows 
 remote attackers to bypass intended access restrictions in
 opportunistic circumstances by leveraging multiple Require
 directives, as demonstrated by a configuration that specifies
 authorization for one group to access a certain directory, and
 authorization for a second group to access a second directory.
 | URL:http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2014-8109
 | [CVE-2015-3184] mod_authz_svn in Apache Subversion 1.7.x before
 1.7.21 and 1.8.x before 1.8.14, when using Apache httpd2.4.x, does 
 not properly restrict anonymous access, which allows remote
 anonymous users to read hidden files via the path name.
 | URL:http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2015-3184
 |
 | Exploit-DB - http://www.exploit-db.com:
 | [360] Apache HTTPd - Arbitrary Long HTTP Headers DoS (Perl)
 | URL:http://www.exploit-db.com/exploits/360
 | [371] Apache HTTPd - Arbitrary Long HTTP Headers DoS (C)
 | URL:http://www.exploit-db.com/exploits/371
 | [17696] Apache httpd - Remote Denial of Service (Memory
 Exhaustion)
 | URL:http://www.exploit-db.com/exploits/17696
 | [19536] Apache 1.1 / NCSA httpd 1.5.2 / Netscape Server
 1.12/1.1/2.0 - a nph-test-cgi
 | URL:http://www.exploit-db.com/exploits/19536
 | [20435] Apache 0.8.x/1.0.x& NCSA httpd1.x - test-cgi Directory 
 Listing
 | URL:http://www.exploit-db.com/exploits/20435
 | [20595] NCSA 1.3/1.4.x/1.5 / Apache httpd 0.8.11/0.8.14 - 
 ScriptAlias Source Retrieval
 | URL:http://www.exploit-db.com/exploits/20595
 |
 | scipVulDB - http://www.scip.ch/en/?vuldb:
 | There were no matches. =(
 |_

How it works...

在前面的命令中,标志 -sV 启用服务检测,参数 -- 脚本 vulscan 启动 NSE 脚本 vulscan。NSE 脚本 vulscan 将使用版本信息来匹配来自 CVE、Scipvuldb 和 exploitdb 的任何公开漏洞公告。

脚本 vulscan 会解析每个服务名称和版本,并将其与数据库的本地副本进行比较。这种方法远非完美,因为 vulscan 中的名称匹配仍存在一些错误,当然,它还依赖于 Nmap 的版本检测,但在查找可能影响扫描服务的公共漏洞时,它仍然非常有用。

There's more...

切记不断更新漏洞数据库。您可以手动更新,或使用 Nmap 使用以下命令更新:

$nmap -p80 -sV --script vulscan --script-args vulscan.updatedb <target> 

如果一切按预期进行,您应该会看到以下信息:

 |_vulscan: Vulnerability databases updated.