Container 漏洞掃描實戰:Trivy + Grype 全面檢測教學

# Container 漏洞掃描實戰:Trivy + Grype 全面檢測教學 Container 化部署已經成為現代 IT 基礎設施嘅標準,但你有冇諗過——你啲 Docker image 入面可能藏住幾十個已知漏洞?今日就同大家實戰示範兩大神器 Trivy 同 Grype,教你點樣 5 分鐘內掃描全公司 container image,將安全漏洞一網打盡。 ## Container 漏洞掃描:點...

Container 漏洞掃描實戰:Trivy + Grype 全面檢測教學 - 文章重點速覽 infographic

# Container 漏洞掃描實戰:Trivy + Grype 全面檢測教學

Container 化部署已經成為現代 IT 基礎設施嘅標準,但你有冇諗過——你啲 Docker image 入面可能藏住幾十個已知漏洞?今日就同大家實戰示範兩大神器 Trivy 同 Grype,教你點樣 5 分鐘內掃描全公司 container image,將安全漏洞一網打盡。

## Container 漏洞掃描:點解咁重要?

根據 Sysdig 2026 年容器安全報告,超過 87% 嘅生產環境 container image 包含至少一個 Critical 或 High severity 漏洞。最恐怖嘅係——呢啲漏洞唔係嚟自你寫嘅 code,而係嚟自 base image(例如 Ubuntu、Alpine、Node.js 官方 image)入面嘅系統套件。

傳統嘅安全掃描工具(例如 Nessus、Qualys)主要針對 VM 同 bare metal,對 container 嘅短暫生命週期同 immutable 特性支援有限。Container 漏洞掃描工具就專為呢個場景設計——佢哋直接分析 image layer,對照 CVE database,幾秒鐘就出到完整報告。

## Container 漏洞掃描:Trivy 實戰

Trivy 係 Aqua Security 開發嘅開源漏洞掃描器,支援 container image、filesystem、Git repository,甚至 Kubernetes cluster。安裝超簡單:

# macOS
brew install aquasecurity/trivy/trivy

# Linux (curl)
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh

# 或者直接用 Docker 行
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy image nginx:latest

基本掃描一個 image:

# 掃描本地 Docker image
trivy image nginx:latest

# 掃描 remote registry image(唔使 pull)
trivy image registry.example.com/myapp:v1.2.3

# 只顯示 Critical + High severity
trivy image --severity CRITICAL,HIGH nginx:latest

# 輸出 JSON 格式(方便 CI/CD 整合)
trivy image --format json --output result.json nginx:latest

Trivy 嘅輸出會清楚列出每個漏洞嘅 CVE ID、severity、affected package、fixed version,仲會幫你判斷有冇 fix available。呢個功能超實用——有 fix 嘅漏洞你可以直接 upgrade package,冇 fix 嘅就要評估風險同考慮 workaround。

## Container 漏洞掃描:Grype 實戰

Grype 係 Anchore 開發嘅另一個開源漏洞掃描器,特色係速度快、輸出靚、同 Syft(SBOM 生成工具)完美整合:

# macOS
brew install anchore/grype/grype

# Linux
curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh

基本用法:

# 掃描 image
grype nginx:latest

# 掃描 SBOM file(先用 Syft 生成)
syft nginx:latest -o json > nginx.sbom.json
grype sbom:nginx.sbom.json

# 只顯示 Critical + High
grype --fail-on critical nginx:latest

# JSON 輸出
grype -o json nginx:latest > grype_result.json

Grype 嘅 terminal output 特別靚——有顏色分 severity、表格形式顯示,一眼就睇到邊個漏洞最嚴重。而且 Grype 用嘅 vulnerability database 係佢自己 curated,有時會包埋 Trivy 未收錄嘅漏洞。

## Container 漏洞掃描:Trivy vs Grype 點揀?

| 特性 | Trivy | Grype |
|——|——-|——-|
| 掃描速度 | 快(~10s/image) | 更快(~5s/image) |
| Database 來源 | 多源(NVD, Red Hat, Debian 等) | Anchore curated |
| SBOM 整合 | 內建 CycloneDX/SPDX 輸出 | 配合 Syft 使用 |
| K8s 掃描 | ✅ 原生支援 | ❌ 需要額外工具 |
| CI/CD 整合 | GitHub Action, Jenkins plugin | GitHub Action |
| License 掃描 | ✅ | ❌ |

**建議:** CI/CD pipeline 用 Trivy(功能全面 + K8s 支援),開發階段快速檢查用 Grype(速度更快 + output 靚)。兩個一齊用仲好——互相補足 database coverage。

## Container 漏洞掃描:CI/CD Pipeline 整合

將 Trivy 整合入 GitHub Actions:

name: Container Security Scan
on:
  push:
    branches: [main]
jobs:
  trivy-scan:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Build image
        run: docker build -t myapp:${{ github.sha }} .
      - name: Trivy scan
        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 SARIF
        uses: github/codeql-action/upload-sarif@v3
        with:
          sarif_file: trivy-results.sarif

呢個 workflow 會喺每次 push 自動 build image → Trivy 掃描 → 有 Critical/High 漏洞就 fail build → SARIF report upload 去 GitHub Security tab。真正做到「Shift Left Security」——漏洞喺 CI/CD 階段就攔截,唔會流入 production。

## Container 漏洞掃描:定時掃描 Production Image

就算 CI/CD 攔截咗,production 環境入面已經 deploy 咗嘅 image 都要定時掃描——因為新 CVE 每日都出,今日安全嘅 image 聽日可能就有新漏洞。

#!/bin/bash
# 每日凌晨掃描所有 production image
IMAGES=$(kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.spec.containers[*].image}{"\n"}{end}' | sort -u)

for img in $IMAGES; do
  echo "=== Scanning: $img ==="
  trivy image --severity CRITICAL,HIGH --no-progress "$img"
done

將呢個 script 放喺 cron job,每日自動 scan 一次,有新 Critical 漏洞就 send alert 去 Slack/Teams。咁樣先係真正嘅持續安全監控。

## 總結

Container 漏洞掃描係 DevSecOps 入面最基本但最重要嘅一環。Trivy 同 Grype 兩個工具都係免費開源、安裝簡單、速度快,冇藉口唔用。由今日開始,將 container scanning 加入你嘅 CI/CD pipeline 同 production monitoring,將漏洞攔截喺源頭。

> 📌 **延伸閲讀:** 想深入學 Docker 安全?睇返我哋之前嘅「Docker Container 安全加固 5 步教學」同「K8s 安全設定實戰指南」,由 image scanning 到 runtime protection 一條龍 coverage。

#Container安全 #Trivy #Grype #DevSecOps #漏洞掃描