Các cổng dễ bị tổn thương tự động quét các phần phụ thuộc, container image và mã hóa các lỗ hổng bảo mật đã biết, chặn việc triển khai khi phát hiện thấy các vấn đề nghiêm trọng.

Tổng quan

Các ứng dụng hiện đại phụ thuộc vào hàng trăm thư viện của bên thứ ba và cơ sở image. Cổng dễ bị tổn thương đảm bảo rằng:

  • Không có CVE quan trọng nào nhập production
  • Các phiên bản phụ thuộc được theo dõi và giám sát
  • Các lỗ hổng đã biết sẽ kích hoạt hành động ngay lập tức
  • Đội an ninh có tầm nhìn vào bề mặt tấn công

Tác động trong thế giới thực

Vi phạm Equifax (2017)

  • Khai thác Apache Struts CVE-2017-5638 (CVSS 10.0)
  • Bản vá có sẵn 2 tháng trước khi vi phạm
  • 147 triệu hồ sơ bị xâm phạm
  • Cost: 1,4 tỷ USD thanh toán
  • Prevention: Cổng dễ bị tổn thương sẽ chặn phiên bản dễ bị tổn thương

Công cụ quét phổ biến

Chuyện tầm thường (Được khuyến nghị)

Ưu điểm:

  • Quét nhanh, toàn diện (gói hệ điều hành, phụ thuộc ngôn ngữ, IaC, bí mật)
  • Dễ dàng tích hợp, nhị phân đơn
  • Hỗ trợ SBOM tuyệt vời
  • Nguồn mở và miễn phí

Nhược điểm:

  • Hạn chế quản lý dương tính giả
  • Không có bảng điều khiển trung tâm (sử dụng với Trivy Operator cho Kubernetes)

Grype

Ưu điểm:

  • Quét nhanh với dung lượng bộ nhớ thấp
  • Hoạt động với Syft SBOM
  • Bảo hiểm cơ sở dữ liệu dễ bị tổn thương tốt

Nhược điểm:

  • Ít kiểu quét hơn Trivy (không quét IaC/bí mật)
  • Hệ sinh thái kém trưởng thành hơn

Snyk

Ưu điểm:

  • Trải nghiệm tuyệt vời của nhà phát triển với các đề xuất sửa lỗi
  • Tích hợp IDE
  • Hỗ trợ ngôn ngữ toàn diện
  • Lời khuyên khắc phục

Nhược điểm:

  • Thương mại (giới hạn cấp miễn phí)
  • Có thể chậm hơn Trivy/Grype
  • Yêu cầu tài khoản/xác thực

Ngưỡng nghiêm trọng

Chính sách được đề xuất

# .trivy-policy.yaml
severity:
  CRITICAL:
    action: BLOCK
    max_allowed: 0
    notify:
      - security-team@company.com
      - slack:#security-alerts
  
  HIGH:
    action: WARN
    max_allowed: 5
    require_jira_ticket: true
  
  MEDIUM:
    action: WARN
    max_allowed: 20
  
  LOW:
    action: ALLOW
    report_only: true

ignore:
  - CVE-2023-12345  # False positive - not applicable
  - CVE-2023-67890  # Fix scheduled, approved exception

## Trivy Integration

### Basic Container Scanning

```bash
# Install Trivy
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin

# Scan container image
trivy image --severity CRITICAL,HIGH nginx:latest

# Exit with error code if vulnerabilities found
trivy image --exit-code 1 --severity CRITICAL nginx:latest

# Generate report
trivy image --format json --output report.json nginx:latest

Quét hệ thống tập tin

# Scan project dependencies
trivy fs --severity CRITICAL,HIGH .

# Scan specific package manager files
trivy fs --scanners vuln --severity CRITICAL package.json
trivy fs --scanners vuln --severity CRITICAL requirements.txt
trivy fs --scanners vuln --severity CRITICAL pom.xml

Quét dựa trên SBOM

# Generate SBOM with Syft
syft nginx:latest -o cyclonedx-json > sbom.json

# Scan SBOM with Trivy
trivy sbom sbom.json --severity CRITICAL,HIGH

# This approach separates SBOM generation from vulnerability scanning
# Benefits: faster CI/CD, reusable SBOMs, audit trail

Ví dụ về hành động GitHub

name: Vulnerability Gate

on:
  pull_request:
    branches: [main]
  push:
    branches: [main]

jobs:
  scan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Build Docker image
        run: docker build -t myapp:${{ github.sha }} .
      
      - name: Run Trivy vulnerability scanner
        uses: aquasecurity/trivy-action@master
        with:
          image-ref: myapp:${{ github.sha }}
          format: 'sarif'
          output: 'trivy-results.sarif'
          severity: 'CRITICAL,HIGH'
          exit-code: '1'  # Fail if vulnerabilities found
      
      - name: Upload Trivy results to GitHub Security
        uses: github/codeql-action/upload-sarif@v3
        if: always()
        with:
          sarif_file: 'trivy-results.sarif'
      
      - name: Scan dependencies
        uses: aquasecurity/trivy-action@master
        with:
          scan-type: 'fs'
          scan-ref: '.'
          format: 'table'
          severity: 'CRITICAL'
          exit-code: '1'

Ví dụ CI GitLab

vulnerability-gate:
  stage: security
  image: aquasec/trivy:latest
  script:
    - trivy image --exit-code 1 --severity CRITICAL,HIGH $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
    - trivy fs --exit-code 1 --severity CRITICAL .
  artifacts:
    reports:
      container_scanning: gl-container-scanning-report.json
  only:
    - merge_requests
    - main

Tích hợp Grype

Cách sử dụng cơ bản

# Install Grype
curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/bin

# Scan container image
grype nginx:latest

# Fail on high severity
grype nginx:latest --fail-on high

# Scan from SBOM
syft nginx:latest -o cyclonedx-json | grype

Hành động GitHub với Grype

name: Grype Vulnerability Scan

on: [pull_request]

jobs:
  scan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Build image
        run: docker build -t myapp:test .
      
      - name: Scan with Grype
        uses: anchore/scan-action@v3
        with:
          image: myapp:test
          fail-build: true
          severity-cutoff: high

Nâng cao: Cơ sở dữ liệu về lỗ hổng bảo mật tùy chỉnh

Đối với môi trường có air-gap hoặc theo dõi CVE tùy chỉnh:

# Download Trivy vulnerability database
trivy image --download-db-only --cache-dir /tmp/trivy-cache

# Use offline database
trivy image --skip-db-update --cache-dir /tmp/trivy-cache nginx:latest

Xử lý kết quả dương tính giả

Tập tin bỏ qua Trivy

Tạo .trivyignore trong repo của bạn:

# False positive - vulnerability not applicable to our use case
CVE-2023-12345

# Acknowledged risk - approved by security team (JIRA: SEC-789)
CVE-2023-67890

# Waiting for upstream fix - temporary exception expires 2025-06-01
CVE-2024-11111

Ngoại lệ dựa trên chính sách

# trivy-policy.rego
package trivy

import rego.v1

# Ignore specific CVE in specific package
ignore contains result if {
    input.PkgName == "spring-core"
    input.VulnerabilityID == "CVE-2023-12345"
    result := {"reason": "False positive - not using vulnerable code path"}
}

# Allow medium severity in dev environment
ignore contains result if {
    input.Severity == "MEDIUM"
    os.Getenv("ENVIRONMENT") == "dev"
    result := {"reason": "Medium severity allowed in dev"}
}

Sử dụng với Trivy:

trivy image --ignore-policy trivy-policy.rego nginx:latest

Tích hợp với Cổng SBOM

Kết hợp tạo SBOM với quét lỗ hổng:

#!/bin/bash
# combined-gate.sh

set -e

IMAGE="$1"

# Generate SBOM
echo "Generating SBOM..."
syft "$IMAGE" -o cyclonedx-json > sbom.json

# Upload SBOM to artifact repository
echo "Uploading SBOM to registry..."
curl -X POST -H "Content-Type: application/json" \
  --data @sbom.json \
  "https://sbom-registry.company.com/api/upload"

# Scan for vulnerabilities
echo "Scanning for vulnerabilities..."
trivy sbom sbom.json --severity CRITICAL,HIGH --exit-code 1

echo "✓ All gates passed"

Giám sát liên tục

Toán tử Trivy Kubernetes

Để giám sát lỗ hổng thời gian chạy:

# Install Trivy Operator
helm repo add aqua https://aquasecurity.github.io/helm-charts/
helm install trivy-operator aqua/trivy-operator \
  --namespace trivy-system --create-namespace

# View vulnerability reports
kubectl get vulnerabilityreports -A
kubectl describe vulnerabilityreport <report-name>

Cảnh báo về CVE mới

# cronjob-vuln-monitor.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: vuln-monitor
spec:
  schedule: "0 */6 * * *"  # Every 6 hours
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: trivy
            image: aquasec/trivy:latest
            command:
            - sh
            - -c
            - |
              trivy image --severity CRITICAL myapp:production > /tmp/report.txt
              if [ -s /tmp/report.txt ]; then
                curl -X POST "$SLACK_WEBHOOK" \
                  -d "{\"text\":\"⚠️ New critical vulnerabilities detected in production\"}"
              fi
            env:
            - name: SLACK_WEBHOOK
              valueFrom:
                secretKeyRef:
                  name: slack-webhook
                  key: url
          restartPolicy: OnFailure

Thực tiễn tốt nhất

1. Quét ở nhiều giai đoạn

Development → Build → Deploy → Runtime
    ↓           ↓        ↓         ↓
  IDE scan   Image    Registry  Operator
             scan      scan      monitoring

2. Thực thi tiến bộ

# Week 1-2: Monitor only (exit-code 0)
trivy image --severity CRITICAL nginx:latest

# Week 3-4: Block CRITICAL only
trivy image --exit-code 1 --severity CRITICAL nginx:latest

# Week 5+: Block CRITICAL and HIGH
trivy image --exit-code 1 --severity CRITICAL,HIGH nginx:latest

3. Ghim phụ thuộc

Luôn ghim các phiên bản chính xác để đảm bảo các bản dựng có thể tái tạo:

# Bad: vulnerable to supply chain attacks
FROM node:18

# Good: pinned to specific digest
FROM node:18.19.0@sha256:a6385a6bb2fdcb7c48fc871e35e32af8daaa82c518900be49b76d10c005864c2

4. Khắc phục tự động

name: Auto-update Dependencies

on:
  schedule:
    - cron: '0 0 * * 1'  # Weekly on Monday

jobs:
  update:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Update dependencies
        run: |
          npm update
          npm audit fix
      
      - name: Run vulnerability scan
        run: trivy fs --exit-code 1 --severity CRITICAL .
      
      - name: Create pull request
        if: success()
        uses: peter-evans/create-pull-request@v5
        with:
          title: "chore: Update dependencies (weekly)"
          body: "Automated dependency updates passed vulnerability gates"

Số liệu cần theo dõi

  • Thời gian trung bình để khắc phục (MTTR): Thời gian trung bình từ khi phát hiện CVE đến khi khắc phục
  • Vulnerability Debt: Tổng số lỗ hổng đã biết theo mức độ nghiêm trọng
  • Tốc độ chặn cổng: Tỷ lệ phần trăm các bản dựng bị chặn bởi các cổng dễ bị tổn thương
  • Tỷ lệ dương tính giả: Tỷ lệ phần trăm CVE được gắn cờ được đánh dấu là không áp dụng

Khắc phục sự cố

Lỗi "Quá nhiều lỗ hổng"

Problem: Ứng dụng kế thừa với hàng trăm CVE chặn tất cả hoạt động triển khai.

Solution: Triển khai đường cơ sở và giảm dần:

# Generate baseline
trivy image myapp:current --format json > baseline.json

# Only fail on NEW vulnerabilities
trivy image myapp:new --format json > current.json
diff baseline.json current.json || exit 1

Lỗi cập nhật cơ sở dữ liệu

Problem: Trivy không tải xuống được cơ sở dữ liệu về lỗ hổng trong CI.

Solution: Lưu trữ cơ sở dữ liệu:

- name: Cache Trivy DB
  uses: actions/cache@v3
  with:
    path: ~/.cache/trivy
    key: trivy-db-${{ github.run_id }}
    restore-keys: trivy-db-

Vấn đề về hiệu suất

Problem: Quá trình quét mất quá nhiều thời gian trong CI pipeline.

Solution: Sử dụng chức năng quét song song:

# Scan image and filesystem in parallel
trivy image myapp:test &
trivy fs . &
wait

Các bước tiếp theo