开头语
写在前面:如有问题,以你为准,
目前24年应届生,各位大佬轻喷,部分资料与图片来自网络
内容较长,页面右上角目录方便跳转
可以通过下面查看镜像层构建
[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: 是一种用于容器镜像、文件系统、Git仓库的漏洞扫描工具。发现目标软件存在的漏洞。
Trivy: 易于使用,只需安装二进制文件即可进行扫描,方便集成CI系统。
https://github.com/aquasecurity/trivy
这些资源都可以活用于镜像安全的各类实施上。下面的源码分析内容建议结合 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深入实践 - 知乎
通过国外机器来下载
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:是一个针对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