关于 detecting-privilege-escalation-in-kubernetes-pods
用于检测 Kubernetes Pod 和容器权限提升的安全技能,结合准入策略、运行时监控与审计日志,涵盖危险安全上下文、Falco 规则、策略控制及调查流程。
主要功能
- 01识别高风险 Pod 安全设置
- 02应用 Pod Security Admission 策略
- 03创建 Gatekeeper 准入控制
- 04使用 Falco 检测运行时活动
- 05调查 Kubernetes 审计日志事件
适用场景
- 01检查 Pod 配置中的提权风险
- 02在准入阶段阻止不安全工作负载
- 03监控运行时权限提升尝试
- 04调查可疑 RBAC 或 Pod 变更
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill detecting-privilege-escalation-in-kubernetes-pods安装后在 Claude Code 中输入/detecting-privilege-escalation-in-kubernetes-pods确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENSEassets/template.mdreferences/api-reference.mdstandards.mdworkflows.mdscripts/agent.pyprocess.py详细介绍
检测 Kubernetes Pod 中的权限提升
概述
Kubernetes 中的权限提升是指 Pod 或容器获得超出预期范围的权限。这包括以 root 身份运行、使用特权模式、挂载主机文件系统、启用危险的 Linux 功能,或利用内核漏洞。检测需要结合准入控制(预防)、运行时监控(检测)和审计日志(调查)。
适用场景
- 调查需要检测 Kubernetes Pod 中权限提升的安全事件时
- 为此领域编写检测规则或威胁狩猎查询时
- SOC 分析师需要针对这类分析制定结构化流程时
- 验证对相关攻击技术的安全监控覆盖情况时
前提条件
- Kubernetes 集群 v1.25+(支持 Pod Security Admission)
- 具有 cluster-admin 访问权限的 kubectl
- Falco 或类似的运行时安全工具
- 用于准入策略的 OPA Gatekeeper 或 Kyverno
Kubernetes 中的权限提升途径
| 途径 | 风险 | 检测方法 |
|---|---|---|
| privileged: true | 完全访问主机 | 准入控制 + 审计 |
| hostPID: true | 访问主机进程 | 准入控制 |
| hostNetwork: true | 访问主机网络堆栈 | 准入控制 |
| hostPath volumes | 读写主机文件系统 | 准入控制 |
| SYS_ADMIN capability | 近似特权访问 | 准入控制 + 运行时 |
| allowPrivilegeEscalation: true | setuid/setgid 利用 | 准入控制 |
| runAsUser: 0 | 容器 root | 准入控制 |
| automountServiceAccountToken | 窃取令牌以访问 API | 准入控制 |
| 可写的 /proc 或 /sys | 操纵内核参数 | 运行时监控 |
使用准入控制进行检测
Pod Security Admission(内置)
# Enforce restricted policy on namespace
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 策略
# Block dangerous capabilities
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("Container %v adds dangerous capability: %v", [container.name, cap])
}
violation[{"msg": msg}] {
container := input.review.object.spec.containers[_]
container.securityContext.privileged == true
msg := sprintf("Container %v runs in privileged mode", [container.name])
}
violation[{"msg": msg}] {
container := input.review.object.spec.containers[_]
container.securityContext.allowPrivilegeEscalation == true
msg := sprintf("Container %v allows privilege escalation", [container.name])
}
violation[{"msg": msg}] {
input.review.object.spec.hostPID == true
msg := "Pod uses host PID namespace"
}
violation[{"msg": msg}] {
input.review.object.spec.hostNetwork == true
msg := "Pod uses host network"
}
使用 Falco 进行运行时检测
# /etc/falco/rules.d/privesc-detection.yaml
- rule: Setuid Binary Execution in Container
desc: Detect execution of setuid/setgid binaries in a container
condition: >
spawned_process and container and
(proc.name in (su, sudo, newgrp, chsh, passwd) or
proc.is_exe_upper_layer=true)
output: >
Setuid/setgid binary executed in container
(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: Detect when a process gains elevated capabilities
condition: >
evt.type = capset and container and
evt.arg.cap != ""
output: >
Process gained capabilities in container
(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: Detect container launched with dangerous capabilities
condition: >
container_started and container and
(container.image.repository != "registry.k8s.io/pause") and
(container.cap_effective contains SYS_ADMIN or
container.cap_effective contains SYS_PTRACE or
container.cap_effective contains SYS_MODULE)
output: >
Container with dangerous capabilities
(container=%container.name image=%container.image.repository
caps=%container.cap_effective)
priority: CRITICAL
tags: [container, privilege-escalation, T1068]
- rule: Write to /etc/passwd in Container
desc: Detect writes to /etc/passwd inside container
condition: >
open_write and container and fd.name = /etc/passwd
output: >
Write to /etc/passwd in container
(container=%container.name image=%container.image.repository
command=%proc.cmdline user=%user.name)
priority: CRITICAL
tags: [container, privilege-escalation, T1136]
Kubernetes 审计日志检测
# audit-policy.yaml - Capture privilege escalation events
apiVersion: audit.k8s.io/v1
kind: Policy
rules:
# Log pod creation with security context details
- level: RequestResponse
resources:
- group: ""
resources: ["pods"]
verbs: ["create", "update", "patch"]
# Log privilege escalation attempts
- level: RequestResponse
resources:
- group: "rbac.authorization.k8s.io"
resources: ["clusterroles", "clusterrolebindings", "roles", "rolebindings"]
verbs: ["create", "update", "patch", "bind", "escalate"]
# Log service account token requests
- level: Metadata
resources:
- group: ""
resources: ["serviceaccounts/token"]
verbs: ["create"]
查询审计日志以检测权限提升
# Find pods created with privileged security context
kubectl logs -n kube-system kube-apiserver-* | \
jq 'select(.verb == "create" and .objectRef.resource == "pods") |
select(.requestObject.spec.containers[].securityContext.privileged == true)'
# Find RBAC escalation attempts
kubectl logs -n kube-system kube-apiserver-* | \
jq 'select(.objectRef.resource == "clusterrolebindings" and .verb == "create")'
调查手册
# Check pod security context
kubectl get pod <pod-name> -n <ns> -o jsonpath='{.spec.containers[*].securityContext}'
# Check effective capabilities
kubectl exec <pod-name> -n <ns> -- cat /proc/1/status | grep -i cap
# List pods running as root
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}'
# Check for hostPath volumes
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]}'
最佳实践
- 在生产命名空间中,以
restricted级别启用 Pod Security Admission - 移除 ALL 功能,仅添加所需功能
- 在所有容器上设置 allowPrivilegeEscalation: false
- 以非 root 身份运行(runAsNonRoot: true,runAsUser > 0)
- 除非需要访问 API,否则禁用 automountServiceAccountToken
- 使用 Falco 监控运行时权限提升尝试
- 通过 Kubernetes 审计日志审计 RBAC 变更
- 使用 seccomp 配置文件限制系统调用
评论
支持 Markdown