| name | scanning-docker-images-with-trivy |
| description | Trivy 是 Aqua Security 开源的综合性漏洞扫描器,用于检测容器镜像中操作系统软件包、语言特定依赖项的漏洞、错误配置、密钥和许可证违规,并集成到 CI/CD 流水线,支持 SARIF、CycloneDX 和 SPDX 等多种输出格式。 |
| domain | cybersecurity |
| subdomain | container-security |
| tags | ["containers","docker","security","trivy","vulnerability-scanning"] |
| version | 1.0 |
| author | mahipal |
| license | Apache-2.0 |
使用 Trivy 扫描 Docker 镜像
概述
Trivy 是 Aqua Security 开源的综合性漏洞扫描器,用于检测容器镜像中操作系统软件包、语言特定依赖项的漏洞、错误配置、密钥和许可证违规。它可集成到 CI/CD 流水线,支持 SARIF、CycloneDX 和 SPDX 等多种输出格式。
前置条件
- Docker Engine 20.10+
- Trivy v0.50+
- 互联网访问权限(用于漏洞数据库更新)
- 容器镜像仓库凭据(用于私有仓库)
核心概念
扫描器类型
| 扫描器 | 参数 | 检测内容 |
|---|
| 漏洞扫描 | --scanners vuln | 操作系统软件包和库中的 CVE |
| 错误配置扫描 | --scanners misconfig | Dockerfile/K8s 清单错误配置 |
| 密钥扫描 | --scanners secret | 硬编码密码、API 密钥、令牌 |
| 许可证扫描 | --scanners license | 软件许可证合规问题 |
严重程度级别
- 严重(CRITICAL):CVSS 9.0-10.0 - 需立即处理
- 高危(HIGH):CVSS 7.0-8.9 - 生产部署前修复
- 中危(MEDIUM):CVSS 4.0-6.9 - 计划修复
- 低危(LOW):CVSS 0.1-3.9 - 机会性接受或修复
- 未知(UNKNOWN):未评分 - 手动评估
漏洞数据库
Trivy 使用多个漏洞数据库:
- NVD(国家漏洞数据库)
- Red Hat 安全数据
- Alpine SecDB
- Debian Security Tracker
- Ubuntu CVE Tracker
- Amazon Linux Security Center
- GitHub Advisory Database
实施步骤
步骤 1:安装 Trivy
sudo apt-get install wget apt-transport-https gnupg lsb-release
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | sudo tee /usr/share/keyrings/trivy.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update && sudo apt-get install trivy
brew install trivy
docker pull aquasecurity/trivy:latest
步骤 2:基本镜像扫描
trivy image python:3.12-slim
trivy image --severity CRITICAL,HIGH nginx:latest
trivy image --ignore-unfixed alpine:3.19
docker build -t myapp:latest .
trivy image myapp:latest
docker save myapp:latest -o myapp.tar
trivy image --input myapp.tar
步骤 3:高级扫描选项
trivy image --scanners vuln,misconfig,secret,license myapp:latest
trivy image --format cyclonedx --output sbom.cdx.json myapp:latest
trivy image --format spdx-json --output sbom.spdx.json myapp:latest
trivy image --format json --output results.json myapp:latest
trivy image --format sarif --output results.sarif myapp:latest
trivy image --format template --template "@contrib/html.tpl" --output report.html myapp:latest
trivy image --list-all-pkgs myapp:latest
步骤 4:扫描 Kubernetes 清单
trivy config Dockerfile
trivy config k8s-deployment.yaml
trivy config ./helm-chart/
trivy config ./terraform/
步骤 5:CI/CD 集成
name: Trivy 容器扫描
on: push
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: 构建镜像
run: docker build -t myapp:${{ github.sha }} .
- name: 运行 Trivy 漏洞扫描
uses: aquasecurity/trivy-action@master
with:
image-ref: myapp:${{ github.sha }}
format: sarif
output: trivy-results.sarif
severity: CRITICAL,HIGH
exit-code: 1
- name: 上传 Trivy 扫描结果
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file:
trivy-scan:
stage: security
image:
name: aquasecurity/trivy:latest
entrypoint: [""]
script:
- trivy image --exit-code 1 --severity CRITICAL,HIGH
--format json --output gl-container-scanning-report.json
$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
artifacts:
reports:
container_scanning: gl-container-scanning-report.json
步骤 6:使用 .trivyignore 执行策略
CVE-2023-12345 exp:2025-06-01
CVE-2024-67890
CVE-2023-11111
步骤 7:扫描私有仓库镜像
trivy image myregistry.azurecr.io/myapp:latest
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <account>.dkr.ecr.us-east-1.amazonaws.com
trivy image <account>.dkr.ecr.us-east-1.amazonaws.com/myapp:latest
trivy image gcr.io/my-project/myapp:latest
TRIVY_USERNAME=user TRIVY_PASSWORD=pass trivy image registry.example.com/myapp:latest
验证命令
trivy version
trivy image --download-db-only
trivy image --severity CRITICAL python:3.12
trivy image --exit-code 1 --severity CRITICAL myapp:latest
echo "退出码:$?"
参考资料