scanning-docker-images-with-trivy について
Trivy で Docker イメージやコンテナ設定をスキャンする方法を扱うスキルです。コンテナセキュリティや CI/CD の確認を担うチーム向けに、脆弱性などの検出と SBOM 生成を説明します。
主な機能
- 01OS パッケージと依存関係の脆弱性検出
- 02設定ミス・シークレット・ライセンスの確認
- 03重要度による検出結果の絞り込み
- 04CycloneDX または SPDX SBOM の生成
- 05Kubernetes マニフェストと Helm チャートのスキャン
活用シーン
- 01CI/CD パイプラインでのイメージ確認
- 02リリース前のイメージリスク監査
- 03Kubernetes 設定の問題検出
- 04イメージのソフトウェア部品表作成
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill scanning-docker-images-with-trivyインストール後、Claude Code で/scanning-docker-images-with-trivyを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
LICENSEassets/template.mdreferences/api-reference.mdstandards.mdworkflows.mdscripts/agent.pyprocess.py詳細
Trivy による Docker イメージのスキャン
概要
Trivy は、Aqua Security が提供する包括的なオープンソースの脆弱性スキャナーです。コンテナイメージ内の OS パッケージ、言語固有の依存関係、設定ミス、シークレット、ライセンス違反を検出します。CI/CD パイプラインに統合でき、SARIF、CycloneDX、SPDX など、複数の出力形式に対応しています。
使用する場面
- Trivy を使って Docker イメージをスキャンするセキュリティ評価を実施するとき
- 関連するセキュリティインシデントの対応手順に従うとき
- 定期的なセキュリティテストや監査を実施するとき
- 実地テストを通じてセキュリティ制御を検証するとき
前提条件
- Docker Engine 20.10+
- Trivy v0.50+ がインストールされていること
- 脆弱性データベースを更新するためのインターネット接続
- コンテナレジストリの認証情報(プライベートレジストリの場合)
基本概念
スキャナーの種類
| スキャナー | フラグ | 検出対象 |
|---|---|---|
| 脆弱性 | --scanners vuln | OS パッケージとライブラリの CVE |
| 設定ミス | --scanners misconfig | Dockerfile/K8s マニフェストの設定ミス |
| シークレット | --scanners secret | ハードコードされたパスワード、API キー、トークン |
| ライセンス | --scanners license | ソフトウェアライセンスのコンプライアンス上の問題 |
重大度レベル
- CRITICAL:CVSS 9.0-10.0 - 直ちに対応が必要です
- HIGH:CVSS 7.0-8.9 - 本番環境へのデプロイ前に修正してください
- MEDIUM:CVSS 4.0-6.9 - 修正計画を立ててください
- LOW:CVSS 0.1-3.9 - 受容するか、機会を見て修正してください
- UNKNOWN:スコアなし - 手動で評価してください
脆弱性データベース
Trivy は複数の脆弱性データベースを使用します:
- NVD(National Vulnerability Database)
- Red Hat Security Data
- Alpine SecDB
- Debian Security Tracker
- Ubuntu CVE Tracker
- Amazon Linux Security Center
- GitHub Advisory Database
ワークフロー
ステップ 1:Trivy のインストール
# Linux (apt)
sudo apt-get install wget apt-transport-https gnupg lsb-release
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | sudo tee /usr/share/keyrings/trivy.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update && sudo apt-get install trivy
# macOS
brew install trivy
# Docker
docker pull aquasecurity/trivy:latest
ステップ 2:基本的なイメージスキャン
# Scan a public image
trivy image python:3.12-slim
# Scan with severity filter
trivy image --severity CRITICAL,HIGH nginx:latest
# Ignore unfixed vulnerabilities
trivy image --ignore-unfixed alpine:3.19
# Scan local image
docker build -t myapp:latest .
trivy image myapp:latest
# Scan from tar archive
docker save myapp:latest -o myapp.tar
trivy image --input myapp.tar
ステップ 3:高度なスキャンオプション
# All scanners (vuln + misconfig + secret + license)
trivy image --scanners vuln,misconfig,secret,license myapp:latest
# Generate SBOM in CycloneDX format
trivy image --format cyclonedx --output sbom.cdx.json myapp:latest
# Generate SBOM in SPDX format
trivy image --format spdx-json --output sbom.spdx.json myapp:latest
# JSON output for programmatic processing
trivy image --format json --output results.json myapp:latest
# SARIF output for GitHub Security tab
trivy image --format sarif --output results.sarif myapp:latest
# Template-based output
trivy image --format template --template "@contrib/html.tpl" --output report.html myapp:latest
# Scan specific layers only
trivy image --list-all-pkgs myapp:latest
ステップ 4:Kubernetes マニフェストのスキャン
# Scan Dockerfile for misconfigurations
trivy config Dockerfile
# Scan Kubernetes manifests
trivy config k8s-deployment.yaml
# Scan Helm charts
trivy config ./helm-chart/
# Scan Terraform files
trivy config ./terraform/
ステップ 5:CI/CD への統合
# GitHub Actions
name: Trivy Container Scan
on: push
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build 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
output: trivy-results.sarif
severity: CRITICAL,HIGH
exit-code: 1
- name: Upload Trivy scan results
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file: trivy-results.sarif
- name: Generate SBOM
uses: aquasecurity/trivy-action@master
with:
image-ref: myapp:${{ github.sha }}
format: cyclonedx
output: sbom.cdx.json
# GitLab CI
trivy-scan:
stage: security
image:
name: aquasecurity/trivy:latest
entrypoint: [""]
script:
- trivy image --exit-code 1 --severity CRITICAL,HIGH
--format json --output gl-container-scanning-report.json
$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
artifacts:
reports:
container_scanning: gl-container-scanning-report.json
ステップ 6:.trivyignore によるポリシーの適用
# .trivyignore - Ignore specific CVEs with expiry
# Accepted risk: low-impact vulnerability in dev dependency
CVE-2023-12345 exp:2025-06-01
# False positive: not exploitable in our configuration
CVE-2024-67890
# Vendor will not fix
CVE-2023-11111
ステップ 7:プライベートレジストリのイメージをスキャン
# Docker Hub (uses ~/.docker/config.json)
trivy image myregistry.azurecr.io/myapp:latest
# ECR
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <account>.dkr.ecr.us-east-1.amazonaws.com
trivy image <account>.dkr.ecr.us-east-1.amazonaws.com/myapp:latest
# GCR
trivy image gcr.io/my-project/myapp:latest
# With explicit credentials
TRIVY_USERNAME=user TRIVY_PASSWORD=pass trivy image registry.example.com/myapp:latest
検証コマンド
# Verify Trivy installation
trivy version
# Update vulnerability database
trivy image --download-db-only
# Quick scan with table output
trivy image --severity CRITICAL python:3.12
# Verify no CRITICAL vulnerabilities
trivy image --exit-code 1 --severity CRITICAL myapp:latest
echo "Exit code: $?" # 0 = no vulns, 1 = vulns found
参考資料
コメント
Markdown 対応