首页 资讯 > 内容页

容器安全之镜像扫描

时间 : 2023-03-31 09:11:28 来源:博客园
目录一、镜像扫描二、镜像扫描可以存在的场景三、 docker scan3.1、Centos 安装插件3.2、登录 dockerhub 用户3.3、扫描3.4、使用限制四、Trivy(推荐使用)4.1、 安装4.2、下载DB4.3、使用五、镜像扫描到部署阶段5.1、ImagePolicyWebhook 容器镜像扫描一、镜像扫描docker scanTrivy(推荐使用)clairharbor-scanner 支持中文漏洞库

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

本文主要介绍下 docker scanTrivy

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

构建镜像的整个流水线


(资料图片仅供参考)

Jenkins 或者 gitlab Runner集成

docker scanTrivy

镜像仓库定期扫描

harbor-scanner-trivyharbor-scanner

运行时,在镜像拉取到主机节点,启动时扫描镜像漏洞,禁止镜像运行。

ImagePolicyWebhook三、 docker scan

Docker 本地镜像的漏洞扫描

docker scandocker官方推出来的插件。

使用之前需要安装此插件

3.1、Centos 安装插件
yum install docker-scan-plugin

获取当前安装的版本

docker scan --accept-license --versionVersion:    v0.12.0Git commit: 1074dd0Provider:   Snyk (1.790.0 (standalone))
3.2、登录 dockerhub 用户

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

[root@ops-111-111 ~]# docker scan hello-worldDocker Scan relies upon access to Snyk, a third party provider, do you consent to proceed using Snyk? (y/N)yfailed 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: djxslpPassword: WARNING! Your password will be stored unencrypted in /root/.docker/config.json.Configure a credential helper to remove this warning. Seehttps://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeeded
3.3、扫描
[root@ops-111-111 ~]# docker scan hello-worldTesting hello-world...Package manager:   linuxProject name:      docker-image|hello-worldDocker image:      hello-worldPlatform:          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 ImageFilesystemGit Repository (remote)Virtual Machine ImageKubernetesAWS4.1、 安装
wget https://github.com/aquasecurity/trivy/releases/download/v0.38.1/trivy_0.38.1_Linux-64bit.debapt install trivy_0.38.1_Linux-64bit.deb

示例命令

trivy image python:3.4-alpinetrivy 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+0800INFONeed to update DB2023-03-29T10:10:09.318+0800INFODB Repository: ghcr.io/aquasecurity/trivy-db2023-03-29T10:10:09.318+0800INFODownloading 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 的话,就可以部署,否则的话,就不允许部署。

x