Trivy 扫描 IaC 与镜像安全
scanning-iac-and-images-with-trivy
使用 Trivy 的 vuln、misconfig、secret 和 license 扫描器扫描容器镜像、基础设施即代码(Terraform、CloudFormation、Kubernetes 清单、Dockerfile、Helm)、文件系统、git 仓库和 SBOM。在构建 CI/CD 安全门控时使用,可在推送前扫描镜像、apply 前扫描 IaC、扫描 SBOM 以发现供应链弱点,并在违反策略时使构建失败。
- License 许可
- Apache-2.0
- 最近更新
- 2026-08-31
- Stars
- ★ 3.6w
- Forks
- 4.4k
01 前言 Front Matter
- name
- scanning-iac-and-images-with-trivy
- tags
- [ "devsecops", "trivy", "container-security", "vulnerability-scanning", "iac-security", "sbom", "ci-cd", "misconfiguration" ]
- author
- mahipal
- domain
- cybersecurity
- license
- Apache-2.0
- version
- 1.0
- nist_csf
- [ "ID.RA-01" ]
- subdomain
- devsecops
- description
- Scans container images, Infrastructure-as-Code (Terraform, CloudFormation, Kubernetes manifests, Dockerfile, Helm), filesystems, git repos, and SBOMs with Trivy's vuln, misconfig, secret, and license scanners. Use when building a CI/CD security gate that scans images before push, IaC before apply, or SBOMs for supply-chain weaknesses, and fails the build on policy violations.
- mitre_attack
- [ "T1525" ]
02 资源文件 4
- 许可证LICENSE11.0 KB
- 参考文档references/api-reference.md2.9 KB
- 参考文档references/standards.md1.4 KB
- 脚本scripts/agent.py6.0 KB
03 SKILL.md 正文
Scanning IaC and Images with Trivy
Overview
Trivy (by Aqua Security) is a comprehensive, open-source security scanner that finds vulnerabilities (CVEs), misconfigurations (IaC), secrets, software licenses, and software supply-chain weaknesses across a wide range of targets: container images, filesystems, Git repositories, virtual machine images, Kubernetes clusters, and SBOM documents. It is widely adopted as a "shift-left" gate in CI/CD pipelines because it is fast, runs as a single static binary, requires no agent, and supports machine-readable output formats (JSON, SARIF, CycloneDX, SPDX) for integration with code-scanning dashboards.
Trivy bundles four primary scanners that can be toggled with --scanners:
- vuln — OS package and language-dependency vulnerability detection (CVE matching against the Trivy vulnerability DB).
- misconfig — Infrastructure-as-Code and configuration misconfiguration detection (Terraform, CloudFormation, Kubernetes manifests, Dockerfile, Helm) using built-in and custom Rego policies.
- secret — Hard-coded secret/credential detection (API keys, tokens, private keys).
- license — Software license identification and policy enforcement.
This skill covers building a Trivy-based scanning workflow that gates a CI/CD pipeline: scanning images before push, scanning IaC before apply, generating and re-scanning SBOMs, and failing builds on policy violations. Detecting these weaknesses defends against the MITRE ATT&CK technique T1525 (Implant Internal Image), where adversaries plant malicious or vulnerable images in a registry to be deployed across the environment.
When to Use
- When integrating vulnerability and misconfiguration scanning into a CI/CD pipeline as a quality/security gate before images are pushed or infrastructure is applied.
- When auditing container images in a registry for known CVEs prior to deployment.
- When validating Terraform, CloudFormation, Kubernetes, Dockerfile, or Helm IaC for security misconfigurations.
- When generating an SBOM (CycloneDX/SPDX) for supply-chain transparency and later re-scanning that SBOM for newly disclosed CVEs.
- When scanning a running Kubernetes cluster for vulnerable workloads and misconfigured RBAC/resources.
- When enforcing license compliance policy on dependencies.
Prerequisites
- A Linux/macOS/Windows host or CI runner with network access to download the Trivy vulnerability database.
- Docker (optional) if scanning local images by name or using the containerized Trivy.
- Install Trivy (Aqua Security official methods):
# Debian/Ubuntu (APT repository)
sudo apt-get install -y wget gnupg
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 generic main" | sudo tee /etc/apt/sources.list.d/trivy.list
sudo apt-get update
sudo apt-get install -y trivy
# RHEL/CentOS (YUM repository), macOS (Homebrew), and install script
brew install trivy
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sudo sh -s -- -b /usr/local/bin
# Containerized usage (no install)
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy:latest image python:3.10-alpine
# Verify
trivy --version
- For air-gapped or rate-limited environments, pre-download the DB with
trivy image --download-db-onlyand the Java/policy bundles as needed.
Objectives
- Scan a container image for OS and language vulnerabilities and fail the build above a severity threshold.
- Scan IaC (Terraform/Kubernetes/Dockerfile) for misconfigurations.
- Detect hard-coded secrets in a repository or filesystem.
- Generate a CycloneDX or SPDX SBOM and re-scan it for vulnerabilities.
- Emit SARIF for GitHub code scanning and JSON for programmatic gating.
- Wire Trivy into a CI/CD pipeline with
--exit-codeto enforce policy.
MITRE ATT&CK Mapping
| Technique ID | Name | Tactic | Relevance |
|---|---|---|---|
| T1525 | Implant Internal Image | Persistence | Trivy detects vulnerable or malicious images/layers and embedded secrets before they are implanted in a registry and propagated to running workloads. |
Workflow
1. Scan a container image for vulnerabilities
Run a vulnerability-only scan of a registry image, restricting to high/critical findings and ignoring CVEs with no available fix:
trivy image \
--scanners vuln \
--severity HIGH,CRITICAL \
--ignore-unfixed \
--format table \
python:3.10-alpine
Scan an image saved as a tarball (useful when the image is built but not yet pushed):
docker save myorg/app:1.4.0 -o app.tar
trivy image --input app.tar --severity CRITICAL --format json --output app-vulns.json
2. Enable multiple scanners on an image
Run vulnerability, misconfiguration, secret, and license scanners together:
trivy image \
--scanners vuln,misconfig,secret,license \
--severity MEDIUM,HIGH,CRITICAL \
myorg/app:1.4.0
Scan the image's embedded config (Dockerfile-equivalent build history and history secrets):
trivy image --image-config-scanners misconfig,secret myorg/app:1.4.0
3. Scan Infrastructure-as-Code (misconfiguration)
Scan a directory of Terraform / Kubernetes / Dockerfile / Helm / CloudFormation for misconfigurations using the config target:
# Scan a Terraform / IaC directory
trivy config \
--severity HIGH,CRITICAL \
--format table \
./infra
# Scan with custom Rego policies and a specific policy namespace
trivy config \
--config-policy ./policies \
--policy-namespaces user \
./infra
Alternatively use the fs (filesystem) target with the misconfig scanner explicitly:
trivy fs --scanners misconfig,secret --severity HIGH,CRITICAL ./infra
4. Scan a repository and detect secrets
Scan a local working tree (or remote repo) for vulnerabilities in lockfiles and hard-coded secrets:
# Local filesystem (dependencies + secrets)
trivy fs --scanners vuln,secret --severity HIGH,CRITICAL .
# Remote Git repository
trivy repository --scanners vuln,secret https://github.com/myorg/myrepo
5. Generate and re-scan an SBOM
Produce a CycloneDX SBOM from an image, then scan the SBOM itself for vulnerabilities (so a stored SBOM can be re-evaluated as new CVEs are disclosed):
# Generate CycloneDX SBOM
trivy image --format cyclonedx --output sbom.cdx.json myorg/app:1.4.0
# Generate SPDX SBOM
trivy image --format spdx-json --output sbom.spdx.json myorg/app:1.4.0
# Re-scan the SBOM for vulnerabilities later
trivy sbom --severity HIGH,CRITICAL sbom.cdx.json
6. Emit SARIF for code-scanning dashboards
Produce SARIF for GitHub Advanced Security / code scanning ingestion:
trivy image \
--format sarif \
--output trivy-results.sarif \
--severity HIGH,CRITICAL \
myorg/app:1.4.0
7. Gate the CI/CD pipeline with exit codes
Use --exit-code 1 so the pipeline step fails when findings at or above the chosen severity are present. Separate the "report everything" run (exit 0) from the "enforce" run (exit 1):
# 1) Informational report (never fails the build)
trivy image --severity LOW,MEDIUM,HIGH,CRITICAL --exit-code 0 --format table myorg/app:1.4.0
# 2) Enforcement gate (fails build on HIGH/CRITICAL with a fix available)
trivy image \
--severity HIGH,CRITICAL \
--ignore-unfixed \
--exit-code 1 \
--format json --output gate.json \
myorg/app:1.4.0
Example GitHub Actions step using the official action:
- name: Run Trivy image scan (gate)
uses: aquasecurity/trivy-action@master
with:
image-ref: 'myorg/app:1.4.0'
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'HIGH,CRITICAL'
ignore-unfixed: true
exit-code: '1'
8. Manage false positives and the DB
Suppress accepted-risk findings with a .trivyignore file and keep the DB current:
# .trivyignore — one CVE/AVD/secret rule ID per line
echo "CVE-2023-12345" >> .trivyignore
echo "AVD-AWS-0089" >> .trivyignore
# Refresh DBs explicitly (useful for caching layers in CI)
trivy image --download-db-only
trivy image --download-java-db-only
# Scan a Kubernetes cluster (summary report)
trivy k8s --report summary --severity HIGH,CRITICAL cluster
Tools and Resources
| Tool / Resource | Purpose | Link |
|---|---|---|
| Trivy | Core scanner CLI | https://github.com/aquasecurity/trivy |
| Trivy Documentation | Official docs (targets, scanners, flags) | https://trivy.dev/latest/docs/ |
| trivy-action | GitHub Actions integration | https://github.com/aquasecurity/trivy-action |
| Trivy Operator | In-cluster Kubernetes continuous scanning | https://github.com/aquasecurity/trivy-operator |
| Trivy vulnerability DB | OSS vulnerability data source | https://github.com/aquasecurity/trivy-db |
| CycloneDX | SBOM standard emitted by Trivy | https://cyclonedx.org/ |
Validation Criteria
- [ ] Trivy installed and
trivy --versionreports a valid version. - [ ] Container image scanned for vulnerabilities with severity filtering applied.
- [ ] Image scanned with multiple scanners (vuln, misconfig, secret, license).
- [ ] IaC directory scanned with
trivy configand misconfigurations reviewed. - [ ] Secret scanning run against the repository.
- [ ] CycloneDX/SPDX SBOM generated and successfully re-scanned with
trivy sbom. - [ ] SARIF output produced for the code-scanning dashboard.
- [ ] CI/CD gate fails the build on HIGH/CRITICAL findings via
--exit-code 1. - [ ]
.trivyignoreconfigured for accepted-risk findings with documented justification.
04 同仓库更多 Skills
SLSA 构建来源验证
verifying-build-provenance-with-slsa-sigstore
使用 Sigstore 的 cosign(verify、verify-attestation、verify-blob-attestation)和 slsa-verifier(verify-artifact)验证工件签名和 SLSA 来源(provenance),强制校验 keyless OIDC 构建者身份和源仓库是否符合 SLSA Build 级别。适用于 CI/CD 中部署工件前、消费第三方证明时、建立 SLSA Build L3 管道,或在事件响应或准入控制期间确认来源的场景。
TPM 测量启动验证
validating-tpm-measured-boot-attestation
使用 tpm2-tools 验证 TPM 2.0 测量启动(measured boot)的完整性和远程证明:读取 PCR(tpm2_pcrread)、重放启动事件日志、生成并检查签名报价(tpm2_quote/tpm2_checkquote),以及将机密封印(seal)到 PCR 策略。适用于确认系统已启动可信固件/内核以实现零信任(Zero Trust)设备状态、通过 PCR 漂移检测启动链篡改,或构建黄金值基线用于机群(fleet)证明。
备份完整性验证
validating-backup-integrity-for-recovery
通过加密哈希验证、自动化恢复测试、损坏检测和可恢复性检查来验证备份完整性,确认备份对于灾难恢复和勒索软件响应是可靠的。适用于在依赖备份进行恢复之前、构建备份后验证管道、审计备份基础设施以满足合规要求,或检查不可变/air-gapped(气隙)备份是否存在静默损坏或篡改的场景。
KAPE Windows 取证收集
triaging-windows-with-kape
运行 KAPE(Kroll Artifact Parser and Extractor),通过 Targets 收集目标取证工件(注册表配置单元、$MFT、事件日志、prefetch、浏览器数据),并使用封装 Eric Zimmerman 的 EZ Tools(PECmd、MFTECmd、RECmd)的 Modules 进行解析。适用于事件遏制/分诊早期阶段——此时完整磁盘镜像不切实际,但需要快速获得可采信、可解析的 Windows 工件集,包括大规模远程收集。
SSVC 漏洞优先级分诊
triaging-vulnerabilities-with-ssvc-framework
使用 CISA 的 Stakeholder-Specific Vulnerability Categorization(SSVC,利益相关方特定漏洞分类)决策树对漏洞进行分诊和优先级排序,权衡利用状态(通过 CISA KEV 目录和 FIRST EPSS API)、技术影响、自动化程度和任务普遍性,输出 Track/Track*/Attend/Act 决策。适用于对漏洞扫描结果(OpenVAS、Nessus、Qualys)进行优先级排序以制定修复计划,而不局限于原始 CVSS 分数的场景。
安全事件初步分诊
triaging-security-incident
使用 NIST SP 800-61r3 和 SANS PICERL 框架对安全事件进行初步分诊,分类事件类型、按业务影响分配优先级,并路由到相应的响应团队。适用于 SIEM/EDR 告警需要人工分类、并发告警必须确定优先级,或用户报告或威胁情报 IOC 匹配需要初步事件分类的场景。