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

# اختطاف مكتبة Python (hashlib) - تصعيد امتيازات لينكس

## ما هو

يستغل اختطاف مكتبة 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="/files/df77d734ae40284cee7866b97ff46b94c4f5856d" alt=""><figcaption></figcaption></figure>

في هذه الحالة، لدينا **صلاحيات قراءة ولكن دون كتابة**، نلاحظ أنه يستورد **المتغير hashlib**، إذا قمنا بإجراء **locate** باستخدام اسم المتغير، سنرى أين يتم **تخزينه**، أخيرًا، نلاحظ أنه في **PATH الخاص ببايثون**، الأول فارغ وهذا يمثل ثغرة:

<figure><img src="/files/771878058087ccfa49bef339ad64545eed9217a1" alt=""><figcaption></figcaption></figure>

في نفس دليل /tmp، ننشئ **hashlib.py** الملف بالمحتوى التالي. هذا السكربت، عندما نشغّل السكربت **example.py** بشكل عام، تبدأ بايثون بالبحث عن هذه المكتبات في دليل العمل الحالي، ثم في المسارات المحددة في **المتغير sys.path**.

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

أخيرًا، عند تشغيل **example.py**، سيوفر لنا الوصول إلى Bash كمستخدم jordanmacia (سنكون قد **انتقلنا إلى مستخدم** ذو صلاحيات أكثر على الأرجح):

<figure><img src="/files/68582c2bb85261d14c41ed3820a353f745a5fd01" 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/python-library-hijacking/python-library-hijacking-hashlib-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.
