当前位置:网站首页>trivy【1】工具扫描运用
trivy【1】工具扫描运用
2022-07-28 01:49:00 【ghostwritten】
文章目录

1. 简介
Trivy 是一个简单而全面的漏洞/错误配置/秘密扫描器,用于容器和其他工件。 检测操作系统包(Alpine、RHEL、CentOS 等)和特定语言包(Bundler、Composer、npm、yarn 等)的漏洞。此外,扫描Terraform 和 Kubernetes 等基础架构即代码 (IaC) 文件,以检测使您的部署面临攻击风险的潜在配置问题。 还扫描硬编码的秘密vyTrivyTrivyTrivy比如密码、API 密钥和令牌。 Trivy易于使用。只需安装二进制文件,您就可以扫描了。扫描所需要做的就是指定一个目标,例如容器的image名称

Trivy 检测到两种类型的安全问题:
- 漏洞
- 错误配置
Trivy 可以扫描四种不同的工件:
- 容器镜像
- 文件系统和Rootfs
- Git 存储库
- Kubernetes
Trivy 可以在两种不同的模式下运行:
- Standalone
- Client/Server
Trivy 可以作为 Kubernetes Operator 运行:
- Kubernetes Operator
2. 特征
全面的漏洞检测
- 操作系统包(Alpine、Red Hat Universal Base Image、Red Hat Enterprise
Linux、CentOS、AlmaLinux、Rocky Linux、CBL-Mariner、Oracle
Linux、Debian、Ubuntu、Amazon Linux、openSUSE Leap、SUSE Enterprise
Linux、Photon OS 和 Distroless) - 特定于语言的包(Bundler、Composer、Pipenv、Poetry、npm、yarn、pnpm、Cargo、NuGet、Maven
和 Go)
检测 IaC 错误配置
开箱即用地提供了多种内置策略:
- Kubernetes
- docker
- Terraform
支持自定义策略
简单
- 仅指定镜像名称、包含 IaC 配置的目录或工件名称
快速
- 第一次扫描将在 10 秒内完成(取决于您的网络)。随后的扫描将在几秒钟内完成。
- 与其他扫描程序在第一次运行时需要很长时间(约 10 分钟)获取漏洞信息并鼓励您维护持久的漏洞数据库不同,Trivy 是无状态的,不需要维护或准备。
简易安装
- apt-get install,yum install并且brew install是可能的(参见安装)
- 没有先决条件,例如安装数据库、库等。
高准确率
- 尤其是 Alpine Linux 和 RHEL/CentOS
- 其他操作系统也很高
DevSecOps
- 适用于Travis CI、CircleCI、Jenkins、GitLab CI 等 CI。
- 请参阅CI 示例
支持多种格式
- 容器图像
- Docker Engine 中作为守护进程运行的本地映像
- Podman (>=2.0) 中的本地图像暴露了一个套接字
- Docker Registry 中的远程镜像,例如 Docker Hub、ECR、GCR 和 ACR
- 存储在docker save/podman save格式文件中的 tar 存档
- 符合OCI 图像格式的图像目录
- 本地文件系统和 rootfs
- 远程 git 仓库
SBOM(软件物料清单)支持
- CycloneDX
- SPDX
- GitHub Dependency Snapshots
3. 扫描镜像
trivy image nginx:1.18.0
trivy image --severity CRITICAL nginx:1.18.0
trivy image --severity CRITICAL, HIGH nginx:1.18.0
trivy image --ignore-unfixed nginx:1.18.0
# Scanning image tarball
docker save nginx:1.18.0 > nginx.tar
trivy image --input archive.tar
# Scan and output results to file
trivy image --output python_alpine.txt python:3.10.0a4-alpine
trivy image --severity HIGH --output /root/python.txt python:3.10.0a4-alpine
# Scan image tarball
trivy image --input alpine.tar --format json --output /root/alpine.json
扫描解压镜像文件系统
$ docker export $(docker create alpine:3.10.2) | tar -C /tmp/rootfs -xvf -
$ trivy rootfs /tmp/rootfs
4. 嵌入 Dockerfile 扫描
通过将 Trivy 嵌入 Dockerfile 来扫描您的图像作为构建过程的一部分。这种方法可用于更新当前使用 Aqua 的Microscanner的 Dockerfile
$ cat Dockerfile
FROM alpine:3.7
RUN apk add curl \
&& curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin \
&& trivy rootfs --exit-code 1 --no-progress /
$ docker build -t vulnerable-image .
或者,您可以在多阶段构建中使用 Trivy。从而避免了不安全curl | sh。图像也没有改变。
[...]
# Run vulnerability scan on build image
FROM build AS vulnscan
COPY --from=aquasec/trivy:latest /usr/local/bin/trivy /usr/local/bin/trivy
RUN trivy rootfs --exit-code 1 --no-progress /
[...]
5. 扫描文件系统
5.1 独立模式
本地项目
trivy fs /path/to/project
trivy fs ~/src/github.com/aquasecurity/trivy-ci-test
单个文件
trivy fs ~/src/github.com/aquasecurity/trivy-ci-test/Pipfile.lock
5.2 client/server
trivy server
trivy fs --server http://localhost:4954 --severity CRITICAL ./integration/testdata/fixtures/fs/pom/
6. 扫描 Rootfs
扫描根文件系统(例如主机、虚拟机映像或未打包的容器映像文件系统)
$ trivy rootfs /path/to/rootfs
$ docker run --rm -it alpine:3.11
/ # curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
/ # trivy rootfs /
7. 扫描 git 仓库
7.1 扫描您的远程 git 存储库
trivy repo https://github.com/knqyf263/trivy-ci-test
7.2 扫描分支
在提供的远程存储库上传递–branch具有有效分支名称的 agrument:
$ trivy repo --branch <branch-name> <repo-name>
7.3 扫描到 Commit
在提供的远程存储库上传递–commit具有有效提交哈希的 agrument:
$ trivy repo --commit <commit-hash> <repo-name>
7.4 扫描标签
在提供的远程存储库上传递–tag带有有效标签的 agrument:
$ trivy repo --tag <tag-name> <repo-name>
7.5 扫描私有存储库
为了扫描私有 GitHub 或 GitLab 存储库,必须分别设置环境变量GITHUB_TOKEN或,并使用有权访问正在扫描的私有存储库的有效令牌:GITLAB_TOKEN
环境变量将GITHUB_TOKEN优先于GITLAB_TOKEN,因此如果要扫描私有 GitLab 存储库,则GITHUB_TOKEN必须取消设置。
例如:
$ export GITHUB_TOKEN="your_private_github_token"
$ trivy repo <your private GitHub repo URL>
$
$ # or
$ export GITLAB_TOKEN="your_private_gitlab_token"
$ trivy repo <your private GitLab repo URL>
8. 扫描错误配置
只需指定一个包含 IaC 文件的目录,例如 Terraform、CloudFormation 和 Dockerfile。
格式:trivy config [YOUR_IaC_DIRECTORY]
实例
$ ls build/
Dockerfile
$ trivy config ./build
2022-05-16T13:29:29.952+0100 INFO Detected config files: 1
Dockerfile (dockerfile)
=======================
Tests: 23 (SUCCESSES: 22, FAILURES: 1, EXCEPTIONS: 0)
Failures: 1 (UNKNOWN: 0, LOW: 0, MEDIUM: 1, HIGH: 0, CRITICAL: 0)
MEDIUM: Specify a tag in the 'FROM' statement for image 'alpine'
══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
When using a 'FROM' statement you should use a specific tag to avoid uncontrolled behavior when the image is updated.
See https://avd.aquasec.com/misconfig/ds001
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Dockerfile:1
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 [ FROM alpine:latest
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
您还可以通过--security-checks config.
$ trivy image --security-checks config IMAGE_NAME
$ trivy fs --security-checks config /path/to/dir
与config子命令不同image,fs和repo子命令还可以同时扫描漏洞和秘密。您可以指定--security-checks vuln,config,secret启用漏洞和秘密检测以及错误配置检测。
$ ls myapp/
Dockerfile Pipfile.lock
$ trivy fs --security-checks vuln,config,secret --severity HIGH,CRITICAL myapp/
2022-05-16T13:42:21.440+0100 INFO Number of language-specific files: 1
2022-05-16T13:42:21.440+0100 INFO Detecting pipenv vulnerabilities...
2022-05-16T13:42:21.440+0100 INFO Detected config files: 1
Pipfile.lock (pipenv)
=====================
Total: 1 (HIGH: 1, CRITICAL: 0)
┌──────────┬────────────────┬──────────┬───────────────────┬───────────────┬───────────────────────────────────────────────────────────┐
│ Library │ Vulnerability │ Severity │ Installed Version │ Fixed Version │ Title │
├──────────┼────────────────┼──────────┼───────────────────┼───────────────┼───────────────────────────────────────────────────────────┤
│ httplib2 │ CVE-2021-21240 │ HIGH │ 0.12.1 │ 0.19.0 │ python-httplib2: Regular expression denial of service via │
│ │ │ │ │ │ malicious header │
│ │ │ │ │ │ https://avd.aquasec.com/nvd/cve-2021-21240 │
└──────────┴────────────────┴──────────┴───────────────────┴───────────────┴───────────────────────────────────────────────────────────┘
Dockerfile (dockerfile)
=======================
Tests: 17 (SUCCESSES: 16, FAILURES: 1, EXCEPTIONS: 0)
Failures: 1 (HIGH: 1, CRITICAL: 0)
HIGH: Last USER command in Dockerfile should not be 'root'
════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Running containers with 'root' user can lead to a container escape situation. It is a best practice to run containers as non-root users, which can be done by adding a 'USER' statement to the Dockerfile.
See https://avd.aquasec.com/misconfig/ds002
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Dockerfile:3
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
3 [ USER root
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
在上面的示例中,Trivy 检测到了 Python 依赖项的漏洞和 Dockerfile 中的错误配置。
9. 类型检测
指定目录可以包含混合类型的 IaC 文件。Trivy 自动检测配置类型并应用相关策略。
例如,以下示例将 Terraform、CloudFormation、Kubernetes、Helm Charts 和 Dockerfile 的 IaC 文件保存在同一目录中。
$ ls iac/
Dockerfile deployment.yaml main.tf mysql-8.8.26.tar
$ trivy conf --severity HIGH,CRITICAL ./iac
输出:
Dockerfile (dockerfile)
=======================
Tests: 23 (SUCCESSES: 22, FAILURES: 1, EXCEPTIONS: 0)
Failures: 1 (HIGH: 1, CRITICAL: 0)
...
deployment.yaml (kubernetes)
============================
Tests: 28 (SUCCESSES: 15, FAILURES: 13, EXCEPTIONS: 0)
Failures: 13 (MEDIUM: 4, HIGH: 1, CRITICAL: 0)
...
main.tf (terraform)
===================
Tests: 23 (SUCCESSES: 14, FAILURES: 9, EXCEPTIONS: 0)
Failures: 9 (HIGH: 6, CRITICAL: 1)
...
bucket.yaml (cloudformation)
============================
Tests: 9 (SUCCESSES: 3, FAILURES: 6, EXCEPTIONS: 0)
Failures: 6 (UNKNOWN: 0, LOW: 0, MEDIUM: 2, HIGH: 4, CRITICAL: 0)
...
mysql-8.8.26.tar:templates/primary/statefulset.yaml (helm)
==========================================================
Tests: 20 (SUCCESSES: 18, FAILURES: 2, EXCEPTIONS: 0)
Failures: 2 (MEDIUM: 2, HIGH: 0, CRITICAL: 0)
参考:
边栏推荐
- Use of Day6 functions and modules
- Interpretation of cesium3dtilesets using customshader and examples of Omni effects
- [leetcode] 13. linked list cycle · circular linked list
- Four methods of modifying MySQL password (suitable for beginners)
- [TA frost wolf \u may - hundred people plan] Figure 3.7 TP (d) r architecture of mobile terminal
- Usage of delegate
- [tutorial of using idea] shortcut key of idea
- 【信号去噪】基于卡尔曼滤波实现信号去噪附matlab代码
- Consolidate the data foundation in the data center
- [hcip] BGP features
猜你喜欢

retainface使用报错:ModuleNotFoundError: No module named 'rcnn.cython.bbox'

【微信小程序开发(六)】绘制音乐播放器环形进度条

Hardware standard

关于Sqli-labs单引号不报错的问题

Learn this trick and never be afraid to let the code collapse by mistake

The virtual host website cannot access the self-test method

【信号去噪】基于卡尔曼滤波实现信号去噪附matlab代码
![[solution] solve the problem of SSH connection being inactive for a long time and being stuck and disconnected](/img/66/99bd61223cbe622db3e28474f4fa15.png)
[solution] solve the problem of SSH connection being inactive for a long time and being stuck and disconnected

别人发你的jar包你如何使用(如何使用别人发您的jar包)

LoRaWAN中的网关和chirpstack到底如何通信的?UDP?GRPC?MQTT?
随机推荐
[brother hero's July training] day 26: check the collection
数据中台夯实数据基础
A 64 bit 8-stage pipelined adder based on FPGA
Say yes, I will love you, and I will love you well
[hcip] BGP features
【OpenGL】GLES20.glClear
Emotional drama in the world Zhou Bingkun lost his job because he saw Tu Zhiqiang and was shot
Usage of delegate
[tutorial of using idea] shortcut key of idea
欢迎使用CSDN-markdown编辑器阿萨德
POC模拟攻击利器 —— Nuclei入门(一)
LoRaWAN中的网关和chirpstack到底如何通信的?UDP?GRPC?MQTT?
Redis AOF log persistence
Arm32进行远程调试
Flutter神操作学习之(满级攻略)
How is insert locked in MySQL? (glory Collection Edition)
what‘s the meaning of “rc“ in release name
Representation of children and brothers of trees
[understanding of opportunity -53]: Yang Mou stands up and plots to defend himself
Smart contract security -- selfdestroy attack