Difference between revisions of "The Stack"

From SkullSecurity
Jump to navigation Jump to search
Line 451: Line 451:


== Local Variables ==
== Local Variables ==
At the beginning of most functions, space to store local variables in is allocated. This is done by subtracting the total size of all local variables from the stack pointer at the start of the function.
== Frame Pointer ==
== Frame Pointer ==
== Balance ==
== Balance ==

Revision as of 17:07, 13 March 2007

Assembly Language Tutorial
Please choose a tutorial page:

The stack is, at best, a difficult concept to understand. However, understanding the stack is essential to reverse engineering code.

The stack register, esp, is basically a register that points to an arbitrary location in memory called "the stack". The stack is just a really big section of memory where temporary data can be stored and retrieved. When a function is called, some stack space is allocated to the function, and when a function returns the stack should be in the same state it started in.

The stack always grows downwards, towards lower values. The esp register always points to the lowest value on the stack. Anything below esp is considered free memory that can be overwritten.

The stack stores function parameters, local variables, and the return address of every function.

Function Parameters

When a function is called, its parameters are typically stored on the stack before making the call. Here is an example of a function call in C:

func(1, 2, 3); 

And here is the equivalent call in assembly:

push 3
push 2
push 1
call func
add esp, 0Ch

The parameters are put on the stack, then the function is called. The function has to know it's getting 3 parameters, which is why function parameters have to be declared in C.

After the function returns, the stack pointer is still 12 bytes ahead of where it started. In order to restore the stack to where it used to be, 12 (0x0c) has to be added to the stack pointer. The three pushes, of 4 bytes each, mean that a total of 12 was subtracted from the stack.

Here is what the initial stack looked like (with ?'s representing unknown stack values):

esp ?
esp - 4 ?
esp - 8 ?
esp - 12 ?
esp - 16 ?

Note that the same 5 32-bit stack values are shown in all these examples, with the stack pointer at the left moved. The stack goes much further up and down, but that isn't shown here.

Here are the three pushes:


push 3
esp + 4 ?
esp 3
esp - 4 ?
esp - 8 ?
esp - 12 ?


push 2
esp + 8 ?
esp + 4 3
esp 2
esp - 4 ?
esp - 8 ?


push 1
esp + 12 ?
esp + 8 3
esp + 4 2
esp 1
esp - 4 ?

Now all three values are on the stack, and esp is pointing at the 4. The function is called, and returns, leaving the stack the way it started. Now the final instruction runs:


add esp, 0Ch
esp ?
esp + 4 3
esp + 8 2
esp - 12 1
esp - 16 ?

Note that the 3, 2, and 1 are still on the stack. However, they're below the stack pointer, which means that they are considered free memory and will be overwritten.

call and ret Revisited

The call instruction pushes the address of the next instruction onto the stack, then jumps to the specified function.

The ret instruction pops the next value off the stack, which should have been put there by a call, and jumps to it.

Here is some example code:

0x10000000 push 3
0x10000001 push 2
0x10000002 push 1
0x10000003 call 0x10000020
0x10000007 add esp, 12
0x10000011 exit ; This isn't a real instruction, but pretend it is
0x10000020 mov eax, 1
0x10000024 ret

Now here is what the stack looks like at each step in this code:


0x10000000 push 3
esp + 4 ?
esp 3
esp - 4 ?
esp - 8 ?
esp - 12 ?
esp - 16 ?
esp - 20 ?


0x10000001 push 2
esp + 8 ?
esp + 4 3
esp 2
esp - 4 ?
esp - 8 ?
esp - 12 ?
esp - 16 ?


0x10000002 push 1
esp + 12 ?
esp + 8 3
esp + 4 2
esp 1
esp - 4 ?
esp - 8 ?
esp - 12 ?


0x10000003 call 0x10000020
esp + 16 ?
esp + 12 3
esp + 8 2
esp + 4 1
esp 0x1000007
esp - 4 ?
esp - 8 ?


0x10000020 mov eax, 1
esp + 16 ?
esp + 12 3
esp + 8 2
esp + 4 1
esp 0x1000007
esp - 4 ?
esp - 8 ?


0x10000024 ret
esp + 12 ?
esp + 8 3
esp + 4 2
esp 1
esp - 4 0x1000007
esp - 8 ?
esp - 12 ?


0x10000007 add esp, 12
esp ?
esp - 4 3
esp - 8 2
esp - 12 1
esp - 16 0x1000007
esp - 20 ?
esp - 24 ?


0x10000011 exit ; This isn't a real instruction, but pretend it is
esp ?
esp - 4 3
esp - 8 2
esp - 12 1
esp - 16 0x1000007
esp - 20 ?
esp - 24 ?

Note the return address being pushed onto the stack by call, and being popped off the stack by ret.

Local Variables

At the beginning of most functions, space to store local variables in is allocated. This is done by subtracting the total size of all local variables from the stack pointer at the start of the function.

Frame Pointer

Balance

Questions

Feel free to edit this section and post questions, I'll do my best to answer them. But you may need to contact me to let me know that a question exists.