資安週報Security Weekly 攻擊手法通報 × 資安工具Advisories × Tooling
供應鏈安全Supply chain Apache-2.0

Trivy

一支指令掃遍容器映像、原始碼相依套件、IaC 設定與密鑰外洩。One command to scan container images, code dependencies, IaC configs, and leaked secrets.

這是什麼What it is

Trivy 的定位是「開發者第一個裝的掃描器」:不需要資料庫伺服器、不需要註冊帳號,trivy image nginx:latest 就會列出該映像裡所有已知漏洞的套件與修補版本。

除了容器映像,它同時支援掃描 Git repo 的相依檔(package-lock.jsongo.sumrequirements.txt 等)、Terraform/Kubernetes/Dockerfile 的設定風險,以及誤commit 的 API 金鑰。對小團隊來說,這一支就能取代原本要三四種工具才能做的事。

它也能產生 SBOM(CycloneDX/SPDX 格式),在供應鏈稽核要求日益普遍的情況下相當實用。

Trivy positions itself as the first scanner a developer installs: no database server, no account, just trivy image nginx:latest to list every known-vulnerable package in that image along with the fixed version.

Beyond images it scans repository manifests (package-lock.json, go.sum, requirements.txt, …), misconfigurations in Terraform / Kubernetes / Dockerfiles, and accidentally committed API keys. For a small team it replaces what used to take three or four separate tools.

It also emits SBOMs in CycloneDX and SPDX formats, which matters increasingly as supply-chain audits become standard.

什麼時候用得上When to use it

- CI pipeline 卡關:用 --exit-code 1 --severity CRITICAL,HIGH 讓有重大漏洞的映像無法進到 registry。
- 上線前的相依盤點:出現重大套件漏洞(例如某個廣泛使用的 npm 套件被投毒)時,快速確認自家專案有沒有中招。
- IaC 審查:掃 Terraform 找出對外開放的 security group、未加密的儲存桶。

第一次跑會下載漏洞資料庫,之後會快取。CI 環境建議把 ~/.cache/trivy 一起快取起來。

- CI gating: --exit-code 1 --severity CRITICAL,HIGH stops vulnerable images from reaching your registry.
- Dependency triage: when a widely used package is compromised, check in seconds whether your projects pull it in.
- IaC review: scan Terraform for world-open security groups and unencrypted buckets.

The first run downloads the vulnerability database and caches it — cache ~/.cache/trivy in CI.

注意事項Caveats

回報的是「已知漏洞的套件版本」,不代表該漏洞在你的程式路徑中真的可被觸發。大型專案第一次掃常會有數百筆結果,建議先以 --severity CRITICAL,HIGH 加上 --ignore-unfixed 收斂到可行動的範圍。

It reports vulnerable package versions, not whether the flaw is reachable in your code path. A first scan of a large project often returns hundreds of findings — narrow to something actionable with --severity CRITICAL,HIGH --ignore-unfixed.

安裝Install

brew install trivy
# 或 / or
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy

快速上手Quick start

# 掃描容器映像
trivy image nginx:1.25

# 掃描專案原始碼:相依套件 + 設定 + 密鑰
trivy fs --scanners vuln,misconfig,secret .

# CI:有 CRITICAL/HIGH 且已有修補版本就讓建置失敗
trivy image --exit-code 1 --severity CRITICAL,HIGH --ignore-unfixed myapp:latest

# 產生 SBOM
trivy image --format cyclonedx --output sbom.json myapp:latest

出現在Featured in