detecting-privilege-escalation-in-kubernetes-pods

通过使用 Falco 和 OPA 策略监控安全上下文、能力和系统调用模式,检测并防止 Kubernetes Pod 中的权限提升。

9 stars

Best use case

detecting-privilege-escalation-in-kubernetes-pods is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

通过使用 Falco 和 OPA 策略监控安全上下文、能力和系统调用模式,检测并防止 Kubernetes Pod 中的权限提升。

Teams using detecting-privilege-escalation-in-kubernetes-pods should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/detecting-privilege-escalation-in-kubernetes-pods/SKILL.md --create-dirs "https://raw.githubusercontent.com/killvxk/cybersecurity-skills-zh/main/skills/detecting-privilege-escalation-in-kubernetes-pods/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/detecting-privilege-escalation-in-kubernetes-pods/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How detecting-privilege-escalation-in-kubernetes-pods Compares

Feature / Agentdetecting-privilege-escalation-in-kubernetes-podsStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

通过使用 Falco 和 OPA 策略监控安全上下文、能力和系统调用模式,检测并防止 Kubernetes Pod 中的权限提升。

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# 检测 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 安全准入(内置)

```yaml
# 对命名空间强制执行受限策略
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 策略

```yaml
# 阻止危险能力
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 := input.review.object.spec.containers[_]
          container.securityContext.privileged == true
          msg := sprintf("容器 %v 以特权模式运行", [container.name])
        }

        violation[{"msg": msg}] {
          container := input.review.object.spec.containers[_]
          container.securityContext.allowPrivilegeEscalation == true
          msg := sprintf("容器 %v 允许权限提升", [container.name])
        }

        violation[{"msg": msg}] {
          input.review.object.spec.hostPID == true
          msg := "Pod 使用主机 PID 命名空间"
        }

        violation[{"msg": msg}] {
          input.review.object.spec.hostNetwork == true
          msg := "Pod 使用主机网络"
        }
```

## 使用 Falco 进行运行时检测

```yaml
# /etc/falco/rules.d/privesc-detection.yaml
- 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: 检测使用危险能力启动的容器
  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=%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: 检测容器内写入 /etc/passwd 的操作
  condition: >
    open_write and container and fd.name = /etc/passwd
  output: >
    容器中写入 /etc/passwd
    (container=%container.name image=%container.image.repository
     command=%proc.cmdline user=%user.name)
  priority: CRITICAL
  tags: [container, privilege-escalation, T1136]
```

## Kubernetes 审计日志检测

```yaml
# audit-policy.yaml - 捕获权限提升事件
apiVersion: audit.k8s.io/v1
kind: Policy
rules:
  # 记录带有安全上下文详情的 Pod 创建
  - 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"]
```

### 查询审计日志中的权限提升

```bash
# 查找使用特权安全上下文创建的 Pod
kubectl logs -n kube-system kube-apiserver-* | \
  jq 'select(.verb == "create" and .objectRef.resource == "pods") |
  select(.requestObject.spec.containers[].securityContext.privileged == true)'

# 查找 RBAC 提升尝试
kubectl logs -n kube-system kube-apiserver-* | \
  jq 'select(.objectRef.resource == "clusterrolebindings" and .verb == "create")'
```

## 调查手册

```bash
# 检查 Pod 安全上下文
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

# 列出以 root 身份运行的 Pod
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}'

# 检查 hostPath 卷
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]}'
```

## 最佳实践

1. 对生产命名空间启用 **Pod 安全准入**,级别为 `restricted`
2. **丢弃所有能力**,然后只添加所需的能力
3. 在所有容器上设置 **allowPrivilegeEscalation: false**
4. **以非 root 用户运行**(runAsNonRoot: true, runAsUser > 0)
5. 除非需要 API 访问,否则**禁用 automountServiceAccountToken**
6. 使用 **Falco 监控**运行时权限提升尝试
7. 使用 Kubernetes 审计日志**审计 RBAC 变更**
8. 使用 **seccomp profile** 限制系统调用

Related Skills

securing-kubernetes-on-cloud

9
from killvxk/cybersecurity-skills-zh

本技能涵盖通过实施 Pod 安全标准(Pod Security Standards)、网络策略、工作负载身份、RBAC 权限控制、镜像准入控制和运行时安全监控,对 EKS、AKS 和 GKE 上的托管 Kubernetes 集群进行加固。涉及云平台特定安全功能,包括 EKS 的 IRSA、GKE 的工作负载身份(Workload Identity)以及 AKS 的托管身份(Managed Identity)。

scanning-kubernetes-manifests-with-kubesec

9
from killvxk/cybersecurity-skills-zh

使用 Kubesec 对 Kubernetes 资源清单执行安全风险分析,识别错误配置、权限提升风险以及与安全最佳实践的偏差。

performing-privileged-account-discovery

9
from killvxk/cybersecurity-skills-zh

发现并清点企业基础设施中的所有特权账户,包括域管理员、本地管理员、服务账户、数据库管理员、云 IAM 角色和应用管理员账户。

performing-privileged-account-access-review

9
from killvxk/cybersecurity-skills-zh

对特权账户进行系统性审查,验证访问权限,识别过多权限,并在 PAM 基础设施中执行最小权限原则。

performing-privilege-escalation-on-linux

9
from killvxk/cybersecurity-skills-zh

Linux 权限提升是指在已控制的系统上从低权限用户账户提升至 root 访问权限。红队通过利用错误配置、存在漏洞的服务、内核漏洞利用和弱权限来实现 root 访问。

performing-privilege-escalation-assessment

9
from killvxk/cybersecurity-skills-zh

在已入侵的 Linux 和 Windows 系统上执行权限提升评估,识别从低权限访问到 root 或 SYSTEM 级别控制的路径。 测试人员枚举错误配置、存在漏洞的服务、内核漏洞、SUID 二进制文件、未加引号的服务路径和凭据存储, 以演示初始入侵的完整影响。适用于权限提升测试、本地漏洞利用、后渗透提权或操作系统级安全评估等请求场景。

performing-kubernetes-penetration-testing

9
from killvxk/cybersecurity-skills-zh

Kubernetes 渗透测试(Penetration Testing)通过对 API server、kubelet、etcd、Pod、RBAC、网络策略和密钥模拟攻击者技术,系统性评估集群安全性。使用 kube-hunter、Kubescape、peirates 等工具识别可能导致集群被攻陷的错误配置。

performing-kubernetes-etcd-security-assessment

9
from killvxk/cybersecurity-skills-zh

通过评估静态加密、TLS 配置、访问控制、备份加密和网络隔离,评估 Kubernetes etcd 集群的安全态势。

performing-kubernetes-cis-benchmark-with-kube-bench

9
from killvxk/cybersecurity-skills-zh

使用 kube-bench 对照 CIS Benchmark 审计 Kubernetes 集群安全态势,对控制平面、工作节点和 RBAC 执行自动化检查。

performing-aws-privilege-escalation-assessment

9
from killvxk/cybersecurity-skills-zh

在 AWS 环境中执行已授权的权限提升(Privilege Escalation)评估,使用 Pacu、CloudFox、Principal Mapper 和手动 IAM 策略分析技术,识别允许用户或角色提升权限的 IAM 配置错误。

implementing-zero-standing-privilege-with-cyberark

9
from killvxk/cybersecurity-skills-zh

部署 CyberArk Secure Cloud Access,通过基于时间、权限和审批控制的即时访问,在混合云和多云环境中消除常设权限。

implementing-rbac-hardening-for-kubernetes

9
from killvxk/cybersecurity-skills-zh

通过实施最小权限策略、审计角色绑定、消除 cluster-admin 权限蔓延并集成外部身份提供商,加固 Kubernetes 基于角色的访问控制(RBAC)。