| name | scanning-container-images-with-grype |
| description | 使用 Anchore Grype 扫描容器镜像的已知漏洞(Vulnerability),支持基于 SBOM 的匹配和可配置的严重性阈值。 |
| domain | cybersecurity |
| subdomain | container-security |
| tags | ["grype","vulnerability-scanning","container-security","sbom","anchore","supply-chain"] |
| version | 1.0 |
| author | mahipal |
| license | Apache-2.0 |
使用 Grype 扫描容器镜像
概述
Grype 是 Anchore 开源的漏洞扫描器,用于检查容器镜像、文件系统和 SBOM 中的已知 CVE。它利用 Syft 生成的 SBOM,将软件包与多个漏洞数据库进行匹配,包括 NVD、GitHub Advisory 和特定操作系统的漏洞数据源。
前置条件
- 已安装 Docker 或 Podman
- 已安装 Grype CLI(
curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/bin)
- Syft CLI(可选,用于生成 SBOM)
- 网络访问权限(用于拉取漏洞数据库)
核心命令
安装 Grype
curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/bin
grype version
brew install grype
扫描容器镜像
grype nginx:latest
grype docker:myapp:1.0
grype docker-archive:image.tar
grype oci-dir:path/to/oci/
grype sif:image.sif
grype dir:/path/to/project
输出格式
grype alpine:3.18
grype alpine:3.18 -o json > results.json
grype alpine:3.18 -o cyclonedx
grype alpine:3.18 -o sarif > grype.sarif
grype alpine:3.18 -o template -t /path/to/template.tmpl
过滤与阈值
grype nginx:latest --fail-on critical
grype nginx:latest --only-fixed
grype nginx:latest --only-notfixed
grype nginx:latest --only-fixed -o json | jq '[.matches[] | select(.vulnerability.severity == "High")]'
grype nginx:latest --explain --id CVE-2024-1234
使用 SBOM
syft nginx:latest -o spdx-json > nginx-sbom.json
grype sbom:nginx-sbom.json
grype sbom:bom.json
配置文件(.grype.yaml)
check-for-app-update: false
fail-on-severity: "high"
output: "json"
scope: "squashed"
quiet: false
ignore:
- vulnerability: CVE-2023-12345
reason: "误报 - 在我们的环境中不可利用"
- vulnerability: CVE-2023-67890
fix-state: unknown
db:
auto-update: true
cache-dir: "/tmp/grype-db"
max-allowed-built-age: 120h
match:
java:
using-cpes: true
python:
using-cpes: true
javascript:
using-cpes: false
CI/CD 集成
- name: 使用 Grype 扫描镜像
uses: anchore/scan-action@v4
with:
image: "myregistry/myapp:${{ github.sha }}"
fail-build: true
severity-cutoff: high
output-format: sarif
id: scan
- name: 上传 SARIF
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: ${{ steps.scan.outputs.sarif }}
container_scan:
stage: test
image: anchore/grype:latest
script:
- grype ${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHA} --fail-on high -o json > grype-report.json
artifacts:
reports:
container_scanning: grype-report.json
数据库管理
grype db status
grype db update
grype db delete
grype db list
关键漏洞数据源
| 数据源 | 覆盖范围 |
|---|
| NVD | 所有生态系统的 CVE |
| GitHub Advisories | 开源软件包漏洞 |
| Alpine SecDB | Alpine Linux 软件包 |
| Amazon Linux ALAS | Amazon Linux AMI |
| Debian Security Tracker | Debian 软件包 |
| Red Hat OVAL | RHEL、CentOS |
| Ubuntu Security | Ubuntu 软件包 |
| Wolfi SecDB | Wolfi/Chainguard 镜像 |
最佳实践
- 固定镜像标签 - 始终扫描特定摘要,而非
latest
- 设置严重性失败阈值 - 在 CI 门控中设置
--fail-on high 或 critical
- 使用 SBOM - 用 Syft 生成 SBOM 以实现可复现的扫描
- 抑制误报 - 使用带记录原因的
.grype.yaml 忽略规则
- 扫描所有层 - 使用
--scope all-layers 捕获中间层中的漏洞
- 自动化数据库更新 - 在 CI runner 中保持漏洞数据库为最新
- 比较扫描结果 - 随时间追踪漏洞数量变化以检测回归