開頭語
寫在前面:如有問題,以你為準,
目前24年應屆生,各位大佬輕噴,部分資料與圖片來自網(wǎng)絡
內(nèi)容較長,頁面右上角目錄方便跳轉(zhuǎn)
Dockerfile 文件優(yōu)化
- 減少鏡像層:一次RUN指令形成新的一層,盡量Shll命令都寫在一行,減少鏡像層。
- 多階段構(gòu)建: 多階段構(gòu)建能夠幫助我們大幅減少最終鏡像的大小,而無需費力減少中間層和文件的數(shù)量
- 清理無用文件:清理對應的殘留數(shù)據(jù),例如yu緩存。
- 清理無用的軟件包:基礎(chǔ)鏡像默認會帶一些dbug工具,可以刪除掉,僅保留應用程序所需軟件,防止黑客利用。
- 利用構(gòu)建緩存: 構(gòu)建鏡像時,Docker 會逐步執(zhí)行 Dockerfile 中的指令,并按照指定的順序執(zhí)行每個指令。在檢查每條指令時,Docker 會在其緩存中查找可以重用的現(xiàn)有鏡像,而不是創(chuàng)建新的(重復)鏡像
- 選擇最小的基礎(chǔ)鏡像:例如alpine
- 使用非root用戶運行:USER指令指定普通用戶
減少鏡像層
可以通過下面查看鏡像層構(gòu)建
[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指令都是會導致多一層,所以盡量將多個要執(zhí)行命令寫在一個RUN里面
# 第一種方式
RUN ls && mkdir test && cd test
# 第二種方式
RUN ls && \
mkdir test && \
cd test
清理無用數(shù)據(jù)
可以構(gòu)建鏡像是加上如下命令
yum clean all && rm -rf /var/cache/yum/*
Trivy 鏡像漏洞掃描工具
Trivy: 是一種用于容器鏡像、文件系統(tǒng)、Git倉庫的漏洞掃描工具。發(fā)現(xiàn)目標軟件存在的漏洞。
Trivy: 易于使用,只需安裝二進制文件即可進行掃描,方便集成CI系統(tǒng)。
https://github.com/aquasecurity/trivy
- ArtifactDeatil 結(jié)構(gòu)和 各類Analyzer 實現(xiàn)
- trivy-db 的軟件包漏洞列表
- defsec 的配置檢測規(guī)則
這些資源都可以活用于鏡像安全的各類實施上。下面的源碼分析內(nèi)容建議結(jié)合 trivy 源碼閱讀,可以更好地理解一些細節(jié)
實操示例
[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
# 正在下載數(shù)據(jù)庫,會因為網(wǎng)絡問題下載不了
# trivy image --download-db-only 僅下載漏洞數(shù)據(jù)庫
# trivy image nginx:1.17.1 --skip-db-update 跳過數(shù)據(jù)庫下載,但是第一次不給跳過
# ERROR The first run cannot skip downloading DB
Trivy 默認是每隔十二個小時就會更新一次漏洞庫,離線情況下或者網(wǎng)絡不好時可能就會更新失?。蝗绻阆胍@取最新的漏洞庫就可以用這種方法手動去更新一下
漏洞數(shù)據(jù)庫分為兩個版本
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 其中網(wǎng)址打開就是漏洞介紹和修復方式
?
常用命令
# 跳過拉取漏洞數(shù)據(jù)庫
--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資源清單文件進行安全配置評估的工具,根據(jù)安全配置
https://kubesec.io/
https://github.com/controlplaneio/kubesec
注:因為網(wǎng)絡問題,所以手動下載檢測文件
通過容器更方便,避免網(wǎng)絡問題
命令示例
kubesec scan -h
# --schema-location 是本地json檢測文件
主要是因為網(wǎng)絡問題,所以建議自行下載后通過這個參數(shù)指定
[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
通過容器調(diào)用檢測
開啟一個容器,并將本地要檢測的yaml傳入返回結(jié)果文章來源:http://www.zghlxwxcb.cn/news/detail-789212.html
[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文件文章來源地址http://www.zghlxwxcb.cn/news/detail-789212.html
網(wǎng)絡問題
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
到了這里,關(guān)于供應鏈安全-鏡像 Trivy kubesec的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!