1. Trivy คืออะไร
Trivy (อ่านว่า "ทริ-วี" เหมือน trigger + envy) เป็น all-in-one security scanner ที่พัฒนาโดย Aqua Security โดยเป็นเครื่องมือโอเพ่นซอร์สภายใต้สัญญาอนุญาต Apache-2.0 ซึ่งใช้ภาษา Go ในการพัฒนา
Trivy ได้รับการยอมรับว่าเป็น container scanner ยอดนิยมอันดับ 1 โดยใช้งานโดยบริษัทชั้นนำทั่วโลก เช่น GitLab, MasterCard, Wise, Azure และอีกมากมาย
ภาพรวมสถาปัตยกรรม Trivy
เร็วและง่าย
ติดตั้งและใช้งานได้ทันที สแกนเร็วกว่าเครื่องมืออื่นๆ
ครอบคลุม
สแกน CVE, IaC, Secrets, Licenses ในเครื่องมือเดียว
ฟรีและโอเพ่นซอร์ส
Apache-2.0 License ใช้งานได้ฟรีไม่จำกัด
2. ความสามารถหลักของ Trivy
Vulnerability Scanning (CVE)
ตรวจจับช่องโหว่ด้านความปลอดภัยที่รู้จัก (Common Vulnerabilities and Exposures) ใน:
- OS Packages (Alpine, Debian, Ubuntu, CentOS)
- Language Dependencies (npm, pip, maven, go mod)
- Application Binaries
Misconfiguration Scanning
ตรวจสอบการตั้งค่าที่ผิดพลาดใน Infrastructure as Code:
- Terraform / OpenTofu
- Kubernetes Manifests
- Dockerfile
- CloudFormation, ARM Templates
Secret Scanning
ตรวจจับข้อมูลลับที่รั่วไหลในโค้ดและ configurations:
- API Keys (AWS, GCP, Azure)
- Private Keys (SSH, GPG)
- Database Credentials
- JWT Tokens, OAuth Secrets
SBOM Generation
สร้าง Software Bill of Materials สำหรับ Supply Chain Security:
- SPDX Format
- CycloneDX Format
- Compliance Ready (US Executive Order)
3. การติดตั้ง Trivy
หมายเหตุ: เลือกวิธีติดตั้ง เพียงวิธีเดียว ที่เหมาะกับระบบของคุณ
วิธีที่ 1 ติดตั้งผ่าน apt (Ubuntu/Debian)
# เพิ่ม repository GPG key
sudo apt-get install wget
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add -
# เพิ่ม repository
echo "deb 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
# ตรวจสอบเวอร์ชัน
trivy --version
วิธีที่ 2 ติดตั้งผ่าน Homebrew (macOS/Linux)
# ติดตั้งด้วย Homebrew
brew install trivy
# ตรวจสอบเวอร์ชัน
trivy --version
วิธีที่ 3 ใช้งานผ่าน Docker
# สแกน image ด้วย Docker
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
aquasec/trivy:latest image nginx:latest
# สร้าง alias สำหรับใช้งานสะดวก
alias trivy='docker run --rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy'
วิธีที่ 4 ดาวน์โหลด Binary โดยตรง
# ดาวน์โหลด binary (Linux AMD64)
wget https://github.com/aquasecurity/trivy/releases/download/v0.69.1/trivy_0.69.1_Linux-64bit.tar.gz
# แตกไฟล์
tar -zxvf trivy_0.69.1_Linux-64bit.tar.gz
# ย้ายไปยัง path
sudo mv trivy /usr/local/bin/
# กำหนดสิทธิ์
sudo chmod +x /usr/local/bin/trivy
4. ตัวอย่างการใช้งาน
สแกน Container Image
การสแกน Docker Image เพื่อหาช่องโหว่:
# สแกน image จาก Docker Hub
trivy image nginx:latest
# สแกน image ที่อยู่ในเครื่อง
trivy image myapp:latest
# สแกน image จาก private registry
trivy image registry.example.com/myapp:v1.0
# สแกนเฉพาะ CRITICAL และ HIGH
trivy image --severity CRITICAL,HIGH nginx:latest
# ส่งผลลัพธ์เป็น JSON
trivy image --format json --output results.json nginx:latest
ตัวอย่างผลลัพธ์:
nginx:latest (debian 12.5)
=========================
Total: 15 (CRITICAL: 2, HIGH: 5, MEDIUM: 6, LOW: 2)
┌──────────────────┬────────────────┬──────────┬───────────────────┬───────────────┐
│ Library │ Vulnerability │ Severity │ Installed Version │ Fixed Version │
├──────────────────┼────────────────┼──────────┼───────────────────┼───────────────┤
│ openssl │ CVE-2024-1234 │ CRITICAL │ 3.0.11-1 │ 3.0.12-1 │
│ libcurl4 │ CVE-2024-5678 │ HIGH │ 7.88.1-10 │ 7.88.1-11 │
└──────────────────┴────────────────┴──────────┴───────────────────┴───────────────┘
สแกน Filesystem (โปรเจค)
สแกนโค้ดในโปรเจคเพื่อหาช่องโหว่และข้อมูลลับ:
# สแกนทั้งหมด (vulnerabilities + misconfigurations + secrets)
trivy fs .
# สแกนเฉพาะ vulnerabilities
trivy fs --scanners vuln .
# สแกนเฉพาะ secrets
trivy fs --scanners secret .
# สแกนหลายอย่างพร้อมกัน
trivy fs --scanners vuln,secret,misconfig .
# กำหนดระดับความรุนแรง
trivy fs --severity CRITICAL,HIGH .
สแกน Infrastructure as Code
ตรวจสอบการตั้งค่าที่ผิดพลาดใน IaC files:
# สแกน Terraform files
trivy config ./terraform/
# สแกน Kubernetes manifests
trivy config ./k8s-manifests/
# สแกน Dockerfile
trivy config ./Dockerfile
# สแกน CloudFormation
trivy config ./cloudformation/
# สแกนทุกประเภทในโฟลเดอร์
trivy config ./iac/
สร้าง SBOM (Software Bill of Materials)
สร้าง SBOM สำหรับ compliance:
# สร้าง SBOM ในรูปแบบ SPDX
trivy image --format spdx-json --output sbom-spdx.json nginx:latest
# สร้าง SBOM ในรูปแบบ CycloneDX
trivy image --format cyclonedx --output sbom-cyclonedx.json nginx:latest
# สร้าง SBOM จาก filesystem
trivy fs --format spdx-json --output sbom.json .
5. ผสานรวมกับ CI/CD Pipeline
GitHub Actions
name: Trivy Security Scan
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
trivy-scan:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
scan-ref: '.'
severity: 'CRITICAL,HIGH'
format: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: 'trivy-results.sarif'
GitLab CI/CD
stages:
- security
trivy-scan:
stage: security
image: docker:latest
services:
- docker:dind
before_script:
- apk add --no-cache curl
- curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin v0.69.1
script:
- trivy image --severity CRITICAL,HIGH --exit-code 1 $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
allow_failure: false
6. สแกน Kubernetes Cluster
Trivy สามารถสแกน Kubernetes cluster ได้โดยตรง:
# สแกนทั้ง cluster
trivy k8s --report summary cluster
# สแกนเฉพาะ namespace
trivy k8s -n default --report summary all
# สแกนเฉพาะ deployment
trivy k8s deployment/my-app
# สแกนและแสดงรายละเอียดครบถ้วน
trivy k8s --report all cluster
# ส่งผลลัพธ์เป็น JSON
trivy k8s --format json --output k8s-report.json cluster
# สแกนเฉพาะชนิดของ resources
trivy k8s --include-kinds=Deployments,Pods cluster
Vulnerability Scanning
สแกน container images ที่รันอยู่ใน cluster
Misconfiguration
ตรวจสอบการตั้งค่า K8s resources ที่ไม่ปลอดภัย
7. Best Practices
1. เริ่มจาก Critical
เริ่มแก้ไขช่องโหว่ระดับ CRITICAL และ HIGH ก่อน ไม่ต้องกังวลกับ LOW ในขั้นต้น
2. รันใน CI/CD
ตั้งค่า Trivy ใน pipeline ให้ fail เมื่อพบ CRITICAL vulnerabilities
3. อัปเดตฐานข้อมูล
Trivy จะอัปเดต vulnerability database อัตโนมัติ แต่สามารถบังคับด้วย trivy image --download-db-only
4. Ignore False Positives
สร้างไฟล์ .trivyignore เพื่อข้าม CVE ที่เป็น false positive
ตัวอย่างไฟล์ .trivyignore
# ข้าม CVE ที่ทราบว่าเป็น false positive
CVE-2024-1234
CVE-2024-5678
# ข้ามพร้อมเหตุผล (comment)
# CVE-2024-9999 # Not applicable to our use case
8. แก้ไขปัญหาที่พบบ่อย
ปัญหา: Database update fails
ไม่สามารถอัปเดต vulnerability database ได้
# ลบ database เก่าและดาวน์โหลดใหม่
rm -rf ~/.cache/trivy
trivy image --download-db-only
# หรือระบุ cache directory
trivy image --cache-dir /tmp/trivy-cache nginx:latest
ปัญหา: Permission denied
ไม่มีสิทธิ์เข้าถึง Docker socket
# เพิ่ม user ใน docker group
sudo usermod -aG docker $USER
# หรือรันด้วย sudo
sudo trivy image nginx:latest
ปัญหา: Scan takes too long
การสแกนใช้เวลานานเกินไป
# สแกนเฉพาะ OS packages (เร็วกว่า)
trivy image --vuln-type os nginx:latest
# ใช้ offline mode ถ้ามี database แล้ว
trivy image --offline nginx:latest
# กำหนด timeout
trivy image --timeout 5m nginx:latest