nuclei数据库存储:扫描结果的持久化与查询
概述
在网络安全扫描中,结果数据的持久化存储和高效查询是至关重要的环节。nuclei作为一款现代化的高性能漏洞扫描器,提供了强大的数据库存储功能,能够将扫描结果持久化到多种数据库系统中,并支持灵活的查询和分析操作。
本文将深入探讨nuclei的数据库存储机制,涵盖LevelDB本地存储、MongoDB、Elasticsearch等外部数据库集成,以及如何利用这些功能进行扫描结果的持久化和查询分析。
核心存储架构
1. LevelDB去重存储
nuclei内置了基于LevelDB的去重存储系统,用于防止重复报告相同的漏洞发现。该系统通过SHA1哈希算法对扫描结果进行唯一性标识:
// 哈希生成逻辑
hasher := sha1.New()
if result.TemplateID != "" {
_, _ = hasher.Write(conversion.Bytes(result.TemplateID))
}
if result.MatcherName != "" {
_, _ = hasher.Write(conversion.Bytes(result.MatcherName))
}
// ... 更多字段参与哈希计算
hash := hasher.Sum(nil)
2. 多数据库支持
nuclei支持多种数据库系统作为扫描结果的存储后端:
| 数据库类型 | 配置文件选项 | 适用场景 |
|---|---|---|
| MongoDB | MongoDBExporter | 大规模分布式存储 |
| Elasticsearch | ElasticsearchExporter | 全文搜索和分析 |
| JSON文件 | JSONExporter | 简单本地存储 |
| JSONL文件 | JSONLExporter | 流式处理 |
配置数据库存储
1. 配置文件设置
创建report-config.yaml配置文件:
# 报告配置示例
MongoDBExporter:
ConnectionString: "mongodb://localhost:27017"
DatabaseName: "nuclei_scans"
CollectionName: "vulnerabilities"
BatchSize: 100
ElasticsearchExporter:
Address: "http://localhost:9200"
IndexName: "nuclei-results"
Username: ""
Password: ""
JSONLExporter:
File: "scan-results.jsonl"
2. 命令行使用
# 使用MongoDB存储
nuclei -u https://example.com -config report-config.yaml
# 使用JSONL文件存储
nuclei -u https://example.com -jsonl-export results.jsonl
# 使用报告数据库(LevelDB)
nuclei -u https://example.com -report-db scan_results.db
数据结构设计
1. 扫描结果核心字段
type ResultEvent struct {
TemplateID string `json:"template-id"`
TemplatePath string `json:"template-path"`
Info model.Info `json:"info,inline"`
Type string `json:"type"`
Host string `json:"host"`
Port string `json:"port"`
Matched string `json:"matched-at"`
ExtractedResults []string `json:"extracted-results"`
Request string `json:"request"`
Response string `json:"response"`
Timestamp time.Time `json:"timestamp"`
Severity severity.Severity `json:"severity"`
Metadata map[string]interface{} `json:"meta"`
}
2. MongoDB集合设计
// MongoDB文档结构
{
_id: ObjectId,
template_id: "CVE-2021-44228",
template_name: "Apache Log4j RCE",
host: "example.com",
port: 443,
severity: "CRITICAL",
matched_at: "/api/endpoint",
timestamp: ISODate("2024-01-15T10:30:00Z"),
request: "...",
response: "...",
extracted_data: ["重要信息"],
metadata: {
cvss_score: 9.8,
cwe: "CWE-502"
}
}
查询操作指南
1. MongoDB查询示例
// 查询高严重性漏洞
db.vulnerabilities.find({
severity: "CRITICAL",
timestamp: {
$gte: ISODate("2024-01-01"),
$lte: ISODate("2024-01-31")
}
})
// 按模板分组统计
db.vulnerabilities.aggregate([
{
$group: {
_id: "$template_id",
count: { $sum: 1 },
hosts: { $addToSet: "$host" }
}
}
])
// 时间范围查询
db.vulnerabilities.find({
timestamp: {
$gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)
}
})
2. Elasticsearch查询示例
{
"query": {
"bool": {
"must": [
{
"term": { "severity": "HIGH" }
},
{
"range": {
"timestamp": {
"gte": "now-7d/d"
}
}
}
]
}
},
"aggs": {
"by_template": {
"terms": {
"field": "template_id.keyword",
"size": 10
}
}
}
}
高级功能
1. 批量处理优化
nuclei支持批量写入优化,减少数据库IO操作:
// 批量写入配置
MongoDBExporter:
BatchSize: 1000 # 每1000条记录批量写入一次
ElasticsearchExporter:
BatchSize: 500 # 每500条记录批量写入一次
2. 数据去重机制
3. 自定义导出器
您可以实现自定义的Exporter接口来支持其他数据库系统:
type Exporter interface {
Close() error
Export(event *output.ResultEvent) error
}
// 自定义MySQL导出器示例
type MySQLExporter struct {
db *sql.DB
}
func (m *MySQLExporter) Export(event *output.ResultEvent) error {
// 实现MySQL插入逻辑
_, err := m.db.Exec(`
INSERT INTO vulnerabilities
(template_id, host, severity, timestamp)
VALUES (?, ?, ?, ?)`,
event.TemplateID, event.Host, event.Info.Severity, event.Timestamp)
return err
}
性能优化建议
1. 数据库连接池配置
MongoDBExporter:
ConnectionString: "mongodb://localhost:27017"
MaxPoolSize: 100
MinPoolSize: 10
ElasticsearchExporter:
Address: "http://localhost:9200"
MaxRetries: 3
Timeout: "30s"
2. 索引优化
// MongoDB索引创建
db.vulnerabilities.createIndex({ timestamp: -1 })
db.vulnerabilities.createIndex({ host: 1 })
db.vulnerabilities.createIndex({ template_id: 1, severity: 1 })
// Elasticsearch映射优化
PUT nuclei-results
{
"mappings": {
"properties": {
"timestamp": { "type": "date" },
"host": { "type": "keyword" },
"severity": { "type": "keyword" }
}
}
}
实战案例
1. 企业级扫描结果管理
# 启动扫描并将结果存储到MongoDB
nuclei -l targets.txt -config enterprise-config.yaml -report-db scan_db
# 配置示例(enterprise-config.yaml)
MongoDBExporter:
ConnectionString: "mongodb://cluster1,cluster2,cluster3/nuclei?replicaSet=rs0"
DatabaseName: "security_scans"
CollectionName: "prod_vulnerabilities"
BatchSize: 1000
2. 安全仪表板集成
// 实时漏洞统计查询
const vulnerabilityStats = await db.vulnerabilities.aggregate([
{
$match: {
timestamp: { $gte: new Date(Date.now() - 24 * 60 * 60 * 1000) }
}
},
{
$group: {
_id: "$severity",
count: { $sum: 1 },
uniqueHosts: { $addToSet: "$host" }
}
}
])
故障排除
常见问题及解决方案
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 数据库连接超时 | 网络问题或配置错误 | 检查连接字符串,增加超时时间 |
| 批量写入失败 | 批次过大或内存不足 | 减小BatchSize参数 |
| 重复数据 | 去重机制失效 | 检查LevelDB存储路径权限 |
监控建议
# 监控数据库连接状态
mongostat -n 5
# 检查存储性能
db.vulnerabilities.stats()
# 监控磁盘空间使用
df -h /path/to/leveldb/storage
总结
nuclei的数据库存储功能为安全团队提供了强大的扫描结果管理能力。通过合理配置LevelDB、MongoDB、Elasticsearch等存储后端,可以实现:
- 高效去重:避免重复报告相同漏洞
- 持久化存储:长期保存扫描历史记录
- 灵活查询:支持复杂的检索和分析需求
- 集成扩展:与现有安全工具链无缝集成
掌握nuclei的数据库存储功能,将显著提升您的漏洞管理效率和安全性态势感知能力。建议根据实际业务需求选择合适的存储方案,并定期优化数据库性能和存储结构。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



