使用 Grype 掃描容器映像和檔案系統中的漏洞,整合 CI/CD 自動化安全檢測
專案簡介
Grype 是 Anchore 開發的容器映像漏洞掃描工具,可掃描容器映像、檔案系統、SBOM 中的已知漏洞。與 Syft 整合使用,是容器安全的重要工具。
GitHub Stars: 11K+
主要功能
- 容器掃描 - 掃描 Docker/OCI 映像
- 檔案系統 - 掃描本地目錄
- SBOM 掃描 - 掃描 SBOM 檔案
- 多資料庫 - CVE、NVD、GitHub Advisory
- CI/CD 整合 - 易於自動化
安裝
快速安裝
1
| curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/bin
|
Homebrew
Docker
1
| docker run --rm anchore/grype:latest --help
|
基本使用
掃描容器映像
1
2
3
4
5
6
7
8
| # 從 Registry
grype nginx:latest
# 從本地 Docker
grype docker:nginx:latest
# 從 tar 檔案
grype docker-archive:image.tar
|
掃描目錄
1
| grype dir:/path/to/project
|
掃描 SBOM
1
2
3
4
5
| # 先產生 SBOM
syft nginx:latest -o json > sbom.json
# 掃描 SBOM
grype sbom:sbom.json
|
輸出格式
表格(預設)
輸出範例:
1
2
3
| NAME INSTALLED FIXED-IN TYPE VULNERABILITY SEVERITY
libssl3 3.0.2 3.0.7 deb CVE-2022-3786 High
zlib1g 1:1.2.11 1:1.2.13 deb CVE-2022-37434 Critical
|
JSON
1
| grype nginx:latest -o json > results.json
|
SARIF
1
| grype nginx:latest -o sarif > results.sarif
|
CycloneDX
1
| grype nginx:latest -o cyclonedx > results.xml
|
過濾和忽略
嚴重程度過濾
1
2
| # 只顯示 High 和 Critical
grype nginx:latest --only-fixed --fail-on high
|
忽略規則
1
2
3
4
5
6
7
8
| # .grype.yaml
ignore:
- vulnerability: CVE-2022-12345
- vulnerability: CVE-2022-67890
fix-state: not-fixed
- package:
name: openssl
type: deb
|
使用忽略檔
1
| grype nginx:latest --config .grype.yaml
|
CI/CD 整合
GitHub Actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| name: Security 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: Scan Image
uses: anchore/scan-action@v3
with:
image: myapp:${{ github.sha }}
fail-build: true
severity-cutoff: high
- name: Upload SARIF
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: results.sarif
|
GitLab CI
1
2
3
4
5
6
7
8
| security-scan:
stage: security
image: anchore/grype:latest
script:
- grype $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --fail-on high -o json > grype-report.json
artifacts:
paths:
- grype-report.json
|
Jenkins
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| pipeline {
agent any
stages {
stage('Security Scan') {
steps {
sh '''
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
anchore/grype:latest \
docker:myapp:latest \
--fail-on high
'''
}
}
}
}
|
與 Syft 整合
產生和掃描 SBOM
1
2
3
4
5
6
7
8
| # 產生 SBOM
syft nginx:latest -o spdx-json > sbom.json
# 掃描 SBOM
grype sbom:sbom.json
# 或一行完成
syft nginx:latest -o json | grype
|
工作流程
1
| Image → Syft → SBOM → Grype → Vulnerabilities
|
資料庫管理
更新資料庫
檢查狀態
資料庫位置
1
2
3
4
5
| # 預設
~/.cache/grype/db
# 自訂
export GRYPE_DB_CACHE_DIR=/custom/path
|
進階設定
設定檔
1
2
3
4
5
6
7
8
9
10
11
12
13
| # .grype.yaml
check-for-app-update: false
fail-on-severity: high
only-fixed: true
scope: all-layers
output: json
db:
auto-update: true
cache-dir: /custom/cache
log:
level: warn
|
掃描範圍
1
2
3
4
5
| # 所有層(預設)
grype nginx:latest --scope all-layers
# 只掃描最終層
grype nginx:latest --scope squashed
|
漏洞資料來源
支援的資料庫
| 來源 | 涵蓋範圍 |
|---|
| Alpine SecDB | Alpine 套件 |
| Amazon Linux Security | Amazon Linux |
| Debian Security | Debian 套件 |
| GitHub Advisories | 各語言套件 |
| NVD | 全面 CVE |
| Red Hat Security | RHEL/CentOS |
| Ubuntu Security | Ubuntu 套件 |
最佳實踐
建置流程整合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| #!/bin/bash
set -e
# 建置映像
docker build -t myapp:latest .
# 產生 SBOM
syft myapp:latest -o spdx-json > sbom.json
# 掃描漏洞
grype sbom:sbom.json --fail-on high
# 通過後推送
docker push myapp:latest
|
定期掃描
1
2
| # Cron job
0 0 * * * grype docker:production-app:latest --fail-on critical -o json >> /var/log/grype-scans.log
|
相關連結
延伸閱讀