# Write-Up

### <mark style="color:purple;">Objective</mark>

The goal of this machine is to "find the flag."

***

### <mark style="color:purple;">Reconnaissance</mark>

We start by scanning the open ports of the target machine using nmap:

```bash
sudo nmap -F -T5 -sCV <ip_target>
```

* `-F` scans the 100 most common ports
* `-T5` performs a fast, aggressive scan
* `-sC` runs default scripts for common vulnerabilities
* `-sV` provides service version information

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXf0iFwxOdg4QlbnsZ7x7HgGx_a_e2up6AxDmj-4rfyCn9WMj5XZyJUBZ0kFJmzw48ykyiJ55996Du8LzuD_XVZvNQBkSBZFN7dV7xrrXdCz_CPCCCmqciCr2YyViAuvEwhbvBIL?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="563"><figcaption></figcaption></figure>

The open ports are:

* 22 - SSH
* 80 - HTTP

***

### Accessing the Web Application

We first navigate to port 80:

```bash
http://<ip_target>:80
```

The website redirects us to `titanic.htb`, so we need to add this to our `/etc/hosts` file:

```bash
sudo vim /etc/hosts
```

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfkQJtK3AQFh9arK-6PEoXORYkY61ck4QT7MU5K_Y-GmTPoUZaEWeIgPzbkPbx4nKq9Q4tOpzZoJDT1AJFpF22VAVQgV2kejwS81atfpElRvaSjf3L897Q0uZUTPxj-NWPpQ3QzSQ?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="375"><figcaption></figcaption></figure>

After editing the hosts file, we retry accessing the website.

***

### <mark style="color:purple;">Website Navigation</mark>

On the website, we quickly realize that the only functional buttons are "book your trip" and "book now," both of which lead to the same form. When we fill and submit the form, a JSON file is downloaded.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfUzsgsXAa0cXmfjnjkmfjkXhVGZ6ohl2q7eiDDRw-M9sfFC9GNCuh4nwmw_271hVrXrUJmHdE7pt_T8x0Lbdun4KP4AhUFmN6elPtsEgIkWJMQYMyOgFNnWP1qiz3oW_dGpk0zkA?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="375"><figcaption></figcaption></figure>

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXeWTJlWbaRjL5l0W0ydrzByLPBHPFDBYx4sDMhtonxC_j3861EAl2Vtp388a6LiYnKUedL4JytS8N3GcbeTS24DCAgbXhoR8evQtTDDop1JvGLSUL-vGa3WlzSkNJUkDdjEhyamJQ?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="563"><figcaption></figcaption></figure>

At this point, we decide to explore the directories on the server. For this, we use **gobuster**:

```bash
gobuster dir -u http://titanic.htb -w /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt
```

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfFRMjJZsqsP3eEiDpCIUqCYlYTFQDT1XlIJ7OVfXvpg3JLkAwNbgyEc9GSXCsieqfTKSX1UsdcijOtx6lepZfr9ES2BzITwqukmF0yhZLT4O-OihzPmXd3c5RndNOOKZmI1pm3nw?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="563"><figcaption></figcaption></figure>

We find two interesting directories:

* `/download`
* `/book` (this leads to the JSON form)

Let's check out `/download`.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfcjMVidU0woDzBDQyl-9kyfJBAAaX5GdA1HFbdwQz2oBsGnhvUgBjbb10jn6vX1rYy6u-lyoR9lcZ5e6G2tIeS4sc0F_nqt6H4CsAQAhS1lbvcSQoksj36XVmUCYSobgsFp1qIMA?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="375"><figcaption></figcaption></figure>

***

### <mark style="color:purple;">Local File Inclusion (LFI) and Discovery of User</mark>

In the `/download` directory, we notice that the `ticket` parameter is required. We try performing **Local File Inclusion (LFI)** by modifying the URL:

```bash
http://titanic.htb/download?ticket=../../../../etc/passwd
```

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdRx5VipzHvM82ZGbqbI8Zp31cGNRo-0Zo67lqvtvjoAzYcOmxwbi1523EnJDfgBNmY4P6D-iyfxVuxv9lkb5kDaEYzwyhUF76Dfg4LkRRqumURQwypiA-DD930_KwMtAtx0tbPIA?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="375"><figcaption></figcaption></figure>

We successfully download the file and save it as `passwd.titanic.json`. Let's use `grep` to find user information:

```bash
cat passwd.titanic.json | grep home/
```

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXeU6GFNAjV3HHh7L-k43U4v6B1C5vBNTnmk5wvp2twH2cKR5TvqoX8_IMbf3J_IdNj8NcFNq_Amr9V4e0ODJzBjE_iVDyj8Kxc7jmyK1cElr5l9MClVupV1jcgz6nRw4m6OJ88l?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="563"><figcaption></figcaption></figure>

We discover the `developer` user. Next, we attempt to brute-force the SSH login using **hydra**:

```bash
hydra -l developer -P /usr/share/wordlists/rockyou.txt ssh://titanic.htb
```

However, we encounter a "connection refused" error. Despite this, we now have LFI, so let's explore other files.

***

### <mark style="color:purple;">Discovering a Subdomain</mark>

We attempt to access `/etc/hosts` using LFI:

```bash
http://titanic.htb/download?ticket=../../../../etc/hosts
```

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcQtA2quTg2gxTC3Rcujiumk-vGeeS0DWhsysZ3H2enMGO-048PwCbVg1_tBmTE-WxzTWBimZXK1CzSvWG3wKdAkFXtx0c4L1bDyLm3jJMLtMU8HNLBog26EeQ8CCgmWcwhkIzf?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="563"><figcaption></figcaption></figure>

This reveals a subdomain: `dev.titanic.htb`. We add this to our `/etc/hosts` and navigate to it.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXeJF-6Ldv5pxpk9ndDDcboUOD_t83dJka1a9WXPXYc468BU4VQm3__qjjwgnV9UOQ5DOHnIqx9Qi9iCeNZZDxka8uZD02lvH2MDkWohY7ueFAcR9QjCci1AVeRei6bbB08xZglwMw?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="375"><figcaption></figcaption></figure>

***

### Accessing Gitea

The subdomain leads us to **Gitea**, a self-hosted Git service. We see a login page, so we try logging in with `developer:developer`, but we get an error. To verify if "developer" is a valid username, we create a new user, `developer`.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfyzE2glcN2DN2Exzuwztm1VHD2I5cfEbMqbxPUnOO-3eDa61M3zJ1NPrAQaXZZFh5vZjD1XlMk-RKe6VDMbJi1PrYGwmcjv1csld5LWkyVZ5GuqdUnxbEBKoLTqCmqNBUv_6H0Rw?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="375"><figcaption></figcaption></figure>

We can confirm that "developer" is already taken. Now, we create a new user, `developer1`.

We navigate through the "Explore" section and find the **docker-config** repository under the `developer` user.&#x20;

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfSjC_VQuHiGmcFWcmStTYS05Z0v95EzPVPmZLMJfB1rSH3CC1afjzH2eXnnpSACyhtV-QJbtL7vS018Y0W1ZXxpnC8rmhatdfMfsUQLGFo7ivUCPGTGwbGDQpwGfanXy3_wvllkw?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="563"><figcaption></figcaption></figure>

Inside the repository, we explore the `mysql` folder and discover database credentials for the **ticketing system**. We try connecting via MySQL using **DBeaver**, but we don't get access.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfy1er6kFrdgGB-yKqbsr3uX2uQ8Ha9sfPPfNFgwa-5kPZYBOgudZs84dCQbnti3QuXOj_XNGo8_UHa8n845irmQHd87P4bqmfVlwsYccxv-JCUqu2-_Q-71gWELEMq9LxGGK5DMw?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="563"><figcaption></figcaption></figure>

***

### <mark style="color:purple;">Investigating the Gitea Repository</mark>

The `docker-config` repo contains a `README.md` file with information on how the ticketing system works. We also see an `app.py` file that contains route definitions, but nothing out of the ordinary. The `static` folder contains a `.css` file, and the `templates` folder contains an `.html` file. The only remaining folder is `tickets`.

We explore the `tickets` folder and find ticket data, but there's nothing immediately useful. However, based on [Gitea's documentation](https://docs.gitea.com/next/help/faq#where-does-gitea-store-x-file), we learn that it stores the database at:

```shell-session
%(APP_DATA_PATH)/gitea.db
```

***

### <mark style="color:purple;">Using LFI to Access the Database</mark>

We go back to the `/download` directory and modify the URL to access the Gitea database file:

```bash
http://titanic.htb/download?ticket=/home/developer/gitea/data/gitea/gitea.db
```

We open the database and navigate to the `user` table.&#x20;

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdOEuoLUVhkeVyB-Qi4rMURYrxtZShmHFLmI89WYVykqr6jXKlg2YkLhnEH4rcvRirCkgn9Rs-dpe0MBt6OWefiqhocYp2Xb2pS8OcJx0-XzISM3CGXDMzevT20ULu2HjwxcXD6ZA?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="563"><figcaption></figcaption></figure>

Here, we find the hash and salt for the `developer` user. To crack this, we use the [**gitea2hashcat**](https://github.com/unix-ninja/hashcat/blob/master/tools/gitea2hashcat.py) Python script to extract the hash and salt, then run **hashcat** to brute-force the password:

```bash
hashcat -m 10900 hashdeveloper.txt /usr/share/wordlists/rockyou.txt
```

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXeaTFh1L_OeMyq9w0POcGNbHpmhWWv_-AUrZFcz-GJT2RDPd_pAnDw6pJNcnShg-uu0UMvPSEbCWxzuh93ffc14tn0nq2RupLWthqm-JUh_rbrLk_DC389WNFxGvQAti45ahQ_6kw?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="563"><figcaption></figcaption></figure>

After a while, we successfully recover the password and log into SSH as `developer`.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfReJW4pi-ktkloA0acnz4pausX7Z8cQi767KXvI0K5TDjXlira6XuxTqi809MsQRz71Wl90BpueWf9jnh6ofDpXN-59agzFLqzVwGIg9zAiy43AS6XcVFwwdefRYjW04pxU2QIrQ?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="563"><figcaption></figcaption></figure>

***

### <mark style="color:purple;">Privilege Escalation</mark>

Now that we have SSH access, we attempt to escalate privileges. We first check for sudo permissions and SUID binaries, but nothing useful comes up. We decide to investigate manually.

We navigate to the `/opt` directory, where third-party software is stored. Here, we find a script named `identify_images.sh`. After examining the script, we notice it uses **ImageMagick**.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXeWyZcu4aTHIkXFeTpJ2WzKRHu43Wn4YMo2_3UjAvZk5utPLakXqxqViJMGbqg4O4Q57QmTIECq7LDfvIA_PKbhWNdQz7uQTXToexWeom9mmq75mRjUAS9YrrQJ_bPg0Zi2m9B3Nw?key=grehggMPlsE398e0RZJ1Y9Ux" alt="" width="563"><figcaption></figcaption></figure>

Running `magick --version` reveals that the version of **ImageMagick** installed is **7.1.1-35**. We search for known vulnerabilities in this version and find **CVE-2024-41817**, which allows arbitrary code execution.

***

### <mark style="color:purple;">Exploiting ImageMagick Vulnerability</mark>

We create a shared library in the current directory to obtain root privileges and run `cat root.txt` to capture the flag.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfJskOH34v4xIxVqluGJ7Q4Z41SmYSgNSJ7p0z6TQsp2RF92RTnypdQZXjyHfaUkGW1Sx4RHuZQIAgvabGXghCx68dqArOE2c9fQKI2H8sNSZuCHr2tfVA41Uj2KxKWUj2R7u8MyA?key=grehggMPlsE398e0RZJ1Y9Ux" alt=""><figcaption></figcaption></figure>

With a simple `cat root.txt` the flag is our.
