بخش هفدهم بافر اورفلو
چطور فقط با نگاه کردن به اسمبلی بافر اورفلو پیدا کنیم
قراره چی کار کنیم
تا الان فهمیدیم توابع خطرناک چی هستن و فریم استک چطوری ساخته میشه
الان میخایم یاد بگیریم حتی اگر سورس کد نداشتیم فقط از روی اسمبلی بفهمیم احتمال بافر اورفلو وجود داره یا نه
نشونه اول
وجود بافر روی استک
مثال:
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]