Wonderland is probably one of my favorite machines on TryHackMe, not because itβs realistic or anything, but because it teaches a lot of concepts if you havenβt been exposed to them before, and I also like literature references. Iβll start by following a trail of directories on a webpage to get some credentials which I can use to get on the machine. From there, Iβll hijack a python program and a bash script to move laterally, and finally abuse linux capabilities to get root.
Recon
Something, something nmap.
Nothing out of the ordinary yet, except for the fact that the webpage is presumably written in Golang. Letβs take a look at that webpage.
A whole lot of nothing. Iβll feroxbuster without recursion (because it can sometimes blow up a server) and see if I can find anything useful.
That /r directory is weird. If we navigate to http://10.10.239.191/r/, we see this.
The source code doesnβt have comments either.
Shell as alice
Letβs have another go with feroxbuster and see what happens, and this time, Iβll add recursion because it just seems to be static pages (and I shouldnβt break the site as easily).
I think you see where this is going. At http://10.10.239.191/r/a/b/b/i/t/, we see this:
I went to look at the source code, and I found what are probably credentials.
Letβs try signing into SSH.
I would grab the user flag, but it appears everything is what it isnβt.
Shell as rabbit
Normally, I would default to getting linpeas on here, but since I have a password, itβs worth just checking sudo -l to see what alice might be able to run as sudo.
We can check out what walrus_and_the_carpenter.py is.
Usually, python is most easily abused when using a function like eval() where you can try and inject commands to spawn a new shell. However, this program is unmodifiable by us, and there isnβt any user input. But, we can always check the Python import PATH (not sure what itβs actually called but thatβs what Iβm going with).
Since python is checking the current directory first, we can actually do some hijacking. This is something that shows up in a bunch of different forms in CTFs/security (e.g. DLL Hijacking, function hooking), but the general idea is that we replace a dependency/library with one of our own, causing a program to run our code instead of what was intended.
In aliceβs home directory we can create a new random.py file as follows:
When the walrus python script decides to import random, rather than import from the library that comes with python, it will import our βmaliciousβ script instead. Since weβre running with the privileges as rabbit, weβll spawn a shell as that user, instead of a new alice shell.
Shell as hatter
We donβt have a password for rabbit, so weβre not going to be running anything with sudo anytime soon. Letβs check out rabbitβs home directory.
The teaParty binary is interesting. We see that it has a privilege marked with an s, indicating SUID, we could possibly use this to escalate to root. Letβs try running it to see what it does.
After playing around with this, it seems like the message will always display a time that is a couple hours past the current time. rude. How is it getting my time correctly though? Iβll have to take this binary offline so I can analyze it, since strings doesnβt exist on the remote machine. My goto exfiltration method is pyftpdlib, but youβre free to do what you want.
Normally, I would use ghidra for reverse engineering, but I think thatβs a little overkill. Running strings gives us the following.
At first glance, it doesnβt seem like we can do anything with this. However, notice how echo is being called with an absolute path, but date is being called by its name, which leaves it open for another hijack. If we can stick the rabbit home directory at the beginning of the PATH, and make our own date function in the home directory, we can easily get a new shell as a different user (thereβs a setuid command buried in the strings output).
Shell as root
Letβs check out hatterβs home directory.
Surely thereβs something good in password.txt.
Hmmm. It doesnβt seem to be the root password, but it might be hatterβs password.
At least weβre actually signed in as hatter now. After some manual poking around, I decided to run linpeas.sh. This result was pretty interesting.
If we look at the link attached we learn the following.
Linux capabilities provide a subset of the available root privileges to a process. This effectively breaks up root privileges into smaller and distinctive units. Each of these units can then be independently be granted to processes. This way the full set of privileges is reduced and decreasing the risks of exploitation.
/usr/bin/perl has the set_uid capability, meaning we can just set our UID to root. After reading a little bit more from hacktricks, we can use GTFOBins to find the command that will take us to root
I can then grab the user flag (which is in the root directory), and the root flag which we missed from before.