关于 sca-trivy
sca-trivy 是一项技能,指导开发者和安全团队使用 Trivy 扫描容器、项目文件和代码库。它提供漏洞分析、修复、CI/CD 集成及 SBOM 生成工作流和脚本。
主要功能
- 01容器与依赖项漏洞扫描
- 02IaC 错误配置检查
- 03秘密信息与许可证扫描
- 04基于 CVSS 的修复报告
- 05生成 CycloneDX 和 SPDX SBOM
- 06CI/CD 模板与基线跟踪
适用场景
- 01确定容器镜像漏洞的修复优先级
- 02检查依赖项并在更新后重新扫描
- 03检查 Terraform 和 Kubernetes 配置
- 04为合规与供应链审查生成 SBOM
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ agentsecops/ secopsagentkit --skill sca-trivy安装后在 Claude Code 中输入/sca-trivy确认已加载。
详细介绍
使用 Trivy 进行软件成分分析
概述
Trivy 是一款全面的安全扫描器,可用于扫描容器、文件系统和 git 仓库。它可以检测操作系统软件包和应用程序依赖项中的漏洞(CVE)、IaC 配置错误、暴露的密钥以及软件许可证。本技能提供漏洞扫描、SBOM 生成、CI/CD 集成和修复优先级排序工作流,并与 CVSS 和 OWASP 标准保持一致。
快速开始
扫描容器映像中的漏洞:
# Install Trivy
brew install trivy # macOS
# or: apt-get install trivy # Debian/Ubuntu
# or: docker pull aquasec/trivy:latest
# Scan container image
trivy image nginx:latest
# Scan local filesystem for dependencies
trivy fs .
# Scan IaC files for misconfigurations
trivy config .
# Generate SBOM
trivy image --format cyclonedx --output sbom.json nginx:latest
核心工作流
工作流 1:容器映像安全评估
进度:
[ ] 1. 确定目标容器映像(仓库:标签)
[ ] 2. 使用 trivy image <image-name> 运行全面的 Trivy 扫描
[ ] 3. 按严重程度(CRITICAL、HIGH、MEDIUM、LOW)分析漏洞发现结果
[ ] 4. 将 CVE 发现结果映射到 CWE 类别和 OWASP 参考资料
[ ] 5. 检查是否有可用补丁和更新后的基础映像
[ ] 6. 生成修复优先级报告,并提供升级建议
请系统地完成每个步骤,并勾选已完成的项目。
工作流 2:依赖项漏洞扫描
扫描项目依赖项,查找已知漏洞:
# Scan filesystem for all dependencies
trivy fs --severity CRITICAL,HIGH .
# Scan specific package manifest
trivy fs --scanners vuln package-lock.json
# Generate JSON report for analysis
trivy fs --format json --output trivy-report.json .
# Generate SARIF for GitHub/GitLab integration
trivy fs --format sarif --output trivy.sarif .
对于每个漏洞:
- 查看 CVE 详情和 CVSS 评分
- 检查是否有可用的修复版本
- 查阅
references/remediation_guide.md,获取特定语言的指导 - 将依赖项更新到已修补版本
- 重新扫描以验证修复
工作流 3:基础设施即代码安全
检测 IaC 文件中的配置错误:
# Scan Terraform configurations
trivy config ./terraform --severity CRITICAL,HIGH
# Scan Kubernetes manifests
trivy config ./k8s --severity CRITICAL,HIGH
# Scan Dockerfile best practices
trivy config --file-patterns dockerfile:Dockerfile .
# Generate report with remediation guidance
trivy config --format json --output iac-findings.json .
按类别查看发现结果:
- 安全性:身份验证、授权、加密
- 合规性:CIS 基准、安全标准
- 最佳实践:资源限制、不可变性、最小权限
工作流 4:CI/CD 流水线集成
GitHub Actions
name: Trivy Security Scan
on: [push, pull_request]
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
scan-ref: '.'
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'CRITICAL,HIGH'
- name: Upload results to GitHub Security
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: 'trivy-results.sarif'
GitLab CI
trivy-scan:
stage: test
image: aquasec/trivy:latest
script:
- trivy fs --exit-code 1 --severity CRITICAL,HIGH --format json --output trivy-report.json .
artifacts:
reports:
dependency_scanning: trivy-report.json
when: always
allow_failure: false
如需支持其他平台,请使用 assets/ci_integration/ 中附带的模板。
工作流 5:SBOM 生成
生成软件物料清单,以提高供应链透明度:
# Generate CycloneDX SBOM
trivy image --format cyclonedx --output sbom-cyclonedx.json nginx:latest
# Generate SPDX SBOM
trivy image --format spdx-json --output sbom-spdx.json nginx:latest
# SBOM for filesystem/project
trivy fs --format cyclonedx --output project-sbom.json .
SBOM 用例:
- 漏洞跟踪:监控依赖项中的新 CVE
- 许可证合规:识别许可证义务和风险
- 供应链安全:验证组件来源
- 法规合规:满足 CISA 的 SBOM 要求
安全注意事项
敏感数据处理
- 注册表凭据:使用环境变量或凭据助手,切勿硬编码
- 扫描报告:包含漏洞详情和软件包版本,应按敏感信息处理
- SBOM 文件:可能暴露内部架构,应妥善控制访问权限
- 密钥扫描:启用
--scanners secret,检测映像中暴露的凭据
访问控制
- 容器注册表访问:扫描映像需要拉取权限
- 文件系统访问:需要有权读取依赖项清单和 IaC 文件
- CI/CD 集成:在密钥管理系统中妥善保护 API 令牌和注册表凭据
- 报告存储:限制对漏洞报告和 SBOM 产物的访问
审计日志
为满足合规要求和响应安全事件,请记录以下信息:
- 扫描执行时间戳和扫描范围(映像、文件系统、仓库)
- 按严重程度统计的漏洞数量
- 策略违规和阻止决策
- SBOM 生成和分发事件
- 修复操作和版本更新
合规要求
- PCI-DSS 6.2:确保系统组件免受已知漏洞的影响
- SOC2 CC7.1:检测可能影响安全性的变更并采取行动
- NIST 800-53 SI-2:缺陷修复和漏洞扫描
- CIS 基准:容器和 Kubernetes 安全加固
- OWASP Top 10 A06:易受攻击和过时的组件
- CWE-1104:使用未维护的第三方组件
附带资源
脚本(scripts/)
trivy_scan.py- 全面扫描,支持 JSON/SARIF 输出和严重性筛选sbom_generator.py- 生成 SBOM,支持 CycloneDX 和 SPDX 格式vulnerability_report.py- 解析 Trivy 输出,并生成包含 CVSS 评分的修复报告baseline_manager.py- 创建基线,仅跟踪新发现的漏洞
参考资料(references/)
scanner_types.md- 漏洞、错误配置、机密和许可证扫描的详细指南remediation_guide.md- 针对特定语言和生态系统的修复策略cvss_prioritization.md- CVSS 评分解读和漏洞优先级排序框架iac_checks.md- IaC 安全检查完整列表及其 CIS 基准映射
资源(assets/)
trivy.yaml- 自定义 Trivy 配置,包含安全策略和忽略规则ci_integration/github-actions.yml- 完整的 GitHub Actions 工作流,包含安全门禁ci_integration/gitlab-ci.yml- 完整的 GitLab CI 流水线,包含依赖项扫描ci_integration/jenkins.groovy- 集成 Trivy 的 Jenkins 流水线policy_template.rego- 用于自定义漏洞策略的 OPA 策略模板
常见模式
模式 1:多阶段安全扫描
结合多种扫描类型进行全面的安全评估:
# 1. Scan container image for vulnerabilities
trivy image --severity CRITICAL,HIGH myapp:latest
# 2. Scan IaC for misconfigurations
trivy config ./infrastructure --severity CRITICAL,HIGH
# 3. Scan filesystem for dependency vulnerabilities
trivy fs --severity CRITICAL,HIGH ./app
# 4. Scan for exposed secrets
trivy fs --scanners secret ./app
# 5. Generate comprehensive SBOM
trivy image --format cyclonedx --output sbom.json myapp:latest
模式 2:漏洞基线跟踪
实施基线扫描,仅跟踪新发现的漏洞:
# Initial scan - create baseline
trivy image --format json --output baseline.json nginx:latest
# Subsequent scans - detect new vulnerabilities
trivy image --format json --output current.json nginx:latest
./scripts/baseline_manager.py --baseline baseline.json --current current.json
模式 3:许可证合规扫描
检测许可证合规风险:
# Scan for license information
trivy image --scanners license --format json --output licenses.json myapp:latest
# Filter by license type
trivy image --scanners license --severity HIGH,CRITICAL myapp:latest
审查发现:
- 高风险:GPL、AGPL(强著佐权)
- 中风险:LGPL、MPL(弱著佐权)
- 低风险:Apache、MIT、BSD(宽松)
模式 4:自定义策略执行
使用 OPA 应用自定义安全策略:
# Create Rego policy in assets/policy_template.rego
# Deny images with CRITICAL vulnerabilities or outdated packages
# Run scan with policy enforcement
trivy image --format json --output scan.json myapp:latest
trivy image --ignore-policy assets/policy_template.rego myapp:latest
集成点
CI/CD 集成
- GitHub Actions:原生
aquasecurity/trivy-action,支持将 SARIF 上传到 Security 标签页 - GitLab CI:使用依赖项扫描报告格式,供 Security Dashboard 使用
- Jenkins:基于 Docker 的扫描,并生成 JUnit XML 报告
- CircleCI:使用 Docker 执行器和制品存储
- Azure Pipelines:基于任务的集成,并发布结果
容器平台
- Docker:推送到注册表前扫描镜像
- Kubernetes:使用 trivy-operator 的准入控制器进行运行时扫描
- Harbor:内置 Trivy 集成,用于注册表扫描
- AWS ECR:推送时扫描镜像,并启用增强扫描
- Google Artifact Registry:漏洞扫描集成
安全工具生态系统
- SIEM 集成:将 JSON 发现结果导出到 Splunk、ELK 或 Datadog
- 漏洞管理:将 SARIF/JSON 导入 Snyk、Qualys 或 Rapid7
- SBOM 工具:CycloneDX 和 SPDX 与 dependency-track 和 GUAC 兼容
- 策略执行:集成 OPA/Rego,实现策略即代码
故障排除
问题:误报率高
症状:报告了许多不适用于当前使用场景的漏洞
解决方案:
- 使用
.trivyignore文件,附上理由以抑制特定 CVE - 按可利用性筛选:
trivy image --ignore-unfixed myapp:latest - 按严重性筛选:
--severity CRITICAL,HIGH - 查看供应商特定的安全公告,验证误报
- 参阅
references/false_positives.md了解常见模式
问题:大型镜像的性能问题
症状:扫描耗时过长或内存使用量过高
解决方案:
- 使用缓存数据库:
trivy image --cache-dir /path/to/cache myapp:latest - 跳过不必要的扫描器:
--scanners vuln(排除配置和机密扫描) - 首次下载数据库后使用离线模式:
--offline-scan - 增加超时时间:
--timeout 30m - 扫描特定层:使用
--removed-pkgs排除已删除的软件包
问题:未检测到特定语言的漏洞
症状:未检测到应用依赖中预期的 CVE
解决方案:
- 验证语言支持:检查支持的语言和文件模式
- 确保存在依赖清单文件(package.json、go.mod、requirements.txt)
- 添加锁定文件,以准确检测版本
- 对于已编译的二进制文件,请单独扫描源代码
- 查看
references/scanner_types.md,了解特定语言的要求
问题:注册表身份验证失败
症状:无法扫描私有容器镜像
解决方案:
# Use Docker credential helper
docker login registry.example.com
trivy image registry.example.com/private/image:tag
# Or use environment variables
export TRIVY_USERNAME=user
export TRIVY_PASSWORD=pass
trivy image registry.example.com/private/image:tag
# Or use credential file
trivy image --username user --password pass registry.example.com/private/image:tag
高级配置
自定义 Trivy 配置
创建 trivy.yaml 配置文件:
# trivy.yaml
vulnerability:
type: os,library
severity: CRITICAL,HIGH,MEDIUM
ignorefile: .trivyignore
ignore-unfixed: false
skip-files:
- "test/**"
- "**/node_modules/**"
cache:
dir: /tmp/trivy-cache
db:
repository: ghcr.io/aquasecurity/trivy-db:latest
output:
format: json
severity-sort: true
使用方法:trivy image --config trivy.yaml myapp:latest
Trivy 忽略文件
创建 .trivyignore,以抑制特定 CVE:
# .trivyignore
# False positive - patched in vendor fork
CVE-0000-12345
# Risk accepted by security team - JIRA-1234
CVE-0000-67890
# No fix available, compensating controls in place
CVE-0000-11111
离线隔离环境扫描
对于隔离环境:
# On internet-connected machine:
trivy image --download-db-only --cache-dir /path/to/db
# Transfer cache to air-gapped environment
# On air-gapped machine:
trivy image --skip-db-update --cache-dir /path/to/db --offline-scan myapp:latest