Reputation:
I am very new to C and as a class assignment my instructor wanted us to play with buffer overflows. I found the following online as an example and I can't figure out how to use it!
#include <stdio.h>
char temp[32];
unsigned int setThis=1;
printf("Enter your temp: \n");
fgets(temp, 34, stdin); //Takes a 34 buffer size when temp can only be 32
printf("Value of you setThis: %d", setThis);
So my question is, how do i set "setThis" to a certain variable? Any help is appreciated, BeastlyJman.
Upvotes: 0
Views: 433
Reputation: 34839
There's no guaranteed way to do it, but typically variables are put on the stack such that the first variable is last in memory. So if you declare setThis
before temp[32]
, then setThis
will be at the end of the temp
array, and you can overwrite it.
But as I said, there's no guarantee that's what the compiler will do. You should really check the assembly code that the compiler generates to see where temp
and setThis
are located.
Also, you can save yourself some typing if you reduce the size of temp
to temp[8]
and then pass 10
to fgets
. To cause an overflow, you need to type more characters than the buffer can hold.
Upvotes: 0