Understanding the Stack --The assembly Language Perspective
By Sebas Sujeen aka "0x90" and Sridhar aka "phr3ak"
For developing and understanding the exploit development process, it is imperative to understand the working of the stack from the assembly language perspective too. This article will make you understand how stack works from this perspective. To get the most from this article, we would advise you to refer the Assembly Language Primer for Hackers by Vivek Ramachandran(http://www.securitytube.net) before reading this
compile and create an assembly file using the '-S' switch in gcc
An example C Program
Note we are giving the preferred stack boundary as 2^2 (2 refers to the power of 2 ie 4 bytes) to simplify the debug process. And if you are using latest versions of Linux, you can turn off stack-protector and also disable the NX(non executable stack) by using '-fno-stack-protector' and '-z execstack' respectively.
$gcc -mpreferred-stack-boundary=2 -fno-stack-protector -z execstack -o test.s test.c -S
Below is the listing of the test.s file..
Note that the instructions are numbered only for convenience for referring it further..
Ok, here we go!!! The code is explained here!!
.string "Hello" -->defines a string "Hello" with label .LC0 similar to variable in C
.text --> This is the text section, actual code goes here. This section is read only and any attempt to write into this section will result in Segmentation fault
.globl main --> start of the main function, in the Assembly Language primer this is given as _start: , since we are assembling and linking in the same step using gcc and since gcc recognizes only main, we give it as main
.type main, @function -->defines that main is a function
main: --> start of main function
pushl %ebp -->1
movl %esp, %ebp -->2
subl $8, %esp -->3
movl $0, -4(%ebp) -->4
jmp .L2 -->5
movl $.LC0, %eax -->6
movl %eax, (%esp) -->7
call printf -->8
addl $1, -4(%ebp) -->9
cmpl $9, -4(%ebp) -->10
jle .L3 -->11
These 3 statements constitute the function prologue.Refer the assembly language primer for more details about function prologue. But one interesting thing to note is that, if main is the first function to be executed , where is it returning to?? Keep that thought in mind, we will get to it when we debug the program. Also we subtract esp by 8 bytes to allocate space for the local variable 'i' and the address of "Hello" which printf() needs.
Lets debug it by firing up gdb..
before that , lets create the executable file
The '-ggdb' switch makes debugging symbols to be loaded into the executable for analysis by the debugger..
$gcc -o test test.s -ggdb
Then lets fire up gdb..
'-q' is for quiet mode...
list command is used to list the disassembled code
set the break point at the appropriate location, lets set the break point at the first instruction after main function (ie) in this case line no 9
1 .file "first.c"
2 .section .rodata
4 .string "Hello"
6 .globl main
7 .type main, @function
9 pushl %ebp
10 movl %esp, %ebp
11 subl $8, %esp
12 movl $0, -4(%ebp)
13 jmp .L2
15 movl $.LC0, %eax
16 movl %eax, (%esp)
17 call printf
18 addl $1, -4(%ebp)
20 cmpl $9, -4(%ebp)
21 jle .L3
24 .size main, .-main
25 .ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
26 .section .note.GNU-stack,"",@progbits
Hope it helps!!! Upcoming papers will be about exploit development!!(mainly Linux)..
This will set the break point at line no 9 and now if you run the program , the program will pause the execution at line no 9.
this will run the program and the execution will halt at the break point
Analyse the esp register which points to the top of the stack
(gdb)i r esp
this will display the address "pointed to" by esp register
in our case ,"0xbffff44c"
This will step the execution to the next instruction, so our instruction pushl %ebp is executed, now wat would have happened?? After a push instruction the stack pointer esp decrements by 4 bytes since the stack grows down in memory from higher to lower address ( 4 bytes since we use 32 bit processor)
(gdb)i r ebp
this will display the address "pointed to" by ebp , in our case " 0xbffff4c8"
(gdb) i r esp
this will display "0xbffff448" , note the decrement of 4 bytes from the previous value
Now the current instruction is (2).. The implication of this instruction is to make the ebp to point to the new stack frame.
(gdb)i r esp
(gdb)i r ebp
both will display "0xbffff448"
Now the instruction is (3), this will decrement the address "pointed to" by esp by 8 bytes to allocate space for local variable "i" and address to "hello" required by printf()
(gdb) i r esp
This will display "0xbffff440"
The next instruction is (4)..Before that a few words, the ebp will be used to refer to the variables using an offset, now you can argue that why not use esp for the same purpose, but since esp's value changes with each push and pop it will require additional overhead to calculate the offset each time so we use ebp whose value doesnt change within a stack frame. Of the 8 bytes allocated , the first 4 bytes will be for "i" and the next 4 bytes to hold the address of "Hello". movl $0,-4(%ebp) , this is the indirect addressing mode. the () acts as a dereferencing operator , similar to '*' used with pointers in C. so what this instruction will do is that, it will 0 to address pointed to by (ebp)-4. Using the debugger will make it clear..
(gdb)i r ebp
This means 'get the address "pointed to" by ebp and subtract 4 from the address'
Now there is a way to examine the memory using the 'x' in gdb.. Refer the assembly language primer to more knw about the examine command
This will examine 1 word (in hex) at address 0xbffff444
this will return 0x00000000
The next insruction is "cmpl $9, -4(%ebp)"
ie it will compare 0 with 9 , basically as 0-9 but wont reflect the result back to -4(%ebp). This will obviously return a negative value
You can check that by referring the EFLAGS register and you can see that the Sign Flag is set (this will be set only if the result of an arithmetic expression returns a negative value)
(gdb)i r eflags
[ CF AF SF IF ID ] --> Note sign flag is set
the next instruction is jle .L3.. this is the conditional jump statement. This will jump to .L3 if the result of the previous instruction is negative or zero(ie if the SF or ZF is set)
the next instruction is "movl $.LC0, %eax"
This will move the address of "Hello" into eax register
(gdb)i r eax
this returns "0x80484d0"
Now examine the memory at this address
This will return '0x80484d0: "Hello"'
the next instruction is call printf(obvious what it does!!)
There are some things to remember when you call a function within an assembly program.
Assume the function call in C is like this : fn(1,2)
This is interpreted like this in assembly language
Note the arguments are pushed in reverse order coz stack is a LIFO structure so the argument pushed last comes out first!!
the next instruction is addl $1,-4(%ebp)
this will increment the value of i by 1
And the process gets repeated until i <=9, thereby printing "Hello" ten times on the screen
Set a break point at line 22 (leave) and continue
This will continue the execution till 'leave' instruction is encountered and pauses there
The leave instruction will clear up the space allocated for local vars and args to function..
This is equivalent to "movl %ebp,%esp"
"popl %ebp" this will pop the saved frame pointer so that the function that "called" this function can resume
The last instruction "ret" will put the saved EIP value into the eip register so that the execution can resume from the function which "called" the main function
Atlast after the ret instruction is executed, if u step with gdb
you can see this , 0x00144bd6 in __libc_start_main () from /lib/tls/i686/cmov/libc.so.6
This means that main is called from __libc_start_main() and returns to that..
Greetz : fb1h2s, Team SG and all g4h members!!!
feedback is welcome , whatever it may be!!!
References: Smashing the stack for fun and profit (Aleph1), Hacking the art of exploitation 2nd Edition, http://www.securitytube.net
his is a bit complicated considering the previous program!!But if you are able to understand the disassembled code...Then you have a greater clarity in understanding the stack...Gud Luck....Until next time..\m/ Peace Out
Food for thought:
Try disassembling this and play with it!!!
int mul(int x,int y)