跳转至

597_Trivy安全扫描

一句话概述:Trivy 是 Aqua Security 开源的全面安全扫描器,能扫描容器镜像、文件系统、Git 仓库、K8s 集群、IaC 配置(Terraform/Dockerfile/Helm)中的漏洞、配置错误和敏感信息泄露,一个工具覆盖整个 DevSecOps 流程。

核心知识点表

概念白话解释
CVECommon Vulnerabilities and Exposures,公开的安全漏洞编号
SBOMSoftware Bill of Materials,软件物料清单(依赖列表)
IaC Scanning基础设施即代码扫描,检查 Terraform/Dockerfile 配置是否安全
Secret Scanning密钥扫描,检测代码中泄露的 API Key、密码等
Misconfiguration配置错误,如 Docker 以 root 运行、K8s Pod 未设资源限制
Severity严重级别:Critical > High > Medium > Low > Unknown
VEXVulnerability Exploitability eXchange,标记漏洞是否真正影响你的应用

安装配置

安装 Trivy

# macOS
brew install trivy                       # Homebrew 安装

# Ubuntu/Debian
sudo apt-get install wget apt-transport-https gnupg
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo gpg --dearmor -o /usr/share/keyrings/trivy.gpg
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb generic main" | sudo tee /etc/apt/sources.list.d/trivy.list
sudo apt update && sudo apt install trivy

# CentOS/RHEL
sudo rpm -ivh https://github.com/aquasecurity/trivy/releases/latest/download/trivy_0.70.0_Linux-64bit.rpm

# Docker(无需安装)
docker run aquasec/trivy image nginx:latest  # 直接用 Docker 运行

# 验证安装
trivy version                            # 查看版本(当前 v0.70.x)

基本使用

容器镜像扫描

# 扫描公共镜像
trivy image nginx:latest                 # 扫描 nginx 最新镜像
# 输出:漏洞列表(CVE 编号、严重级别、已安装版本、修复版本)

# 扫描本地镜像
trivy image my-app:v1                    # 扫描本地 Docker 镜像

# 只显示高危以上漏洞
trivy image --severity HIGH,CRITICAL nginx:latest
# --severity → 过滤严重级别

# 只显示有修复方案的漏洞
trivy image --ignore-unfixed nginx:latest
# --ignore-unfixed → 忽略未修复的漏洞

# 扫描并输出 JSON 格式
trivy image -f json -o results.json nginx:latest
# -f json → JSON 格式
# -o → 输出到文件

# 扫描并设置退出码(CI/CD 中用于阻断流水线)
trivy image --exit-code 1 --severity CRITICAL nginx:latest
# --exit-code 1 → 发现指定级别漏洞时返回非零退出码

文件系统扫描

# 扫描项目目录中的漏洞(检查依赖)
trivy fs .                               # 扫描当前目录
# 支持:package.json, requirements.txt, go.mod, pom.xml 等

# 扫描指定路径
trivy fs /path/to/project                # 扫描指定项目

# 同时扫描漏洞和密钥泄露
trivy fs --scanners vuln,secret .        # 多种扫描器

IaC 配置扫描

# 扫描 Terraform 配置
trivy config ./terraform/               # 扫描 Terraform 目录
# 检查:安全组过于开放、S3 桶未加密、IAM 策略过宽等

# 扫描 Dockerfile
trivy config ./Dockerfile               # 检查 Dockerfile 安全问题
# 检查:以 root 运行、使用 latest 标签、COPY 了敏感文件等

# 扫描 Kubernetes YAML
trivy config ./k8s-manifests/           # 扫描 K8s 配置
# 检查:特权容器、缺少资源限制、挂载了 hostPath 等

# 扫描 Helm Chart
trivy config ./my-chart/                # 扫描 Helm Chart

# 支持的 IaC 类型:
# Terraform | CloudFormation | Dockerfile | Kubernetes
# Helm | Docker Compose | Azure ARM Templates

密钥泄露扫描

# 扫描代码中的密钥泄露
trivy fs --scanners secret .             # 只扫描密钥
# 检测:AWS Key、GitHub Token、数据库密码、私钥等

# 扫描 Git 仓库
trivy repo https://github.com/user/repo  # 扫描远程仓库
trivy repo .                             # 扫描本地仓库

高级用法

CI/CD 集成

# === GitHub Actions ===
# .github/workflows/trivy.yml
name: Security Scan
on: push
jobs:
  scan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: 构建镜像
        run: docker build -t my-app:${{ github.sha }} .

      - name: Trivy 漏洞扫描
        uses: aquasecurity/trivy-action@master
        with:
          image-ref: my-app:${{ github.sha }}  # 要扫描的镜像
          format: table                        # 输出格式
          exit-code: 1                         # 发现漏洞时失败
          severity: CRITICAL,HIGH              # 扫描级别
# === Drone CI / Woodpecker CI ===
steps:
  - name: security-scan
    image: aquasec/trivy                 # 使用 Trivy 镜像
    commands:
      - trivy image --exit-code 1 --severity CRITICAL my-app:latest

Kubernetes 集群扫描

# 扫描整个 K8s 集群的安全状态
trivy k8s --report summary cluster      # 集群安全摘要
# 检查:
# - Pod 安全配置
# - RBAC 权限
# - 网络策略
# - 镜像漏洞

# 扫描特定命名空间
trivy k8s -n production --report all     # 扫描 production 命名空间

# 输出合规性报告
trivy k8s --compliance k8s-cis          # CIS Kubernetes 基准检查
trivy k8s --compliance k8s-nsa          # NSA 加固指南检查

SBOM 生成

# 生成软件物料清单
trivy image --format spdx-json -o sbom.json nginx:latest
# SPDX 格式的 SBOM

trivy image --format cyclonedx -o sbom.xml nginx:latest
# CycloneDX 格式的 SBOM

# 从 SBOM 扫描漏洞
trivy sbom sbom.json                     # 基于 SBOM 扫描

自定义策略

# 使用 .trivyignore 忽略特定漏洞
# .trivyignore
CVE-2023-12345                           # 忽略特定 CVE
CVE-2024-67890                           # 已评估无影响

# 使用自定义 Rego 策略
# custom-policy.rego
package custom

deny[msg] {
    input.kind == "Deployment"
    not input.spec.template.spec.securityContext.runAsNonRoot
    msg := "Deployment 必须以非 root 用户运行"
}

trivy config --policy ./policies/ .      # 使用自定义策略扫描

常见报错

报错信息原因解决方案
FATAL: DB error漏洞数据库下载失败检查网络或使用 --skip-db-update
unable to initialize a scanner扫描器初始化失败更新 Trivy 到最新版本
image not foundDocker 镜像不存在检查镜像名和 Tag
timeout exceeded扫描超时--timeout 10m 增加超时时间
permission denied文件权限不足检查目标目录的读取权限

速查表

# === 扫描目标 ===
trivy image IMAGE              # 容器镜像
trivy fs PATH                  # 文件系统
trivy repo URL                 # Git 仓库
trivy config PATH              # IaC 配置
trivy k8s cluster              # K8s 集群
trivy sbom FILE                # SBOM 文件

# === 常用参数 ===
--severity HIGH,CRITICAL       # 过滤级别
--ignore-unfixed               # 忽略未修复漏洞
--exit-code 1                  # 发现漏洞时非零退出
-f json/table/sarif            # 输出格式
-o FILE                        # 输出到文件
--scanners vuln,secret,misconfig  # 扫描类型
--timeout 10m                  # 超时时间

# === 数据库管理 ===
trivy image --download-db-only # 只下载数据库
trivy image --skip-db-update   # 跳过数据库更新
trivy image --db-repository    # 自定义数据库源

# === 扫描类型 ===
# vuln        → 漏洞扫描
# misconfig   → 配置错误
# secret      → 密钥泄露
# license     → 许可证检查

同类对比

特性TrivyGrypeSnykClair
镜像扫描
IaC 扫描
密钥扫描
K8s 扫描有限
SBOM
开源Apache 2.0Apache 2.0免费增值Apache 2.0
速度
CI/CD 集成优秀优秀一般

选型建议:全面安全扫描首选 Trivy(覆盖最全、完全开源、32k+ Stars);只需镜像漏洞扫描用 Grype(更轻量);企业级安全平台考虑 Snyk(商业功能更丰富)。