Fork me on GitHub

容器安全之镜像扫描

一、镜像扫描

  1. docker scan
  2. Trivy(推荐使用)
  3. clair
  4. harbor-scanner 支持中文漏洞库

针对上述解决方案,我们调查了 TrivyClaireAnchore EngineQuayDocker hubGCR 等几种扫描工具,从不同维度进行对比。(来源于网络)

img

本文主要介绍下 docker scanTrivy

二、镜像扫描可以存在的场景

  1. 构建镜像的整个流水线

    Jenkins 或者 gitlab Runner集成

    1. docker scan
    2. Trivy
  2. 镜像仓库定期扫描

    1. harbor-scanner-trivy
    2. harbor-scanner
  3. 运行时,在镜像拉取到主机节点,启动时扫描镜像漏洞,禁止镜像运行。

    1. ImagePolicyWebhook

三、 docker scan

Docker 本地镜像的漏洞扫描

docker scandocker 官方推出来的插件。

使用之前需要安装此插件

3.1、Centos 安装插件

yum install docker-scan-plugin

获取当前安装的版本

docker scan --accept-license --version
Version:    v0.12.0
Git commit: 1074dd0
Provider:   Snyk (1.790.0 (standalone))

3.2、登录 dockerhub 用户

在使用 docker scan 之前,我们需要登录我们的 dockerhub 用户,否则扫描不了。

[root@ops-111-111 ~]# docker scan hello-world
Docker Scan relies upon access to Snyk, a third party provider, do you consent to proceed using Snyk? (y/N)
y
failed to get DockerScanID: You need to be logged in to Docker Hub to use the scan feature.

If you are not using Docker Desktop, either
- use the "docker login" CLI command with a username and password. Note this will not work if
  2FA is required or if SSO enforcement is enabled on Docker Hub; or
- use the "docker login" CLI command with a username and Personal Access Token. This requires
  a token to be generated in advance.

If you are using Docker Desktop: login via the UI or whale menu

登录

[root@ops-111-111 ~]# docker login 
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: djxslp
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

3.3、扫描

[root@ops-111-111 ~]# docker scan hello-world

Testing hello-world...

Package manager:   linux
Project name:      docker-image|hello-world
Docker image:      hello-world
Platform:          linux/amd64

✔ Tested hello-world for known vulnerabilities, no vulnerable paths found.

Note that we do not currently have vulnerability data for your image.

For more free scans that keep your images secure, sign up to Snyk at https://dockr.ly/3ePqVcp

3.4、使用限制

免费扫描限制为10/月。

四、Trivy(推荐使用)

Trivy 是 漏洞、配置错误、Secret 等 全面且多功能的安全扫描仪。

GitHub - aquasecurity/trivy: Find vulnerabilities, misconfigurations, secrets, SBOM in containers, Kubernetes, code repositories, clouds and more

阿里云镜像仓库,镜像云安全扫描引擎也默认提供Trivy扫描引擎

支持扫描

  • Container Image
  • Filesystem
  • Git Repository (remote)
  • Virtual Machine Image
  • Kubernetes
  • AWS

4.1、 安装

wget https://github.com/aquasecurity/trivy/releases/download/v0.38.1/trivy_0.38.1_Linux-64bit.deb
apt install trivy_0.38.1_Linux-64bit.deb

示例命令

trivy image python:3.4-alpine

trivy image  -s  HIGH   python:3.4-alpine  # 指定漏洞等级

trivy k8s --report summary cluster # 扫描集群

4.2、下载DB

注意下载 DB 是比较慢的,如果我们有对应的梯子,可以用的话最好使用梯子。 我这边是通过我电脑的 Clash 监听的端口,然后我服务器配置 http 和 https 代理。

2023-03-29T10:10:09.318+0800	INFO	Need to update DB
2023-03-29T10:10:09.318+0800	INFO	DB Repository: ghcr.io/aquasecurity/trivy-db
2023-03-29T10:10:09.318+0800	INFO	Downloading DB...

临时使用代理

export http_proxy="http://http_proxy:7890"
export https_proxy="http://http_proxy:7890"

4.3、使用

trivy  image -s MEDIUM,HIGH,CRITICAL  test-demo  --timeout 30m 

结果

Total: 752 (MEDIUM: 688, HIGH: 61, CRITICAL: 3)
┌──────────────────────────┬──────────────────┬──────────┬───────────────────────┬──────────────────────────┬──────────────────────────────────────────────────────────────┐
│         Library          │  Vulnerability   │ Severity │   Installed Version   │      Fixed Version       │                            Title                             │
├──────────────────────────┼──────────────────┼──────────┼───────────────────────┼──────────────────────────┼──────────────────────────────────────────────────────────────┤
│ audit-libs               │ CVE-2015-5186    │ MEDIUM   │ 2.8.1-3.el7           │                          │ Audit: log terminal emulator escape sequences handling       │
│                          │                  │          │                       │                          │ https://avd.aquasec.com/nvd/cve-2015-5186                    │
├──────────────────────────┼──────────────────┤          ├───────────────────────┼──────────────────────────┼──────────────────────────────────────────────────────────────┤
│ bash                     │ CVE-2012-6711    │          │ 4.2.46-30.el7         │                          │ bash: heap-based buffer overflow during echo of unsupported  │
│                          │                  │          │                       │                          │ characters                                                   │
│                          │                  │          │                       │                          │ https://avd.aquasec.com/nvd/cve-2012-6711                    │
│                          ├──────────────────┤          │                       ├──────────────────────────┼──────────────────────────────────────────────────────────────┤
│                          │ CVE-2019-9924    │          │                       │ 4.2.46-34.el7            │ bash: BASH_CMD is writable in restricted bash shells         │
│                          │                  │          │                       │                          │ https://avd.aquasec.com/nvd/cve-2019-9924                    │
Java (jar)
==========
Total: 14 (MEDIUM: 2, HIGH: 10, CRITICAL: 2)

┌─────────────────────────────────────────────────┬────────────────┬──────────┬───────────────────┬──────────────────────────────────────────────────────┬─────────────────────────────────────────────────────────────┐
│                     Library                     │ Vulnerability  │ Severity │ Installed Version │                    Fixed Version                     │                            Title                            │
├─────────────────────────────────────────────────┼────────────────┼──────────┼───────────────────┼──────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────┤
│                                                 │ CVE-2019-10247 │ MEDIUM   │                   │ 9.2.28.v20190418, 9.3.27.v20190418, 9.4.17.v20190418 │ jetty: error path information disclosure                    │
│                                                 │                │          │                   │                                                      │ https://avd.aquasec.com/nvd/cve-2019-10247                  │
├─────────────────────────────────────────────────┼────────────────┼──────────┤      

五、镜像扫描到部署阶段

5.1、ImagePolicyWebhook 容器镜像扫描

具体的操作可以查看 k8s 官方文档: ImagePolicyWebhook

其中我们可以看到需要调用到第三方的webhook, 是存在一个kainlite/kube-image-bouncer:ImagePolicyWebhook 和 GenericAdmissionWebhook Kubernetes 入场控制器的简单端点 (github.com) 组件。 这个组件实现的内容就是判断我们当前的镜像是否为 latest 的镜像。 是返回 "allowed": false

我们其实是可以对于这个组件进行改造,增加一些定制化的内容, 比如去调用一些接口获取对应的镜像的安全扫描结果,如果扫描结果是pass 的话,就可以部署,否则的话,就不允许部署。

posted @ 2023-03-31 09:21  自由早晚乱余生  阅读(1232)  评论(0编辑  收藏  举报