你的 Docker 镜像里藏着多少已知漏洞?一个基于 ubuntu:20.04 的 Python 镜像,未经扫描往往包含数十个高危 CVE——Log4Shell、OpenSSL 漏洞、glibc 提权……这些漏洞随着应用镜像被部署到香港服务器,成为攻击者的入口。Trivy 和 Grype 是目前最成熟的开源容器安全扫描工具,将其集成到 CI 流水线后,每次构建自动扫描,严重漏洞阻断发布,让安全防线前移到开发阶段。
一、Trivy vs Grype 对比
| 维度 | Trivy(Aqua Security) | Grype(Anchore) |
|---|---|---|
| 扫描目标 | 镜像/文件系统/Git仓库/IaC/SBOM | 镜像/文件系统/SBOM |
| 漏洞数据库 | 多来源(NVD/GHSA/OS厂商) | 多来源(grype-db) |
| 语言支持 | Go/Node/Python/Ruby/Java/PHP等 | Go/Node/Python/Ruby/Java等 |
| Secret扫描 | ✅(内置) | ❌ |
| IaC扫描 | ✅(Terraform/K8s YAML) | ❌ |
| SBOM生成 | ✅(CycloneDX/SPDX) | ✅ |
| 速度 | 快(首次需下载DB) | 快 |
| 推荐用途 | 全面扫描,CI/CD首选 | SBOM工作流,与Syft配合 |
二、安装 Trivy
<code"># 官方安装脚本 curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin # 或 apt 安装 apt install -y apt-transport-https gnupg wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor > /usr/share/keyrings/trivy.gpg echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb generic main" \ > /etc/apt/sources.list.d/trivy.list apt update && apt install -y trivy trivy --version
三、本地扫描(快速上手)
<code"># 扫描本地 Docker 镜像 trivy image myapp:latest # 扫描远程镜像(从 Harbor/Docker Hub 拉取) trivy image registry.yourdomain.com/myapp/backend:v1.0.0 # 只显示高危和严重漏洞(过滤低危噪音) trivy image --severity HIGH,CRITICAL myapp:latest # 示例输出: # Total: 12 (HIGH: 8, CRITICAL: 4) # # ┌──────────────┬────────────────┬──────────┬───────────────────┬─────────────────────────────────────┐ # │ Library │ Vulnerability │ Severity │ Installed Version │ Title │ # ├──────────────┼────────────────┼──────────┼───────────────────┼─────────────────────────────────────┤ # │ openssl │ CVE-2024-5535 │ CRITICAL │ 3.0.2-0ubuntu1.14 │ OpenSSL: SSL_select_next_proto buf.. │ # │ libpython3.1 │ CVE-2024-6923 │ HIGH │ 3.10.12-1~22.04.5 │ python: email header injection │ # └──────────────┴────────────────┴──────────┴───────────────────┴─────────────────────────────────────┘
<code"># 扫描文件系统(扫描项目目录,检测依赖漏洞) trivy fs /opt/myapp/ # 扫描 Git 仓库(含历史提交中的 Secret) trivy repo https://github.com/yourorg/yourrepo # 扫描 Terraform 配置(IaC 安全) trivy config /opt/terraform/ # 生成 SBOM(软件物料清单) trivy image --format cyclonedx --output sbom.json myapp:latest
四、CI/CD 集成(GitHub Actions)
<code"># .github/workflows/security-scan.yml
name: Container Security Scan
on:
push:
branches: [main]
pull_request:
jobs:
build-and-scan:
runs-on: ubuntu-latest
permissions:
security-events: write # 写入 Security Dashboard 需要此权限
steps:
- uses: actions/checkout@v4
- name: Build Docker image
run: docker build -t myapp:${{ github.sha }} .
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: myapp:${{ github.sha }}
format: sarif # SARIF 格式,可上传到 GitHub Security
output: trivy-results.sarif
severity: HIGH,CRITICAL
exit-code: '1' # 发现高危漏洞时流水线失败
- name: Upload Trivy scan results to GitHub Security
uses: github/codeql-action/upload-sarif@v3
if: always() # 即使上一步失败也上传结果
with:
sarif_file: trivy-results.sarif
# 如果扫描通过,继续推送镜像
- name: Push image to registry
if: success()
run: |
docker tag myapp:${{ github.sha }} registry.yourdomain.com/myapp:${{ github.sha }}
docker push registry.yourdomain.com/myapp:${{ github.sha }}
五、Woodpecker CI 集成
<code"># .woodpecker.yml(适用于 Gitea + Woodpecker CI)
steps:
build:
image: docker:24
volumes:
- /var/run/docker.sock:/var/run/docker.sock
commands:
- docker build -t myapp:${CI_COMMIT_SHA} .
security-scan:
image: aquasec/trivy:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /tmp/trivy-cache:/root/.cache/trivy # 缓存漏洞数据库
commands:
# 更新漏洞数据库(缓存后后续扫描很快)
- trivy image --download-db-only
# 扫描并在发现严重漏洞时失败
- |
trivy image \
--severity HIGH,CRITICAL \
--exit-code 1 \
--no-progress \
--format json \
--output /tmp/trivy-report.json \
myapp:${CI_COMMIT_SHA}
- cat /tmp/trivy-report.json | python3 -c "
import json,sys
data = json.load(sys.stdin)
total = sum(len(r.get('Vulnerabilities', [])) for r in data.get('Results', []) if r.get('Vulnerabilities'))
print(f'发现漏洞总数: {total}')
"
when:
event: push
deploy:
# 仅在安全扫描通过后执行部署
depends_on: [security-scan]
image: appleboy/drone-ssh
# ... 部署配置 ...
六、漏洞忽略配置(处理误报和已知可接受风险)
<code"># .trivyignore(放在项目根目录) # 格式:CVE编号 [过期时间] [备注] # 已确认不影响我们使用场景的漏洞 CVE-2023-1234 until:2026-12-31 reason:不使用该功能,已评估无风险 # 等待上游修复的漏洞(临时忽略) CVE-2024-5678 until:2025-06-01 reason:上游已发布修复,等待基础镜像更新 # 使用 .trivyignore 扫描 trivy image --ignorefile .trivyignore myapp:latest
<code"># 漏洞处理决策流程 # # 发现 CRITICAL 漏洞: # 1. 查看是否影响实际代码路径(很多漏洞在容器内不可触达) # 2. 检查是否有修复版本(trivy 会显示 Fixed Version) # 3. 如有修复:升级基础镜像或依赖版本 # 4. 如无修复:评估风险,写注释到 .trivyignore # # 升级基础镜像示例: # FROM python:3.10-slim ← 可能含漏洞 # 改为: # FROM python:3.12-slim ← 更新版本包含安全修复
七、Harbor 集成 Trivy(镜像仓库级扫描)
<code"># Harbor 内置 Trivy 扫描(在第五阶段 Harbor 文章中已涉及) # 在 Harbor 管理后台配置: # 系统管理 → 审查服务 → 添加 Trivy # 配置策略:推送时自动扫描 # 项目 → 配置 → 自动扫描:启用 # 阻止漏洞镜像:严重性 >= HIGH → 阻止拉取 # CI/CD 流水线推送到 Harbor 后, # Harbor 自动扫描,扫描结果在 Harbor UI 可视化查看 # 拉取镜像时检查是否通过扫描
八、安装 Grype(补充 SBOM 工作流)
<code"># 安装 Grype curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/bin grype version # 安装 Syft(生成 SBOM) curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin # SBOM 工作流: # 1. 生成 SBOM syft myapp:latest -o cyclonedx-json > sbom.json # 2. 用 Grype 扫描 SBOM(更快,无需访问容器层) grype sbom:./sbom.json --fail-on high # 优势:SBOM 可以存档,随时重新扫描历史版本
九、总结
将 Trivy 集成到 CI/CD 流水线是成本最低、效果最明显的容器安全实践:每次代码推送自动扫描,严重漏洞阻断发布,让安全问题在开发阶段就被发现和修复,而不是等到部署到香港服务器后被攻击者利用。配合 Harbor 的仓库级扫描策略,形成「构建时扫描 + 推送时扫描 + 拉取时检查」的多层防线,大幅降低已知漏洞被利用的风险。
