Nuclei进阶指南:安全研究者的扫描与模板技巧
1. Nuclei概述
Nuclei是由ProjectDiscovery开发的基于YAML模板的现代漏洞扫描器,具有以下核心特点:
- 高性能:支持大规模并发扫描
- 精准检测:基于社区验证的模板库
- 持续更新:活跃的开源社区维护
- 易于扩展:简单的YAML语法编写自定义检测
2. 核心概念
2.1 架构原理
Nuclei的核心组件包括:
| 组件 | 功能 | 示例 |
|---|---|---|
| 模板 | 定义检测逻辑 | cves/2023/CVE-2023-1234.yaml |
| 匹配器 | 验证漏洞特征 | status: 200 + words: "admin" |
| 提取器 | 获取关键信息 | 提取session ID、版本号 |
| 变量 | 动态内容替换 | {{BaseURL}}、{{Hostname}} |
3. 安装配置
3.1 安装方式
-
Go安装(推荐):
go install -v github.com/projectdiscovery/nuclei/v2/cmd/nuclei@latest -
预编译二进制:
从GitHub Releases下载对应平台的二进制文件 -
Docker容器:
docker pull projectdiscovery/nuclei:latest
3.2 环境配置
初始化配置:
nuclei
配置文件示例(~/.config/nuclei/config.yaml):
update-directory: /path/to/nuclei-templates
templates-directory: /path/to/custom-templates
4. 基础使用
4.1 基本命令结构
nuclei -t <模板> -u <目标> [选项]
4.2 目标指定方式
- 单个URL:
-u https://example.com - 文件列表:
-l urls.txt - CIDR范围:
192.168.1.1/24
4.3 实用参数组合
快速扫描:
nuclei -u https://example.com -t cves/ -severity critical,high -silent
深度扫描:
nuclei -u https://example.com -t all -depth 3 -rate-limit 50
静默扫描:
nuclei -l targets.txt -t exposures/ -silent -no-color -json -o results.json
5. 模板编写
5.1 基础模板结构
id: example-vuln
info:
name: Example Vulnerability
author: yourname
severity: medium
description: |
This template detects Example Vulnerability.
requests:
- method: GET
path:
- "{{BaseURL}}/vulnerable-endpoint"
matchers:
- type: word
words:
- "vulnerable string"
5.2 实际模板示例
简单GET请求检测:
id: exposed-phpinfo
info:
name: Exposed phpinfo
author: pdteam
severity: low
requests:
- method: GET
path:
- "{{BaseURL}}/phpinfo.php"
matchers:
- type: word
words:
- "<title>phpinfo()</title>"
- "PHP Version"
condition: and
POST请求with负载:
id: sql-injection-check
requests:
- method: POST
path: "{{BaseURL}}/login"
body: "username=admin' OR 1=1--&password=test"
matchers:
- type: word
words:
- "Welcome, admin"
5.3 匹配器详解
状态码匹配:
matchers:
- type: status
status:
- 200
- 302
字符串匹配:
matchers:
- type: word
words:
- "admin panel"
- "dashboard"
condition: or
正则表达式匹配:
matchers:
- type: regex
regex:
- "API_KEY: ([A-Z0-9]{32})"
DSL表达式匹配:
matchers:
- type: dsl
dsl:
- "status_code == 200 && contains(body, 'secret')"
6. 内置功能
6.1 变量系统
内置变量:
| 变量 | 描述 | 示例值 |
|---|---|---|
{{BaseURL}} |
完整基础URL | https://example.com |
{{RootURL}} |
根URL | https://example.com |
{{Hostname}} |
主机名 | example.com |
{{Host}} |
主机地址 | example.com:443 |
{{Port}} |
端口号 | 443 |
{{Path}} |
路径部分 | /api/v1 |
{{Scheme}} |
协议 | https |
自定义变量:
variables:
username: "admin"
password: "password123"
6.2 内置函数库
Nuclei提供了丰富的内置函数:
编码解码函数:
base64()- Base64编码hex_encode()- 十六进制编码url_encode()- URL编码
字符串处理函数:
to_upper()- 转为大写to_lower()- 转为小写trim_space()- 去除空格
随机生成函数:
rand_char()- 随机字符rand_text_alphanumeric()- 随机字母数字rand_int()- 随机整数
时间函数:
unix_time()- 当前Unix时间戳wait_for()- 等待指定秒数
完整函数参考:访问 Nuclei官方文档 - Helper Functions
6.3 条件逻辑和循环
条件判断:
requests:
- method: GET
path: "{{BaseURL}}/status"
matchers:
- type: dsl
name: is_healthy
dsl:
- "contains(body, 'healthy')"
- method: GET
path: "{{BaseURL}}/admin"
matchers-condition: and
condition: '{{is_healthy}} == true'
循环结构:
variables:
endpoints:
- "/api/v1"
- "/api/v2"
- "/admin"
requests:
- method: GET
path:
- "{{BaseURL}}{{endpoint}}"
payloads:
endpoint: "{{endpoints}}"
7. 高级技巧
7.1 多步骤攻击链
模拟真实的渗透测试流程,通过多个请求步骤完成复杂的漏洞检测:
id: multi-step-auth-bypass
requests:
# 第一步:获取CSRF token
- method: GET
path: "{{BaseURL}}/login"
extractors:
- type: regex
name: csrf_token
regex:
- 'name="csrf_token" value="([a-f0-9]{32})"'
# 第二步:尝试登录
- method: POST
path: "{{BaseURL}}/login"
body: "username=admin&password=password123&csrf_token={{csrf_token}}"
matchers:
- type: word
words:
- "Welcome, admin"
7.2 负载攻击模式
Nuclei支持四种攻击模式来处理多个负载:
Batteringram模式:
使用同一个负载填充所有位置:
payloads:
user: ["admin", "test", "guest"]
pass: ["password", "123456", "admin123"]
attack: batteringram
Pitchfork模式:
按位置顺序配对使用负载:
payloads:
user: ["admin", "test", "guest"]
pass: ["password", "123456", "admin123"]
attack: pitchfork
Clusterbomb模式:
所有负载的笛卡尔积组合:
payloads:
user: ["admin", "test"]
pass: ["password", "123456"]
attack: clusterbomb
7.3 外部交互检测(OAST)
检测盲注、SSRF等需要外部交互确认的漏洞:
id: oast-ssrf-check
requests:
- method: GET
path: "{{BaseURL}}/fetch?url=http://{{interactsh-url}}"
matchers:
- type: word
part: interactsh_protocol
words:
- "http"
7.4 原始请求模板
对于需要精确控制HTTP请求格式的场景:
requests:
- raw:
- |
GET /path HTTP/1.1
Host: {{Hostname}}
User-Agent: Mozilla/5.0
Accept: */*
- |
POST /submit HTTP/1.1
Host: {{Hostname}}
Content-Type: application/x-www-form-urlencoded
Content-Length: 15
param1=test
7.5 条件执行控制
根据前置条件控制后续请求的执行:
requests:
- method: GET
path: "{{BaseURL}}/check"
matchers:
- type: dsl
name: is_vulnerable
dsl:
- "contains(body, 'vulnerable')"
- method: POST
path: "{{BaseURL}}/exploit"
condition: '{{is_vulnerable}} == true'
8. 最佳实践
8.1 模板编写规范
命名约定:
- 使用小写字母和连字符
- 按类型组织目录结构
- 包含完整的信息字段
完整的信息字段:
info:
name: Apache Struts OGNL Injection
author: pdteam
severity: critical
description: |
Detects Apache Struts OGNL injection vulnerability (CVE-2017-5638).
reference:
- https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-5638
- https://struts.apache.org/docs/s2-045.html
tags: cve,cve2017,struts,injection
8.2 性能优化策略
智能请求控制:
- 合理设置
rate-limit参数 - 使用
-headless模式减少资源消耗 - 避免不必要的重定向
优化匹配器性能:
- 优先使用
word匹配器而非regex - 使用
matchers-condition减少不必要的匹配 - 合理设置
max-size限制响应大小
批量扫描优化:
nuclei -l targets.txt -t cves/ -bulk-size 50 -c 100 -timeout 10
8.3 安全和道德考虑
负责任的漏洞检测:
- 避免使用破坏性payload
- 添加
self-contained: true标记非侵入式模板 - 遵循目标网站的robots.txt规则
避免破坏性测试:
info:
classification:
cvss-metrics: CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H
cvss-score: 9.8
cwe-id: CWE-89
metadata:
self-contained: true
verified: true
8.4 错误处理和调试
调试模式使用:
nuclei -u https://example.com -t my-template.yaml -debug
错误处理最佳实践:
- 使用
stop-at-first-match: true减少不必要的请求 - 设置合理的
timeout值 - 使用
retries处理网络波动
8.5 模板组织和管理
目录结构建议:
nuclei-templates/
├── cves/
├── vulnerabilities/
├── exposures/
├── misconfigurations/
└── workflows/
Git工作流程:
- 从官方仓库fork
- 创建特性分支
- 提交模板变更
- 发起Pull Request
8.6 集成和自动化
CI/CD集成示例(GitHub Actions):
name: Nuclei Scan
on: [push, pull_request]
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run Nuclei
uses: projectdiscovery/nuclei-action@main
with:
targets: https://example.com
templates: cves/
output: results.json
Docker容器化扫描:
docker run -v $(pwd):/data projectdiscovery/nuclei \
-u https://example.com -t /data/custom-templates -o /data/results.json
9. 故障排除
9.1 常见问题诊断
-
模板语法错误:
- 使用
nuclei -validate验证模板 - 检查YAML缩进和格式
- 使用
-
网络连接问题:
- 增加
-timeout值 - 检查代理设置
- 增加
-
性能和内存问题:
- 减少
-c并发数 - 使用
-headless模式
- 减少
-
结果输出问题:
- 检查文件权限
- 确保输出目录存在
9.2 调试技巧
模板调试流程:
- 使用
-debug参数运行 - 检查请求和响应详情
- 逐步验证匹配器逻辑
性能分析:
nuclei -u https://example.com -stats -metrics
9.3 错误代码和解决方案
| 错误类型 | 原因 | 解决方案 |
|---|---|---|
| template not found | 模板路径错误 | 检查-t参数路径 |
| invalid yaml | YAML语法错误 | 使用-validate检查 |
| connection timeout | 网络连接超时 | 增加-timeout值 |
| too many requests | 速率限制触发 | 降低-rate-limit值 |
| memory allocation | 内存不足 | 减少-c并发数 |
| template execution failed | 模板执行错误 | 检查模板逻辑和语法 |
10. 总结
10.1 核心能力
- ✅ 模板语法精通:熟练编写YAML检测模板
- ✅ 函数库应用:善用内置函数和表达式
- ✅ 性能优化:合理配置参数和并发策略
- ✅ 实战经验:在真实环境中积累使用技巧
10.2 进阶技能
- 多协议支持:HTTP、DNS、TCP等协议检测
- 自动化集成:CI/CD流程和监控告警
- 社区贡献:编写高质量模板回馈社区
- 安全意识:负责任的漏洞披露和道德使用
通过本指南,您应该已经掌握了Nuclei的高级使用技巧和模板编写方法。建议持续关注ProjectDiscovery官方文档和社区更新,以获取最新的功能和最佳实践。