| name | trivy |
| description | Run Aqua Trivy for comprehensive security scanning of containers, filesystems, git repos, and IaC. Use when scanning container images, detecting vulnerabilities, secrets, misconfigurations, or generating SBOMs. |
| allowed-tools | ["Bash","Read","Glob","Grep"] |
Aqua Trivy - Comprehensive Security Scanner
When to Use Trivy
Ideal scenarios:
- Container image vulnerability scanning
- Filesystem and repository scanning
- Infrastructure-as-Code (IaC) misconfiguration detection
- Secrets detection in code and images
- Software Bill of Materials (SBOM) generation
- License compliance checking
- Kubernetes cluster security assessment
- CI/CD security gates
Complements other tools:
- Use alongside Semgrep/CodeQL for application code analysis
- Combine with KICS for additional IaC coverage
- Use with Gitleaks for dedicated secrets scanning
- Pair with OSV-Scanner/Depscan for enhanced SCA
When NOT to Use
Do NOT use this skill for:
- Deep application code vulnerability analysis (use Semgrep or CodeQL)
- API endpoint discovery (use Noir)
- Advanced SAST with taint tracking (use CodeQL)
- Penetration testing (use specialized tools)
Installation
Homebrew (Recommended for macOS/Linux)
brew install trivy
trivy --version
Other Installation Methods
docker pull aquasec/trivy:latest
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
sudo apt-get install wget apt-transport-https gnupg lsb-release
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 $(lsb_release -sc) main" | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update
sudo apt-get install trivy
sudo rpm -ivh https://github.com/aquasecurity/trivy/releases/latest/download/trivy_*_Linux-64bit.rpm
choco install trivy
scoop install trivy
go install github.com/aquasecurity/trivy/cmd/trivy@latest
Core Workflow
1. Quick Scan
trivy image nginx:latest
trivy fs /path/to/project
trivy fs .
trivy repo https://github.com/owner/repo
trivy k8s --report summary cluster
2. SARIF Output
trivy image --format sarif --output results.sarif nginx:latest
trivy fs --format sarif --output results.sarif /path/to/project
trivy repo --format sarif --output results.sarif https://github.com/owner/repo
trivy config --format sarif --output iac-results.sarif /path/to/terraform
3. Specific Scanner Types
trivy image --scanners vuln nginx:latest
trivy fs --scanners secret /path/to/project
trivy config /path/to/iac
trivy image --scanners license nginx:latest
trivy fs --scanners vuln,secret,misconfig /path/to/project
4. SBOM Generation
trivy image --format cyclonedx --output sbom.json nginx:latest
trivy image --format spdx-json --output sbom.spdx.json nginx:latest
trivy sbom sbom.json
Scan Targets
| Target | Command | Description |
|---|
| Container Image | trivy image IMAGE | Scan container images from registries or local |
| Filesystem | trivy fs PATH | Scan local project directory |
| Repository | trivy repo URL | Scan remote git repository |
| Kubernetes | trivy k8s | Scan Kubernetes cluster resources |
| IaC/Config | trivy config PATH | Scan IaC files (Terraform, CloudFormation, etc.) |
| SBOM | trivy sbom FILE | Scan existing SBOM file |
| VM Image | trivy vm IMAGE | Scan virtual machine images |
| Rootfs | trivy rootfs PATH | Scan root filesystem |
Supported Ecosystems
Package Managers
| Ecosystem | Manifest/Lock Files |
|---|
| npm | package.json, package-lock.json, yarn.lock, pnpm-lock.yaml |
| Python | requirements.txt, Pipfile.lock, poetry.lock, setup.py |
| Go | go.mod, go.sum |
| Rust | Cargo.lock |
| Java | pom.xml, build.gradle, gradle.lockfile |
| Ruby | Gemfile.lock |
| PHP | composer.lock |
| .NET | packages.lock.json, *.deps.json |
| Swift | Package.resolved |
| Dart | pubspec.lock |
| Elixir | mix.lock |
| Conan (C/C++) | conan.lock |
IaC Platforms
| Platform | File Types |
|---|
| Terraform | *.tf, *.tf.json |
| CloudFormation | *.yaml, *.json (CFN templates) |
| Kubernetes | *.yaml (K8s manifests) |
| Helm | Chart.yaml, values.yaml |
| Docker | Dockerfile |
| Azure ARM | *.json (ARM templates) |
Output Formats
trivy image nginx:latest
trivy image --format json --output results.json nginx:latest
trivy image --format sarif --output results.sarif nginx:latest
trivy image --format cyclonedx --output sbom.cdx.json nginx:latest
trivy image --format spdx-json --output sbom.spdx.json nginx:latest
trivy image --format github nginx:latest
trivy image --format template --template "@contrib/html.tpl" --output report.html nginx:latest
Advanced Options
Severity Filtering
trivy image --severity CRITICAL,HIGH nginx:latest
trivy image --severity UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL nginx:latest
trivy image --exit-code 1 --severity HIGH,CRITICAL nginx:latest
Vulnerability Options
trivy image --ignore-unfixed nginx:latest
trivy image --ignorefile .trivyignore nginx:latest
trivy image --skip-db-update nginx:latest
trivy image --offline-scan nginx:latest
Image Scanning Options
trivy image --input image.tar
trivy image --platform linux/amd64 nginx:latest
trivy image --image-config-scanners config nginx:latest
trivy image --skip-files "/path/to/skip" nginx:latest
trivy image --skip-dirs node_modules nginx:latest
Secret Scanning
trivy fs --scanners secret /path/to/project
trivy fs --scanners secret --secret-config trivy-secret.yaml /path/to/project
CI/CD Integration (GitHub Actions)
name: Trivy Security Scan
on:
push:
branches: [main]
pull_request:
schedule:
- cron: '0 0 * * *'
jobs:
trivy-scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run Trivy vulnerability scanner (filesystem)
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
scan-ref: '.'
format: 'sarif'
output: 'trivy-fs-results.sarif'
severity: 'CRITICAL,HIGH'
- name: Upload Trivy SARIF (filesystem)
if: always()
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: 'trivy-fs-results.sarif'
[]
Configuration
Config File (trivy.yaml)
severity:
- CRITICAL
- HIGH
- MEDIUM
exit-code: 1
ignore-unfixed: true
vulnerability:
type:
- os
- library
secret:
config: trivy-secret.yaml
misconfiguration:
terraform:
excluded-checks:
- AVD-AWS-0013
skip-files:
- "**/*.test.js"
- "**/testdata/**"
skip-dirs:
- node_modules
- .git
- vendor
cache:
dir: /tmp/trivy-cache
Ignore File (.trivyignore)
# .trivyignore
# Ignore specific CVEs
CVE-2023-12345
CVE-2023-67890
# Ignore with expiration
CVE-2024-11111 exp:2025-06-01
# Ignore specific package vulnerabilities
CVE-2024-22222 pkg:lodash
# Secret ignore patterns
aws-access-key-id
Secret Config (trivy-secret.yaml)
rules:
- id: custom-api-key
category: general
title: Custom API Key
severity: HIGH
regex: 'CUSTOM_API_KEY[=:]\s*["\']?([A-Za-z0-9]{32})["\']?'
allow-rules:
- id: allow-test-secrets
description: Allow test/mock secrets
path: '.*test.*|.*mock.*'
Common Use Cases
1. Container Security Pipeline
docker build -t myapp:latest .
trivy image --severity HIGH,CRITICAL --exit-code 1 myapp:latest
trivy image --format cyclonedx --output sbom.json myapp:latest
trivy image --format sarif --output results.sarif myapp:latest
2. IaC Security Review
trivy config --format sarif --output iac-results.sarif ./terraform
trivy config --format sarif --output k8s-results.sarif ./k8s
trivy config --format sarif --output docker-results.sarif .
3. Repository Security Audit
trivy fs --scanners vuln,secret,misconfig \
--format sarif --output full-scan.sarif \
/path/to/project
trivy repo --format sarif --output repo-scan.sarif \
https://github.com/owner/repo
4. Kubernetes Cluster Assessment
trivy k8s --report summary cluster
trivy k8s --format sarif --output k8s-cluster.sarif cluster
trivy k8s --namespace production --report all cluster
5. Supply Chain Security
trivy image --format cyclonedx --output sbom.cdx.json myapp:latest
trivy sbom sbom.cdx.json
cosign sign-blob --key cosign.key sbom.cdx.json
Understanding Output
SARIF Structure
Trivy SARIF v2.1.0 includes:
- Rules: Each vulnerability, misconfiguration, or secret type
- Results: Individual findings with location
- Properties:
- CVE/vulnerability ID
- Severity (CRITICAL, HIGH, MEDIUM, LOW, UNKNOWN)
- CVSS scores
- Package name and version
- Fixed version (if available)
- References and links
- File path and line number (for secrets/misconfig)
Vulnerability Information
Each finding includes:
- VulnerabilityID: CVE-YYYY-NNNNN
- PkgName: Affected package name
- InstalledVersion: Current vulnerable version
- FixedVersion: Version with fix (if available)
- Severity: CRITICAL, HIGH, MEDIUM, LOW, UNKNOWN
- Title: Brief vulnerability description
- Description: Detailed explanation
- References: Links to advisories
- CVSS: Score vectors if available
Remediation Workflow
Step 1: Scan
trivy image --format json --output vulns.json myapp:latest
Step 2: Prioritize
jq '.Results[].Vulnerabilities[] | select(.Severity == "CRITICAL")' vulns.json
jq '[.Results[].Vulnerabilities[].Severity] | group_by(.) | map({severity: .[0], count: length})' vulns.json
Step 3: Fix
jq -r '.Results[].Vulnerabilities[] | select(.FixedVersion != null) | "\(.PkgName): \(.InstalledVersion) -> \(.FixedVersion)"' vulns.json
docker build -t myapp:latest .
Step 4: Verify
trivy image --format json --output post-fix.json myapp:latest
echo "Before: $(jq '[.Results[].Vulnerabilities[]] | length' vulns.json)"
echo "After: $(jq '[.Results[].Vulnerabilities[]] | length' post-fix.json)"
Performance Optimization
trivy image --skip-db-update nginx:latest
trivy image --offline-scan nginx:latest
trivy fs --skip-dirs node_modules,.git,vendor /path/to/project
trivy image --cache-dir /tmp/trivy-cache nginx:latest
trivy image --parallel 4 nginx:latest
Database Management
trivy image --download-db-only
trivy clean --all
trivy version --format json | jq '.VulnerabilityDB'
Limitations
- Image layers: Only scans final image filesystem, not intermediate layers
- Runtime: Static analysis only; doesn't detect runtime vulnerabilities
- Custom packages: May not detect vulnerabilities in custom/private packages
- False positives: OS package detection may include packages not actually installed
- Zero-days: Only detects publicly disclosed vulnerabilities
Rationalizations to Reject
| Shortcut | Why It's Wrong |
|---|
| "Base image from vendor is secure" | Vendors lag on patches; always scan regardless of source |
| "Only scan production images" | Dev/staging images can leak secrets or introduce supply chain attacks |
| "Skip IaC scanning for speed" | Misconfigurations are easier to fix before deployment |
| "Ignore unfixed vulnerabilities" | Even without patches, you can mitigate or compensate with other controls |
| "Weekly scans are enough" | New CVEs are disclosed daily; scan on every build and daily on deployed images |
| "Low severity = safe to ignore" | Low severity issues can combine into exploitable chains |
References