I am using an Ubuntu 16.04 a 32-bit Virtual Machine OS.
I executed
sysctl -w kernel.randomize_va_space=0
to disable ASLR in root prior to gcc.
I have also compiled this using the command:
gcc -g -fno-stack-protector -z execstack -m32 -mpreferred-stack-boundary=2 testlab.c -o test
I have the following code block in C:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void print_addr(char *al) {
printf("Value of this pointer: %p\n", al);
}
void vulnerable(char *str) {
char buffer[16];
int toyCanary = 0x11223344;
print_addr(buffer); // **Outputs 0xbfffed0c**
strcpy(buffer, str);
if (toyCanary != 0x11223344) {
printf("You malicious behaviour is detected!\n");
exit(0);
}
}
int main(int argc, char **argv) {
char payload[128];
print_addr(payload); // **Outputs 0xbfffed28**
vulnerable(payload);
return 0;
}
From what I calculated, the offset between the 2 addresses of payload and buffer is 28 bytes (addresses are given in HEX in comments next to initialization). I assume the compiler hasn't allocated the full 128 bytes to payload because I haven't input anything yet, so I'll assume only 4 bytes have been allocated.
So what else is there occupying the rest of the 24 bytes?
From what I've learnt in my course right now, char* str will be pushed after payload taking up 4 bytes, and then comes in the return_address_to_main at 4 bytes, a saved ebp pointer for the previous frame which takes 4 more bytes and then buffer should be initialized.
But this only adds up to an offset of 16 bytes between the 2 addresses mentioned, what else is in there that I am unable to observe?
I will add this, I am sure that only 4 bytes are allotted to payload right now, because when I initialize a test standard integer variable right after, the address of the integer variable is only offset by 4 bytes, and accordingly the offset between payload and buffer becomes 32 bytes.
So in conclusion I am totally stumped, can anyone maybe point me as to what I am forgetting to consider? Where are the missing 12 bytes going off to? I need to know this because I have to do a stack overflow attack.
Alternatively, you could confirm my suspicion that this doesn't matter and that those 12 bytes consistently vanish whenever another function is called.
payloadis fully allocated. You can also see what the compiler decided to put between the two frames.buffer, whose size is just 16 bytes, that has the lower address, so (i) the space between them is 28 - 16 = 12 bytes, and (ii) there is no reason there to think thatpayloadhas size different from its declared 128 bytes.