Modifying a variable the tricky way
Ok so now that we know variables are stored into the stack, we can show some nasty ways of modifying function local variables from another function that (supposedly) knows nothing about that local variable.
Code: Select all
#include <stdio.h>
void trick(void)
{
int* p;
p = (int*)((int)&p + 0x28);
*p = 12;
}
int main()
{
int a = 10;
trick();
printf("%d\n", a);
return 0;
}Code: Select all
int* p;Code: Select all
$ gdb -q example01
Reading symbols from /home/m0skit0/Temp/example01...done.
(gdb) b 13
Breakpoint 1 at 0x80483ef: file example01.c, line 13.
(gdb) r
Starting program: /home/m0skit0/Temp/example01
Breakpoint 1, main () at example01.c:13
13 trick();
(gdb) p &a
$1 = (int *) 0xbffff3acCode: Select all
(gdb) b 6
Breakpoint 2 at 0x80483ca: file example01.c, line 6.
(gdb) c
Continuing.
Breakpoint 2, trick () at example01.c:6
6 p = (int*)((int)&p + 0x28);
(gdb) p &p
$2 = (int **) 0xbffff384Code: Select all
(gdb) p/x 0xbffff3ac - 0xbffff384
$3 = 0x28Code: Select all
p = (int*)((int)&p + 0x28);Code: Select all
*p = 12;<< Prev Next >>



