ReverseEngineering
1.32K subscribers
50 photos
11 videos
106 files
888 links
Download Telegram
Constant Obfuscation وقتی حتی عدد ها هم مخفی میشن

تا اینجا دیدیم که چطور رشته‌ها رو میشه مخفی کردولی فقط رشته‌ها نیستن که Obfuscate میشن عددها و ثابت‌ های برنامه هم میتونن مخفی بشن
فرض کنید برنامه باید مقدار 100 رو استفاده کنه در حالت عادی ممکنه توی اسمبلی چیزی شبیه این ببینید:

mov eax, 100


حالا برنامه‌ نویس یا ابزار Obfuscation میتونه همون مقدار رو به شکل پیچیده‌ تری تولید کنه

مثلا:

mov eax, 73
add eax, 27


در نهایت مقدار eax میشه 100

اما یک قدم جلوتر:

mov eax, 0x12345678
xor eax, 0x12345614


نتیجه XOR دوباره میتونه یک مقدار مشخص باشه در این حالت وقتی فقط یک دستور رو میبینید مقدار واقعی ثابت فورا مشخص نمیشه بعصی وقتا حتی محاسبات بیشتری استفاده میشه:

XOR
ADD
SUB
ROL
ROR


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

یعنی میپرسیم:

این مقدار از کجا اومد؟
چه عملیاتی روش انجام شده؟
در نهایت کجا استفاده شده؟

مثلا:

مقدار اولیه

↓
XOR
↓
ADD
↓
SUB
↓
مقدار نهایی

اگر بتونید این زنجیره رو ساده کنید مقدار واقعی ثابت دوباره مشخص میشه
این کار مخصوصا وقتی مهم میشه که ثابت‌ها بخشی از یک الگوریتم باشن
مثلا یک برنامه ممکنه یک مقدار ثابت رو برای مقایسه محاسبه یا ساختن یک جدول استفاده کنه اگر مقدار اصلی مخفی شده باشه فهمیدن الگوریتم هم سخت‌ تر میشه
یک نکته جالب اینه که بعضی وقتا Decompiler خودش میتونه این محاسبات رو ساده کنه

مثلا چند دستور اسمبلی رو تبدیل کنه به:


x = 100;


اما همیشه نباید به خروجی Decompiler اعتماد کرد گاهی Obfuscation باعث میشه خروجی چیزی کاملا پیچیده و گمراه‌کننده باشه

پس یکی از مهارت‌های مهم Reverse Engineer اینه که بتونه بین سه چیز حرکت کنه:

Assembly
Decompiler
منطق واقعی برنامه


تمرین:

این عبارت رو بدون اجرای برنامه ساده کنید:

int x = 73;
x = x + 27;
x = x ^ 0;


بعد یک مثال پیچیده‌ تر برای خودتون بسازید که در نهایت به یک عدد مشخص برسه بعد همون برنامه رو Compile کنید و داخل Ghidra ببینید Compiler چه شکلی از اون ساخته

هدف تمرین این نیست که فقط جواب عدد رو پیدا کنید هدف اینه که یاد بگیرید یک مقدار رو از مسیر محاسباتش دنبال کنید

@reverseengine
ReverseEngineering
Constant Obfuscation وقتی حتی عدد ها هم مخفی میشن تا اینجا دیدیم که چطور رشته‌ها رو میشه مخفی کردولی فقط رشته‌ها نیستن که Obfuscate میشن عددها و ثابت‌ های برنامه هم میتونن مخفی بشن فرض کنید برنامه باید مقدار 100 رو استفاده کنه در حالت عادی ممکنه توی اسمبلی…
Constant Obfuscation When Even Numbers Are Hiding

So far we have seen how strings can be hidden, but it is not only strings that are obfuscated, numbers and program constants can also be hidden
Suppose the program needs to use the value 100, in normal case you might see something like this in the assembly:

mov eax, 100


Now the programmer or the Obfuscation tool can generate the same value in a more complex way

For example:

mov eax, 73

add eax, 27


Finally the value of eax becomes 100

But one step further:

mov eax, 0x12345678

xor eax, 0x12345614


The result of XOR can again be a specific value. In this case, when you see only one instruction, the actual value of the constant is not immediately clear. Sometimes even more calculations are used:

XOR
ADD
SUB
ROL
ROR


And all this
They are only used to produce a constant value. For example, a program might execute several instructions to generate a simple number. What we do here is that instead of looking at each instruction individually, we follow the path of the value. That is, we ask:

Where did this value come from?

What operation did the method perform?

Where was it finally used?

For example:

Initial value

↓
XOR
↓
ADD
↓
SUB
↓
Final value


If you can simplify this chain, the actual value of the constant is revealed again. This is especially important when constants are part of an algorithm. For example, a program might use a constant value to compare calculations or build a table. If the original value is hidden, it becomes harder to understand the algorithm. One interesting thing is that sometimes the decompiler itself can simplify these calculations.

For example, it can convert a few assembly instructions to:

x = 100;


But you shouldn't always trust the output of the Decompiler. Sometimes Obfuscation can make the output of something quite complex and misleading.

So one of the important skills of a Reverse Engineer is to be able to move between three things:

Assembly
Decompiler
The actual logic of the program

Exercise:

Simplify this expression without running the program:

int x = 73;

x = x + 27;

x = x ^ 0;


Then create a more complex example for yourself that will eventually reach a specific number. Then compile the same program and see what the Compiler makes of it in Ghidra.

The goal of the exercise is not to just find the answer to the number. The goal is to learn to follow a value through its calculation path.

@reverseengine
Pointer
چیه و چرا در Binary Exploitation مهمه؟

تا اینجا درباره‌ی Heap و باگ‌ هایی مثل UAF Double Free و Heap Overflow صحبت کردیم

اما برای اینکه بفهمیم این باگ‌ها چطور میتونن روی رفتار برنامه تاثیر بذارن باید یک مفهوم پایه‌ ای رو خیلی خوب بلد باشیم: Pointer


Pointer

به زبون ساده Pointer متغیریه که به جای خود داده آدرس اون داده در حافظه رو نگه میداره

مثلا:

int value = 100;
int *ptr = &value;


اینجا:

value
│
│ 100
▼
[ 100 ]

ptr
│
└──────────► آدرس value

پس ptr خودش مقدارش 100 نیست

بلکه میدونه 100 کجای حافظه قرار گرفته




چرا Pointer در C و C++ این‌قدر مهمه؟

چون خیلی از کارهای سطح پایین با Pointer انجام میشن

مثلا وقتی می‌نویسیم:

char *buffer = malloc(100);


متغیر buffer یک Pointer هست

یعنی malloc() یک بخش از Heap رو در اختیار برنامه قرار میده و آدرس اون بخش داخل buffer قرار میگیره

به شکل ساده:

buffer
│
▼
Heap
+----------------+
| 100 bytes |
+----------------+
مشکل از کجا شروع میشه؟

وقتی Pointer دیگه به یک حافظه‌ ی معتبر اشاره نکنه

مثلا:

char *buffer = malloc(100);

free(buffer);


بعد از free() استفاده از buffer میتونه مشکل‌ساز باشه

چون Pointer هنوز ممکنه یک آدرس داشته باشه اما حافظه‌ی مربوط به اون دیگه معتبر نیست

این همون مفهومی بود که در Use-After-Free دیدیم




Dangling Pointer

به Pointer ی که به یک ناحیه‌ی حافظه‌ی دیگه معتبر نیست معمولا Dangling Pointer میگیم

مثلا:

buffer
│
▼
[ Chunk ]
│
free()
│
▼
[ آزاد شده ]
خود buffer هنوز ممکنه مقدار قبلی رو داشته باشه

اما اون آدرس دیگه نباید به عنوان یک Object معتبر استفاده بشه



Pointer و Heap Overflow
چه ارتباطی دارن



فرض کنید داخل Heap چند Object وجود داره:

+----------------+
| Object A |
+----------------+
| Pointer |
+----------------+
| Object B |
+----------------+
اگر یک Memory Corruption باعث تغییر یک Pointer بشه برنامه ممکنه بعدا از اون Pointer برای دسترسی به یک آدرس متفاوت استفاده کنه

یعنی یک خطای نوشتن میتونه در ادامه باعث یک خطای خوندن یا نوشتن در جای دیگری از حافظه بشه

به همین دلیل Pointer ها در تحلیل Memory Corruption اهمیت زیادی دارن




Pointer با آدرس یکیه؟

تقریبا ولی بهتره دقیقتر بگیم:

Pointer
یک متغیر یا مقدار داده‌ایه که یک آدرس حافظه رو نگه میداره

مثلا:

ptr = 0x12345678


اینجا ptr یک Pointer هست و مقدارش یک آدرس محسوب میشه

خود Pointer هم در حافظه ذخیره میشه

یعنی:

Pointer
│
▼
[ address ]
پس Pointer هم خودش یک داده است و طبیعتا میتونه تحت تاثیر Memory Corruption قرار بگیره



چرا این موضوع برای Exploitation مهمه؟

چون در Binary Exploitation فقط دنبال خراب کردن داده‌ها نیستیم

یکی از سوال‌های مهم اینه:

آیا میتونیم چیزی رو تغییر بدیم که برنامه بعدا از اون به عنوان یک آدرس استفاده کنه؟


اگر جواب مثبت باشه یک Memory Corruption میتونه اثر خیلی بیشتری از تغییر یک عدد معمولی داشته باشه

به همین دلیل هنگام تحلیل Heap باید به Pointer های موجود در:

Objectها

ساختارهای داده

Heap metadata

جدول‌ها و reference ها


توجه ویژه داشته باشیم






Pointer
متغیریه که یک آدرس حافظه رو نگه میداره در برنامه‌های C و C++ پوینتر ها نقش بسیار مهمی در مدیریت Heap و دسترسی به Objectها دارن اگر یک Pointer خراب یا نامعتبر بشه ممکنه برنامه بعدا به آدرس اشتباهی دسترسی پیدا کنه به همین دلیل شناخت Pointer ها برای درک UAF Heap Overflow و بسیاری از Memory Corruption ها ضروریه

@reverseengine
ReverseEngineering
Pointer چیه و چرا در Binary Exploitation مهمه؟ تا اینجا درباره‌ی Heap و باگ‌ هایی مثل UAF Double Free و Heap Overflow صحبت کردیم اما برای اینکه بفهمیم این باگ‌ها چطور میتونن روی رفتار برنامه تاثیر بذارن باید یک مفهوم پایه‌ ای رو خیلی خوب بلد باشیم: Pointer…
What is Pointer
and why is it important in Binary Exploitation?

So far we have talked about Heap and bugs like UAF Double Free and Heap Overflow

But to understand how these bugs can affect the behavior of the program, we need to know a basic concept very well: Pointer

Pointer

In simple terms, a Pointer is a variable that holds the address of that data in memory instead of its own data

For example:
int value = 100;

int *ptr = &value;

Here:
value
│
│ 100
▼
[ 100 ]

ptr
│
└────────► address of value

So ptr itself does not have the value 100

But it knows where 100 is located in memory

Why is Pointer so important in C and C++?

Because many low-level tasks are done with Pointers

For example, when we write:
char *buffer = malloc(100);

The buffer variable is a Pointer

That is, malloc() provides a section of the Heap to the program and the address of that section is placed in the buffer

In simple terms:
buffer
│
▼
Heap
+----------------+
| 100 bytes |
+----------------+
Where does the problem start?

When the Pointer no longer points to a valid memory

For example:
char *buffer = malloc(100);

free(buffer);

Using a buffer after free() can be problematic

Because the Pointer may still have an address, but the memory associated with it is no longer valid

This was the same concept we saw in Use-After-Free

Dangling Pointer

A Pointer that is not valid to another memory area is usually called a Dangling Pointer

For example:
buffer
│
▼
[ Chunk ]
│
free()
│
▼
[ Freed ]
The buffer itself may still have the previous value

But that address should no longer be used as a valid Object

What is the relationship between Pointer and Heap Overflow

Suppose there are several Objects in the Heap:
+----------------+
| Object A |
+----------------+
| Pointer |
+----------------+
| Object B |
+----------------+
If a memory corruption causes a pointer to change, the program may later use that pointer to access a different address

That is, a write error can subsequently cause a read or write error elsewhere in memory

That is why pointers are very important in memory corruption analysis

Is a pointer the same as an address?

Roughly, but to be more precise:

A pointer
is a variable or data value that holds a memory address

For example:
ptr = 0x12345678

Here ptr is a pointer and its value is considered an address

The pointer itself is also stored in memory

That is:
Pointer
│
▼
[ address ]
So the pointer itself is data and can naturally be affected by memory corruption

Why is this important for exploitation?

Because in Binary Exploitation we are not just looking to corrupt data

One of the important questions is:

Can we change something that the program will later use as an address?

If the answer is yes, a Memory Corruption can have a much greater effect than changing a regular number

That is why when analyzing the Heap, we should pay special attention to the Pointers in:

Objects

Data structures

Heap metadata

Tables and references


Pointer is a variable that holds a memory address. In C and C++ programs, pointers play a very important role in Heap management and accessing objects. If a Pointer becomes corrupted or invalid, the program may later access the wrong address. That is why understanding Pointers is essential to understanding UAF Heap Overflow and many Memory Corruptions

@reverseengine
Direct Syscall vs Indirect Syscall

تا اینجا فهمیدیم Direct Syscall یعنی برنامه تلاش میکنه مسیر معمول User-Mode API رو کوتاه‌ تر کنه

حالا سوال:

Indirect Syscall

ایده اصلی

در Direct Syscall اجرای دستور syscall از کدی انجام میشه که خود برنامه یا یک Stub مشخص فراهم کرده

به‌صورت مفهومی:

Application
│
▼
Custom Syscall Stub
│
▼
syscall
│
▼
Kernel


اما در Indirect Syscall ایده اینه که اجرای syscall از یک مسیر/Stub موجود در فضای User Mode انجام بشه

Application
│
▼
Indirect Path
│
▼
Known syscall stub
│
▼
Kernel


هدف مفهومی این تکنیک تغییر شکل Call Stack و مسیر User-Mode execution نسبت به Direct Syscall هست

چرا این موضوع برای EDR مهمه؟

EDR
فقط نمی‌پرسه:

syscall اتفاق افتاد؟

بلکه میتونه سوال‌های بیشتری بپرسه:

چه Process ی syscall رو انجام داده؟

Call Stack چطور بوده؟

Thread از کجا شروع شده؟

Memory مربوط به کجاست؟

قبل و بعد از syscall چه اتفاقی افتاده؟

پس:

Direct Syscall
≠
Invisible

و:

Indirect Syscall

EDR Bypass

این دو بیشتر تغییر مسیر اجرای User Mode هستن نه حذف کامل visibility

حالا یک لایه پایین‌تر

اینجا میرسیم به یکی از مهم‌ترین چیزهایی که باید برای فهم EDR Evasion بلد باشید:

Memory Permissions

هر Memory Region میتونه مجوزهایی مثل این داشته باشه:

R = Read
W = Write
X = Execute

مثلا:

RW

یعنی قابل خوندن و نوشتنه ولی نباید به‌ عنوان کد اجرا بشه
و:

RX

یعنی قابل خوندن و اجراست ولی نوشتن روی اون مجاز نیست

پس RWX چیه؟

RWX

یعنی یک ناحیه همزمان:

Read + Write + Execute

داره
این نوع Memory Permission میتونه برای ما جالب باشه چون کدی که همزمان قابل تغییر و اجراست در بعضی سناریوها ریسک بیشتری ایجاد میکنه البته RWX به‌تنهایی به معنی بدافزار بودن نیست بعضی نرم‌افزارها کاملا legitimate هم ممکنه چنین Memory هایی داشته باشن

نکته مهم این قسمت

EDR
ها معمولا فقط به این نگاه نمیکنن که این Memory چه Permission ی داره
بلکه تغییرات Permission و رفتار اطرافش هم اهمیت داره
مثلا از دید تحلیلی

Memory Allocation
↓
Write
↓
Permission Change
↓
Execution


همین زنجیره میتونه مهم‌تر از دیدن یک Memory Region به‌ تنهایی باشه


زنجیره‌ مون اینه:

API Hooking
↓
User Mode / Kernel Mode
↓
Direct Syscall
↓
Indirect Syscall
↓
Memory Permissions


@reverseengine
ReverseEngineering
Direct Syscall vs Indirect Syscall تا اینجا فهمیدیم Direct Syscall یعنی برنامه تلاش میکنه مسیر معمول User-Mode API رو کوتاه‌ تر کنه حالا سوال: Indirect Syscall ایده اصلی در Direct Syscall اجرای دستور syscall از کدی انجام میشه که خود برنامه یا یک Stub…
Direct Syscall vs Indirect Syscall

So far we have understood that Direct Syscall means that the application tries to shorten the usual path of the User-Mode API

Now the question:

Indirect Syscall

The main idea

In Direct Syscall, the execution of the syscall command is done from the code that the application itself or a specific stub provides

Conceptually:

Application
│
▼
Custom Syscall Stub
│
▼
syscall
│
▼
Kernel


But in Indirect Syscall, the idea is to execute the syscall from a path/stub existing in the User Mode space

Application
│
▼
Indirect Path
│
▼
Known syscall stub
│
▼
Kernel


The conceptual goal of this technique is to change the Call Stack shape and the User-Mode execution path compared to Direct Syscall

Why is this important for EDR?

EDR
does not just ask:

Did the syscall happen?

It can ask more questions:

What process made the syscall?

What was the call stack like?

Where did the thread start?

Where is the memory?

What happened before and after the syscall?

So:

Direct Syscall
≠
Invisible

And:

Indirect Syscall

EDR Bypass


These two are more of a redirection of User Mode execution than a complete removal of visibility

Now one layer lower

Here we come to one of the most important things you need to know to understand EDR Evasion:

Memory Permissions

Each Memory Region can have permissions like this:

R = Read

W = Write

X = Execute


For example:

RW

It can be read and written, but it should not be executed as code

And:

RX

It can be read and executed, but writing to it is not allowed

So what is RWX?

RWX

It means a simultaneous region:

Read + Write + Execute

This type of Memory Permission can be interesting for us because code that can be modified and executed simultaneously poses a higher risk in some scenarios. Of course, RWX alone does not mean it is malware. Some completely legitimate software may also have such memories.

The important point of this section

EDRs usually do not only look at what permission this memory has.

But the permission changes and the behavior around it are also important.

For example, from an analytical point of view

Memory Allocation
↓
Write
↓
Permission Change
↓
Execution


This chain can be more important than looking at a Memory Region alone.

Our chain is:

API Hooking
↓
User Mode / Kernel Mode
↓
Direct Syscall
↓
Indirect Syscall
↓
Memory Permissions


@reverseengine
بخش بیست و شیشم بافر اورفلو

Valgrind
چیه و چه فرقی با ASan داره
توی قسمت قبل با ASan آشنا شدیم
اینجا میخوایم بریم سراغ Valgrind و ببینیم چطور میتونه Memory Bugها رو پیدا کنه
بعد هم خیلی ساده ASan و Valgrind رو با هم مقایسه میکنیم


Valgrind
برنامه رو زیر نظر میگیره و دسترسی‌ های حافظه رو بررسی میکنه
مثلا میتونه مواردی مثل اینا رو پیدا کنه

Invalid Read
Invalid Write
Use After Free
Memory Leak
استفاده نادرست از حافظه

یک مثال ساده:
فایل
C
#include <stdio.h>
#include <stdlib.h>

int main()
{
int *data = malloc(4 * sizeof(int));

data[5] = 100;

free(data);

return 0;
}



اینجا فقط برای 4 عدد حافظه گرفتیم
ولی داریم عضو شماره 5 رو مینویسیم
پس یک Out of Bounds Write داریم

کامپایل
shell
gcc -g file22_demo.c -o file22_demo



بعد با Valgrind اجراش میکنیم
shell
valgrind ./file22_demo


Valgrind
گزارش میده که برنامه یک دسترسی غیرمجاز به حافظه داشته

قسمت مهمش برای Reverse Engineering

فرض کنید یک برنامه پیچیده دارید
برنامه کرش میکنه ولی هنوز نمیدونید مشکل دقیقا کجاست

Valgrind
میتونه Stack Trace و اطلاعات مربوط به دسترسی اشتباه رو نشون بده
بعد

میتونید همون تابع رو داخل Ghidra یا IDA باز کنید و Assembly اون قسمت رو بررسی کنید

یعنی دوباره این مسیر رو داریم

برنامه
↓
Valgrind
↓
Memory Error
↓
Stack Trace
↓
Ghidra / IDA
↓
Assembly Analysis
Valgrind
در مقابل ASan
خیلی ساده بخوایم بگیم
ASan
معمولا سریع‌ تره و برای Fuzzing و تست‌های مداوم خیلی کاربردیه
Valgrind
نیازی به کامپایل با ASan نداره و ابزارهای مختلفی برای تحلیل رفتار برنامه در اختیارمون میذاره البته Valgrind معمولا سربار اجرایی بیشتری داره

یک نکته مهم:

Valgrind
و ASan جای Reverse Engineering رو نمیگیرن

اونا فقط کمک میکنن سریع‌ تر بفهمیم
کجا باید دنبال مشکل بگردیم
بعد کار اصلی ما شروع میشه
یعنی رفتن داخل Ghidra یا IDA و فهمیدن اینکه چرا این Memory Bug اتفاق افتاده


تا اینجا سه ابزار مهم رو داریم
Fuzzer
↓
Crash پیدا می‌کنه

ASan / Valgrind
↓
Memory Bug رو تحلیل میکنن

Ghidra / IDA
↓
علت Bug رو از روی Binary بررسی میکنیم این دقیقا همون ترکیبیه که یک Reverse Engineer برای تحلیل Memory Bug ها باید کم‌ کم بهش مسلط بشه

@reverseengine
❤2
Part 26 Buffer Overflow



What is Valgrind and how is it different from ASan?

We met ASan in the previous section.

Here we want to go to Valgrind and see how it can find Memory Bugs.

Then we will compare ASan and Valgrind very simply.

Valgrind
monitors the program and checks memory accesses.

For example, it can find things like these:

Invalid Read

Invalid Write

Use After Free

Memory Leak

Incorrect memory usage


A simple example:

C file

#include <stdio.h>

#include <stdlib.h>

int main()

{
int *data = malloc(4 * sizeof(int));

data[5] = 100;

free(data);

return 0;
}


Here we only got 4 memory slots

But we are writing member number 5

So we have an Out of Bounds Write

Compile
shell

gcc -g file22_demo.c -o file22_demo


Then we run it with Valgrind
shell
valgrind ./file22_demo


Valgrind
reports that the program has an illegal memory access

The important part is for Reverse Engineering

Suppose you have a complex program

The program crashes but you still don't know exactly where the problem is

Valgrind
can show Stack Trace and information about the incorrect access

Then

You can open the same function in Ghidra or IDA and check the Assembly of that part

That means we have this path again

Program
↓
Valgrind
↓
Memory Error
↓
Stack Trace
↓
Ghidra / IDA
↓
Assembly Analysis


Valgrind vs. ASan
To put it simply
ASan
is usually faster and for Fuzzing and continuous testing are very useful
Valgrind
Does not require compilation with ASan and provides us with various tools to analyze the behavior of the program, of course Valgrind usually has more execution overhead
An important point:
Valgrind
and ASan do not replace Reverse Engineering
They only help us understand faster
Where to look for the problem
Then our main work begins
That is, going into Ghidra or IDA and understanding why this Memory Bug occurred
So far we have three important
tools

Fuzzer
↓
Finds a crash
ASan / Valgrind
↓
Analyzes the Memory Bug

Ghidra / IDA
↓


We examine the cause of the Bug from the Binary This is exactly the combination that a Reverse Engineer should gradually master to analyze Memory Bugs

@reverseengine
x86 and amd64 instruction reference

https://www.felixcloutier.com/x86

@reverseengine
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