> 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/python-library-hijacking/python-library-hijacking-hashlib-linux-privilege-escalation.md).

# Python-Library-Hijacking (hashlib) - Linux-Privilegieneskalation

## Was es ist

Beim Python-Library-Hijacking wird die Python-Importauflösung ausgenutzt, wenn ein privilegiertes Skript Module aus einem schreibbaren Verzeichnis importiert. Durch das Platzieren eines schädlichen Moduls weiter vorne im Importpfad kann der Angreifer Code als privilegierter Benutzer ausführen. Diese spezielle Seite konzentriert sich auf **Python-Library-Hijacking (hashlib)** 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
python3 -c "import sys; print('/n'.join(sys.path))"
find / -name '*.py' -writable 2>/dev/null
```

## Beispiele

Während **Benutzer jordan**, sehen wir, welche sudoers-Berechtigungen vorhanden sind, und versuchen, das **Python-Skript** als jordanmacia auszuführen, und es funktioniert korrekt:

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

In diesem Fall haben wir **Leserechte, aber keine Schreibrechte**. Wir beobachten, dass es das **Variable hashlib**. Wenn wir eine **locate** mit dem Namen der Variable durchführen, sehen wir, wo sie **gespeichert**. Schließlich beobachten wir, dass in der **PATH von Python**, der erste Eintrag leer ist, und das ist eine Sicherheitslücke:

<figure><img src="/files/64279221a730b0402611ccd3b9aba5325e029f98" alt=""><figcaption></figcaption></figure>

Im selben /tmp-Verzeichnis erstellen wir die **hashlib.py** Datei mit dem folgenden Inhalt. Dieses Skript, wenn wir das Skript ausführen **example.py** beginnt Python im Allgemeinen, in diesem Verzeichnis nach diesen Bibliotheken zu suchen, dann in den in der **Variable sys.path**.

```python
import os 
os.system("bash)
```

Schließlich wird durch Ausführen der **example.py**, verschafft es uns Zugriff auf Bash als Benutzer jordanmacia (wir werden **zu einem Benutzer gewechselt sein** mit vermutlich mehr Berechtigungen):

<figure><img src="/files/4a9d44dfe3988a874799ef4be8b9ddedf4b0eae7" 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/python-library-hijacking/python-library-hijacking-hashlib-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.
