I'm trying to exploit a binary file which I have access to the source code.
int flag = 0;
int main() {
char buf[0x50];
puts("Who are you? ");
printf("> ");
fgets(buf, 0x50, stdin);
printf(buf);
if (flag == 1337) {
puts("Enjoy your shell!");
system("/bin/sh");}
else {
puts("Not allowed");
}
return 0;
}
As you can see from source code, if flag is 1377 I'll get a shell. I can see that with gdb aswell.
0x4007ec <main+175> mov eax, DWORD PTR [rip+0x200892] # 0x601084 <flag>
0x4007f2 <main+181> cmp eax, 0x539
Security measures are setup like this:
Canary : Yes
NX : Yes
PIE : No
Fortify : No
RelRO : Partial
So, first of all I cannot do the classic buffer overflow because the program uses fgets
to gather input. Of course canary is there too, but will make no harm because If I was able to change flag's value (before Canary's check was made) I would be successful on getting that shell. I don't know If what I'm thinking this right, so please correct me If I'm wrong.
My first conclusion on this was that, I would not be able to exploit buf
in order to rewrite flag's value. (I assumed that buf
and flag
would be placed right next to each other on the stack). I think I'm right on this because when I took a look at $rsp
register and found that only the allowed amount of "A"'s were placed on the stack. So even if flag was placed right beneath it, flag's value would not be overwritten. Am I right so far? That would be my first question
0x7fffffffdaf0: 0x41414141 0x41414141 0x41414141 0x41414141
0x7fffffffdb00: 0x41414141 0x41414141 0x41414141 0x41414141
0x7fffffffdb10: 0x41414141 0x41414141 0x41414141 0x41414141
0x7fffffffdb20: 0x41414141 0x41414141 0x41414141 0x41414141
0x7fffffffdb30: 0x41414141 0x41414141 0x41414141 0x00414141
0x7fffffffdb40: 0x00400840 0x00000000 0x96703f00 0x948afed7
0x7fffffffdb50: 0xffffdc40 0x00007fff 0x00000000 0x00000000
So, how would I be able to range that value? I think that the exploit must come from the payload provided by a malicious user but buf
is the only way to get that payload into the stack. Since I cannot overwrite registers by overflowing buf
I'm a bit lost.