I am attempting to overwrite a variable in a c program using a format string attack. This IS a homework assignment for a security class, and I am not asking for you to do my homework for me. However I am struggling to get this to work and I can't understand why. Also, I understand that there have been other questions asked on this topic, but none of those posts were able to help me.
I am working on a VM that my professor has set up for me. The vulnerable code was built by him so I'm not sure how he did it, the OS is Linux 3.13.0-65-generic i686, and ASLR is supposed to have been disabled by the professor. We did not receive any instructions about setting up an environment to allow this attack to succeed because the environment was completely set up by the professor. Also I don't have sudo permissions on the VM to do those things myself.
This is the code of the vulnerable application that my attacks target:
#include <stdio.h>
#ifndef MAGICNUM
#define MAGICNUM 0x41424344
#endif
int x = 0;
void vuln() {
int y = 1;
char buf[128];
printf("This is vuln() \tx = %08x \ty = %08x\n", x, y);
printf("Enter your input: ");
scanf("%127s", buf);
printf("You entered: ");
printf(buf);
printf("\n\n");
printf("Now x = %08x and y = %08x\n", x, y);
if(x==MAGICNUM) {
printf("Success!\n");
system("/bin/sh");
}
else {
puts("Sorry, try again.");
}
return;
}
int main(int argc, char* argv[])
{
vuln();
return 0;
}
Once again my goal is to overwrite the variable x using a format string attack. The homework assignment wants me to overwrite it with MAGICNUM but for now I am simply trying to assign any arbitrary value to x.
Using GDB I was able to find that the address of x is 0x0804a030
.
I know that I need to use the %n
format specifier and these are some examples of input strings I have tried so far:
"\x30\xa0\x04\x08%08x.%08x.%08x.%08x%n"
"\x30\xa0\x04\x08%8s%n"
"\x30\xa0\x04\x08%.8%n"
All of those input strings trigger a segmentation fault and I can't figure out why they aren't working.
Any insight on what I can do to successfully overwrite x would be great.