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

# Python 库劫持（hashlib）- Linux 权限提升

Python 库劫持（hashlib）Linux 权限提升笔记，包含枚举步骤、利用示例和面向报告的验证。

## 这是什么

Python 库劫持利用了 Python 的导入解析机制：当一个特权脚本从可写位置导入模块时，通过将恶意模块放在导入路径更靠前的位置，攻击者就可以以特权用户身份执行代码。此页面具体聚焦于 **Python 库劫持（hashlib）** 并保持利用流程实用：识别条件，安全验证，然后运行证明影响所需的最小有效载荷。

## 枚举

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

```bash
python3 -c "import sys; print('/n'.join(sys.path))"
find / -name '*.py' -writable 2>/dev/null
```

## 示例

随着 **用户 jordan**，我们查看 sudoers 权限，然后尝试运行 **Python 脚本** 以 jordanmacia 身份运行，且正常工作：

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8811cb4cc3ac43e54659bb8b1b968d3abdc5a107%2FPasted%20image%2020231123232109.png?alt=media" alt=""><figcaption></figcaption></figure>

在这种情况下，我们有 **读取权限但没有写入权限**。我们观察到它导入了 **变量 hashlib**。如果我们执行 **locate** 并使用该变量名，我们将看到它位于哪里 **存储**。最后，我们注意到在 **Python 的 PATH**中，第一个是空的，这就是一个漏洞：

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-fe8ee3d97fda77a8cccf9372bf38b74b07447ce8%2FPasted%20image%2020231123233030.png?alt=media" alt=""><figcaption></figcaption></figure>

在同一个 /tmp 目录中，我们创建 **hashlib.py** 文件，内容如下。这个脚本在我们运行脚本时 **example.py** 通常，Python 会先在当前工作目录中搜索这些库，然后再在以下定义的路径中搜索 **变量 sys.path**.

```python
import os 
os.system("bash)
```

最后，通过运行 **example.py**，这将使我们能够以 jordanmacia 用户身份访问 Bash（我们将会 **迁移到一个用户** ，可能拥有更多权限）：

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-07a7095f2cae3107125046188f00601336bbb245%2FPasted%20image%2020231123233815.png?alt=media" alt="" width="563"><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 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/python-library-hijacking/python-library-hijacking-hashlib-linux-privilege-escalation.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.
