ReverseEngineering
1.32K subscribers
50 photos
11 videos
108 files
895 links
Download Telegram
Analyzing the PayloadRestrictions.dll Export Address Filtering

https://windows-internals.com/an-exercise-in-dynamic-analysis

@reverseengine
LOLEXFIL

Living off the land Data Exfiltration methods

https://lolexfil.github.io

@reverseengine
DllShimmer

DllShimmer is a tool for rapidly creating and injecting backdoors into DLLs, facilitating remote control and exploitation of target processes. It simplifies the process of DLL hijacking for security research and potentially malicious activity.

https://github.com/Print3M/DllShimmer

@reverseengine
کرنل چیه؟

تا اینجا گفتیم سیستم‌ عامل بین برنامه‌ها و سخت‌افزار قرار میگیره

اما سوال مهم:

آیا کل سیستم‌ عامل همیشه در حال اجراست؟

نه

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

یک مثال ساده:

فرض کنید یک شرکت بزرگ داریم:

کارمندان = برنامه‌ها

ساختمان و تجهیزات = سخت‌افزار

مدیرعامل = Kernel

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

کرنل چه کارهایی انجام میده؟

مدیریت پردازنده (CPU)

تصمیم می‌گیرد:

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

مدیریت حافظه (RAM)

تصمیم می‌گیرد:

هر برنامه چقدر حافظه بگیره؟
حافظه برنامه‌ها از هم جدا بمونه
یک برنامه نتونه حافظه برنامه دیگه ای رو بخونه

مدیریت فایل‌ها

وقتی برنامه‌ای فایل باز میکنه:

Plain text

read()

write()

open()

در نهایت کرنل مسئول انجام این عملیاته

مدیریت دستگاه‌ها

مثل:
کیبورد
ماوس
هارد
کارت شبکه
USB
همه از طریق کرنل کنترل میشن


User Mode و Kernel Mode

یکی از مهم‌ترین مفاهیم کل سیستم‌
عامل همینجاست

پردازنده معمولا دو حالت اجرا داره:

User Mode

جایی که برنامه‌های عادی اجرا میشن

مثل:
Chrome

Firefox

Telegram

Notepad


در این حالت برنامه محدودیت داره

Kernel Mode

جایی که کرنل اجرا میشه

در این حالت تقریبا دسترسی کامل به سیستم وجود دارد.

چرا این جداسازی مهمه؟

فرض کنید یک برنامه باگ داشته باشه

اگر مستقیم به سخت‌افزار دسترسی کامل داشته باشه:

سیستم کرش میکنه
اطلاعات خراب میشن
امنیت از بین میره

برای همین سیستم‌عامل برنامه‌ها رو در User Mode نگه میداره


ارتباط برنامه با کرنل چجوریه؟

از طریق System Call

مثلا وقتی برنامه میخاد:

فایل باز کنه
حافظه بگیره
پردازه جدید بسازه

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


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

وقتی داخل دیباگر توابعی مثل اینها رو میبینید:
C

CreateProcess

CreateThread

VirtualAlloc

ReadFile

WriteFile


پشت صحنه تقریبا همه اونا در نهایت به کرنل میرسن

به همین دلیل مهندس معکوس باید همیشه بدوند:

الان کد در User Mode اجرا میشه یا در Kernel Mode؟

این سوال پایه بسیاری از مباحث بعدی مثل:


Process

Memory

System Call

Driver

Windows Internals

هست


What is a kernel?

So far, we have said that the operating system is located between the programs and the hardware

But the important question:

Is the entire operating system always running?

No

At the heart of every operating system is a very important part called the Kernel

The kernel is the most important part of the operating system and works directly with the hardware

A simple example:

Let's assume we have a large company:

Employees = programs

Buildings and equipment = hardware

CEO = Kernel

Employees cannot do whatever they want

For example, they cannot directly enter the server room or remove equipment

They must direct their requests to the CEO or system management

The kernel has exactly the same role

What does the kernel do?

Processor (CPU) management

Decides:

Which program to run?

How long to run?

When to stop?

Memory Management (RAM)

Decides:

How much memory should each program take?

Program memory should be kept separate

A program cannot read another program's memory

File Management

When a program opens a file:

Plain text

read()

write()

open()


Ultimately, the kernel is responsible for performing this operation

Device Management

For example:
Keyboard
Mouse
Hardware
Network Card
USB

All are controlled by the kernel

User Mode and Kernel Mode

One of the most important concepts of the entire operating system is here

The processor usually has two execution modes:

User Mode

Where normal programs are executed

For example:

Chrome

Firefox

Telegram

Notepad


In this mode, the program has restrictions

Kernel Mode

Where the kernel is executed

In this mode, there is almost complete access to the system.

Why is this separation important?

Suppose a program has a bug

If it has full access to the hardware directly:

The system crashes

Data gets corrupted

Security is lost

That's why the operating system keeps programs in User Mode
❤2
How does the program communicate with the kernel?

Through System Call

For example, when the program wants to:

Open a file

Get memory

Create a new process

It actually asks the kernel for help

Important point for reverse engineering:

When you see functions like these in the debugger:

C

CreateProcess

CreateThread

VirtualAlloc

ReadFile

WriteFile


Behind the scenes, almost all of them end up in the kernel

That's why the reverse engineer should always know:

Is the code currently running in User Mode or Kernel Mode?

This question is the basis for many subsequent topics such as:

Process

Memory

System Call

Driver

Windows Internals



@reverseengine
❤1
Forwarded from Source Byte
Static Devirtualization of Themida
This article demonstrates devirtualization of CodeVirtualizer/Themida protected code, however the techniques described here apply to pretty much every virtual machine based obfuscator. Only requiring some minor modifications to support each of them. The following is a non-exhaustive list of obfuscators that can be reduced using the technique described in this article.

https://back.engineering/blog/09/05/2026/
Shared_library_hijacking.pdf
2.8 MB
Resolving the Correct Library: A Loader-Level Defense Solution Against Shared Object Hijackin
hooking_windows_named_pipes.pdf
1.5 MB
Hooking Windows Named Pipes
بخش هفدهم بافر اورفلو


چطور فقط با نگاه کردن به اسمبلی بافر اورفلو پیدا کنیم

قراره چی کار کنیم

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

نشونه اول

وجود بافر روی استک

مثال:
Asm

sub rsp, 0x40


این یعنی 64 بایت فضا روی استک رزرو شده
اگر چند خط پایین تر دیدید

Asm

lea rax, [rbp-0x40]


یا
Asm

lea rcx, [rsp+0x10]


معمولاً با یک بافر طرف هستید

نشونه دوم
ورودی کاربر وارد بافر میشه

مثال
Asm

mov rdi, rax
call gets


یا
Asm

call fgets


یا
Asm

call read


یعنی یک داده از بیرون وارد برنامه شده
هر وقت ورودی دیدید باید حساس بشید

نشونه سوم

کپی بدون بررسی طول

مثال:
Asm

call strcpy

یا
Asm

call strcat


یا
Asm

call sprintf


اینها زنگ خطرهای کلاسیک هستن
چون طول ورودی رو چک نمیکنن

نشونه چهارم

بافر کوچک ورودی بزرگ
مثلا اینو تو دی‌ کامپایلر ببینید
C

char buf[16];
strcpy(buf,input);


یا تو اسمبلی ببینید
Asm

lea rdi,[rbp-0x10]
call strcpy


بافر فقط 16 بایته
ولی هیچ محدودیتی برای input وجود نداره
پس احتمال بافر اورفلو زیاده

نشونه پنجم

نبودن Canary
اگر اول تابع این چیزها رو ندیدید

Asm

mov rax, qword ptr fs:[0x28]
mov [rbp-0x8], rax


احتمالا Stack Canary وجود نداره
وجود این دستورات معمولا نشون میده کامپایلر محافظ استک فعال کرده

نشونه شیشم

تابع قبل از ret هیچ بررسی انجام نمیده
تابع آسیب‌پذیر معمولا آخرش این شکلیه

Asm

leave
ret


اگر قبل از ret هیچ بررسی امنیتی انجام نشه و بالاتر strcpy دیده باشید باید بیشتر دقت کنید

مثال واقعی تحلیل:

فرض کنید این اسمبلی رو دیدید

Asm

push rbp
mov rbp,rsp
sub rsp,0x20

mov rdx,rdi

lea rax,[rbp-0x10]
mov rdi,rax

call strcpy

leave
ret

سوال

آیا این مشکوکه؟

جواب

بله

چون
Asm

lea rax,[rbp-0x10]


نشون میده بافر 16 بایتی داریم

و
Asm

call strcpy


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

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

سه مورد زیر رو پیدا کنید

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

اگر این سه مورد رو پیدا کردید عملا دارید مثل یک Reverse Engineer واقعی فکر میکنید


Part 17 Buffer Overflow


How to find buffer overflow just by looking at the assembly

What are we going to do

So far we have understood what dangerous functions are and how stack frames are created

Now we are going to learn how to find out if there is a buffer overflow even if we don't have the source code just from the assembly

First example

There is a buffer on the stack

Example:

Asm

sub rsp, 0x40


This means that 64 bytes of space on the stack are reserved

If you see a few lines below

Asm

lea rax, [rbp-0x40]


or

Asm

lea rcx, [rsp+0x10]


Usually you are dealing with a buffer

Second example

User input enters the buffer

Example

Asm

mov rdi, rax
call gets


or

Asm

call fgets


or

Asm

call read


This means that data has entered the program from outside

Whenever you see input, you should be sensitive Besh

Third example

Copy without length check

Example:

Asm

call strcpy


or

Asm

call strcat


or

Asm

call sprintf


These are classic alarms
because they don't check the length of the input

Fourth example

Small input buffer, large input
For example, see this in the decompiler

C

char buf[16];
strcpy(buf,input);


Or see in the assembly
Asm

lea rdi,[rbp-0x10]
call strcpy


The buffer is only 16 bytes
But there is no limit for input
So the probability of buffer overflow is high

Fifth example

No Canary
If you did not see these functions first

Asm

mov rax, qword ptr fs:[0x28]

mov [rbp-0x8], rax


There is probably no Stack Canary
The presence of these commands usually indicates that the compiler has enabled stack protection

Sixth example

The function does not perform any checks before ret
A vulnerable function usually ends like this

Asm

leave
ret


If no security checks are performed before ret and you have seen strcpy above, you should be more careful

Real example of analysis:

Suppose you saw this assembly

Asm

push rbp
mov rbp,rsp
sub rsp,0x20

mov rdx,rdi

lea rax,[rbp-0x10]

mov rdi,rax

call strcpy

leave
ret


Question

Is this suspicious?

Answer

Yes

Because

Asm

lea rax,[rbp-0x10]