scanning-container-images-with-grype について
Anchoreのオープンソース脆弱性スキャナーGrypeで、コンテナイメージ、ファイルシステム、SBOMの既知CVEを調べるスキルです。絞り込み、出力形式、データベース管理、CI/CDでの利用を扱います。
主な機能
- 01イメージ、ファイルシステム、SBOMのスキャン
- 02深刻度と修正状況による絞り込み
- 03JSON・CycloneDX形式での出力
- 04脆弱性の除外ルール設定
- 05CI/CDパイプラインでのスキャン
活用シーン
- 01デプロイ前のコンテナイメージ検査
- 02深刻度のしきい値によるCIビルド失敗
- 03Syftで生成した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、OS 固有のフィードなど、複数の脆弱性データベースと照合します。
使用する場面
- 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 イメージ |
ベストプラクティス
- イメージタグを固定する -
latestではなく、常に特定のダイジェストをスキャンします - 深刻度に基づいて失敗させる - CI のゲートで
--fail-on highまたはcriticalを設定します - SBOM を使用する - 再現可能なスキャンのために Syft で SBOM を生成します
- 誤検知を抑制する - 理由を記録した
.grype.yamlの無視ルールを使用します - すべてのレイヤーをスキャンする - 中間レイヤーの脆弱性を検出するために
--scope all-layersを使用します - データベースの更新を自動化する - CI ランナーの脆弱性データベースを最新の状態に保ちます
- スキャン結果を比較する - 脆弱性の件数を継続的に追跡し、リグレッションを検出します
コメント
Markdown 対応