供应链安全-镜像 Trivy kubesec

开头语

写在前面:如有问题,以你为准,

目前24年应届生,各位大佬轻喷,部分资料与图片来自网络

内容较长,页面右上角目录方便跳转

Dockerfile 文件优化

  • 减少镜像层:一次RUN指令形成新的一层,尽量Shll命令都写在一行,减少镜像层。
  • 多阶段构建: 多阶段构建能够帮助我们大幅减少最终镜像的大小,而无需费力减少中间层和文件的数量
  • 清理无用文件:清理对应的残留数据,例如yu缓存。
  • 清理无用的软件包:基础镜像默认会带一些dbug工具,可以删除掉,仅保留应用程序所需软件,防止黑客利用。
  • 利用构建缓存: 构建镜像时,Docker 会逐步执行 Dockerfile 中的指令,并按照指定的顺序执行每个指令。在检查每条指令时,Docker 会在其缓存中查找可以重用的现有镜像,而不是创建新的(重复)镜像
  • 选择最小的基础镜像:例如alpine
  • 使用非root用户运行:USER指令指定普通用户

减少镜像层

可以通过下面查看镜像层构建

[root@master gvisor]# docker history busybox:1.30
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
64f5d945efcc   4 years ago   /bin/sh -c #(nop)  CMD ["sh"]                   0B
<missing>      4 years ago   /bin/sh -c #(nop) ADD file:e36dd1822f36a8169…   1.2MB

每跑一次RUN指令都是会导致多一层,所以尽量将多个要执行命令写在一个RUN里面

# 第一种方式
RUN ls && mkdir test && cd test
# 第二种方式
RUN ls && \
  mkdir test && \
  cd test

清理无用数据

可以构建镜像是加上如下命令

yum clean all && rm -rf /var/cache/yum/*

Trivy 镜像漏洞扫描工具

Trivy: 是一种用于容器镜像、文件系统、Git仓库的漏洞扫描工具。发现目标软件存在的漏洞。

Trivy: 易于使用,只需安装二进制文件即可进行扫描,方便集成CI系统。

https://github.com/aquasecurity/trivy
  • ArtifactDeatil 结构和 各类Analyzer 实现
  • trivy-db 的软件包漏洞列表
  • defsec 的配置检测规则

这些资源都可以活用于镜像安全的各类实施上。下面的源码分析内容建议结合 trivy 源码阅读,可以更好地理解一些细节

实操示例

[root@master images-security]# trivy image nginx:1.17.1
2023-11-14T02:26:55.898-0500    INFO    Need to update DB
2023-11-14T02:26:55.898-0500    INFO    DB Repository: ghcr.io/aquasecurity/trivy-db
2023-11-14T02:26:55.898-0500    INFO    Downloading DB...
1.01 MiB / 40.75 MiB [->______________________________________________________________________] 2.48% 435.31 KiB p/s ETA 1m33s
# 正在下载数据库,会因为网络问题下载不了
# trivy image --download-db-only 仅下载漏洞数据库
# trivy image nginx:1.17.1 --skip-db-update 跳过数据库下载,但是第一次不给跳过
# ERROR   The first run cannot skip downloading DB

Trivy 默认是每隔十二个小时就会更新一次漏洞库,离线情况下或者网络不好时可能就会更新失败;如果你想要获取最新的漏洞库就可以用这种方法手动去更新一下

漏洞数据库分为两个版本

version1 :有时候漏洞库真的下不下来 - 简书

Releases · aquasecurity/trivy-db · GitHub

version2:镜像安全扫描工具Trivy深入实践 - 知乎

Package trivy-db · GitHub

通过国外机器来下载

mkdir db
trivy --cache-dir ./db image --download-db-only
tar -cf ./db.tar.gz -C ./db/db metadata.json trivy.db

通过winscp或者软件拿到本地 /root/.cache/trivy

[root@master trivy]# tree
.
├── db
│   ├── metadata.json
│   └── trivy.db
├── fanal
│   └── fanal.db
└── java-db
    ├── metadata.json
    └── trivy-java.db

db等文件详细解析:镜像安全扫描工具Trivy深入实践 - 知乎

漏洞检测

[root@master trivy]# trivy image nginx:1.17.1 --skip-db-update --skip-java-db-update
2023-11-15T02:10:00.448-0500    INFO    Vulnerability scanning is enabled
2023-11-15T02:10:00.448-0500    INFO    Secret scanning is enabled
2023-11-15T02:10:00.448-0500    INFO    If your scanning is slow, please try '--scanners vuln' to disable secret scanning
2023-11-15T02:10:00.448-0500    INFO    Please see also https://aquasecurity.github.io/trivy/v0.46/docs/scanner/secret/#recommendation for faster secret detection
2023-11-15T02:10:26.595-0500    INFO    Detected OS: debian
2023-11-15T02:10:26.595-0500    INFO    Detecting Debian vulnerabilities...
2023-11-15T02:10:26.625-0500    INFO    Number of language-specific files: 0
2023-11-15T02:10:26.685-0500    WARN    This OS version is no longer supported by the distribution: debian 9.9
2023-11-15T02:10:26.685-0500    WARN    The vulnerability detection may be insufficient because security updates are not provided

nginx:1.17.1 (debian 9.9)

Total: 204 (UNKNOWN: 7, LOW: 40, MEDIUM: 52, HIGH: 74, CRITICAL: 31)

┌────────────────────────┬──────────────────┬──────────┬──────────────┬────────────────────────┬────────────────────────┬──────────────────────────────────────────────────────────────┐
│        Library         │  Vulnerability   │ Severity │    Status    │   Installed Version    │     Fixed Version      │                            Title                             │
├────────────────────────┼──────────────────┼──────────┼──────────────┼────────────────────────┼────────────────────────┼──────────────────────────────────────────────────────────────┤
│ apt                    │ CVE-2020-27350   │ MEDIUM   │ fixed        │ 1.4.9                  │ 1.4.11                 │ apt: integer overflows and underflows while parsing .deb     │
│                        │                  │          │              │                        │                        │ packages                                                     │
│                        │                  │          │              │                        │                        │ https://avd.aquasec.com/nvd/cve-2020-27350                   │
│                        ├──────────────────┤          │              │                        ├────────────────────────┼──────────────────────────────────────────────────────────────┤
│                        │ CVE-2020-3810    │          │              │                        │ 1.4.10                 │ Missing input validation in the ar/tar implementations of    │
│                        │                  │          │              │                        │                        │ APT before v ......                                          │
│                        │                  │          │              │                        │                        │ https://avd.aquasec.com/nvd/cve-2020-3810                    │

漏洞解析

CVE-2016-2779 漏洞编号(可以进行通过编号去修复漏洞)

HIGH 级别

1:2.29.2-1+deb9u1 目前的版本

Title 其中网址打开就是漏洞介绍和修复方式

常用命令

# 跳过拉取漏洞数据库
--skip-update=true
# 查看命令帮助
trivy -h
# 容器镜像扫描
trivy image nginx
trivy image -i nginx.tar
# 打印指定 (高危,严重)
trivy image -s HIGH,nginx
trivy image -s HIGH,CRITICAL nginx
# JSON 格式输出并保存到文件
trivy image nginx -f json -o ./output.json


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

kubesec 检测yaml文件安全配置

kubesec:是一个针对K8s资源清单文件进行安全配置评估的工具,根据安全配置

https://kubesec.io/ 
https://github.com/controlplaneio/kubesec

注:因为网络问题,所以手动下载检测文件

通过容器更方便,避免网络问题

命令示例

kubesec scan -h
# --schema-location 是本地json检测文件
主要是因为网络问题,所以建议自行下载后通过这个参数指定
[root@master kubesec]# kubesec scan deploy.yaml --schema-location deployment-apps-v1.json
[
  {
    "object": "Deployment/web.default",
    "valid": true,
    "fileName": "deploy.yaml",
    "message": "Passed with a score of 0 points",
    "score": 0,
    "scoring": {
      "advise": [
        {
          "id": "ApparmorAny",
          "selector": ".metadata .annotations .\"container.apparmor.security.beta.kubernetes.io/nginx\"",
          "reason": "Well defined AppArmor policies may provide greater protection from unknown threats. WARNING: NOT PRODUCTION READY",
          "points": 3
        },
        {
          "id": "ServiceAccountName",
          "selector": ".spec .serviceAccountName",
          "reason": "Service accounts restrict Kubernetes API access and should be configured with least privilege",
          "points": 3
        },

上图就是让你配置apparmor 和 SA

通过容器调用检测

开启一个容器,并将本地要检测的yaml传入返回结果

[root@master opa]# docker run -d -p 8080:8080 kubesec/kubesec http 8080
Unable to find image 'kubesec/kubesec:latest' locally
latest: Pulling from kubesec/kubesec
5843afab3874: Pull complete
0e12e15a6490: Pull complete
5a1816831e29: Pull complete
0e871d09ee04: Pull complete
c94217ef84fb: Pull complete
9412ea52a867: Pull complete
Digest: sha256:e8b73f2f8fd00508c0c7523600c43fe79300692b0a1f6f4eaf9cf1c1f341cb35
Status: Downloaded newer image for kubesec/kubesec:latest
b9d416dd87d7d738bdaf905faab99ad796bfa7210f01ed7b12dda731d2935bb1
curl -sSX POST --data-binary @deploy.yaml http://0.0.0.0:8080/scan

@deploy.yaml 为当前目录下的要检测的yaml文件

网络问题

185.199.111.133 raw.githubusercontent.com
[root@master kubesec]# kubectl create deploy web --image=nginx --dry-run=client -o yaml > deploy.yaml
[root@master kubesec]# kubesec scan deploy.yaml
[
  {
    "object": "Deployment/web.default",
    "valid": false,
    "fileName": "deploy.yaml",
    "message": "failed downloading schema at https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/master-standalone-strict/deployment-apps-v1.json: Get \"https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/master-standalone-strict/deployment-apps-v1.json\": dial tcp 0.0.0.0:443: connect: connection refused",
    "score": 0,
    "scoring": {}
  }
]
wget https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/master-standalone-strict/deployment-apps-v1.json

 kubesec scan deploy.yaml --schema-location deployment-apps-v1.json

  • 23
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值