Ready for the writeup I wrote up of Writeup? This is the most meta box I’ve seen; the web server has walkthroughs of other HackTheBox machines, even an “early draft” of a walkthrough of itself. Although initial access is a standard “identify CMS, look up CVE” process, privilege escalation is a fun lesson on $PATH priority.
Initial scan
Only SSH and HTTP are open. Let’s see what the web server has in store for us.
Enumerating the web server
At http://10.10.10.138, I find a message about some DoS protection installed on the server.
Any of my attempts to brute-force directories is foiled by this DoS protection. Luckily, the nmap output shows that robots.txt has one disallowed entry: /writeup/
At the /writeup/ page, I find a page with links to three HackTheBox walk-throughs.
(Yes, I really did think I could find the solution to Writeup in the “writeup” link . . . )
The bottom of the page mentions that the site was not made with vim.
I take this as a hint to dig into what the site was actually built with. If you look at the source code, you’ll see that this page was created using “CMS Made Simple.”
The copyright ends at 2019, so I can assume that the CMS is updated to at least the 2019 version as well. I google for “CMS Made Simple 2019 exploit”—and one of the first results is an unauthenticated SQL injection exploit on the Exploit Database.
SQL injection exploit
The script enumerates the site for a username and password hash using blind time-based SQL injection. Once the script pulls the hash, it proceeds to crack that hash with the wordlist you’ve passed in the command. rockyou does the trick.
The uncovered credentials (jkr:raykayjay9) can be used to SSH into the box.
The user flag is in jkr’s home directory.
Privilege escalation: Abusing $PATH
This is tough to find if you’re all alone on the box (i.e., VIP users are practically on Expert mode here), but if you run pspy64 to snoop on processes while a user is SSH’ing into the box, you’ll see some interesting commands running as root (indicated by UID=0).
In the first line here, we see the value of the $PATH variable. To get root on this box, you have to understand the importance of $PATH.
When a user runs a binary without a full path—for example, ifconfig instead of /sbin/ifconfig—the shell looks at the leftmost directory in $PATH for an executable called ifconfig and runs that. If ifconfig can’t be found there, the shell checks next directory in the $PATH vairable, and if it fails again, the next directory, etc.
Based on the third line of the pspy64 output above, we know root runs unamewithout the full path every time a user SSH’s into the box. So if we can create our own uname binary and place it earlier in $PATH, we can trigger root to execute the command by simply SSH’ing into the box. But first, I need to verify a couple of things, such as: Where even is the uname binary located?
It’s in /bin, which is the rightmost directory in $PATH. So if we drop our custom uname binary in any other directory in $PATH, that binary will execute instead of the /bin/uname binary. We need write access to this directory though, so I check to see what privileges I have over /usr/local/sbin.
Anyone in the staff group can write to the /usr/local/sbin directory. I run id to see if jkr is part of this group.
So we should be good! I move to /usr/local/sbin and create the new uname file with vi. My script (below) simply grabs the root flag, moves it to the /tmp folder, and grants everyone read/write/execute permissions over it.
I make my file executable.
Now all that’s left is triggering root’s execution of uname. To do this, I exit my SSH session and initiate a new one.
I check the /tmp folder, and the root flag is there.
Bonus: Root shell
To get a root shell, just turn the uname binary into a reverse shell payload. The only roadblock is that netcat and ncat aren’t on the machine.
I could download the netcat binary from my Kali box, but it’s quieter to live off the land and use tools already provided on the box. socat works as a solid alternative.
I use vi to create a new uname binary that initiates a socat reverse shell. Here’s the script:
I make it executable as well.
Before triggering the command, I set up a socat listener on my Kali’s port 443.
I exit and re-enter the SSH session to trigger the uname command.