As part of an assignment, I am expected to exploit the printf() vulnerability in the C code shared below. It should be in a way that when I run the code with a string (eg. ./format "foo"), I should change the "1" in "X equals to 1" with something else. I believe I need to change the value of X variable but if you have a different idea, please do not hesitate to share. Here is the code:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char **argv)
{
int *p;
int x = 1;
p=&x;
printf("x=%d, sizeof(x): %zu, %x = %p, sizeof((p):%zu,&p = %p, \n", x, sizeof(x), &x, sizeof(p),&p);
printf(argv[1]);
printf("\nX equals: %d \n", x);
return 0;
}
printfcall does not have enough arguments, and some arguments are of the wrong type for the format specifier. Why would anyone release code like this for you to exploit?printf(argv[1])line. But asprintf(argv[1])is not exactly a secure way to print out, it can be exploited by using strings such as"%n..."or"%s%s%s...."(for instance, you may read the memory with these strings, etc.). What I need to do is to change the X variable but I cannot find the proper string to change a variable.argcbeforeprintf(argv[1]);printf("x=%d, sizeof(x): %zu, %x = %p, sizeof((p):%zu,&p = %p, \n", x, sizeof(x), &x, sizeof(p),&p);and%nis used for inserting code. It is doable because of the mismatch. Trying to figure out how I can use%nwithout getting asegmentation error 11. @mfro