容器安全之鏡像掃描

一、鏡像掃描

  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 的話,就可以部署,否則的話,就不允許部署。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章