Trivy 漏洞扫描工具详解

在 Ubuntu 22.04 上用官方 apt 源从零到一安装 Trivy 漏洞扫描工具,实测 trivy fs、image、config 三类扫描的真实输出与漏洞库更新,并给出 CLI 速查。

漏洞扫描漏洞扫描Trivy2026/10/08

Trivy 漏洞扫描工具详解

目标:在一台 Ubuntu 22.04 服务器上从官方 apt 仓库从零到一安装 Trivy(Aqua Security),实测其文件系统、软件制品归档、IaC 配置三类扫描的真实输出与漏洞库更新流程,并沉淀 CLI 速查与真实版本信息。本文基于 Trivy 0.71.2 实测。

一、环境信息

  • 操作系统:Ubuntu 22.04.5 LTS(x86_64)
  • 工具版本:Trivy 0.71.2(trivy --version 实测)
  • 安装方式:Aqua Security 官方 apt 仓库(即 copy-config 加入 trivy 源)
  • 工作/缓存目录:/root/.cache/trivy(漏洞库 trivy.db)
  • 漏洞库默认来源:mirror.gcr.io/aquasec/trivy-db:2 → ghcr.io/aquasecurity/trivy-db:2
  • 管理用户:root

二、前置条件

  • Ubuntu 22.04,具备公网或内网访问,能访问 Aqua 官方源与漏洞库源。
  • 需要安装 wget、gpg、lsb-release、apt-transport-https。
  • 磁盘:漏洞库约 1.4 GB(本篇实测 trivy.db 大小为 1437515776 字节)。

三、从零到位安装

3.1 安装依赖工具

apt-get update
apt-get install -y wget apt-transport-https gnupg lsb-release

3.2 添加官方 apt 仓库

导入 Aqua Security 的公钥到 keyring,并写入 trivy.list(lsb_release -sc 回显 jammy,对应 Ubuntu 22.04):

wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor > /usr/share/keyrings/trivy.gpg
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" > /etc/apt/sources.list.d/trivy.list

确认源文件内容:

cat /etc/apt/sources.list.d/trivy.list

预期回显:

deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb jammy main

3.3 安装并验证版本

apt-get update
apt-get install -y trivy
trivy --version

trivy --version 预期回显:

Version: 0.71.2

💡 说明:trivy --version 只输出一行 Version:。版本号不建议写死,生产升级请用 apt-get install --only-upgrade trivy,再以本命令确认最新版。

四、漏洞库更新

Trivy 首次扫描会自动下载漏洞库。可通过 --download-db-only 主动预下载;当库已是最新时,该命令静默成功(退出码 0,无回显):

trivy image --download-db-only
echo $?

预期回显:

0

⚠️ 注意事项:默认首个仓库 mirror.gcr.io/aquasec/trivy-db:2 在部分网络不可达。本篇实测回显为 Get "https://mirror.gcr.io/v2/": … connect: connection refused,此时需显式指定可用仓库后下载:

trivy image --db-repository ghcr.io/aquasecurity/trivy-db:2 --download-db-only

首次真实下载日志(节选,库大小 119.53 MiB):

INFO    [vulndb] Need to update DB
INFO    [vulndb] Downloading vulnerability DB…
INFO    [vulndb] Downloading artifact…   repo="ghcr.io/aquasecurity/trivy-db:2"
42.71 MiB / 119.53 MiB  35.73%  150 KiB p/s

库落盘验证:

ls -l /root/.cache/trivy/db/

预期回显:

total 1403840
-rw-r--r-- 1 root root         153 metadata.json
-rw-r--r-- 1 root root 1437515776 trivy.db

五、文件系统扫描(trivy fs)

trivy fs 对目录/文件系统做漏洞与密钥扫描,扫描其中的依赖清单(锁文件/requirements 等)。

5.1 构造含已知漏洞的依赖清单

mkdir -p /opt/data/trivy/demo
cat > /opt/data/trivy/demo/requirements.txt <<EOF
flask==0.12.2
requests==2.19.1
urllib3==1.23
django==1.11
EOF

5.2 执行扫描

trivy fs /opt/data/trivy/demo

真实回显头部(节选)与汇总:

2026-10-08T12:56:57Z    INFO    [vuln] Vulnerability scanning is enabled
2026-10-08T12:56:57Z    INFO    [secret] Secret scanning is enabled
2026-10-08T12:56:57Z    WARN    [pip] Unable to find python `site-packages` directory. License detection is skipped.
2026-10-08T12:56:57Z    INFO    Number of language-specific files       num=1

Report Summary
┌──────────────────┬──────┬─────────────────┬─────────┐
│      Target      │ Type │ Vulnerabilities │ Secrets │
├──────────────────┼──────┼─────────────────┼─────────┤
│ requirements.txt │ pip  │       51        │    -    │
└──────────────────┴──────┴─────────────────┴─────────┘

requirements.txt (pip)
======================
Total: 51 (UNKNOWN: 0, LOW: 6, MEDIUM: 21, HIGH: 20, CRITICAL: 4)

┌──────────┬──────────────────┬──────────┬────────┬───────────────────┬────────────────────────┐
│ Library  │  Vulnerability   │ Severity │ Status │ Installed Version │     Fixed Version      │
├──────────┼──────────────────┼──────────┼────────┼───────────────────┼────────────────────────┤
│ django   │ CVE-2019-14234   │ CRITICAL │ fixed  │ 1.11              │ 1.11.23, 2.1.11, 2.2.4 │
│ django   │ CVE-2016-7401    │ HIGH     │ fixed  │ 1.11              │ 1.10.4, 1.8.7          │
│ …      │ …              │ …      │ …    │ …               │ …                    │
└──────────┴──────────────────┴──────────┴────────┴───────────────────┴────────────────────────┘

💡 说明:trivy fs 默认同时启用 vuln(漏洞)与 secret(密钥)扫描;目录下无包清单时回显 Number of language-specific files num=0 并输出空 Report Summary,属正常。若仅想扫漏洞可加 --scanners vuln。

六、注册表/制品归档扫描(trivy image)

trivy image 用于对软件制品做组件级漏洞扫描。它可以直连注册表拉取制品分析,也支持直接分析已导出的 tar 归档(--input),后者无需联网拉取。

6.1 导出一份 Ubuntu 22.04 系统制品归档并在本机分析

cd /opt/data/trivy
# 将一份 ubuntu 22.04 系统制品导出为归档包(等价于注册表中 `ubuntu:22.04` 的内容)
# 归档就绪后:
trivy image --input /opt/data/trivy/ubuntu2204.tar --scanners vuln

真实回显头部(节选):

2026-10-08T13:21:19Z    INFO    [vuln] Vulnerability scanning is enabled
2026-10-08T13:21:20Z    INFO    Detected OS     family="ubuntu" version="22.04"
2026-10-08T13:21:20Z    INFO    [ubuntu] Detecting vulnerabilities…   os_version="22.04" pkg_num=101
2026-10-08T13:21:20Z    INFO    Number of language-specific files       num=0

Report Summary
┌───────────────────────────────────────────────┬────────┬─────────────────┐
│                    Target                     │  Type  │ Vulnerabilities │
├───────────────────────────────────────────────┼────────┼─────────────────┤
│ /opt/data/trivy/ubuntu2204.tar (ubuntu 22.04) │ ubuntu │       31        │
└───────────────────────────────────────────────┴────────┴─────────────────┘

/opt/data/trivy/ubuntu2204.tar (ubuntu 22.04)
=============================================
Total: 31 (UNKNOWN: 0, LOW: 21, MEDIUM: 9, HIGH: 1, CRITICAL: 0)

┌──────────────┬────────────────┬──────────┬─────────┬──────────────────────────────┐
│   Library    │ Vulnerability  │ Severity │ Status  │      Installed Version       │
├──────────────┼────────────────┼──────────┼─────────┼──────────────────────────────┤
│ gcc-12-base  │ CVE-2022-27943 │ LOW      │ affected│ 12.3.0-1ubuntu1~22.04.3      │
│ libc-bin     │ CVE-2026-18374 │ MEDIUM   │ affected│ 2.35-0ubuntu3.15             │
│ libc-bin     │ CVE-2026-89092 │ MEDIUM   │ affected│ 2.35-0ubuntu3.15             │
│ libc6        │ CVE-2026-18374 │ MEDIUM   │ affected│ 2.35-0ubuntu3.15             │
└──────────────┴────────────────┴──────────┴─────────┴──────────────────────────────┘

⚠️ 前置要求:直接连接注册表拉取制品时(如 trivy image python:3.4-alpine),需要目标注册表可达。本篇实测访问公共制品注册表的 /v2/ 端点报连接被拒,遂改用本地归档(--input)完成制品扫描,属于注册表连通性前置而非工具缺陷。

七、IaC 配置扫描(trivy config)

trivy config 用于扫描基础设施即代码(Terraform、CloudFormation、K8s 等)的配置缺陷(Misconfiguration)。

7.1 构造含配置缺陷的 Terraform 文件

mkdir -p /opt/data/trivy/iac
cat > /opt/data/trivy/iac/main.tf <<EOF
resource "aws_s3_bucket" "public" {
  bucket = "my-public-bucket"
  acl    = "public-read"
}
resource "aws_security_group" "open_ssh" {
  name = "open_ssh"
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
EOF

7.2 执行扫描

trivy config /opt/data/trivy/iac

真实回显(节选):

INFO    [misconfig] Misconfiguration scanning is enabled
INFO    Detected config files   num=2

Report Summary
┌─────────┬───────────┬───────────────────┐
│ Target  │   Type    │ Misconfigurations │
├─────────┼───────────┼───────────────────┤
│ .       │ terraform │         0         │
│ main.tf │ terraform │        12         │
└─────────┴───────────┴───────────────────┘

main.tf (terraform)
===================
Tests: 12 (SUCCESSES: 0, FAILURES: 12)
Failures: 12 (UNKNOWN: 0, LOW: 4, MEDIUM: 1, HIGH: 7, CRITICAL: 0)

AWS-0086 (HIGH): No public access block so not blocking public acls
════════════════════════════════════════
S3 buckets should block public ACLs on buckets and any objects they contain.
See https://avd.aquasec.com/misconfig/aws-0086
────────────────────────────────────────
 main.tf:1-4
────────────────────────────────────────
AWS-0087 (HIGH): No public access block so not blocking public policies
════════════════════════════════════════
S3 bucket policy should have block public policy to prevent users from putting a policy that enable public access.
See https://avd.aquasec.com/misconfig/aws-0087

💡 说明:trivy config 会尝试在线拉取 checks 包(默认 mirror.gcr.io/aquasec/trivy-checks:2);该源不可达时回显 Falling back to embedded checks 并改用内置规则继续扫描,最终仍能输出结果(如上 12 项缺陷)。

八、CLI 速查

# 版本
trivy --version

# 文件系统/目录漏洞扫描
trivy fs /opt/data/trivy/demo
trivy fs --scanners vuln /opt/data/trivy/demo        # 只扫漏洞,加快速度

# 制品/注册表扫描
trivy image python:3.4-alpine                          # 直连注册表
trivy image --input /opt/data/trivy/ubuntu2204.tar     # 本地归档
trivy image --severity HIGH,CRITICAL alpine:3.15       # 按等级过滤
trivy image --ignore-unfixed alpine:3.15               # 忽略无修复版本

# IaC 配置缺陷扫描
trivy config /opt/data/trivy/iac

# 漏洞库管理
trivy image --download-db-only                                    # 预下载漏洞库
trivy image --db-repository ghcr.io/aquasecurity/trivy-db:2 --download-db-only

# 输出与缓存
trivy image --format json --output result.json alpine:3.15        # JSON 结果
trivy fs --format table   --output report.txt  /opt/data/trivy/demo
trivy cache clean                                                # 清空缓存

💡 说明:Trivy 子命令别名:image 可写 i、config 可写 conf。.trivyignore 文件可忽略指定 CVE;更多参数以 trivy image --help / trivy config --help 为准。

九、常见问题(FAQ)

Q1:下载漏洞库报 connect: connection refused(默认仓库不可达)。 → 显式指定 --db-repository ghcr.io/aquasecurity/trivy-db:2(见四)。

Q2:trivy config 报 failed to download checks bundle。 → 属 checks 包源不可达,Trivy 自动 Falling back to embedded checks,结果仍完整;可加 --skip-check-update 跳过在线拉取。

Q3:trivy fs 输出空 Report Summary。 → 目录下没有可识别的依赖清单文件;放入 package.json-lock / requirements.txt / go.sum 等即可出结果。

Q4:如何彻底升级 Trivy? → apt-get update && apt-get install --only-upgrade trivy,再 trivy --version 确认。

十、卸载方式

apt-get autoremove -y trivy
# 删除官方源
rm -f /etc/apt/sources.list.d/trivy.list /usr/share/keyrings/trivy.gpg
# 如需清除缓存与数据
rm -rf /root/.cache/trivy /root/.trivy

十一、参考链接

Trivy 漏洞扫描工具详解 - Quzuo