Trivy:容器镜像与文件系统漏洞扫描
bash curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
**macOS 环境**
```bash
brew install trivy
Windows 环境
可从 GitHub Releases 下载 zip 包解压后添加至 PATH。
验证安装:
trivy --version
第一次运行扫描时会自动下载漏洞数据库,稍等片刻即可完成。
核心概念:扫描对象
Trivy 主要扫描三类对象:
- 容器镜像:分析镜像分层中安装的系统包(如 dpkg, rpm, apk)和应用程序依赖(如 Gemfile.lock, package-lock.json 等)。
- 文件系统:直接扫描宿主机的文件目录、根文件系统或虚拟磁盘镜像。
- Git 仓库:扫描代码仓库中的依赖文件,无需构建镜像。
此外,它还具备 IaC 错误配置扫描 和 Secret 检测 功能,本文侧重漏洞扫描部分。
扫描容器镜像
这是 Trivy 最常用的场景。只需提供镜像名称,即可输出漏洞清单。
基础命令
trivy image nginx:1.25
默认会显示概要和表格形式的漏洞列表,包含漏洞 ID、严重程度、已修复版本等信息。
常用参数
--severity:指定最低严重级别,比如只显示CRITICAL和高危HIGH
trivy image --severity HIGH,CRITICAL python:3.12--ignore-unfixed:只展示已有修复版本的漏洞(忽略无补丁的漏洞)
trivy image --ignore-unfixed ubuntu:22.04--format:输出格式,通常使用table(默认)、json、sarif等
trivy image --format json -o result.json alpine:latest--skip-dirs:跳过镜像中的某些目录(不递归扫描),适合避免扫描大数据目录
trivy image --skip-dirs /usr/local/myapp/logs myimage:tag
扫描私有仓库镜像 需要先进行 Docker 登录或使用 Trivy 的认证配置。
trivy image --username <your-user> --password <your-password> registry.example.com/private-image:latest
更推荐使用 docker login 并让 Trivy 复用 Docker 凭证。
从 tar 包扫描镜像 若已将镜像导出为 tar 文件:
docker save -o nginx.tar nginx:1.25
trivy image --input nginx.tar
扫描文件系统
当你不便构建镜像,或只想检查宿主机某个目录(如挂载的根文件系统)时,可使用文件系统扫描。
扫描本地目录
trivy fs ./my-app-directory
Trivy 会分析目录下的已知依赖文件(如 package-lock.json, Cargo.lock, go.sum 等),并关联操作系统包数据库(若目录包含完整的 Linux 根文件系统布局)。
扫描虚拟机磁盘镜像 支持 raw 和 qcow2 格式。
trivy fs --input my-virtual-disk.qcow2
结合扫描结果的退出代码 扫描发现漏洞时默认退出码非 0,可在脚本中直接利用:
if ! trivy image --no-progress myapp:latest; then
echo "发现高危漏洞,构建中断!"
exit 1
fi
使用 --exit-code 可自定义不同严重等级的退出行为,例如仅当存在严重漏洞时失败:
trivy image --exit-code 1 --severity CRITICAL myapp:latest
定制与高级过滤
高效扫描需要精确控制范围,避免信息过载。
只扫描应用程序依赖(跳过系统包)
trivy image --vuln-type library node:18
--vuln-type 可选值:os(仅系统包)或 library(仅语言库)。
排除特定漏洞
通过 .trivyignore 文件忽略误报或已知低风险漏洞。
# .trivyignore 示例
CVE-2020-12345
CVE-2019-54321 # 已评估无影响
使用时在扫描命令所在目录放置该文件即可。
自定义策略
使用 Rego 编写策略,通过 --policy 参数加载 OPA 规则文件,过滤或标记特定漏洞。
生成报告并集成至 CI/CD
输出 SARIF 格式便于 GitHub 代码扫描
trivy image --format sarif -o trivy-results.sarif myimage:latest
上传此 SARIF 文件至 GitHub Security 即可将漏洞显示在仓库安全页面。
生成可读性更强的 HTML 报告
Trivy 本身不直接输出 HTML,但可以使用 trivy image --format template --template "@contrib/html.tpl" -o report.html myimage:latest,需要从 GitHub 仓库获取模板文件。常用做法是获取社区模板:
curl -sL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/html.tpl -o html.tpl
trivy image --format template --template "@html.tpl" -o myapp-report.html myapp:latest
GitLab CI 示例
trivy_scan:
stage: test
image: docker:stable
services:
- docker:dind
before_script:
- apk add --no-cache curl
- curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh
script:
- trivy image --severity HIGH,CRITICAL --no-progress --exit-code 1 $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
GitHub Actions 示例
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: 'myapp:latest'
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'CRITICAL,HIGH'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: 'trivy-results.sarif'
自动更新漏洞数据库
Trivy 在每次扫描时会自动检查数据库更新,也可以主动更新:
trivy image --download-db-only