I wanted to exploit this code using format string vulnerability:
int jackpot;
void fmt_str(void)
{
char buf[128];
puts("Give me a string to print");
read(0, buf, 128);
printf(buf);
printf("jackpot @ %p = %d [0x%08x]\n", &jackpot, jackpot, jackpot);
if (jackpot == 0x1337)
puts("You won!");
else
puts("You lost :(");
}
int main(void)
{
srand(time(0));
setbuf(stdout, 0);
jackpot = rand();
fmt_str();
return 0;
}
The idea is using format string vulnerability in order to get "You won!". By using objdump
I found out that the address of the global variable jackpot
is 0x0804a04c
.
Since i can't use a Python script to input my string in argc, this is what i tried to do (working on gdb):
Give me a string to print
AAAAAAAAAAAAAAAA '\x4c\xa0\x04\x08' BBBBBBBBBBBBBBB %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x
and this is what i get with print(buf)
AAAAAAAAAAAAAAAA '\x4c\xa0\x04\x08' BBBBBBBBBBBBBBB bffff280 80 0
41414141 41414141 41414141 41414141 785c2720 785c6334 785c3061
785c3430 20273830 42424242 42424242 42424242 20424242 25207825
78252078 20782520 25207825 78252078 20782520 25207825 78252078
20782520
Basically, my intention is having the jackpot
's address between the 41s and the 42s, so that I can better locate it in the printed string, and perform an attack by changing %x
to %n
.
The problem is that with this way of reading input, I can't write the target address in the string, so I can't perform the attack. What do you suggest?
The program is compile with the stack made executable, with -fno-stack-protection and with ASLR disabled.