Good day everyone! I’m trying to understand how buffer overflow works. I’m doing this for my project in a computer security course I’m taking. Right now, I’m in the process of determining the address of the function’s return address which I’m supposed to change to perform a buffer overflow attack. I’ve written a simple program based from an example I’ve read in the internet. What this program does is it creates an integer pointer that will be made to point to the address of the function return address in the stack. To do this, (granted I understand how a function/program variables get organized in the stack), I add 8 to the buffer variable’ address and set it as the value of ret. I’m not doing anything here that would change the address contained in the location of func’s return address.
UPDATE: I've modified the program a bit, to it prints the address of func's parameter a in the stack. As you can see, the distance between a nd buffer is about 8 bytes, so that woyuld probably mean, based from the stack layout, that saved FP and old EIP (func return address) is in between.. is that right?
here's the program: (The COMPLETE program In question, WORKING!)
void func( int a){
char buffer[3];
int *ret;
ret = buffer + 11; // this is the configuratio which made the whole program works... This now points to the address containing func's return address
printf (" address of a is %d\n", &a);
printf ("address of buffer is %x\n", buffer);
printf ("address of ret is %x\n", ret);
printf ("value of ret is %x\n", (*ret));
}
void main(){
int num;
num = 0;
func(num);
num = 1;
printf("Num now is %d", num);
}
Output of the program when gets excecuted:
As you can see, I’m printing the address of the variables buffer and ret. I’ve added an additional statement printing the value of the ret variable (supposed location of func return address, so this should print the address of the next instruction which will get executed after func returns from execution).
Here is the dump which shows the supposed address of the instruction to be executed after func returns. (Underlined in green) As you can see, that value is way different from the value printed contained in the variable ret.
My question is, why are they different? (of course in the assumption that what I’ve done are all right). Else, what have I done wrong? Is my understanding of the program’s runtime stack wrong? Please, help me understand this. My project is due nextweek and I’ve barely touched it yet. I’m sorry if I’m being demanding, I badly need your help.
UPDATE:
Hi Guys, I've already solved the problem. It turned out that a lousy formatting of my outputs (printf) mwas actually the cause of the confusion on my part.