This challenge sees a user shell obtained by exfiltrating sensitive information via a vulnerability called Heartbleed in the OpenSSL cryptography library; which is a widely used implementation of the Transport Layer Security (TLS) protocol.

From there, I was able to overwrite a read-only file (/etc/passwd) and grant myself root user privileges by exploiting a Linux kernel race condition vulnerability called Dirty COW.
Enumeration
Start with a nmap scan which reveals ports 22, 80 and 443.
| |
Browsing to the website on port 80 displays this image.

Since we have a web server, I run a dirbuster scan looking for additional web directories.

I browse to /dev and find two files.

The contents of notes.txt are
| |
Steps 3, 4 and 5 make reference to the other two directories that were discovered (/encode and /decode), but more importantly, indicate that they may be broken/vulnerable.
Exploitation
I check out hype_key which looks to be a file that has been hex encoded.

Using an online hex decoder, I convert the hex string to ascii to reveal an encrypted RSA private key. I save it on my Kali machine as hype_key_encrypted.key

Secret found! Encrypted RSA private key
Next, I search for python heartbleed GitHub and find an exploitation script as the very first result. I read the script and it looks like it’ll job, so I download it to my kali machine.
I run the script with the defaults.
| |
Nothing interesting there. I run it a few more times with similar results.
The way this script works is that it is returning a maximum of 4000 bytes of memory directly adjacent to the SSL request. The problem is, that part of the memory doesn’t always hold useful information. If I run the script a few more times consecutively, hopefully there will be some interesting data within bytes returned.
I look at the help for the script and notice it has a loop (-n) parameter.
| |
I set it to loop 100 times. Still nothing. I set it to loop 500 times and add the -a parameter to output to a file, so I can let it run and parse it later.
| |
The results, reveal a base64 encoded string assigned to a $text variable.

I decode the string.
| |
Secret found! heartbleedbelievethehype
I use the secret to decrypt the private key obtained earlier.

Using the name of the encrypted key that I downloaded (hype_key) as a clue, I made an educated guess on the username, and try to SSH to Valentine as the user hype with the decrypted RSA key.

User flag obtained!
Privilege escalation
I take a look at what Linux version Valentine is running, and notice that it’s very old.
| |
This release is vulnerable to the dirty cow exploit.
After reading through the code and instructions, I copy/paste exploitation code from this github and save to Valentine as /dev/shm/dirtycow.c.
This exploit code uses the dirty cow exploit to overwrite a line to the read-only /etc/passwd/ file. In my case, I want to overwrite the details of the hype user to set the password and grant it root privileges.
First, I edit the user information section of the script.

Next, I compile the code
| |
And finally, fire off the exploit.
| |
Once the script had completed, I check the contents of /etc/passwd to confirm it worked correctly.

I log out, and SSH back in. This time it prompts for a password.

I enter the password I set with DirtyCow, and I’m back in. However this time, with root privilege.
| |
Deconstructing the hack
Heartbleed
Sometimes, the easiest way to explain a concept is with pictures. Take a look at the xkcd comic below which depicts how the techniques I used above was able to exploit the Heartbleed vulnerability and retrieve sensitive information from the remote system.

Firstly, the user Meg asks the server to indicate if it is still online by asking for it to reply with the word “POTATO”, and she specifies the length of the word. This is what happens under the hood with a typical unmodified user request. The server responds exactly as asked. 6 letters and the word “POTATO”. Notice that only the highlighted section was returned, and all surrounding data in memory is not.
Meg tries again. She asks again for the server to indicate if it is still online by responding with the word “BIRD” in four letters. The server complies.
Next, Meg asks for the word “HAT”, however, specifies the response should be 500 letters in length. The server then responds not only with the word “HAT” but also by leaking out an additional 497 characters surrounding the word.
Since memory is volatile, it doesn’t always contain sensitive information, however in this challenge I sent a request to the server and asked it to respond with 4000 bytes of information. Lucky for me, I was able to capture a base64 hashed password that ultimately led to a user shell on the box.
Dirty COW
Dirty COW or Dirty copy-on-write is a vulnerability for the Linux kernel that affects all Linux based operating systems including Android. It is a bug that exploits a race condition in the implementation of the copy-on-write mechanism in the kernel’s memory management subsystem. With the right timing, an attacker can exploit the copy-on-write mechanism to turn a read-only mapping of a file into a writeable mapping, and therefore modify or overwrite its contents. Wikipedia
In this challenge, I used the DirtyCOW vulnerability to overwrite the /etc/passwd file on the remote system and grant my user account root privileges.
