I am trying to overwrite the data using strcpy() function with that stack protection off. The problem I encounter here is that using strcpy() to write an unsigned long variable(address) would change the value because of little-endian(?). I want to know if there is any way to copy the exact value of unsigned long into the stack using strcpy() or I should use another function instead.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// some shellcode in format like "\x31\xc0"
const char shellcode[] = ...;
// vulnerable function bof
void bof(char *str){
char buffer[12];
printf("Come into function bof\n");
strcpy(buffer, str);
}
int main(int argc, char **argv){
char buffer[517];
//random filling of buffer that may not be necessary
strcpy(buffer,"abcdefghijkl1234");
//I believe buffer[16] is the location of return addr when the program goes in bof()
//I write the shell code into buffer[20] first
strcpy(&buffer[20],shellcode);
//Trying to make value of location buffer[16] into address of buffer[20]
strcpy(&buffer[16],&buffer[20]);
// bof is called here
bof(buffer);
printf("Exit from function bof\n");
}
memcpy()rather thanstrcpy().