Nuclei进阶指南:安全研究者的扫描与模板技巧
Nuclei 是一款轻量级、模板驱动的漏洞扫描利器,它能让安全研究者在几秒钟内对大量资产进行高效扫描。本篇文章将带你从 Nuclei 的基础安装、模板使用,到自定义模板编写和高级实战技巧全流程掌握。你将学会如何快速发现 Web 漏洞、构建自动化扫描链路,并结合实战案例提升渗透测试效率。无论你是初学者还是经验丰富的安全工程师,这篇文章都能让你真正玩转 Nuclei,让漏洞无处可藏——安全扫描从此不再
Nuclei完整学习指南 - 从入门到精通
目录导航
- 快速开始
- 核心概念
- 安装配置
- 基础使用
- 模板编写
- 内置功能
- 高级技巧
- 最佳实践
- 故障排除
🎯 快速开始
什么是Nuclei?
Nuclei是由ProjectDiscovery开发的基于YAML模板的现代漏洞扫描器,具有以下特点:
- 高性能:支持大规模并发扫描
- 精准检测:基于社区验证的模板库
- 持续更新:活跃的开源社区维护
- 易于扩展:简单的YAML语法编写自定义检测
🎯 核心概念
架构原理
核心组件
组件 | 功能 | 示例 |
模板 | 定义检测逻辑 |
|
匹配器 | 验证漏洞特征 |
|
提取器 | 获取关键信息 | 提取session ID、版本号 |
变量 | 动态内容替换 |
|
🎯 安装配置
多种安装方式
方式一:Go安装(推荐)
# 安装最新版本
go install -v github.com/projectdiscovery/nuclei/v3/cmd/nuclei@latest
# 验证安装
nuclei -version方式二:预编译二进制
# Linux AMD64
wget https://github.com/projectdiscovery/nuclei/releases/latest/download/nuclei_3.0.0_linux_amd64.zip
unzip nuclei_3.0.0_linux_amd64.zip
sudo mv nuclei /usr/local/bin/
# macOS
brew install nuclei方式三:Docker容器
# 拉取镜像
docker pull projectdiscovery/nuclei:latest
# 运行扫描
docker run --rm projectdiscovery/nuclei -u https://example.com环境配置
初始化配置
# 创建配置目录
mkdir -p ~/.config/nuclei
# 更新模板库(约4000+模板)
nuclei -update-templates
# 生成配置文件
nuclei -config-generate配置文件示例
# ~/.config/nuclei/config.yaml
templates-directory: "~/nuclei-templates"
output: "results"
severity: "critical,high,medium"
concurrent: 25
rate-limit: 150
timeout: 10
retries: 1
verbose: true🎯 基础使用
基本命令结构
nuclei [全局选项] -target [目标] -templates [模板] [输出选项]目标指定方式
# 单个URL
nuclei -u https://example.com
# 多目标文件
nuclei -l targets.txt
# 从stdin读取
echo "https://example.com" | nuclei
# IP段扫描
nuclei -u 192.168.1.1/24
# 端口范围
nuclei -u example.com -p 80,443,8080-8090模板选择策略
# 按严重程度
nuclei -u target -severity critical,high
# 按漏洞类型
nuclei -u target -tags sqli,xss,rce
# 指定模板目录
nuclei -u target -t cves/2023/
# 排除特定模板
nuclei -u target -exclude-templates dos/
# 自定义模板
nuclei -u target -t my-custom-template.yaml实用参数组合
快速扫描
nuclei -u target -tags cve -severity critical -c 50 -rate-limit 100深度扫描
nuclei -u target -tags vuln,exposure -timeout 15 -retries 2 -verbose静默扫描
nuclei -u target -silent -json -o results.json🎯 模板编写
基础模板结构
id: unique-template-id
info:
name: "漏洞名称"
author: "作者"
severity: critical|high|medium|low|info
description: "详细描述"
tags: "tag1,tag2,tag3"
reference:
- "https://reference-url.com"
requests:
- method: GET|POST|PUT|DELETE
path:
- "{{BaseURL}}/endpoint"
matchers:
- type: status|word|regex|dsl
# 匹配条件
extractors:
- type: regex|xpath|json
# 提取规则实际模板示例
简单GET请求检测
id: admin-panel-detection
info:
name: "Admin Panel Detection"
author: "security-team"
severity: info
description: "检测常见的管理员面板"
tags: "panel,admin,exposure"
requests:
- method: GET
path:
- "{{BaseURL}}/admin"
- "{{BaseURL}}/admin.php"
- "{{BaseURL}}/administrator"
- "{{BaseURL}}/wp-admin"
matchers-condition: and
matchers:
- type: status
status:
- 200
- type: word
part: body
words:
- "admin"
- "login"
- "dashboard"
condition: or
case-insensitive: truePOST请求with负载
id: login-bypass-attempt
info:
name: "SQL Injection Login Bypass"
author: "pentest-team"
severity: high
description: "尝试通过SQL注入绕过登录验证"
tags: "sqli,auth-bypass,login"
requests:
- method: POST
path:
- "{{BaseURL}}/login"
- "{{BaseURL}}/admin/login.php"
headers:
Content-Type: "application/x-www-form-urlencoded"
X-Forwarded-For: "127.0.0.1"
body: |
username={{username}}&password={{password}}
payloads:
username:
- "admin' OR '1'='1' --"
- "admin' OR 1=1#"
- "' OR 'x'='x"
password:
- "anything"
attack: pitchfork
matchers:
- type: word
words:
- "welcome"
- "dashboard"
- "logged in successfully"
condition: or匹配器详解
状态码匹配
matchers:
- type: status
status:
- 200
- 302
condition: or字符串匹配
matchers:
- type: word
part: body|header|all
words:
- "error message"
- "exception"
condition: and|or
case-insensitive: true正则表达式匹配
matchers:
- type: regex
part: body
regex:
- "version\s+([0-9]+\.[0-9]+\.[0-9]+)"
- "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b"DSL表达式匹配
matchers:
- type: dsl
dsl:
- "status_code == 200"
- "len(body) > 1000"
- "contains(tolower(body), 'admin')"
condition: and🎯 内置功能
变量系统
内置变量
变量 | 描述 | 示例值 |
| 完整基础URL |
|
| 根URL |
|
| 主机名 |
|
| 主机地址 |
|
| 端口号 |
|
| 路径部分 |
|
| 协议 |
|
自定义变量
variables:
api_key: "sk-1234567890abcdef"
endpoint: "/api/v2/users"
user_id: "12345"
requests:
- method: GET
path:
- "{{BaseURL}}{{endpoint}}/{{user_id}}"
headers:
Authorization: "Bearer {{api_key}}"内置函数库
Nuclei提供了丰富的内置函数,支持编码、字符串处理、随机生成等功能。以下是常用函数的简要介绍:
编码解码函数
{{url_encode("hello world")}} # hello%20world
{{base64("admin:password")}} # YWRtaW46cGFzc3dvcmQ=
{{base64_decode("dGVzdA==")}} # test
{{html_escape("<script>")}} # <script>
{{md5("password")}} # 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
{{sha256("password")}} # ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f字符串处理函数
{{to_lower("HELLO")}} # hello
{{to_upper("hello")}} # HELLO
{{trim(" hello ")}} # hello
{{replace("hello world", "world", "nuclei")}} # hello nuclei
{{substr("testing", 0, 4)}} # test
{{len("hello")}} # 5
{{contains("testing", "test")}} # true随机生成函数
{{rand_char(10)}} # 随机10个字符
{{rand_int(1, 100)}} # 1-100随机整数
{{rand_text_alpha(8)}} # 8位随机字母
{{rand_base64(16)}} # 16字节随机base64
{{rand_uuid()}} # 随机UUID时间函数
{{unix_time()}} # Unix时间戳
{{date_time("%Y-%m-%d")}} # 格式化当前时间
{{year()}} # 当前年份完整函数参考:访问 Nuclei官方文档 - Helper Functions 查看所有可用函数的详细说明
条件逻辑和循环
条件判断
# 简单条件
{{if eq .status_code 200}}
请求成功
{{else}}
请求失败
{{end}}
# 复杂条件
{{if and (eq .status_code 200) (contains .body "success")}}
成功且包含success
{{else if eq .status_code 404}}
页面不存在
{{else}}
其他情况
{{end}}
# 比较操作符
{{if gt .status_code 400}}错误状态{{end}} # 大于
{{if lt .content_length 1000}}内容较少{{end}} # 小于
{{if ne .method "GET"}}非GET请求{{end}} # 不等于循环结构
# 遍历数组
{{range .headers}}
{{.name}}: {{.value}}
{{end}}
# 带索引的遍历
{{range $i, $header := .headers}}
[{{$i}}] {{$header.name}}: {{$header.value}}
{{end}}🎯 高级技巧
多步骤攻击链
模拟真实的渗透测试流程,通过多个请求步骤完成复杂的漏洞检测:
id: multi-step-auth-bypass
info:
name: "Multi-Step Authentication Bypass"
author: "advanced-security"
severity: high
tags: "auth-bypass,multi-step"
requests:
# 步骤1:获取登录页面和CSRF Token
- method: GET
path:
- "{{BaseURL}}/login"
extractors:
- type: regex
name: csrf_token
part: body
internal: true
group: 1
regex:
- 'name="csrf_token" value="([a-zA-Z0-9]+)"'
- type: regex
name: session_id
part: header
internal: true
group: 1
regex:
- 'PHPSESSID=([A-Za-z0-9]+)'
# 步骤2:尝试登录并获取用户权限
- method: POST
path:
- "{{BaseURL}}/login"
headers:
Content-Type: "application/x-www-form-urlencoded"
Cookie: "PHPSESSID={{session_id}}"
body: |
username=admin&password=admin123&csrf_token={{csrf_token}}
extractors:
- type: regex
name: auth_token
part: body
internal: true
group: 1
regex:
- '"auth_token":"([a-zA-Z0-9]+)"'
matchers:
- type: word
words:
- "login successful"
- "dashboard"
# 步骤3:访问管理员功能
- method: GET
path:
- "{{BaseURL}}/admin/users"
headers:
Authorization: "Bearer {{auth_token}}"
Cookie: "PHPSESSID={{session_id}}"
matchers:
- type: word
words:
- "user management"
- "admin panel"
condition: or负载攻击模式
Nuclei支持四种攻击模式来处理多个负载:
Batteringram模式
使用同一个负载填充所有位置:
payloads:
payload: ["admin", "root", "test"]
attack: batteringram # admin/admin, root/root, test/test
body: "username={{payload}}&password={{payload}}"Pitchfork模式
按位置顺序配对使用负载:
payloads:
usernames: ["admin", "root", "guest"]
passwords: ["admin123", "rootpass", "guest123"]
attack: pitchfork # admin/admin123, root/rootpass, guest/guest123
body: "username={{usernames}}&password={{passwords}}"Clusterbomb模式
所有负载的笛卡尔积组合:
payloads:
usernames: ["admin", "root"]
passwords: ["123", "pass", "admin"]
attack: clusterbomb # 2×3=6种组合
body: "username={{usernames}}&password={{passwords}}"外部交互检测(OAST)
检测盲注、SSRF等需要外部交互确认的漏洞:
id: ssrf-detection-oast
info:
name: "SSRF Detection with OAST"
author: "security-researcher"
severity: high
tags: "ssrf,oast,rce"
requests:
- method: GET
path:
- "{{BaseURL}}/fetch?url=http://{{interactsh-url}}"
- "{{BaseURL}}/proxy?target=http://{{interactsh-url}}"
- "{{BaseURL}}/redirect?to=http://{{interactsh-url}}"
matchers:
- type: word
part: interactsh_protocol
words:
- "http"
- "dns"
- method: POST
path:
- "{{BaseURL}}/import"
body: |
{"url": "http://{{interactsh-url}}/callback"}
headers:
Content-Type: "application/json"
matchers:
- type: word
part: interactsh_protocol
words:
- "http"原始请求模板
对于需要精确控制HTTP请求格式的场景:
id: http-request-smuggling
info:
name: "HTTP Request Smuggling Detection"
author: "protocol-security"
severity: high
tags: "smuggling,http"
requests:
- raw:
- |
POST / HTTP/1.1
Host: {{Hostname}}
Content-Length: 44
Transfer-Encoding: chunked
0
POST /admin HTTP/1.1
Host: {{Hostname}}
Content-Length: 15
x=1&y=2&z=3
matchers:
- type: dsl
dsl:
- 'contains(body, "admin") && status_code == 200'条件执行控制
根据前置条件控制后续请求的执行:
requests:
- method: GET
path:
- "{{BaseURL}}"
matchers:
- type: word
words:
- "WordPress"
internal: true # 内部匹配器,不输出结果
- method: GET
path:
- "{{BaseURL}}/wp-content/plugins/"
req-condition: true # 仅当前一个请求匹配成功时执行
matchers:
- type: word
words:
- "Index of /wp-content/plugins"🎯 最佳实践
1. 模板编写规范
命名约定
# ✅ 推荐的命名格式
id: cve-2023-12345-apache-rce
id: wordpress-plugin-xss-authenticated
id: spring-boot-actuator-exposure
id: nginx-alias-traversal
# ❌ 避免的命名格式
id: test1
id: my-template
id: vuln-check
id: scan完整的信息字段
info:
name: "Apache HTTP Server 2.4.49 Path Traversal"
author: "nuclei-community"
severity: high
description: "Apache HTTP Server 2.4.49存在路径遍历漏洞,允许攻击者读取系统文件"
reference:
- "https://httpd.apache.org/security/vulnerabilities_24.html"
- "https://nvd.nist.gov/vuln/detail/CVE-2021-41773"
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-2021-41773"
cwe-id: "CWE-22"
epss-score: 0.97473
epss-percentile: 0.99991
tags: "cve,cve2021,apache,lfi,path-traversal"
metadata:
verified: true
max-request: 2
vendor: "apache"
product: "http_server"
shodan-query: "apache"2. 性能优化策略
智能请求控制
# 使用条件执行减少不必要的请求
requests:
- method: GET
path:
- "{{BaseURL}}"
matchers:
- type: word
words:
- "Apache"
internal: true
- method: GET
path:
- "{{BaseURL}}/cgi-bin/.%2e/.%2e/.%2e/.%2e/etc/passwd"
req-condition: true # 仅当检测到Apache时执行
stop-at-first-match: true # 找到第一个匹配就停止
matchers:
- type: regex
regex:
- "root:.*:0:0:"优化匹配器性能
matchers-condition: and
matchers:
# 先检查快速匹配条件
- type: status
status:
- 200
# 再检查内容匹配
- type: word
part: body
words:
- "specific-error-message"
case-insensitive: true
# 最后检查复杂正则
- type: regex
regex:
- "version\\s+([0-9]+\\.[0-9]+\\.[0-9]+)"批量扫描优化
# 合理设置并发参数
nuclei -l targets.txt -c 25 -rate-limit 150 -timeout 10 -retries 1
# 按严重程度分层扫描
nuclei -l targets.txt -severity critical -silent -o critical.json
nuclei -l targets.txt -severity high -silent -o high.json
nuclei -l targets.txt -severity medium,low -rate-limit 50 -o others.json3. 安全和道德考虑
负责任的漏洞检测
info:
name: "CVE-2023-XXXX Information Disclosure"
description: "检测信息泄露漏洞,不包含实际利用代码"
# 避免包含完整的exploit代码
requests:
- method: GET
path:
- "{{BaseURL}}/debug/info"
matchers:
- type: word
words:
- "database_password"
- "api_secret"
condition: or
# 不提取实际的敏感信息,只确认漏洞存在
extractors:
- type: regex
part: body
regex:
- "(Information disclosed successfully)"避免破坏性测试
# ❌ 避免破坏性操作
# body: "'; DROP TABLE users; --"
# ✅ 使用安全的检测方法
body: "'; SELECT 'nuclei-test' AS result; --"
matchers:
- type: word
words:
- "nuclei-test"4. 错误处理和调试
调试模式使用
# 启用详细调试信息
nuclei -u target -t template.yaml -debug -verbose
# 查看HTTP请求响应详情
nuclei -u target -t template.yaml -debug-req -debug-resp
# 验证模板语法
nuclei -validate -t template.yaml
# 模拟运行(不发送实际请求)
nuclei -u target -t template.yaml -dry-run错误处理最佳实践
requests:
- method: GET
path:
- "{{BaseURL}}/api/endpoint"
# 设置合理的超时时间
timeout: 15s
# 处理各种状态码
matchers:
- type: status
status:
- 200
condition: and
- type: dsl
dsl:
- "len(body) > 0" # 确保响应有内容
- "!contains(tolower(body), 'error')" # 排除错误响应5. 模板组织和管理
目录结构建议
custom-templates/
├── cves/
│ ├── 2023/
│ │ ├── CVE-2023-1234.yaml
│ │ └── CVE-2023-5678.yaml
│ ├── 2024/
│ └── README.md
├── web-applications/
│ ├── cms/
│ │ ├── wordpress/
│ │ ├── drupal/
│ │ └── joomla/
│ ├── frameworks/
│ └── plugins/
├── network-services/
│ ├── databases/
│ ├── web-servers/
│ └── ssh/
├── cloud-services/
│ ├── aws/
│ ├── azure/
│ └── gcp/
└── custom-checks/
├── organization-specific/
└── industry-specific/Git工作流程
# 初始化模板仓库
git init nuclei-custom-templates
cd nuclei-custom-templates
# 设置模板结构
mkdir -p {cves/{2023,2024},web-applications,network-services}
# 添加模板文件
git add .
git commit -m "Initial template structure"
# 创建开发分支
git checkout -b develop
# 开发新模板
git add new-template.yaml
git commit -m "Add CVE-2024-1234 detection template"
# 合并到主分支
git checkout main
git merge develop6. 集成和自动化
CI/CD集成示例(GitHub Actions)
name: Security Scan with Nuclei
on:
schedule:
- cron: '0 2 * * *' # 每天凌晨2点执行
push:
branches: [ main ]
jobs:
nuclei-scan:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Nuclei
run: |
go install -v github.com/projectdiscovery/nuclei/v3/cmd/nuclei@latest
nuclei -update-templates
- name: Run Nuclei Scan
run: |
nuclei -l targets.txt \
-severity critical,high \
-json \
-o nuclei-results.json \
-stats \
-silent
- name: Upload Results
uses: actions/upload-artifact@v3
with:
name: nuclei-results
path: nuclei-results.json
- name: Notify on Slack
if: failure()
uses: 8398a7/action-slack@v3
with:
status: failure
text: "Nuclei scan found critical vulnerabilities!"Docker容器化扫描
FROM projectdiscovery/nuclei:latest
# 添加自定义模板
COPY custom-templates/ /root/nuclei-templates/custom/
# 设置默认配置
COPY nuclei-config.yaml /root/.config/nuclei/config.yaml
# 创建结果目录
VOLUME ["/results"]
# 默认扫描命令
ENTRYPOINT ["nuclei"]
CMD ["-config", "/root/.config/nuclei/config.yaml"]
# 构建镜像
docker build -t custom-nuclei .
# 运行扫描
docker run --rm \
-v $(pwd)/results:/results \
custom-nuclei \
-l /results/targets.txt \
-o /results/scan-results.json \
-json -silent🎯 故障排除
常见问题诊断
1. 模板语法错误
# 问题:模板无法加载
# 解决:验证YAML语法
nuclei -validate -t problematic-template.yaml
# 常见语法错误
❌ 缩进不正确
❌ 引号不匹配
❌ 字段名拼写错误
❌ 列表格式错误2. 网络连接问题
# 问题:连接超时
# 解决方案:
nuclei -u target -timeout 30 -retries 3 -verbose
# 问题:SSL证书错误
# 解决方案:
nuclei -u https://target -disable-certificate-verification
# 问题:代理配置
# 解决方案:
nuclei -u target -proxy http://proxy:8080
nuclei -u target -proxy socks5://proxy:10803. 性能和内存问题
# 问题:内存占用过高
# 解决方案:降低并发数
nuclei -l targets.txt -c 10 -rate-limit 50
# 问题:扫描速度慢
# 解决方案:优化参数
nuclei -l targets.txt -c 50 -timeout 10 -no-mhe
# 问题:模板加载慢
# 解决方案:使用特定标签
nuclei -l targets.txt -tags cve -exclude-tags dos4. 结果输出问题
# 问题:输出格式错误
# 解决方案:检查输出参数
nuclei -u target -json -o results.json
nuclei -u target -csv -o results.csv
# 问题:结果过多
# 解决方案:使用过滤器
nuclei -u target -severity high,critical -silent
nuclei -u target -json | jq '.info.severity' | sort | uniq -c
# 问题:缺少详细信息
# 解决方案:启用详细模式
nuclei -u target -verbose -include-rr -o detailed-results.txt调试技巧
模板调试流程
# 1. 语法验证
nuclei -validate -t template.yaml
# 2. 干运行测试
nuclei -u target -t template.yaml -dry-run
# 3. 调试请求
nuclei -u target -t template.yaml -debug-req
# 4. 调试响应
nuclei -u target -t template.yaml -debug-resp
# 5. 详细日志
nuclei -u target -t template.yaml -debug -verbose性能分析
# 统计信息
nuclei -l targets.txt -stats -stats-interval 10
# 内存使用监控
nuclei -l targets.txt -monitor-memory
# 请求率监控
nuclei -l targets.txt -rate-limit 100 -stats错误代码和解决方案
错误类型 | 原因 | 解决方案 |
| 模板路径错误 | 检查 |
| YAML语法错误 | 使用 |
| 网络连接超时 | 增加 |
| 速率限制触发 | 降低 |
| 内存不足 | 减少 |
| 模板执行错误 | 检查模板逻辑和语法 |
总结
Nuclei作为现代化的漏洞扫描器,通过其强大的模板系统为安全测试提供了极大的便利。掌握Nuclei需要:
核心能力
- ✅ 模板语法精通:熟练编写YAML检测模板
- ✅ 函数库应用:善用内置函数和表达式
- ✅ 性能优化:合理配置参数和并发策略
- ✅ 实战经验:在真实环境中积累使用技巧
进阶技能
- 多协议支持:HTTP、DNS、TCP等协议检测
- 自动化集成:CI/CD流程和监控告警
- 社区贡献:编写高质量模板回馈社区
- 安全意识:负责任的漏洞披露和道德使用