ReverseEngineering – Telegram
ReverseEngineering
1.24K subscribers
40 photos
10 videos
55 files
666 links
Download Telegram
A Linux Toolkit for Reverse-Engineering and Analyzing Malware

https://remnux.org

@reverseengine
1
Local Variables روی استک

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

ساختار فریم استک کامل

بعد از اجرای prologue:

push rbp
mov rbp, rsp
sub rsp, X ; ایجاد فضا برای متغیرهای محلی


ساختار میشه:

[ rbp+16 ] آرگومان سوم
[ rbp+8 ] آدرس برگشت
[ rbp+0 ] RBP قبلی
[ rbp-8 ] متغیر محلی 1
[ rbp-16 ] متغیر محلی 2




مثال:

int foo(int x) {
int t = x + 3;
return t * 2;
}


اسمبلی:

foo:
push rbp
mov rbp, rsp
sub rsp, 16 ; فضای متغیر محلی

mov DWORD PTR [rbp-4], edi ; t = x
add DWORD PTR [rbp-4], 3 ; t = x + 3

mov eax, DWORD PTR [rbp-4]
add eax, eax ; eax = t * 2

leave
ret


نکات مهم:

متغیر t روی استک قرار میگیره آدرسش rbp-4

چون int هست سایز 4 بایت

eax خروجی تابع



Local Variables on the Stack

When a function has a local variable, the compiler pushes it onto the stack

Complete stack frame structure

After executing the prologue:

push rbp
mov rbp, rsp
sub rsp, X ; Create space for local variables


The structure becomes:

[ rbp+16 ] Third argument
[ rbp+8 ] Return address
[ rbp+0 ] Previous RBP
[ rbp-8 ] Local variable 1
[ rbp-16 ] Local variable 2


Example:

int foo(int x) {

int t = x + 3;

return t * 2;
}


Assembly:

foo:

push rbp
mov rbp, rsp
sub rsp, 16 ; Local variable space

mov DWORD PTR [rbp-4], edi ; t = x
add DWORD PTR [rbp-4], 3 ; t = x + 3

mov eax, DWORD PTR [rbp-4]

add eax, eax ; eax = t * 2

leave
ret


Important notes:

The variable t is placed on the stack, its address is rbp-4

Because it is an int, its size is 4 bytes

eax is the output of the function

@reverseengine
👍4