PISA Pakistan — Community CERT

← ⚙️ Red Team Fundamentals (Ethical)

47. Container Escape & Host Compromise: Red Team Tactics for Breaking Out of Containers

⏱ 7 min

Container Escape & Host Compromise: Red Team Tactics for Breaking Out of Containers

Container technologies (Docker, containerd) power many Pakistani fintech and e‑government applications. While containers provide isolation, misconfigurations can allow a red team to escape the container and compromise the underlying host, pivoting to other workloads or stealing secrets.

1. Why Containers Can Be Unsafe

  • Privileged Containers – Run with --privileged, granting almost full host capabilities.
  • 挂载-host文件系统 – Containers that mount the host’s root filesystem (e.g., -v /:/host).
  • Unsafe Capabilities – CAP_SYS_ADMIN, CAP_DAC_READ_SEARCH can be abused.
  • docker.sock – Exposing the Docker daemon socket inside a container enables escape.

2. Red‑Team Attack Flow

  1. Initial Foothold – Exploit a vulnerable web app running inside a container (e.g., CVE‑2022‑42889 in a Java service) to gain a reverse shell.
  2. Recon Inside Container – Use linPEAS to enumerate capabilities, mounts, and processes:
    linpeas.sh
    
  3. Privilege Escalation – If the container is privileged or has CAP_SYS_ADMIN:
    • Mount the host’s filesystem via nsenter or cgroups.
    • Write a SSH key to ~/.ssh/authorized_keys on the host.
  4. Container Escape via docker.sock – If /var/run/docker.sock is mounted, spawn a new privileged container:
    docker -H unix:///host/var/run/docker.sock run -v /:/host -it alpine chroot /host
    
  5. Host Pivot – From the host, move laterally to other pods, access Kubernetes secrets, or exfiltrate data.

3. Real‑World Pakistani Scenario

A red team assessing a Karachi‑based micro‑finance app discovered a container running with --privileged due to a misconfigured Kubernetes PodSecurityPolicy. By escaping the container, they obtained the cluster’s etcd encryption key, decrypting all stored customer transaction records.

4. Do / Don’t

  • Do avoid --privileged containers and drop all capabilities with --cap-drop=all.
  • Do use seccomp, AppArmor, or SELinux profiles to restrict syscalls.
  • Don’t mount the Docker socket inside containers; use kubectl exec for debugging instead.
  • Don’t rely solely on container isolation for security; apply defense‑in‑depth at the host and network layers.

5. Takeaway

Simulate a container escape to reveal gaps in your organization’s container hardening. If you can escape, immediately report findings to the platform team and suggest tightening PodSecurityPolicies, removing unnecessary capabilities, and auditing mounted volumes. Use NCCIA 1799 if sensitive customer data is exposed.

← PrevNext →