L’interface est localisée ; les présentations et documents des Skills sont traduits automatiquement par IA dans votre langue. Le code, les commandes, chemins et termes techniques restent en version originale, et le document de l’auteur fait foi.
Explorer les Skills
Déploiement et opérations · SKILL.md

scanning-docker-images-with-trivy

scanning-docker-images-with-trivy

Trivy 是 Aqua Security 开源的综合性漏洞扫描器,用于检测容器镜像中操作系统软件包、语言特定依赖项的漏洞、错误配置、密钥和许可证违规,并集成到 CI/CD 流水线,支持 SARIF、CycloneDX 和 SPDX 等多种输出格式。

Independent GitHub snapshot · Automated classification · Original documentation governs

Documentation de la Skill

GitHub

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 misconfigDockerfile/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

# 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

步骤 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

# 从 tar 存档扫描
docker save myapp:latest -o myapp.tar
trivy image --input myapp.tar

步骤 3:高级扫描选项

# 启用所有扫描器(漏洞+错误配置+密钥+许可证)
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

步骤 4:扫描 Kubernetes 清单

# 扫描 Dockerfile 中的错误配置
trivy config Dockerfile

# 扫描 Kubernetes 清单
trivy config k8s-deployment.yaml

# 扫描 Helm chart
trivy config ./helm-chart/

# 扫描 Terraform 文件
trivy config ./terraform/

步骤 5:CI/CD 集成

# 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

步骤 6:使用 .trivyignore 执行策略

# .trivyignore - 忽略特定 CVE(可设置过期时间)
# 已接受风险:开发依赖中的低影响漏洞
CVE-2023-12345 exp:2025-06-01

# 误报:在我们的配置中不可利用
CVE-2024-67890

# 供应商不予修复
CVE-2023-11111

步骤 7:扫描私有仓库镜像

# 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 = 发现漏洞

参考资料

Skills enregistrées