Trivy-容器镜像扫描工具

1:概述

Trivy(tri 发音为 trigger,vy 发音为 envy)是一个简单而全面的漏洞/错误配置扫描器,用于容器和其他工件。 软件漏洞是软件或操作系统中存在的故障、缺陷或弱点。 Trivy 检测操作系统包(Alpine、RHEL、CentOS 等)和特定语言包(Bundler、Composer、npm、yarn 等)的漏洞。 此外,Trivy 会扫描基础设施即代码 (IaC) 文件,例如 Terraform 和 Kubernetes,以检测使您的部署面临攻击风险的潜在配置问题。 Trivy 易于使用。 只需安装二进制文件,您就可以开始扫描了。 扫描所需要做的就是指定一个目标,例如容器的图像名称。

image

Trivy 检测两种类型的安全问题 :
1:漏洞
2:配置错误

Trivy 可以扫描三种不同的工件:
1:容器镜像
2:文件系统
3:Git存储库

Trivy 可以在两种不同的模式下运行:
1:独立
2:客户端服务器

它旨在用于 CI。在推送到容器注册表或部署应用程序之前,您可以轻松扫描本地容器映像和其他工件。

2:Trivy 特征

1:全面的漏洞检测
	1.1:操作系统包(Alpine、Red Hat Universal Base Image、Red Hat Enterprise Linux、CentOS、Oracle Linux、Debian、Ubuntu、Amazon Linux、openSUSE Leap、SUSE Enterprise Linux、Photon OS 和 Distroless)
	1.2:特定于语言的包(Bundler、Composer、Pipenv、Poetry、npm、yarn、Cargo、NuGet、Maven 和 Go)
2:检测 IaC 错误配置
	2.1:各种各样的内置策略提供了开箱:
		2.1.1:Kubernetes
		2.1.2:码头工人
		2.1.3:地形
		2.1.4:更多即将推出
	2.2:支持自定义策略
3:简单的
	3.1:仅指定图像名称、包含 IaC 配置的目录或工件名称
4:快速地
	4.1:第一次扫描将在 10 秒内完成(取决于您的网络)。随后的扫描将在几秒钟内完成。
	4.2:与在第一次运行时需要很长时间才能获取漏洞信息(约 10 分钟)并鼓励您维护持久漏洞数据库的其他扫描程序不同,Trivy 是无状态的,不需要维护或准备。
5:简易安装
	5.1:apt-get install 或  yum install并且brew install是可能的
	5.2:没有先决条件,如安装DB的,图书馆等
6:高精确度
	6.1:特别是 Alpine Linux 和 RHEL/CentOS
	6.2:其他操作系统也高
7:开发安全运营
	7.1:适用于Travis CI、CircleCI、Jenkins、GitLab CI 等 CI。
8:支持多种格式
	8.1:容器镜像
		8.2:作为守护进程运行的 Docker Engine 中的本地映像
		8.3:Podman中暴露套接字的本地图像
		8.4:Docker Registry 中的远程镜像,例如 Docker Hub、ECR、GCR 和 ACR
		8.5:存储在docker save/podman save格式文件中的 tar 存档
		8.6:符合OCI 图像格式的图像目录
	8.2:本地文件系统
	8.3:远程 git 仓库

3:Trivy 安装

# Yum 源方式安装
[trivy]
name=Trivy repository
baseurl=https://aquasecurity.github.io/trivy-repo/rpm/releases/$releasever/$basearch/
gpgcheck=0
enabled=1
# yum方式
RELEASE_VERSION=$(grep -Po '(?<=VERSION_ID=")[0-9]' /etc/os-release) 
cat << EOF | sudo tee -a /etc/yum.repos.d/trivy.repo
[trivy]
name=Trivy repository
baseurl=https://aquasecurity.github.io/trivy-repo/rpm/releases/$RELEASE_VERSION/\$basearch/
gpgcheck=0
enabled=1
EOF

yum -y update
yum -y install trivy

# RPM方式
rpm -ivh https://github.com/aquasecurity/trivy/releases/download/v0.28.1/trivy_0.28.1_Linux-64bit.rpm

# 参考:https://aquasecurity.github.io/trivy/v0.28.1/getting-started/installation/

4:容器镜像漏洞扫描

# 只需指定镜像仓库(和tag)
[root@virtual_host ~]# trivy image nginx:alpine

image

漏洞等级:
1:HIGH
2:MEDIUM
3:LOW
4:CRITICAL

5:文件系统漏洞扫描

扫描文件系统(例如主机、虚拟机映像或解压缩的容器映像文件系统)
[root@virtual_host ~]# trivy fs /etc

image

6:Git 存储库漏洞扫描

扫描您的远程 git 存储库
[root@virtual_host ~]# trivy repo https://github.com/caddyserver/caddy.git

image

7:Trivy 过滤漏洞

隐藏未修复得漏洞
默认情况下, Trivy还会检测未修补/未修复的漏洞。这意味着即使您更新所有软件包,您也无法修复这些漏洞。如果您想忽略它们,请使用该 --ignore-unfixed 选项。

[root@virtual_host ~]# trivy image --ignore-unfixed nginx:alpine

image

按严重程度
使用 --severity or -s 选项
[root@virtual_host ~]# trivy image -s HIGH,LOW nginx:alpine

image

按漏洞ID
[root@virtual_host ~]# cat .trivyignore   # 就类似本地漏洞库
CVE-2022-27405

[root@virtual_host ~]# trivy image nginx:alpine
# 这时发现就剩一个了,因为指定了库所以就只能扫描到这一个了

image

按类型
使用 --vuln-type 选项

[root@virtual_host ~]# trivy image --vuln-type os nginx:alpine

image

8:漏洞数据库

跳过漏洞数据库得更新
Trivy开始运行时每 12 小时下载一次漏洞数据库。这通常很快,因为数据库的大小只有 10~30MB。但是,如果您甚至想跳过它,请使用该 --skip-db-update 选项。

[root@virtual_host ~]# trivy image --skip-db-update nginx:alpine

image

只下载漏洞数据库
[root@virtual_host ~]# trivy image --download-db-only
# 轻量级数据库
轻量级数据库不包含漏洞详细信息,例如描述和参考。因此,DB 的大小更小,下载速度更快。
当您不需要漏洞详细信息并且适用于 CI/CD 时,此选项很有用。要查找其他信息,您可以在 NVD 网站上搜索漏洞详细信息。https://nvd.nist.gov/vuln/search

--light 选项不会像下面的例子那样显示标题。

image

9:Trivy 缓存

清除缓存
该 --clear-cache 选项删除缓存。
不执行扫描。
[root@virtual_host ~]# trivy image --clear-cache

# 缓存目录
用指定缓存的存储位置 --cache-dir
[root@virtual_host ~]# trivy --cache-dir /opt/ image nginx:alpine

image

[root@virtual_host ~]# ls /opt/db/
metadata.json  trivy.db

缓存后端
# Trivy 支持本地文件系统和 Redis 作为缓存后端。此选项特别适用于客户端/服务器模式。
两个选项: - fs - 缓存路径可以通过--cache-dir - redis:// -redis://[HOST]:[PORT]

[root@virtual_host ~]# trivy server --cache-backend redis://localhost:6379
2022-06-03T12:03:11.196-0400	INFO	Redis cache: redis://localhost:6379
2022-06-03T12:03:11.196-0400	INFO	Listening localhost:4954...

10:Trivy 漏洞报告格式

表格(默认)
[root@virtual_host ~]# trivy image -f table nginx:alpine

image

JSON
[root@virtual_host ~]# trivy image -f json -o nginx.json nginx:alpine
2022-06-03T12:09:13.867-0400	INFO	Detected OS: alpine
2022-06-03T12:09:13.867-0400	INFO	Detecting Alpine vulnerabilities...
2022-06-03T12:09:13.868-0400	INFO	Number of language-specific files: 0
[root@virtual_host ~]# ls
anaconda-ks.cfg  nginx.json
[root@virtual_host ~]# cat nginx.json 
{
  "SchemaVersion": 2,
  "ArtifactName": "nginx:alpine",
  "ArtifactType": "container_image",
  "Metadata": {
    "OS": {
      "Family": "alpine",
      "Name": "3.15.4"
    },
    "ImageID": "sha256:b1c3acb28882519cf6d3a4d7fe2b21d0ae20bde9cfd2c08a7de057f8cfccff15",
    "DiffIDs": [
      "sha256:4fc242d58285699eca05db3cc7c7122a2b8e014d9481f323bd9277baacfa0628",
      "sha256:4721bfafc708dfeaa56a501848081b91415899e5b9f0d26612be42a8ff43e8e2",
      "sha256:45b275e8a06dd152de9b17143ad936aa971fb8abaa0f6d977f7d822c61604abc",
      "sha256:a43749efe4ecc3db4ca41063def5021c8567743c5ea13865d2e620beab1017ea",
      "sha256:d6dd885da0bbd4268a39c7f79fc0e87c8de6641ba4ea33e5ef7e1caa64f8429d",
      "sha256:c0e7c94aefd832d1ee1c565ddcac257ca7851f41c3ef6f079722c69f14f0662b"
--------

12:详细文档

https://github.com/aquasecurity/trivy
posted @ 2022-06-04 00:13  Layzer  阅读(1054)  评论(0编辑  收藏  举报