en
Feedback

Don't get caught by a cheater! Telemetrio finds and tags such channels 👉 If you want to see the tag, subscribe

ReverseEngineering

ReverseEngineering

Open in Telegram
1 730
Subscribers
-124 hours
-47 days
+28230 days
Posts Archive
Part 27 Buffer Overflow
libFuzzer and Coverage Guided Fuzzing So far, we have understood that Fuzzing means giving a lot of different inputs to the program and waiting for it to mess up somewhere😁 But new Fuzzers don't just generate random inputs. Some of them check what paths each input has taken in the program, which makes it gradually generate more interesting inputs What does Coverage Guided mean Suppose a program looks like this
Input ↓ Check 1 ↓ Check 2 ↓ Hidden Function
If the first input only reaches Check 1 It tries to change the input path so that a new Fuzzer opens For example, it reaches Check 2 Then it uses the same input again and makes more changes The goal is to run more parts of the program because apparently we decided to go everywhere in the program to find the bug 😅 libFuzzer What does libFuzzer do A Fuzzing Engine for C Programs And it's C++ that works with LLVM and Clang. We give it a specific function. Then it runs that function over and over again with different inputs. Each input that leads to a new path becomes more valuable. The main Fuzzing function Usually we have something like this: C
#include <stdint.h> #include <stddef.h> int LLVMFuzzerTestOneInput( const uint8_t *data, size_t size ) { return 0; }
Explanation of the code below This function is the target of the Fuzzer Every time libFuzzer generates a new input, the content of the input is placed in data and its size is placed in size A simple example: C
#include <stdint.h> #include <stddef.h> #include <string.h> int LLVMFuzzerTestOneInput( const uint8_t *data, size_t size ) { if (size >= 5) { if (memcmp(data, "HELLO", 5) == 0) { volatile int x = 1; (void)x; } } return 0; }
What happens here The Fuzzer trys different inputs For example
AAAA
then
HELAA
then maybe
HELLO
When the input reaches HELLO a new path is executed from the program Coverage Guided Fuzzing detects this new path and stores that input to use for finding subsequent paths Compile with Clang: shell
clang -g -fsanitize=fuzzer,address file23_fuzz.c -o file23_fuzz
Here two things are enabled together
libFuzzer + AddressSanitizer
libFuzzer generates input ASan watches for memory errors This combination is very powerful for finding memory bugs Run Fuzzer shell
./file23_fuzz
Then the program starts generating and modifying inputs If the input causes a crash, usually the same input is saved so that we can check it again later Let's do Why is it important to us? Suppose you have a complex program But you don't know exactly what input will cause a critical function to be reached Fuzzer can find new paths by trying different inputs Then you can examine those paths in Ghidra or IDA That is:
Fuzzing ↓ New Code Path ↓ Crash or Behavior ↓ Ghidra / IDA ↓ Assembly Analysis
Coverage Guided Fuzzing It doesn't just look for crashes It also looks for new paths Each new path is a new part of the program that is worth examining and when we combine libFuzzer with ASan we can both generate smarter inputs and detect memory bugs faster Exercise: Change the above function a little and add a new condition for a specific input then think about how the Fuzzer should change the input step by step to reach that new path @reverseengine
25 Aug, 18:27

بخش بیست و هفتم بافر اورفلو
libFuzzer و Coverage Guided Fuzzing تا اینجا فهمیدیم Fuzzing یعنی دادن تعداد زیادی ورودی مختلف به برنامه و منتظر موندن تا یک جایی خرابکاری کنه😁 ولی Fuzzer های جدید فقط ورودی رندوم تولید نمیکنن بعضی از اونها بررسی میکنن هر ورودی برنامه رو از چه مسیر هایی عبور داده و همین باعث میشه کم کم ورودی‌ های جالب‌ تر تولید کنه Coverage Guided یعنی چی فرض کن یک برنامه این شکلیه
Input ↓ Check 1 ↓ Check 2 ↓ Hidden Function
اگر ورودی اول فقط به Check 1 برسه سعی میکنه ورودی مسیر رو تغییر بده تا Fuzzer جدیدی باز بشه مثلا به Check 2 برسه بعد دوباره از همون ورودی استفاده میکنه و تغییرات بیشتری میده هدف اینه که قسمت‌ های بیشتری از برنامه اجرا بشن چون خب ظاهرا ما تصمیم گرفتیم برای پیدا کردن باگ باید به همه جای برنامه سرک بکشیم 😅 libFuzzer چیکار میکنه libFuzzer یک موتور Fuzzing برای برنامه‌های C و ++C است که با LLVM و Clang کار میکنه ما یک تابع مشخص به اون میدیم بعد خودش بار ها و بار ها اون تابع رو با ورودی‌ های مختلف اجرا میکنه هر ورودی که باعث رسیدن به مسیر جدیدی بشه ارزشمند تر میشه تابع اصلی Fuzzing معمولا چیزی شبیه این داریم: C
#include <stdint.h> #include <stddef.h> int LLVMFuzzerTestOneInput( const uint8_t *data, size_t size ) { return 0; }
توضیح کد زیر این تابع هدف Fuzzer هست هر بار libFuzzer یک ورودی جدید تولید میکنه محتوای ورودی داخل data قرار میگیره و اندازه اون داخل size قرار میگیره یک مثال ساده: C
#include <stdint.h> #include <stddef.h> #include <string.h> int LLVMFuzzerTestOneInput( const uint8_t *data, size_t size ) { if (size >= 5) { if (memcmp(data, "HELLO", 5) == 0) { volatile int x = 1; (void)x; } } return 0; }
اینجا چه اتفاقی میوفته Fuzzer ورودی‌ های مختلف رو امتحان میکنه مثلا
AAAAA
بعد
HELAA
بعد شاید
HELLO
وقتی ورودی به HELLO برسه یک مسیر جدید از برنامه اجرا میشه Coverage Guided Fuzzing این مسیر جدید رو تشخیص میده و اون ورودی رو نگه میداره تا از اون برای پیدا کردن مسیرهای بعدی استفاده کنه کامپایل با Clang shell
clang -g -fsanitize=fuzzer,address file23_fuzz.c -o file23_fuzz
اینجا دو چیز با هم فعال شده
libFuzzer + AddressSanitizer
libFuzzer ورودی تولید میکنه ASan مراقب خطا های حافظه هست این ترکیب برای پیدا کردن Memory Bug خیلی قدرتمنده اجرای Fuzzer shell
./file23_fuzz
بعد برنامه شروع میکنه به تولید و تغییر ورودی‌ ها اگر ورودی باعث کرش بشه معمولا همون ورودی ذخیره میشه تا بتونیم بعدا دوباره بررسیش کنیم چرا برای ما مهمه؟ فرض کنید یک برنامه پیچیده دارید ولی دقیقا نمیدونید چه ورودی باعث رسیدن به یک تابع حساس میشه Fuzzer میتونه با امتحان کردن ورودی‌ های مختلف مسیر های جدید رو پیدا کنه بعد شما میتونید همون مسیر ها رو داخل Ghidra یا IDA بررسی کنید یعنی:
Fuzzing ↓ New Code Path ↓ Crash یا Behavior ↓ Ghidra / IDA ↓ Assembly Analysis
Coverage Guided Fuzzing فقط دنبال کرش نیست دنبال مسیر های جدید هم هست هر مسیر جدید یعنی بخش جدیدی از برنامه که ارزش بررسی داره و وقتی libFuzzer رو با ASan ترکیب میکنیم هم میتونیم ورودی‌ های هوشمندانه‌ تر تولید کنیم هم Memory Bug ها رو سریع‌ تر تشخیص بدیم تمرین: تابع بالا رو کمی تغییر بدید و یک شرط جدید برای یک ورودی خاص اضافه کنید بعد فکر کنید Fuzzer چطور باید قدم به قدم ورودی رو تغییر بده تا به اون مسیر جدید برسه @reverseengine
25 Aug, 18:24

Dead Code and Junk Code Code that exists but is not supposed to do anything One of the common methods of Obfuscation is to put a lot of extra code into the program that either does not run at all or runs but has no effect on the final result of the program The goal is that when we open the file, we see a lot of extra instructions and it becomes harder to find the real logic For example, look at this simple code:
int result = a + b; int x = 50; x = x * 2; x = x - 30; return result;
Here, the calculations related to x have no effect on the result, so from the logic of the program this section is considered Dead Code Now let's see an example in assembly:
mov eax, 10 add eax, 20 mov ecx, 500 xor ecx, ecx add ecx, 100 ret
If the value of ecx is not used anywhere later, the part related to ecx has practically no effect on the output of the function. This is what we need to learn to recognize. But Junk Code is not always this simple. An Obfuscator may add instructions that appear to be important:
push rax xor rcx, rcx inc rcx dec rcx pop rax
In appearance, several operations have been performed But at the end, the important state of the program is almost the same as before this block. In real analysis, one of the best questions is: What did this block change that will actually be used later? If the answer is nothing, we are probably dealing with Junk Code. A good way to analyze is to only follow the values ​​that reach the output or subsequent stages of the program. For example, if a value is created in RAX but is overwritten before being used again, the previous calculation probably does not matter. But you have to be careful here. Any code that does not have a clear output is not Junk Code. It may affect flags, change memory, or have side effects. So before mentally deleting a block, you should check: Where do the output values ​​go? Has memory changed? Has an important flag changed? Has another function been called? Will the result of this operation be used later? Deobfuscation That is, gradually things that do not affect the main logic are removed and the real structure of the program is revealed. Exercise: Examine this code: C++
int calculate(int a, int b) { int x = a + b; int temp = 500; temp ^= 123; temp += 20; temp -= 20; return x; }
Determine which part affects the output of the function and which part just makes the analysis busy. Then compile this example and open it in Ghidra and see what the compiler does with the extra part. It may have removed the entire useless part even before you see it in the output file, because compilers sometimes do useful things against our expectations. @reverseengine
25 Aug, 18:03

Dead Code و Junk Code کدی که هست ولی قرار نیست کاری انجام بده یکی از روش‌ های رایج Obfuscation اینه که داخل برنامه مقدار زیادی کد اضافه قرار بدن که یا اصلا اجرا نمیشه یا اجرا میشه ولی هیچ تاثیری روی نتیجه نهایی برنامه نداره هدف اینه که وقتی ما فایل رو باز میکنیم حجم زیادی از دستورهای اضافی ببینیم و پیدا کردن منطق واقعی سخت‌تر بشه مثلا این کد ساده رو ببینید:
int result = a + b; int x = 50; x = x * 2; x = x - 30; return result;
اینجا محاسبات مربوط به x هیچ تاثیری روی result نداره پس از نظر منطق برنامه این بخش Dead Code محسوب میشه حالا یک مثال در اسمبلی ببینیم:
mov eax, 10 add eax, 20 mov ecx, 500 xor ecx, ecx add ecx, 100 ret
اگر مقدار ecx هیچ جا بعدا استفاده نشه بخش مربوط به ecx عملا تاثیری روی خروجی تابع نداره این همون چیزیه که ما باید یاد بگیریم تشخیص بدیم اما Junk Code همیشه به این سادگی نیست ممکنه یک Obfuscator دستورهایی اضافه کنه که ظاهرشون مهم به نظر میرسه:
push rax xor rcx, rcx inc rcx dec rcx pop rax
در ظاهر چند عملیات انجام شده ولی در اخر وضعیت مهم برنامه تقریبا همون چیزیه که قبل از این بلاک بوده در تحلیل واقعی یکی از بهترین سوال‌ها اینه: این بلاک چه چیزی رو تغییر داد که بعدا واقعا استفاده میشه؟ اگر جواب هیچ‌ چیز باشه احتمال داره با Junk Code طرف باشیم یک روش خوب برای تحلیل اینه که فقط مقدار هایی رو دنبال کنید که به خروجی یا مرحله های بعدی برنامه میرسن مثلا اگر یک مقدار داخل RAX ساخته بشه ولی قبل از استفاده دوباره overwrite بشه احتمالا محاسبه قبلی اهمیت نداشته ولی اینجا باید حواستون جمع باشه هر کدی که خروجی واضحی نداره Junk Code نیست ممکنه روی Flagها تاثیر بذاره حافظه رو تغییر بده یا اثر جانبی داشته باشه پس قبل از حذف ذهنی یک بلاک رو باید بررسی کنید: مقدارهای خروجی کجا میرن؟ حافظه تغییر کرده؟ Flag مهمی تغییر کرده؟ تابع دیگه ای صدا زده شده؟ نتیجه این عملیات بعدا استفاده میشه؟ Deobfuscation یعنی همین کم‌ کم چیزهایی که تاثیری روی منطق اصلی ندارن کنار میرن و ساختار واقعی برنامه مشخص میشه تمرین: این کد رو بررسی کنید: C++
int calculate(int a, int b) { int x = a + b; int temp = 500; temp ^= 123; temp += 20; temp -= 20; return x; }
مشخص کنید کدوم قسمت روی خروجی تابع تاثیر داره و کدوم قسمت فقط باعث شلوغ شدن تحلیل میشه بعد همین مثال رو Compile کنید و داخل Ghidra باز کنید ببینید Compiler با بخش اضافی چه کاری میکنه ممکنه حتی قبل از اینکه تو فایل خروجی رو ببینید خودش کل بخش بی‌ استفاده رو حذف کرده باشه چون کامپایلر ها هم بعضی وقتا برخلاف انتظارمون کار مفید انجام میدن @reverseengine
25 Aug, 18:01

این مدیوم منه اگه اونجا هم منو فالو کنید خوشحال میشم اونجا هم همین چیزای کانال رو میزارم و ی سری چیزای اضافه🖤 This is my Medium. I would be happy if you followed me there too. I will post the same things from the channel there and a few extra things🩶 https://medium.com/@addcss012
21 Aug, 18:52

20 Aug, 19:14

Modern iOS Security Features – A Deep Dive into SPTM, TXM, and Exclaves https://arxiv.org/pdf/2510.09272 @reverseengine
20 Aug, 19:13

+4
Frida HandBook.pdf4.85 MB
20 Aug, 19:12

20 Aug, 19:12

20 Aug, 19:10

The best of Reverse Engineering Reading List in world https://github.com/onethawt/reverseengineering-reading-list @reverseengine
20 Aug, 19:08

20 Aug, 19:07

The classic and official reference for ELF structure https://refspecs.linuxfoundation.org/elf/gabi4+/contents.html @reverseengine
20 Aug, 19:05

How does Parent wait for Child? So far we have learned two of the most important parts of the Process API: fork() → create Child exec() → execute a new program inside Child Now a question: How does Parent know when Child is finished? This is where wait() comes in What does wait() do? Very simple: wait() makes Parent wait until one of its Children is finished For example:
Parent │ ├── fork() │ ▼ Child │ ├── does its job │ └── finishes │ ▼ Parent continues
That is, Parent does not say: I have nothing more to do with you So it says: When the job is done, I will continue A very simple example: Suppose we have a program that wants to create a Child:
pid = fork(); if (pid == 0) { // Child exec(...); } else { // Parent wait(...); }
Here: Child executes the new program Parent waits with wait() When Child finishes, Parent returns from wait() and continues its execution Why do we need to wait at all? Suppose you open Shell and type: python test.py If Shell immediately executes the following tasks without any coordination, the output and execution order of the programs may not be what we expect For a typical execution of a command Shell can: Create a Child 2 Run the program inside the Child 3 Wait for the Child to finish 4 Display the Prompt again Approximately:
Shell │ ├── fork() │ ▼ Child │ └── exec() │ ▼ Program │ exit() │ ▼ Shell │ ▼ Prompt
An important point: wait() does not stop the Process Here is a common mistake When Parent wait() it does not mean that the entire operating system is stopped Only that Process waits The CPU can execute other Processes during the same time For example:
Parent → Waiting Child → Running Chrome → Running Browser → Running
The operating system still gives CPU to other processes What if the Child terminates early? If the Child has finished before the Parent reaches wait(), the operating system keeps the necessary information about the end of the Child so that the Parent can get its end status. Here we come to an important concept called Zombie Process. Zombie is a Process that has finished executing but the Parent has not yet received its end status. We will talk about Zombie and wait() in more detail later. Now let's put the main three together. So far we have:
fork()
Create Child:
Parent ↓ Child exec()
Replace the program inside Child:
Child ↓ New Program wait()
Parent Waits for the Child to finish:
Parent ↓ wait() ↓ Child finishes ↓ Parent continues So the classic pattern becomes: Parent │ fork() │ ▼ Child │ exec() │ ▼ New Program │ exit() │ ▼ Parent │ wait() │ ▼ Continue
Of course, the exact order of wait() in relation to Child execution depends on how the program is implemented. This diagram only shows the conceptual pattern. Why is this part important? When you analyze a program on Linux and you come across:
fork exec wait exit
in the code, you can now guess what is happening. For example:
fork() ↓ A new Process ↓ exec() ↓ Executing another program ↓ exit() ↓ wait() ↓ Parent continues.
You no longer just see the names of a few functions, you also understand the logic behind them. So far, we have learned the Process API with three main concepts: fork() → Creating a Child exec() → Replacing a program inside a Process wait() → Waiting for Parent to terminate Child @reverseengine
20 Aug, 19:00

Parent چطور منتظر Child میمونه؟ تا اینجا دو تا از مهم‌ ترین قسمت‌ های Process API رو یاد گرفتیم: fork() → ساخت Child exec() → اجرای یک برنامه جدید داخل Child حالا یک سؤال: Parent از کجا بفهمد Child کارش تموم شده؟ اینجاست که wait() وارد میشه wait() چیکار میکنه؟ خیلی ساده: wait() باعث میشه Parent منتظر بمونه تا یکی از Child هاش تموم شه مثلا:
Parent │ ├── fork() │ ▼ Child │
├── کار خودش رو انجام میده │ └── تموم میشه │ ▼ Parent ادامه میده یعنی Parent نمیگه: من دیگه کاری باهات ندارم پس میگه: کارت که تموم شد من ادامه میدم یک مثال خیلی ساده: فرض کنید یک برنامه داریم که میخاد یک Child بسازه:
pid = fork(); if (pid == 0) { // Child exec(...); } else { // Parent wait(...); }
اینجا: Child برنامه جدید رو اجرا میکنه Parent با wait() منتظر میمونه وقتی Child تموم شد Parent از wait() برمیگرده و اجرای خودش رو ادامه میده چرا اصلا باید منتظر بمونیم؟ فرض کنید Shell رو باز کردید و مینویسید: python test.py اگر Shell بدون هیچ هماهنگی فورا کارهای بعدی رو انجام بده ممکنه خروجی و ترتیب اجرای برنامه‌ها چیزی نباشه که انتظار داریم برای اجرای معمولی یک دستور Shell میتونه: Child بسازه 2 برنامه رو داخل Child اجرا کنه 3 منتظر پایان Child بمونه 4 دوباره Prompt رو نمایش بده تقریبا:
Shell │ ├── fork() │ ▼ Child │ └── exec() │ ▼ Program │ exit() │ ▼ Shell │ ▼ Prompt
یک نکته مهم: wait() Process رو متوقف نمیکنه اینجا یک اشتباه رایج وجود داره وقتی Parent wait() میکنه به این معنی نیست که کل سیستم‌ عامل متوقف شده فقط همون Process منتظر میمونه CPU میتونه در همین مدت Process های دیگه ای رو اجرا کنه مثلا:
Parent → Waiting Child → Running Chrome → Running Browser → Running
سیستم‌ عامل همچنان به بقیه Process ها CPU میده اگر Child زودتر تموم بشه چی؟ اگر Child قبل از اینکه Parent به wait() برسه تموم شده باشه سیستم‌ عامل اطلاعات لازم مربوط به پایان Child رو نگه میداره تا Parent بتونه وضعیت پایان اون رو بگیره اینجا به مفهوم مهمی به نام Zombie Process میرسیم Zombie یعنی Process ی که اجرای خودش رو تموم کرده اما هنوز Parent وضعیت پایان اون رو نگرفته بعدا درباره Zombie و wait() دقیق‌ تر صحبت میکنیم حالا سه‌ تایی اصلی رو کنار هم بذاریم تا اینجا داریم:
fork()
ساختن Child:
Parent ↓ Child exec()
جایگزین کردن برنامه داخل Child:
Child ↓ New Program wait() Parent
منتظر پایان Child میمونه:
Parent ↓ wait() ↓ Child finishes ↓ Parent continues
پس الگوی کلاسیک میشه:
Parent │ fork() │ ▼ Child │ exec() │ ▼ New Program │ exit() │ ▼ Parent │ wait() │ ▼ Continue
البته ترتیب دقیق wait() نسبت به اجرای Child به نحوه پیاده‌ سازی برنامه بستگی دارده این دیاگرام فقط الگوی مفهومی رو نشون میده چرا این قسمت مهمه؟ وقتی یک برنامه رو روی Linux تحلیل میکنید و داخل کد به:
fork exec wait exit
میخورید حالا میتونید حدس بزنید چه اتفاقی در حال رخ دادنه مثلا: fork() ↓ یک Process جدید ↓ exec() ↓ اجرای برنامه دیگه ↓ exit() ↓ wait() ↓ Parent ادامه میده دیگه فقط اسم چند تابع رو نمیبینید منطق پشت اونها رو هم میفهمید تا اینجا Process API رو با سه مفهوم اصلی یاد گرفتیم: fork() → ساخت Child exec() → جایگزین کردن برنامه داخل Process wait() → انتظار Parent برای پایان Child @reverseengine
20 Aug, 18:57

Golden Tip When you encounter Obfuscated code, don't look for the appearance of the instructions Look for their effect on the Machine State That is, check: What happened to the Registers? What changed in Memory? What happened to the Flags? Where did the Control Flow go? If you understand these, even if the Assembly form is completely strange, you can extract the real logic of the program A very good exercise Look at these four pieces:
xor eax, eax
sub eax, eax
mov eax, 0
push 0
pop eax
Don't look at everything from just one angle For each, check: What happens to EAX? What happens to Flags? What changes to the Stack? What changes to Memory? Instruction Substitution: Implement an operation with another instruction or instructions without changing the intended semantics The goal can be: Change the shape of the code Make Pattern Matching harder Increase the variety of instructions Obfuscation Make Static Analysis harder But it has a very important lesson for us: We should not analyze assembly based only on the name of the instructions, we should understand their semantics and their real effect on the CPU And this is exactly our structure:
Assembly Basics         ↓ Disassembly          ↓ Instruction Semantics          ↓ Obfuscation          ↓ Deobfuscation
@reverseengine
20 Aug, 18:23

نکته طلایی وقتی با کد Obfuscated مواجه شدید دنبال شکل ظاهری instruction ها نباشید دنبال اثر اونها روی Machine State باشید یعنی بررسی کنید: Registerها چی شدن؟ Memory چه تغییری کردن؟ Flagها چی شدن؟ Control Flow کجا رفت؟ اگر این‌ها رو بفهمید حتی اگر شکل Assembly کاملا عجیب باشه میتونید منطق واقعی برنامه رو استخراج کنید یک تمرین خیلی خوب این چهار قطعه رو ببینید:
xor eax, eax
sub eax, eax
mov eax, 0
push 0
pop eax
همه رو فقط از یک زاویه نگاه نکنید برای هر کدوم بررسی کنید: EAX چی میشه؟ Flags چی میشن؟ Stack چه تغییری میکنه؟ Memory چه تغییری میکنه؟ Instruction Substitution: یک عملیات رو با instruction یا instruction های دیگه ای پیاده سازی کنیم بدون اینکه semantic مورد نظر تغییر کنه هدف میتونه: تغییر شکل کد سخت‌تر کردن Pattern Matching افزایش تنوع instruction ها Obfuscation سخت‌تر کردن Static Analysis باشده اما برای ما یک درس خیلی مهم داره: Assembly رو نباید فقط بر اساس اسم instruction ها تحلیل کنیم باید semantic و اثر واقعی اونها رو روی CPU بفهمیم و این دقیقا ساختار ماست:
Assembly Basics         ↓ Disassembly         ↓ Instruction Semantics         ↓ Obfuscation         ↓ Deobfuscation
@reverseengine
20 Aug, 18:20

Or in appropriate conditions, more complex combinations Now, if the same thing is repeated in different parts of the program with different forms, pattern-based analysis becomes more difficult For example:
Operation A ↓ Representation 1 Operation A ↓ Representation 2 Operation A ↓
Representation 3 While: Semantic = is one @reverseengine
20 Aug, 18:11

Instruction Substitution When we replace an instruction with several other instructions So far we have talked about different ways that a binary can be made to be more difficult to analyze Now let's move on to one of the simple but very useful techniques: Instruction Substitution The idea is very simple: Instead of using a specific instruction, we make several other instructions produce exactly the same result That is, for example, if we can perform an operation with one instruction, we deliberately replace it with several instructions A simple example: Suppose we want to zero a register in Assembly The usual method:
xor eax, eax
After execution:
EAX = 0
But we can create the same result in another way:
sub eax, eax
Again:
EAX = 0
In terms of the final result, both are the same But in terms of the code format, they are different Another example Suppose:
mov eax, 0
Can also be zeroed in other ways:
xor eax, eax
or:
sub eax, eax
So a single logic can have multiple different representations This is exactly what Instruction Substitution is counting on Why is this important? Here is an important point If an Analyst or Analysis Tool expects to see an operation in a specific form, Instruction Substitution can change that pattern For example, the Analyst might look for:
xor eax, eax
in a binary but the Binary Builder instead writes:
sub eax, eax
The result is the same but the pattern has changed A slightly more interesting example Suppose we want to:
add eax, 1
We can use:
inc eax
Both increase the value of EAX by one That is: Before:
EAX = 10
After:
EAX = 11
But Assembly is different Now a more important point: Instruction Substitution is not just for a simple instruction Sometimes we can implement an operation with multiple instructions For example:
add eax, 5
We can do it in a different way Let's write it:
push 5 pop ecx add eax, ecx
They can be equivalent in terms of the result, although this example is intentionally simple and ineffective. The goal here is not speed. The goal is to: Have several different representations of an operation. Why is it important? Because you shouldn't just look at the form of the instruction. You should ask: What do these instructions do in the end? For example, if we see:
xor eax, eax
we say very quickly:
EAX = 0
But if we see:
sub eax, eax
we still have to recognize the same concept. So in Reverse Engineering, we should gradually move from: Instruction Thinking to: Semantic Thinking That is, instead of just saying: What is this instruction? we should ask: What is the logical result of this part of the program? This is also important in Decompiler Decompilers try to convert Assembly back to something like high-level code For example:
xor eax, eax
may be converted to something like:
eax = 0
If the same thing is done with several different instructions, the Decompiler needs to understand what this set means in the end So:
Assembly ↓ Instructions ↓ Semantic Analysis ↓
High-Level Representation One of the important parts of Reverse Engineering is this semantic recognition But one very important point Instruction Substitution is not always completely equivalent Two instructions may be the same in terms of final value but have different effects on:
Flags Registers Memory Exception behavior Timing
For example:
inc eax
and:
add eax, 1
are similar in terms of EAX value but their Flag behavior is not exactly the same So when we say: two instructions are equivalent Are We need to specify: In what sense? Only the result of the register? Or the entire processor state? This difference is very important in Reverse Engineering Instruction Substitution In Obfuscation Now let's return to the main discussion Suppose a program performs this operation:
xor eax, eax
In the normal case, it is very easy to recognize But if the program creator uses different methods to produce the same result, the Assembly pattern changes For example:
sub eax, eax
20 Aug, 18:11

Instruction Substitution وقتی یک دستور رو با چند دستور دیگه عوض میکنیم تا اینجا درباره روش‌های مختلفی صحبت کردیم که یک باینری میتونه طوری ساخته بشه که تحلیلش سخت‌ تر بشه حالا بریم سراغ یکی از تکنیک‌های ساده ولی خیلی کاربردی: Instruction Substitution ایده‌اش خیلی ساده‌ ست: به‌جای اینکه از یک دستور مشخص استفاده کنیم کاری میکنیم که چند دستور دیگه دقیقا همون نتیجه رو تولید کنه یعنی مثلا اگر یک عملیات رو بتونیم با یک دستور انجام بدیم عمدا اون رو با چند دستور جایگزین میکنیم یک مثال ساده: فرض کنیم در Assembly میخایم یک رجیستر رو صفر کنیم روش معمول:
xor eax, eax
بعد از اجرا:
EAX = 0
اما میتونیم همین نتیجه رو به شکل دیگه ای ایجاد کنیم:
sub eax, eax
باز هم:
EAX = 0
از نظر نتیجه نهایی هر دو یکی ان ولی از نظر شکل کد متفاوتن یک مثال دیگه فرض کنیم:
mov eax, 0
می‌تونه با روش‌های دیگه ای هم به صفر برسه:
xor eax, eax
یا:
sub eax, eax
پس یک منطق واحد میتونه چندین representation مختلف داشته باشه این دقیقا همون چیزیه که Instruction Substitution روی اون حساب میکنه چرا این کار مهمه؟ اینجا یک نکته مهم وجود داره اگر یک Analyst یا ابزار تحلیل انتظار داشته باشیم یک عملیات رو با شکل خاصی ببینیم Instruction Substitution میتونه اون الگو رو تغییر بده مثلا تحلیلگر ممکنه که در یک باینری دنبال:
xor eax, eax
بگرده اما سازنده باینری به‌ جای اون نوشته:
sub eax, eax
نتیجه یکیه ولی pattern عوض شده یک مثال کمی جالب‌تر فرض کنیم می‌خواهیم:
add eax, 1
رو انجام بدیم میتونیم از:
inc eax
استفاده کنیم هر دو مقدار EAX رو یک واحد افزایش میدن یعنی: قبل:
EAX = 10
بعد:
EAX = 11
اما Assembly متفاوته حالا یک نکته مهم‌ تر: Instruction Substitution فقط برای یک دستور ساده نیست بعضی وقتا یک عملیات رو میتونیم با چند دستور پیاده کنیم مثلا:
add eax, 5
میتونیم به شکل دیگه ای بنویسیمش:
push 5 pop ecx add eax, ecx
از نظر نتیجه میتونن معادل باشن هر چند این مثال عمدا ساده و ناکار آمده هدف اینجا سرعت نیست هدف اینه که: یک عملیات چند representation متفاوت داشته باشیم چرا مهمه؟ چون نباید فقط به شکل دستور نگاه کنه باید بپرسید: این چند دستور در اخر چه کاری انجام میدن مثلا اگر ببینیم:
xor eax, eax
خیلی سریع میگیم:
EAX = 0
اما اگر ببینیم:
sub eax, eax
باز هم باید همون مفهوم رو تشخیص بدیم پس در Reverse Engineering باید کم‌ کم از: Instruction Thinking به: Semantic Thinking حرکت کنیم یعنی به‌جای اینکه فقط بگیم: این دستور چیه؟ بپرسیم: نتیجه منطقی این قسمت از برنامه چیه؟ این موضوع در Decompiler هم مهمه Decompiler ها تلاش میکنن Assembly رو دوباره به چیزی شبیه کد سطح بالا تبدیل کنن مثلا:
xor eax, eax
ممکنه به چیزی شبیه:
eax = 0
تبدیل بشه اگر همین کار با چند دستور مختلف انجام شده باشه Decompiler باید بفهمه که این مجموعه در اخر چه معنایی دارن پس:
Assembly ↓ Instructions ↓ Semantic Analysis ↓ High-Level Representation
یکی از بخش‌های مهم Reverse Engineering همین تشخیص semantic هست اما یک نکته خیلی مهم Instruction Substitution همیشه کاملا معادل نیست ممکنه دو دستور از نظر مقدار نهایی یکسان باشن ولی روی:
Flags Registers Memory Exception behavior Timing
تاثیر متفاوتی داشته باشن مثلا:
inc eax
و:
add eax, 1
از نظر مقدار EAX شبیهن اما رفتار Flag هاشون دقیقا یکی نیست پس وقتی میگیم: دو instruction equivalent هستند باید مشخص کنیم: از چه نظر؟ فقط نتیجه رجیستر؟ یا تمام State پردازنده؟ این تفاوت در Reverse Engineering خیلی مهمه Instruction Substitution در Obfuscation حالا برگردیم به بحث اصلی فرض کنیم یک برنامه این عملیات رو انجام میده:
xor eax, eax
در حالت عادی خیلی راحت قابل تشخیصه اما اگر سازنده برنامه از روش‌ های مختلف برای تولید همون نتیجه استفاده کنه الگوی Assembly تغییر میکنه مثلا:
sub eax, eax
یا در شرایط مناسب ترکیب‌ های پیچیده‌ تر حالا اگر همین کار در قسمت‌های مختلف برنامه با شکل‌ های مختلف تکرار بشه تحلیل pattern-based سخت‌ تر میشه مثلا:
Operation A ↓ Representation 1 Operation A ↓ Representation 2 Operation A ↓ Representation 3
در حالی که: Semantic = یکی هست @reverseengine
20 Aug, 18:08

We use cookies to improve your browsing experience.
By clicking “Accept all”, you agree to the cookie policy.