sca-trivy
概述
Trivy 是一个全面的安全扫描器,用于容器、文件系统和 git 仓库。它检测操作系统包和应用程序依赖项中的漏洞(CVE)、IaC 配置错误、暴露的机密和软件许可证。这项技能提供了漏洞扫描、SBOM 生成、CI/CD 集成和基于 CVSS 和 OWASP 标准的补救优先级对齐的工作流程。
快速开始
扫描容器镜像以查找漏洞:
# 安装 Trivy
brew install trivy # macOS
# 或者:apt-get install trivy # Debian/Ubuntu
# 或者:docker pull aquasec/trivy:latest
# 扫描容器镜像
trivy image nginx:latest
# 扫描本地文件系统以查找依赖项
trivy fs .
# 扫描 IaC 文件以查找配置错误
trivy config .
# 生成 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:依赖项漏洞扫描
扫描项目依赖项以查找已知漏洞:
# 扫描文件系统以查找所有依赖项
trivy fs --severity CRITICAL,HIGH .
# 扫描特定包清单
trivy fs --scanners vuln package-lock.json
# 生成 JSON 报告以进行分析
trivy fs --format json --output trivy-report.json .
# 生成 SARIF 报告以集成到 GitHub/GitLab
trivy fs --format sarif --output trivy.sarif .
对于每个漏洞:
- 查看 CVE 详情和 CVSS 评分
- 检查是否有可用的修复版本
- 参考
references/remediation_guide.md以获取特定于语言的指导 - 更新依赖项到修补版本
- 重新扫描以验证修复
工作流程 3:基础设施即代码安全
检测 IaC 文件中的配置错误:
# 扫描 Terraform 配置
trivy config ./terraform --severity CRITICAL,HIGH
# 扫描 Kubernetes 清单
trivy config ./k8s --severity CRITICAL,HIGH
# 扫描 Dockerfile 最佳实践
trivy config --file-patterns dockerfile:Dockerfile .
# 生成带有补救指导的报告
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 生成
生成软件物料清单以提高供应链透明度:
# 生成 CycloneDX SBOM
trivy image --format cyclonedx --output sbom-cyclonedx.json nginx:latest
# 生成 SPDX SBOM
trivy image --format spdx-json --output sbom-spdx.json nginx:latest
# 文件系统/项目 SBOM
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- Jenkins 管道与 Trivy 集成policy_template.rego- 自定义漏洞策略的 OPA 策略模板
常见模式
模式 1:多阶段安全扫描
结合多种扫描类型进行全面的安全评估:
# 1. 扫描容器镜像以查找漏洞
trivy image --severity CRITICAL,HIGH myapp:latest
# 2. 扫描 IaC 以查找配置错误
trivy config ./infrastructure --severity CRITICAL,HIGH
# 3. 扫描文件系统以查找依赖项漏洞
trivy fs --severity CRITICAL,HIGH ./app
# 4. 扫描以查找暴露的秘密
trivy fs --scanners secret ./app
# 5. 生成全面的 SBOM
trivy image --format cyclonedx --output sbom.json myapp:latest
模式 2:基线漏洞跟踪
实施基线扫描以跟踪仅新漏洞:
# 初始扫描 - 创建基线
trivy image --format json --output baseline.json nginx:latest
# 后续扫描 - 检测新漏洞
trivy image --format json --output current.json nginx:latest
./scripts/baseline_manager.py --baseline baseline.json --current current.json
模式 3:许可证合规性扫描
检测许可证合规性风险:
# 扫描许可证信息
trivy image --scanners license --format json --output licenses.json myapp:latest
# 按许可证类型过滤
trivy image --scanners license --severity HIGH,CRITICAL myapp:latest
审查发现:
- 高风险:GPL, AGPL(强版权)
- 中风险:LGPL, MPL(弱版权)
- 低风险:Apache, MIT, BSD(许可)
模式 4:自定义策略执行
使用 OPA 应用自定义安全策略:
# 在 assets/policy_template.rego 中创建 Rego 策略
# 拒绝带有 CRITICAL 漏洞或过时包的镜像
# 运行带有策略执行的扫描
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 上传到安全标签 - GitLab CI:依赖项扫描报告格式适用于安全仪表板
- 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了解常见模式
问题:大型图像性能问题
症状:扫描时间过长或内存使用过高
解决方案:
- 使用缓存 DB:
trivy image --cache-dir /path/to/cache myapp:latest - 跳过不必要的扫描器:
--scanners vuln(排除配置、秘密) - 在初始 DB 下载后使用离线模式:
--offline-scan - 增加超时:
--timeout 30m - 扫描特定层:
--removed-pkgs排除已删除的包
问题:特定语言缺少漏洞
症状:在应用程序依赖项中未检测到预期的 CVE
解决方案:
- 验证语言支持:检查支持的语言和文件模式
- 确保存在依赖项清单(package.json, go.mod, requirements.txt)
- 包括锁定文件以进行准确的版本检测
- 对于编译的二进制文件,单独扫描源代码
- 参考
references/scanner_types.md了解特定于语言的要求
问题:注册表身份验证失败
症状:无法扫描私有容器图像
解决方案:
# 使用 Docker 凭据助手
docker login registry.example.com
trivy image registry.example.com/private/image:tag
# 或者使用环境变量
export TRIVY_USERNAME=user
export TRIVY_PASSWORD=pass
trivy image registry.example.com/private/image:tag
# 或者使用凭据文件
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
# 误报 - 在供应商分支中已修补
CVE-0000-12345
# 安全团队接受的风险 - JIRA-1234
CVE-0000-67890
# 没有可用的修复,已就地补偿控制
CVE-0000-11111
离线空气隔离扫描
对于空气隔离环境:
# 在连接互联网的机器上:
trivy image --download-db-only --cache-dir /path/to/db
# 将缓存转移到空气隔离环境
# 在空气隔离机器上:
trivy image --skip-db-update --cache-dir /path/to/db --offline-scan myapp:latest