| name | detecting-privilege-escalation-in-kubernetes-pods |
| description | 通过使用 Falco 和 OPA 策略监控安全上下文、能力和系统调用模式,检测并防止 Kubernetes Pod 中的权限提升。 |
| domain | cybersecurity |
| subdomain | container-security |
| tags | ["kubernetes","privilege-escalation","security-context","capabilities","detection","pod-security"] |
| version | 1.0 |
| author | mahipal |
| license | Apache-2.0 |
检测 Kubernetes Pod 中的权限提升
概述
Kubernetes 中的权限提升是指 Pod 或容器获得超出其预期范围的提升权限,包括以 root 身份运行、使用特权模式、挂载主机文件系统、启用危险的 Linux 能力或利用内核漏洞。检测结合了准入控制(预防)、运行时监控(检测)和审计日志(调查)三种手段。
前提条件
- Kubernetes 集群 v1.25+(支持 Pod 安全准入)
- kubectl,具有 cluster-admin 访问权限
- Falco 或类似的运行时安全工具
- 用于准入策略的 OPA Gatekeeper 或 Kyverno
Kubernetes 中的权限提升向量
| 向量 | 风险 | 检测方法 |
|---|
| privileged: true | 完整主机访问 | 准入控制 + 审计 |
| hostPID: true | 访问主机进程 | 准入控制 |
| hostNetwork: true | 访问主机网络栈 | 准入控制 |
| hostPath 卷 | 读/写主机文件系统 | 准入控制 |
| SYS_ADMIN 能力 | 接近特权访问 | 准入 + 运行时 |
| allowPrivilegeEscalation: true | setuid/setgid 漏洞利用 | 准入控制 |
| runAsUser: 0 | 容器以 root 运行 | 准入控制 |
| automountServiceAccountToken | 令牌窃取以访问 API | 准入控制 |
| 可写的 /proc 或 /sys | 内核参数操纵 | 运行时监控 |
使用准入控制进行检测
Pod 安全准入(内置)
apiVersion: v1
kind: Namespace
metadata:
name: production
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/enforce-version: latest
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/warn: restricted
OPA Gatekeeper 策略
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: k8sdangerouspriv
spec:
crd:
spec:
names:
kind: K8sDangerousPriv
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8sdangerouspriv
dangerous_caps := {"SYS_ADMIN", "SYS_PTRACE", "SYS_MODULE", "DAC_OVERRIDE", "NET_ADMIN", "NET_RAW"}
violation[{"msg": msg}] {
container := input.review.object.spec.containers[_]
cap := container.securityContext.capabilities.add[_]
dangerous_caps[cap]
msg := sprintf("容器 %v 添加了危险能力: %v", [container.name, cap])
}
violation[{"msg": msg}] {
container := []
, []
}
{
[]
, []
}
{
}
{
}
使用 Falco 进行运行时检测
- rule: Setuid Binary Execution in Container
desc: 检测容器中执行 setuid/setgid 二进制文件
condition: >
spawned_process and container and
(proc.name in (su, sudo, newgrp, chsh, passwd) or
proc.is_exe_upper_layer=true)
output: >
容器中执行了 setuid/setgid 二进制文件
(user=%user.name container=%container.name image=%container.image.repository
command=%proc.cmdline parent=%proc.pname)
priority: WARNING
tags: [container, privilege-escalation, T1548]
- rule: Capability Gained in Container
desc: 检测进程获得提升能力的情况
condition: >
evt.type = capset and container and
evt.arg.cap != ""
output: >
容器中的进程获得了能力
(container=%container.name image=%container.image.repository
capabilities=%evt.arg.cap command=%proc.cmdline)
priority: WARNING
tags: [container, privilege-escalation, T1548.001]
- rule: Container with Dangerous Capabilities Started
desc:
[, , ]
[, , ]
Kubernetes 审计日志检测
apiVersion: audit.k8s.io/v1
kind: Policy
rules:
- level: RequestResponse
resources:
- group: ""
resources: ["pods"]
verbs: ["create", "update", "patch"]
- level: RequestResponse
resources:
- group: "rbac.authorization.k8s.io"
resources: ["clusterroles", "clusterrolebindings", "roles", "rolebindings"]
verbs: ["create", "update", "patch", "bind", "escalate"]
- level: Metadata
resources:
- group: ""
resources: ["serviceaccounts/token"]
verbs: ["create"]
查询审计日志中的权限提升
kubectl logs -n kube-system kube-apiserver-* | \
jq 'select(.verb == "create" and .objectRef.resource == "pods") |
select(.requestObject.spec.containers[].securityContext.privileged == true)'
kubectl logs -n kube-system kube-apiserver-* | \
jq 'select(.objectRef.resource == "clusterrolebindings" and .verb == "create")'
调查手册
kubectl get pod <pod-name> -n <ns> -o jsonpath='{.spec.containers[*].securityContext}'
kubectl exec <pod-name> -n <ns> -- cat /proc/1/status | grep -i cap
kubectl get pods --all-namespaces -o json | \
jq '.items[] | select(.spec.containers[].securityContext.runAsUser == 0 or .spec.containers[].securityContext.privileged == true) | {name: .metadata.name, ns: .metadata.namespace}'
kubectl get pods --all-namespaces -o json | \
jq '.items[] | select(.spec.volumes[]?.hostPath != null) | {name: .metadata.name, ns: .metadata.namespace, paths: [.spec.volumes[].hostPath.path]}'
最佳实践
- 对生产命名空间启用 Pod 安全准入,级别为
restricted
- 丢弃所有能力,然后只添加所需的能力
- 在所有容器上设置 allowPrivilegeEscalation: false
- 以非 root 用户运行(runAsNonRoot: true, runAsUser > 0)
- 除非需要 API 访问,否则禁用 automountServiceAccountToken
- 使用 Falco 监控运行时权限提升尝试
- 使用 Kubernetes 审计日志审计 RBAC 变更
- 使用 seccomp profile 限制系统调用