Nuclei是ProjectDiscovery开发的快速、可定制的漏洞扫描器,基于YAML模板实现漏洞检测。凭借其高性能和灵活的模板系统,已成为安全研究人员和渗透测试人员的必备工具,GitHub Star数超过20k。

使用背景:公司内部攻防演练和周会巡检都用Nuclei,所以文章里会加入一些“踩坑”记录(例如模板过载、OOB忘记关导致告警被打爆)。

一、Nuclei概述

1.1 项目信息

属性 说明
项目地址 https://github.com/projectdiscovery/nuclei
开发语言 Go
GitHub Stars 20k+
开源协议 MIT
最新版本 v3.x
模板仓库 nuclei-templates (9k+ templates)

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
# 方式1:Go安装(推荐)
go install -v github.com/projectdiscovery/nuclei/v3/cmd/nuclei@latest

# 方式2:二进制下载
# 从 https://github.com/projectdiscovery/nuclei/releases 下载

# 方式3:Docker
docker pull projectdiscovery/nuclei:latest

# 方式4:Homebrew (macOS)
brew install nuclei

# 方式5:源码编译
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

# 扫描URL列表
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

# 自定义Header
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

# 系统DNS解析
nuclei -u https://example.com -system-resolvers

# 扫描统计
nuclei -u https://example.com -stats -stats-interval 5

2.4 扫描顺序建议

  1. 先资产后漏洞:我通常先跑technologies/、dns/类模板确认指纹,再分阶段加载CVE模板,避免噪声太大。
  2. 先低速再提速:生产系统初次扫描以-c 20 -rl 50观察目标抗压能力,确认无异常后再递增,别一上来就-c 200。
  3. 落盘策略:扫描前就决定好-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 # 严重程度: info/low/medium/high/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 # 匹配条件: and/or
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
# DNS模板
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"

---
# TCP模板
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"

---
# SSL模板
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:
# 步骤1:检测WordPress
- method: GET
path:
- "{{BaseURL}}/wp-login.php"

matchers:
- type: word
words:
- "WordPress"

extractors:
- type: regex
name: wp_version
internal: true
regex:
- 'ver=([0-9.]+)'

# 步骤2:检测漏洞版本
- method: GET
path:
- "{{BaseURL}}/wp-includes/version.php"

matchers:
- type: dsl
dsl:
- "compare_versions(wp_version, '< 5.8.0')"

---
# 使用OOB交互
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编码
{{base64_decode("c3RyaW5n")}} # Base64解码
{{url_encode("string")}} # URL编码
{{url_decode("%73%74%72")}} # URL解码
{{hex_encode("string")}} # Hex编码
{{hex_decode("737472696e67")}} # Hex解码
{{md5("string")}} # MD5哈希
{{sha256("string")}} # SHA256哈希
{{sha1("string")}} # SHA1哈希
{{mmh3("string")}} # MurmurHash3
{{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
# 扫描所有CVE
nuclei -u https://target.com -t cves/

# 扫描特定年份CVE
nuclei -u https://target.com -t cves/2024/
nuclei -u https://target.com -t cves/2023/

# 扫描特定CVE
nuclei -u https://target.com -t cves/2024/CVE-2024-XXXX.yaml

# 扫描高危CVE
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/

# 综合Web扫描
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
# AWS安全检测
nuclei -u https://target.com -t cloud/aws/

# Azure安全检测
nuclei -u https://target.com -t cloud/azure/

# GCP安全检测
nuclei -u https://target.com -t cloud/gcp/

# Kubernetes安全
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
# GitHub Actions集成
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
# Python集成示例
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 \ # 禁用OOB检测
-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
# ~/.config/nuclei/config.yaml
# 全局配置

# 并发设置
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

# 代理设置
# proxy: http://127.0.0.1:8080

# Header设置
header:
- "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"

# 排除设置
exclude-templates:
- dos/
- fuzzing/

# Interactsh设置
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 安全注意事项

  1. 授权扫描:确保获得目标系统的扫描授权
  2. 速率控制:避免对目标造成DoS影响
  3. 敏感数据:妥善处理扫描结果中的敏感信息
  4. 模板审核:使用前审核第三方模板
  5. 日志记录:保留扫描日志用于审计

七、总结

Nuclei凭借其灵活的模板系统和高性能扫描引擎,已成为现代安全测试的核心工具。其优势在于:

  1. 可扩展性:YAML模板易于编写和共享
  2. 高性能:Go语言实现,支持大规模并发
  3. 社区驱动:9000+官方模板持续更新
  4. 多协议支持:HTTP、DNS、TCP、SSL等
  5. 易于集成:支持CI/CD和自动化流程

掌握Nuclei不仅能提升漏洞发现效率,更能帮助安全团队建立标准化的安全测试流程。

注意:本文内容仅供授权的安全测试和学习研究使用,未经授权扫描他人系统属于违法行为。