Scans Docker images with Trivy for vulnerabilities in OS packages/dependencies, misconfigurations, secrets, and licenses. Supports SARIF/CycloneDX/SPDX outputs for CI/CD integration.
npx claudepluginhub killvxk/cybersecurity-skills-zhThis skill uses the workspace's default tool permissions.
Trivy 是 Aqua Security 开源的综合性漏洞扫描器,用于检测容器镜像中操作系统软件包、语言特定依赖项的漏洞、错误配置、密钥和许可证违规。它可集成到 CI/CD 流水线,支持 SARIF、CycloneDX 和 SPDX 等多种输出格式。
Scans Docker images with Trivy for vulnerabilities, misconfigurations, secrets, and licenses in OS packages and dependencies. Includes CI/CD integration and severity filtering.
Scans Docker images with Trivy for vulnerabilities in OS packages, dependencies, misconfigurations, secrets, and licenses. Useful for CI/CD pipelines, security audits, and container assessments.
Integrates Trivy scanner into GitHub Actions and GitLab CI/CD pipelines to detect vulnerabilities in Docker images, OS packages, app dependencies, Dockerfile misconfigurations, filesystems, Git repos, and enforce severity-based quality gates.
Share bugs, ideas, or general feedback.
Trivy 是 Aqua Security 开源的综合性漏洞扫描器,用于检测容器镜像中操作系统软件包、语言特定依赖项的漏洞、错误配置、密钥和许可证违规。它可集成到 CI/CD 流水线,支持 SARIF、CycloneDX 和 SPDX 等多种输出格式。
| 扫描器 | 参数 | 检测内容 |
|---|---|---|
| 漏洞扫描 | --scanners vuln | 操作系统软件包和库中的 CVE |
| 错误配置扫描 | --scanners misconfig | Dockerfile/K8s 清单错误配置 |
| 密钥扫描 | --scanners secret | 硬编码密码、API 密钥、令牌 |
| 许可证扫描 | --scanners license | 软件许可证合规问题 |
Trivy 使用多个漏洞数据库:
# Linux(apt)
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
# macOS
brew install trivy
# Docker
docker pull aquasecurity/trivy:latest
# 扫描公共镜像
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
# 从 tar 存档扫描
docker save myapp:latest -o myapp.tar
trivy image --input myapp.tar
# 启用所有扫描器(漏洞+错误配置+密钥+许可证)
trivy image --scanners vuln,misconfig,secret,license myapp:latest
# 生成 CycloneDX 格式 SBOM
trivy image --format cyclonedx --output sbom.cdx.json myapp:latest
# 生成 SPDX 格式 SBOM
trivy image --format spdx-json --output sbom.spdx.json myapp:latest
# JSON 格式输出(用于编程处理)
trivy image --format json --output results.json myapp:latest
# SARIF 格式输出(用于 GitHub Security 标签页)
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
# 扫描 Dockerfile 中的错误配置
trivy config Dockerfile
# 扫描 Kubernetes 清单
trivy config k8s-deployment.yaml
# 扫描 Helm chart
trivy config ./helm-chart/
# 扫描 Terraform 文件
trivy config ./terraform/
# GitHub Actions
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-results.sarif
- name: 生成 SBOM
uses: aquasecurity/trivy-action@master
with:
image-ref: myapp:${{ github.sha }}
format: cyclonedx
output: sbom.cdx.json
# GitLab CI
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
# .trivyignore - 忽略特定 CVE(可设置过期时间)
# 已接受风险:开发依赖中的低影响漏洞
CVE-2023-12345 exp:2025-06-01
# 误报:在我们的配置中不可利用
CVE-2024-67890
# 供应商不予修复
CVE-2023-11111
# Docker Hub(使用 ~/.docker/config.json)
trivy image myregistry.azurecr.io/myapp:latest
# ECR
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
# GCR
trivy image gcr.io/my-project/myapp:latest
# 使用显式凭据
TRIVY_USERNAME=user TRIVY_PASSWORD=pass trivy image registry.example.com/myapp:latest
# 验证 Trivy 安装
trivy version
# 更新漏洞数据库
trivy image --download-db-only
# 快速扫描(表格输出)
trivy image --severity CRITICAL python:3.12
# 验证无严重漏洞
trivy image --exit-code 1 --severity CRITICAL myapp:latest
echo "退出码:$?" # 0 = 无漏洞,1 = 发现漏洞