Snyk年费 $25,000+(500个项目),Aqua Security $10,000+。Trivy是完全免费的开源容器安全扫描器,由Aqua Security开发并开源,已成为CNCF毕业项目,是容器安全扫描的事实标准。
为什么选Trivy
| 功能 | Snyk Container | Aqua Security | Trivy |
|---|---|---|---|
| 镜像扫描 | 优秀 | 优秀 | 优秀 |
| 文件系统扫描 | 支持 | 支持 | 支持 |
| Git仓库扫描 | 支持 | 有限 | 支持 |
| IaC扫描 | 支持 | 支持 | 支持 |
| SBOM生成 | 支持 | 支持 | 支持 |
| CI/CD集成 | 完整 | 完整 | 完整 |
| Kubernetes扫描 | 支持 | 支持 | 支持 |
| 漏洞数据库 | 私有 | 私有 | NVD+GitHub Advisory |
| 价格 | $25K+/年 | $10K+/年 | 免费 |
Trivy的核心优势是速度快、误报低、覆盖广。它不仅扫描容器镜像,还能扫描文件系统、Git仓库、Kubernetes集群、IaC配置(Terraform/Kubernetes YAML)。
安装
# Ubuntu/Debian
apt-get install wget apt-transport-https gnupg lsb-release
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | 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" | tee /etc/apt/sources.list.d/trivy.list
apt-get update
apt-get install trivy
# macOS
brew install trivy
# Docker
docker pull aquasec/trivy:latest
# Go安装
go install github.com/aquasecurity/trivy/cmd/trivy@latest
核心用法
1. 容器镜像扫描
# 扫描镜像
trivy image nginx:latest
# 只显示高危和严重漏洞
trivy image --severity HIGH,CRITICAL nginx:latest
# JSON格式输出
trivy image -f json -o results.json nginx:latest
# 扫描本地镜像
trivy image --input myapp.tar
# 扫描私有仓库镜像
trivy image --username admin --password secret registry.example.com/myapp:v1
# 只修复可修复的漏洞
trivy image --ignore-unfixed nginx:latest
2. 文件系统扫描
# 扫描项目依赖漏洞
trivy fs --security-checks vuln /path/to/project
# 扫描配置文件
trivy fs --security-checks config /path/to/project
# 扫描密钥泄露
trivy fs --security-checks secret /path/to/project
# 综合扫描
trivy fs --scanners vuln,config,secret /path/to/project
3. Git仓库扫描
# 扫描远程Git仓库
trivy repo https://github.com/owner/repo
# 扫描本地Git仓库
trivy repo /path/to/repo
# 指定分支
trivy repo --branch main https://github.com/owner/repo
4. Kubernetes扫描
# 扫描Kubernetes集群
trivy k8s --report summary cluster
# 扫描指定命名空间
trivy k8s --namespace default --report summary
# 扫描Kubernetes YAML文件
trivy config k8s/deployment.yaml
5. IaC扫描
# 扫描Terraform
trivy config terraform/
# 扫描CloudFormation
trivy config cloudformation/
# 扫描Dockerfile
trivy config Dockerfile
# 扫描Kubernetes manifests
trivy config k8s/
6. SBOM生成
# 生成SPDX格式SBOM
trivy image --format spdx-json -o sbom.json nginx:latest
# 生成CycloneDX格式SBOM
trivy image --format cyclonedx -o sbom.xml nginx:latest
CI/CD集成
GitLab CI
# .gitlab-ci.yml
trivy-scan:
stage: security
image:
name: aquasec/trivy:latest
entrypoint: [""]
script:
- trivy image --exit-code 1 --severity HIGH,CRITICAL $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
allow_failure: false
GitHub Actions
# .github/workflows/trivy.yml
name: Trivy Scan
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build image
run: docker build -t myapp:${{ github.sha }} .
- name: Run Trivy
uses: aquasecurity/trivy-action@master
with:
image-ref: myapp:${{ github.sha }}
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'CRITICAL,HIGH'
- name: Upload to GitHub Security
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: trivy-results.sarif
Jenkins Pipeline
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'docker build -t myapp:${BUILD_NUMBER} .'
}
}
stage('Security Scan') {
steps {
sh 'trivy image --exit-code 1 --severity HIGH,CRITICAL myapp:${BUILD_NUMBER}'
}
}
}
}
Kubernetes准入控制
使用Trivy Operator
# 安装Trivy Operator
helm repo add aqua https://aquasecurity.github.io/helm-charts/
helm install trivy-operator aqua/trivy-operator \
--namespace trivy-system \
--create-namespace \
--set trivy.ignoreUnfixed=true
# 查看漏洞报告
kubectl get vulnerabilityreports --all-namespaces
# 查看配置审计
kubectl get configauditreports --all-namespaces
配置准入Webhook
# 拒绝部署包含高危漏洞的镜像
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: trivy-webhook
webhooks:
- name: trivy.aquasecurity.github.io
rules:
- apiGroups: [""]
apiVersions: ["v1"]
operations: ["CREATE", "UPDATE"]
resources: ["pods"]
clientConfig:
service:
name: trivy-webhook
namespace: trivy-system
path: /scan
failurePolicy: Fail
自动化扫描脚本
cat > /opt/container-security-scan.sh << 'SCRIPT'
#!/bin/bash
# 扫描所有运行中的容器镜像
REPORT_DIR="/var/log/trivy/$(date +%Y%m%d)"
mkdir -p "$REPORT_DIR"
# 获取所有运行中的镜像
docker ps --format '{{.Image}}' | sort -u | while read image; do
echo "[*] Scanning: $image"
SAFE_NAME=$(echo "$image" | tr '/:' '_')
# 扫描并生成报告
trivy image --severity HIGH,CRITICAL \
--format json \
-o "$REPORT_DIR/${SAFE_NAME}.json" \
"$image"
# 检查是否有高危漏洞
CRITICAL=$(cat "$REPORT_DIR/${SAFE_NAME}.json" | \
python3 -c "import sys,json;d=json.load(sys.stdin);print(len([v for r in d.get('Results',[]) for v in r.get('Vulnerabilities',[]) if v.get('Severity')=='CRITICAL']))")
if [ "$CRITICAL" -gt 0 ]; then
echo "[!] ALERT: $image has $CRITICAL CRITICAL vulnerabilities!"
# 发送告警
curl -s -X POST "https://hooks.slack.com/services/YOUR/WEBHOOK" \
-H 'Content-Type: application/json' \
-d "{\"text\":\"🚨 Container Alert: $image has $CRITICAL CRITICAL vulnerabilities\"}"
fi
done
echo "[*] Scan complete. Reports: $REPORT_DIR"
SCRIPT
chmod +x /opt/container-security-scan.sh
对比表
| 功能 | Snyk | Aqua | Trivy |
|---|---|---|---|
| 镜像扫描 | 优秀 | 优秀 | 优秀 |
| 语言包扫描 | 优秀 | 良好 | 优秀 |
| IaC扫描 | 支持 | 支持 | 支持 |
| 密钥检测 | 支持 | 支持 | 支持 |
| SBOM | 支持 | 支持 | 支持 |
| 准入控制 | 支持 | 支持 | 支持 |
| 扫描速度 | 快 | 中等 | 快 |
| CI/CD集成 | 完整 | 完整 | 完整 |
| 价格 | $25K+/年 | $10K+/年 | 免费 |
Trivy已成为CNCF毕业项目,被AWS、Google Cloud、Azure等主流云平台采用。在容器安全扫描领域,Trivy是绝对的王者。
评论