关于 scanning-container-images-with-grype
介绍如何使用 Anchore 的开源漏洞扫描器 Grype,检查容器镜像、文件系统和 SBOM 中的已知 CVE,并涵盖筛选、输出格式、数据库管理及 CI/CD 用法。
主要功能
- 01扫描镜像、文件系统和 SBOM
- 02按严重性与修复状态筛选
- 03导出 JSON 和 CycloneDX 结果
- 04配置漏洞忽略规则
- 05在 CI/CD 流水线中运行扫描
适用场景
- 01部署前检查容器镜像
- 02按严重性阈值阻止 CI 构建
- 03扫描使用 Syft 生成的 SBOM
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill scanning-container-images-with-grype安装后在 Claude Code 中输入/scanning-container-images-with-grype确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENSEassets/template.mdreferences/api-reference.mdstandards.mdworkflows.mdscripts/agent.pyprocess.py详细介绍
使用 Grype 扫描容器镜像
概述
Grype 是 Anchore 推出的一款开源漏洞扫描器,可检查容器镜像、文件系统和 SBOM 中的已知 CVE。它利用 Syft 生成的 SBOM,将软件包与多个漏洞数据库进行匹配,包括 NVD、GitHub Advisories 以及各操作系统专属的漏洞源。
适用场景
- 开展涉及使用 grype 扫描容器镜像的安全评估时
- 遵循相关安全事件的事件响应流程时
- 执行计划内的安全测试或审计活动时
- 通过动手测试验证安全控制措施时
前提条件
- 已安装 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
# Install via script
curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/bin
# Verify installation
grype version
# Install via Homebrew (macOS/Linux)
brew install grype
扫描容器镜像
# Scan a Docker Hub image
grype nginx:latest
# Scan from Docker daemon
grype docker:myapp:1.0
# Scan a local archive
grype docker-archive:image.tar
# Scan an OCI directory
grype oci-dir:path/to/oci/
# Scan a Singularity image
grype sif:image.sif
# Scan a local directory / filesystem
grype dir:/path/to/project
输出格式
# Default table output
grype alpine:3.18
# JSON output for pipeline processing
grype alpine:3.18 -o json > results.json
# CycloneDX SBOM output
grype alpine:3.18 -o cyclonedx
# SARIF output for GitHub Security tab
grype alpine:3.18 -o sarif > grype.sarif
# Template-based custom output
grype alpine:3.18 -o template -t /path/to/template.tmpl
过滤与阈值
# Fail if vulnerabilities meet or exceed a severity
grype nginx:latest --fail-on critical
# Show only fixed vulnerabilities
grype nginx:latest --only-fixed
# Show only non-fixed vulnerabilities
grype nginx:latest --only-notfixed
# Filter by severity
grype nginx:latest --only-fixed -o json | jq '[.matches[] | select(.vulnerability.severity == "High")]'
# Explain a specific CVE
grype nginx:latest --explain --id CVE-2024-1234
使用 SBOM
# Generate SBOM with Syft then scan
syft nginx:latest -o spdx-json > nginx-sbom.json
grype sbom:nginx-sbom.json
# Scan CycloneDX SBOM
grype sbom:bom.json
配置文件(.grype.yaml)
# .grype.yaml
check-for-app-update: false
fail-on-severity: "high"
output: "json"
scope: "squashed" # or "all-layers"
quiet: false
ignore:
- vulnerability: CVE-2023-12345
reason: "False positive - not exploitable in our context"
- vulnerability: CVE-2023-67890
fix-state: unknown
db:
auto-update: true
cache-dir: "/tmp/grype-db"
max-allowed-built-age: 120h # 5 days
match:
java:
using-cpes: true
python:
using-cpes: true
javascript:
using-cpes: false
CI/CD 集成
# GitHub Actions
- name: Scan image with Grype
uses: anchore/scan-action@v4
with:
image: "myregistry/myapp:${{ github.sha }}"
fail-build: true
severity-cutoff: high
output-format: sarif
id: scan
- name: Upload SARIF
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: ${{ steps.scan.outputs.sarif }}
# GitLab CI
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
数据库管理
# Check database status
grype db status
# Manually update vulnerability database
grype db update
# Delete cached database
grype db delete
# List supported database providers
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 镜像 |
最佳实践
- 固定镜像标签 - 始终扫描特定的 digest,而不是
latest - 按严重程度失败 - 在 CI 门禁中设置
--fail-on high或critical - 使用 SBOM - 使用 Syft 生成 SBOM,以实现可重复的扫描
- 抑制误报 - 使用
.grype.yaml忽略规则,并记录原因 - 扫描所有层 - 使用
--scope all-layers以捕获中间层中的漏洞 - 自动更新数据库 - 在 CI 运行器上保持漏洞数据库为最新
- 对比扫描结果 - 随时间跟踪漏洞数量,以检测回归
评论
支持 Markdown