如何在 Linux 中使用 Nmap 脚本引擎 (NSE) 脚本
Nmap 是一款流行的、功能强大的跨平台命令行网络安全扫描器和探索工具。它还可以帮助您了解连接网络的系统的概况;您可以使用它来查找活动主机的所有 IP 地址、扫描开放端口和在这些主机上运行的服务等等。
Nmap 的一个有趣功能是 Nmap 脚本引擎 (NSE),它为其带来了更大的灵活性和效率。它使您能够用 Lua 编程语言编写自己的脚本,并可能与其他 Nmap 用户共享这些脚本。
另请阅读:Linux Nmap 命令的 29 个实用示例
NSE 脚本有四种类型,分别是:
- 预规则脚本 – 是在任何 Nmap 扫描操作之前运行的脚本,它们在 Nmap 尚未收集有关目标的任何信息时执行。
- 主机脚本 – 是Nmap执行正常操作后执行的脚本,例如针对目标主机的主机发现、端口扫描、版本检测和操作系统检测。
- 服务脚本 – 是针对目标主机上侦听的特定服务运行的脚本。
- 后规则脚本 – 是在 Nmap 扫描其所有目标主机后运行的脚本。
然后,这些脚本被分组到不同的类别中,包括用于身份验证 (auth)、主机发现 (广播)、暴力攻击以猜测身份验证凭据 (brute<),发现有关网络的更多信息(发现),导致拒绝服务(dos),利用某些漏洞(利用)等等。许多脚本属于默认类别。
注意:在我们继续前进之前,您应该记下以下要点:
- 未经严格审查或仅在您信任作者的情况下,请勿执行第三方脚本。这是因为这些脚本不是在沙箱中运行,因此可能会意外或恶意地损坏您的系统或侵犯您的隐私。
- 其次,其中许多脚本可能作为prerule 或postrule 脚本运行。考虑到这一点,建议使用预规则以保持一致性。
- Nmap 使用 scripts/script.db 数据库来找出可用的默认脚本和类别。
要查看所有可用 NSE 脚本的位置,请在终端上运行locate 实用程序,如下所示:
locate *.nse
/usr/share/nmap/scripts/acarsd-info.nse
/usr/share/nmap/scripts/address-info.nse
/usr/share/nmap/scripts/afp-brute.nse
/usr/share/nmap/scripts/afp-ls.nse
/usr/share/nmap/scripts/afp-path-vuln.nse
/usr/share/nmap/scripts/afp-serverinfo.nse
/usr/share/nmap/scripts/afp-showmount.nse
/usr/share/nmap/scripts/ajp-auth.nse
/usr/share/nmap/scripts/ajp-brute.nse
/usr/share/nmap/scripts/ajp-headers.nse
/usr/share/nmap/scripts/ajp-methods.nse
/usr/share/nmap/scripts/ajp-request.nse
/usr/share/nmap/scripts/allseeingeye-info.nse
/usr/share/nmap/scripts/amqp-info.nse
/usr/share/nmap/scripts/asn-query.nse
...
NSE 脚本使用 --script
标志加载,该标志还允许您通过提供类别、脚本文件名或脚本所在的目录名称来运行自己的脚本。
启用脚本的语法如下:
nmap -sC target #load default scripts
OR
nmap --script filename|category|directory|expression,... target
您可以使用 --script-help
选项查看脚本的描述。此外,您可以通过 --script-args
和 --script-args-file
选项将参数传递给某些脚本,后者用于提供文件名而不是命令行参数。
要使用大多数默认脚本执行扫描,请使用 -sC
标志或使用 --script=default
,如图所示。
nmap -sC scanme.nmap.org
OR
nmap --script=default scanme.nmap.org
OR
nmap --script default scanme.nmap.org
样本输出
Starting Nmap 7.01 ( https://nmap.org ) at 2017-11-15 10:36 IST
Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up (0.0027s latency).
Not shown: 999 filtered ports
PORT STATE SERVICE
80/tcp open http
|_http-title: Go ahead and ScanMe!
Nmap done: 1 IP address (1 host up) scanned in 11.74 seconds
要将脚本用于适当的目的,您首先可以获取其实际用途的简短描述,例如http-headers。
nmap --script-help http-headers scanme.nmap.org
样本输出
Starting Nmap 7.01 ( https://nmap.org ) at 2017-11-15 10:37 IST
http-headers
Categories: discovery safe
https://nmap.org/nsedoc/scripts/http-headers.html
Performs a HEAD request for the root folder ("/") of a web server and displays the HTTP headers returned.
加载 NSE 脚本以执行 Nmap 扫描
您可以选择或加载脚本以通过下面说明的不同方法执行扫描。
使用脚本名称
一旦您知道脚本的作用,您就可以使用它执行扫描。您可以使用一个脚本或输入以逗号分隔的脚本名称列表。下面的命令将使您能够查看目标主机的 Web 服务器上配置的 HTTP 标头。
nmap --script http-headers scanme.nmap.org
Starting Nmap 7.01 ( https://nmap.org ) at 2017-11-15 10:39 IST
Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up (0.27s latency).
Not shown: 996 closed ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
| http-headers:
| Date: Wed, 15 Nov 2017 05:10:04 GMT
| Server: Apache/2.4.7 (Ubuntu)
| Accept-Ranges: bytes
| Vary: Accept-Encoding
| Connection: close
| Content-Type: text/html
|
|_ (Request type: HEAD)
179/tcp filtered bgp
31337/tcp open Elite
Nmap done: 1 IP address (1 host up) scanned in 20.96 seconds
使用类别
您还可以从一个类别或从以逗号分隔的类别列表加载脚本。在此示例中,我们使用默认和广播类别中的所有脚本对主机 192.168.56.1 进行扫描。
nmap --script default,broadcast 192.168.56.1
使用 * 通配符
当您想要选择具有给定名称模式的脚本时,这非常有用。例如,要加载名称以 ssh 开头的所有脚本,请在终端上运行以下命令:
nmap --script "ssh-*" 192.168.56.1
使用布尔表达式
您还可以使用布尔表达式选择脚本,并使用and、or和not运算符构建这些脚本。布尔表达式中的名称可以是类别、来自 script.db 的文件名或全部。
以下命令将从默认或广播类别加载脚本。
nmap --script "default or broadcast" 192.168.56.10
这相当于:
nmap --script default,broadcast 192.168.56.10
要加载所有脚本(忽略 vuln 类别中的脚本),请在终端上运行此命令。
nmap --script "not vuln" 192.168.56.10
下一个命令看起来有点复杂,但很容易理解,它选择默认或广播类别中的脚本,忽略名称以 ssh- 开头的脚本:
nmap --script "(default or broadcast) and not ssh-*" 192.168.56.10
重要的是,可以组合类别、脚本名称、包含自定义脚本的目录或布尔表达式来加载脚本,如下所示:
nmap --script broadcast,vuln,ssh-auth-methods,/path/to/custom/scripts 192.168.56.10
将参数传递给 NSE 脚本
下面的示例展示了如何使用 –script-args 选项将参数传递给脚本:
nmap --script mysql-audit --script-args "mysql-audit.username='root', \
mysql-audit.password='password_here', mysql-audit.filename='nselib/data/mysql-cis.audit'"
要传递端口号,请使用 -p nmap 选项:
nmap -p 3306 --script mysql-audit --script-args "mysql-audit.username='root', \
mysql-audit.password='password_here' , mysql-audit.filename='nselib/data/mysql-cis.audit'"
上述命令根据 CIS MySQL v1.0.2 基准测试的部分内容对 MySQL 数据库服务器安全配置进行审核。您还可以为其他 MySQL 审计创建自己有用的自定义审计文件。
现在就这样。您可以在 Nmap 手册页上找到更多信息或查看 NSE 用法。
要开始编写自己的 NSE 脚本,请查看本指南:https://nmap.org/book/nse-tutorial.html
结论
Nmap 是一个真正强大且有用的工具,每个系统或网络管理员都需要在他/她的安全武器库中 - NSE 只是增加了效率。
在本文中,我们向您介绍了 Nmap 脚本引擎,并了解了如何查找和使用不同类别下的各种可用脚本。如果您有任何疑问,请随时通过下面的评论表给我们回信。