ReverseEngineering – Telegram
ReverseEngineering
1.24K subscribers
40 photos
10 videos
55 files
666 links
Download Telegram
بخش چهارم بافر اورفلو


توضیح heap overflow

هیپ جایی که برای تخصیص حافظه پویا با malloc calloc یا new
heap overflow زمانی رخ میده که داده ای زیاد در بلاک های heap نوشته بشه
متادیتای allocator یا بلاک های همجوار رو خراب میکنه
خطاهای heap معمولا با کرش در توابع libc یا هنگام free کردن حافظه دیده میشن و ردگیری اونها با tools مثل valgrind یا heap debugger مفیده


کد هیپ
در این کد یک بلوک روی heap اختصاص داده شده و بعد با memset مقدار بیشتری از اندازه نوشته میشه
هدف دیدن خطا در زمان free یا گزارش valgrind هست

کد هیپ فایل any_heap.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    char *p = malloc(32);
    if (!p) return 1;
    printf("allocated 32 bytes on heap\n");
    /* intentionally overflow the heap block for any_heap
       do not use this pattern in real code */
    memset(p, 'A', 64);
    free(p);
    printf("freed block\n");
    return 0;
}


دستورات اجرا و بررسی heap
از valgrind یا malloc debug استفاده کنید تا متادیتا یا خطاها رو ببینید

gcc -g any_heap.c -o any_heap
valgrind --leak-check=full ./
any_heap


# یا اجرا در محیطی با malloc debug فعال

نکات که هنگام نوشتن کد باید بدونیم خطاهای هیپ ممکنه فوری کرش نکنن و اغلب در زمان free یا عملیات بعدی ظاهر میشن
از ابزارهایی مثل valgrind یا malloc debug برای ردگیری استفاده کنید

Part 4 Buffer Overflow


Heap Overflow Explanation
Heap where to allocate dynamic memory with malloc calloc or new
Heap overflow occurs when too much data is written to heap blocks
Corrupts allocator metadata or adjacent blocks
Heap errors are usually seen with crashes in libc functions or when freeing memory and are useful to trace with tools like valgrind or heap debugger

Heap Code
In this code, a block is allocated on the heap and then written with memset more than the size
The goal is to see the error at free time or in valgrind reports

Heap Code File any_heap.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
char *p = malloc(32);
if (!p) return 1;
printf("allocated 32 bytes on heap\n");
/* intentionally overflow the heap block for any_heap
do not use this pattern in real code */
memset(p, 'A', 64);
free(p);
printf("freed block\n");
return 0;
}


Heap execution and inspection commands
Use valgrind or malloc debug to see metadata or errors

gcc -g any_heap.c -o any_heap
valgrind --leak-check=full ./
any_heap


# or run in an environment with malloc debug enabled

Things to know when writing code Heap errors may not crash immediately and often appear during free or subsequent operations
Use tools like valgrind or malloc debug for tracing


@reverseengine
1
Binary Exploitation (اکسپلویت باینری)

یعنی پیدا کردن و سواستفاده از باگ‌ها یا ضعف‌های امنیتی داخل برنامه‌های باینری  Executable  ها برای تغییر جریان اجرای برنامه یا اجرای کد دلخواه مهاجم

تعریف ساده‌تر:

فرض کنید یه برنامه‌ی C دارید که ورودی کاربر رو بدون بررسی میگیره و توی یه بافر کپی میکنه اگه بیشتر از ظرفیت بافر داده بدید میتونید داده‌ هاتون رو توی قسمت‌ های مهم حافظه بنویسید و برنامه رو مجبور کنید کاری که شما میخاید انجام بده

به این میگن Binary Exploitation یعنی استفاده از باگ‌ های حافظه یا منطقی برای گرفتن کنترل Execution


🧱 ساختار پایه‌ای حافظه فرایند:

برای درک Binary Exploitation باید با ساختار حافظه یه برنامه اشنا باشید:

┌───────────────┐  ← آدرس‌های بالا
│ Stack         │  ← شامل متغیرهای لوکال، Return Addressها
├───────────────┤

│ Heap          │  ← شامل آبجکت‌های داینامیک (malloc/new)
├───────────────┤

│ BSS & Data    │  ← متغیرهای global/static

├───────────────┤

│ Text (.text)  │  ← کد برنامه (read-only)
└───────────────┘  ← آدرس‌های پایین

اکسپلویت معمولا توی Stack یا Heap اتفاق میوفته

🧨 مهم‌ترین نوع باگ‌ها در Binary Exploitation

نوع آسیب‌ پذیری:

Buffer Overflow
نوشتن داده بیش از حد مجاز داخل بافر  میتونه Return Address رو overwrite کنه

Stack-based Overflow Overflow
توی استک  معمولا برای کنترل EIP/RIP استفاده میشه

Heap Overflow
اورفلو توی Heap  باعث خرابی ساختار های malloc میشه

Use-After-Free
استفاده از اشاره‌ گر بعد از free کردن باعث کنترل حافظه آزاد شده میشه

Format String Bug
استفاده ناامن از printf  میتونه memory leak یا write بده

Integer Overflow/Underflow
باعث خطا در تخصیص حافظه یا bypass کردن چک‌ ها میشه

Double Free
آزاد کردن یک pointer دوبار  منجر به corruption توی heap میشه



🧭 مراحل کلی Binary Exploitation:

کرش ایجاد کنید Bug Trigger:

با ورودی خاص باعث کرش برنامه شید

مثلا با یک رشته خیلی بلند بافر رو سرریز کنید



باگ رو آنالیز کنید:

از gdb, pwndbg, gef, یا radare2 استفاده کنید

بفهم چی دقیقا overwrite شده چه رجیستر هایی قابل کنترل هستن و جریان اجرای برنامه چطوریه



لیک اطلاعات امنیتی:

برای بایپس کردن ASLR یا PIE باید آدرس‌ ها رو leak کنید

مثلا از format string استفاده میکنید تا pointer ها رو چاپ کنید



ساخت Payload Exploit:

مثلا ROP chain یا Shellcode بسازید

با دقت Return Address رو به گجت‌ها یا shellcode خودتون تغییر بدید


بایپس کردن Protections:

مرورگرها و باینری‌های مدرن محافظت‌هایی دارن مثل:

ASLR
آدرس‌ ها تصادفی میشن باید leak بگیری

NX / DEP

استک اجرایی نیست باید ROP استفاده کنید

Canary

محافظت در برابر overflow باید نشتش بدید یا دورش بزنید

PIE / RELRO
سخت‌تر کردن کنترل GOT/PLT



گرفتن کنترل نهایی:

معمولا exploit به یکی از این‌ ها ختم میشه:

اجرای Shellcode و گرفتن شل 🐚

اجرای ROP chain برای اجرای دستورات سیستم

تغییر رفتار برنامه به نفع مهاجم



🧰 ابزارهای مهم در Binary Exploitation

ابزارهای پرکاربرد:

gdb + pwndbg یا gef دیباگ و آنالیز کرش و حافظه

radare2, Ghidra, IDA Pro  مهندسی معکوس باینری

ROPgadget, ROPPER پیدا کردن گجت‌های ROP

pwntools ساخت سریع اکسپلویت با پایتون

angr, Qiling تحلیل سمبولیک یا امولیشن پیشرفته

checksec بررسی محافظت‌ های باینری



🧪 مثال خیلی ساده Stack Overflow کلاسیک

کد آسیب‌پذیر:

#include <stdio.h>
#include <string.h>

void vuln() {
    char buf[64];
    gets(buf);  // ناامن
    printf("You said: %s\n", buf);
}

int main() {
    vuln();
    return 0;
}


اگه توی ورودی بیشتر از 64 بایت بدید می‌تونی Return Address رو overwrite کنید و برنامه رو به جای برگشت به main بفرستید روی آدرس shellcode خودتون


Binary Exploitation

It means finding and exploiting bugs or security weaknesses in binary executable programs to change the flow of program execution or execute the attacker's desired code

Simpler definition:

Suppose you have a C program that takes user input without checking and copies it into a buffer. If you give it more than the buffer capacity, you can write your data into important parts of memory and force the program to do what you want

This is called Binary Exploitation, which means using memory or logic bugs to take control of Execution

🧱 Basic Structure of Process Memory:

To understand Binary Exploitation, you need to be familiar with the memory structure of a program:
3
┌──────────────┐ ← High Addresses
│ Stack │ ← Contains Local Variables, Return Addresses
├──────────────┤

│ Heap │ ← Contains Dynamic Objects (malloc/new)
├────────────────────────────────────

│ BSS & Data │ ← Global/static variables

├───────────────────────┘ ← Lower addresses

Exploits usually occur in the Stack or Heap

🧨 Most important types of bugs in Binary Exploitation

Type of vulnerability:

Buffer Overflow
Writing more data than allowed into the buffer can overwrite the Return Address

Stack-based Overflow Overflow
The stack is usually used to control EIP/RIP

Heap Overflow
Overflow in the heap causes malloc structures to fail

Use-After-Free
Using a pointer after freeing causes control over freed memory

Format String Bug
Unsafe use of printf can cause memory leak or write

Integer Overflow/Underflow
Causes memory allocation or bypass errors Checks are

Double Free
Freeing a pointer twice leads to heap corruption

🧭 General steps of Binary Exploitation:

Create a crash Bug Trigger:

Make the program crash with specific input

For example, overflow the buffer with a very long string

Analyze the bug:

Use gdb, pwndbg, gef, or radare2

Understand what exactly was overwritten, what registers are controllable, and what the execution flow is like

Leak Security Information:

To bypass ASLR or PIE, you need to leak addresses

For example, you use a format string to print pointers

Build Payload Exploit:

For example, build a ROP chain or Shellcode

Carefully change the Return Address to your gadgets or shellcode

Bypass Protections:

Modern browsers and binaries have protections such as:

ASLR
Addresses are randomized, you need to leak

NX / DEP
Stack is not executable, you should use ROP

Canary
Overflow protection should be leaked or bypassed

PIE / RELRO
Hardening GOT/PLT control

Taking ultimate control:

Usually an exploit ends in one of the following:

Executing Shellcode and getting a shell 🐚

Executing ROP chain to execute system commands

Changing program behavior in favor of the attacker

🧰 Important tools in Binary Exploitation

Most used tools:

gdb + pwndbg or gef
Debugging and crash and memory analysis

radare2, Ghidra, IDA Pro
Binary reverse engineering

ROPgadget, ROPPER
Finding ROP gadgets

pwntools
Quickly building exploits with Python

angr, Qiling
Symbolic analysis or advanced emulation

checksec Checking binary protections

🧪 Very simple classic Stack Overflow example

Vulnerable code:

#include <stdio.h>
#include <string.h>

void vuln() {
char buf[64];
gets(buf); // Insecure
printf("You said: %s\n", buf);
}

int main() {
vuln();
return 0;
}


If you give more than 64 bytes in input, you can overwrite the Return Address and send the program to your shellcode address instead of returning to main


@reverseengine
7
Forwarded from Fuzzing ZONE (0xB01)
Someone has created a website revealing information about the developers behind Lumma stealer

https://lummakrysy.rip/

@FUZZ0x
1
خوندن تابع توی دیس‌ اسمبلر

چطور از اسمبلی بفهمید یک تابع دقیقا چیکار میکنه پروتکل ورودی‌ها، شرط‌ها، و خروجی‌ها


تا حالا یه تابع رو دیدید توی دیس‌اسمبلر که انگار رمزآلود یا ی جوری حرف میزنه؟ اول دنبال prologue و epilogue باشید اون push/pop و mov های اول و آخر پارامترها معمولا توی رجیسترها یا روی stack میان ببینید کجا با حافظه کار میکنه چه آرگومانی میخونه و چی برمیگردونه اسم‌گذاری ساده بزنید: مثلا check_len یا read_input — بعد pseudocode رو بخونید یه خط کامنت بزنید که این تابع طول رشته رو چک میکنه و اگر > 5 بود OK میده

مثال:
فایل ساده‌ای باز کنید که ورودی میگیره توی Ghidra/IDA تابع main رو باز کنید ببینید push rbp، mov rbp, rsp و چند cmp/jle هست یا نه اگر یه cmp با عدد 5 دیدید حدس بزنید اینجا طول رو مقایسه میکنه اسمش رو بذارید check_length و pseudocode رو بخونید

تشخیص (دیتکشن)

push/pop
‌های زیاد یا غیر متعارف تو پرولوگ/اپیلوگ

cmp/jmp
‌هایی که تعداد زیادی branch تولید میکنن

استفاده مکرر از توابعی مثل strlen، memcpy یا فراخوانی‌ های ورودی/خروجی نشونه پردازش ورودی


میتیگیشن یا کاهش (دفاع ها)

لاگ‌گذاری ورودی‌ها و بررسی edge-case ها با unit test

برای تشخیص رفتاری: Telemetry که فراخوانی‌های حساس مثل خوندن فایل/شبکه رو لاگ کنه تا اگر تابع‌ های مشکوک ظاهر شد سریع ببینید


Reading a Function in a Disassembler

How to Find Out from Assembly What a Function Does Exactly? Input Protocol, Conditions, and Outputs

Have you ever seen a function in a disassembler that seems to be talking cryptically or something? First look for prologue and epilogue. Those push/pop and mov of the first and last parameters are usually in registers or on the stack. See where it works with memory. What arguments it reads and what it returns. Give it a simple name: for example check_len or read_input — then read the pseudocode. Comment a line that says this function checks the length of the string and if it is > 5 it says OK.

Example:
Open a simple file that takes input in Ghidra/IDA. Open the main function. See if there are push rbp, mov rbp, rsp and a few cmp/jle. If you see a cmp with the number 5, guess where it compares the length. Name it check_length and read the pseudocode.

Detection
Too many or unusual push/pops in the prologue/epilogue.

Cmp/jmps that generate a lot of branches.

Repeated use of functions like strlen, memcpy or I/O calls. Process token Input

Mitigation
Logging inputs and examining edge-cases with unit tests

For behavioral detection: Telemetry that logs sensitive calls like file/network reads so you can quickly see if suspicious functions appear

@reverseengine
1
دیباگینگ با Breakpoint و Step


دیباگینگ یعنی اینکه برنامه رو متوقف کنید ببینید دقیقا اون لحظه داره چی کار میکنه یجور دوربین صحنه آهسته روی مغز برنامه‌ ست


اگه تا حالا حس کردید کد یه برنامه مثل جادو کار میکنه وقتشه جادوشو بشکنید💀
با یه دیباگر مثل x64dbg یا gdb میتونید وسط اجرای برنامه ترمز برنامه نویس رو بکشید😂
یه breakpoint بذارید روی یه تابع خاص مثلا جایی که ورودی رو بررسی میکنه بعد با دکمه F8 یا Step برید خط‌ به‌ خط جلو ببینید مقدار رجیسترها و متغیرها چطوری تغییر میکنن
اگه شرطی داره (cmp/jz و از اینا) مقدار رو قبل و بعدش ببینید تا بفهمی چرا true یا false میشه
کم‌کم ذهنتون کد رو از اسمبلی به رفتار واقعی ترجمه میکنه  اونجاست که میفهمید باینری چطور فکر میکنه👾

مثال عملی:
برنامه‌ ای باز کنید که عدد از کاربر میگیره و میگه بزرگ‌تر از ۱۰ هست یا نه
روی جایی که شرط چک میشه (cmp eax, 0Ah) breakpoint بذارید
مقدار eax رو موقع stop ببینید اگه 9 باشه پرش انجام نمیشه اگه 12 باشه میپره به مسیر بله بزرگتره

دیتکشن

اگر دیدید برنامه مدام سعی میکنه دیباگر رو ببنده یا Thread Information Query میفرسته  یعنی anti-debug داره

اگه اجرای معمولی و اجرای با دیباگر خروجی متفاوت بده یعنی داره دنبال حضورتون میگرده👀


میتیگیشن

محقق: همیشه توی VM با snapshot کار کنید تا اگه برنامه قاطی کرد راحت برگردید

مدافع: رفتارهایی مثل تشخیص دیباگر رو در لاگ‌ های امنیتی دنبال کنید چون معمولا نشونه نرم‌ افزار محافظت‌ شده یا بدافزاره



Debugging with Breakpoints and Steps

Debugging means stopping the program and seeing exactly what it is doing at that moment. It is like a slow-motion camera on the brain of the program.

If you have ever felt that the code of a program works like magic, it is time to break its magic💀

With a debugger like x64dbg or gdb, you can brake the programmer in the middle of the program😂

Set a breakpoint on a specific function, for example, where it checks the input. Then use the F8 or Step button to go forward line by line and see how the values ​​of registers and variables change.

If there is a condition (cmp/jz and so on), look at the value before and after to understand why it becomes true or false.

Your mind will gradually translate the code from assembly to real behavior. That is where you will understand how binary thinks👾


Practical example:
Open a program that takes a number from the user and tells whether it is greater than 10 or not.
Set a breakpoint where the condition is checked (cmp eax, 0Ah).

Value Check eax at stop. If it is 9, the jump will not be performed. If it is 12, it will jump to the larger path.

Detection

If you see that the program keeps trying to close the debugger or sends Thread Information Query, it means it has anti-debug.

If normal execution and execution with debugger give different output, it means it is looking for your presence👀

Mitigation

Researcher:
Always work with snapshots in the VM so that if the program messes up, you can easily return.

Defender:
Follow behaviors such as debugger detection in security logs because they are usually signs of protected software or malware.


@reverseengine
👏4
بخش پنجم بافر اورفلو


off by one

off by one زمانی رخ میده که دقیقا یک بایت یا یک واحد کمتر یا بیشتر در نوشتن تحت کنترل باشه

این خطاها کوچک به نظر میرسن اما میتونن باعث تغییر یک بایت از saved rbp یا تغییر null terminator رشته بشن و رفتار غیرمنتظره ایجاد کنن
تشخیص off by one نیاز به دقت در اندازه ها و دیدن بایت های محلی داره

توضیح کد:
در این کد نشون میدیم که نوشتن دقیقا یک بایت میتونه نال ترمینیتور رشته رو از بین ببره یا بایت مهمی رو تغییر بده
هدف دیدن خروجی متفاوت رشته هست

کد آف بای وان offbyone.c

#include <stdio.h>
#include <string.h>

int main() {
    char s[8];
    /* suppose we incorrectly copy 9 bytes into 8 byte buffer */
    memcpy(s, "ABCDEFGH", 8);
    s[7] = 'Z'; /* simulate off by one by modifying last byte */
    printf("string maybe not null terminated %s\n", s);
    return 0;
}


دستورات رو اجرا کنید و ببینید off by one
این مثال رو اجرا کنید و خروجی رو نگاه کنید تا مشکل در اخر رشته مشخص بشه

gcc -g offbyone.c -o offbyone
./offbyone


ببینید که رشته ممکنه نال ترمینیت نشده باشه



Part 5 Buffer Overflow


off by one

off by one occurs when exactly one byte or one unit is under control in the write

These errors seem small, but they can change a byte from the saved rbp or change the null terminator of the string and cause unexpected behavior

Detecting off by one requires accuracy in sizes and seeing local bytes

Code explanation:
In this code, we show that writing exactly one byte can remove the null terminator of the string or change an important byte

The goal is to see different output of the string

Off by one code offbyone.c

#include <stdio.h>
#include <string.h>

int main() {

char s[8];

/* suppose we incorrectly copy 9 bytes into 8 byte buffer */
memcpy(s, "ABCDEFGH", 8);
s[7] = 'Z'; /* simulate off by one by modifying last byte */
printf("string maybe not null terminated %s\n", s);
return 0;
}


Run the commands and see off by one
Run this example and look at the output to see if the problem is at the end of the string

gcc -g offbyone.c -o offbyone
./offbyone


See if the string might not be null terminated


@reverseengine
4