I'm working on a project that i need to load a simple exe
file to a buffer and then call the entry point of the exe
file from within the ram .
i know that i can't load and run a very complicated exe file with a lot of dependencies from buffer but i think its possible to call an independent function of the exe (PE)
file (like an empty main).
to do so,i have to find the entry point address of exe file (its offset) and then cast that address (which is now loaded in a buffer) to a pointer-to-function and then call that, it probably would call the function loaded in ram but i don't know how to find the address .
this is what I've done so far :
int main(){
void (* function)();
unsigned char* buffer;
FILE* fp;
size_t size;
char* path = "address of the file";
fp = fopen(path, "rb");
fseek(fp, 0, SEEK_END);
size = ftell(fp); /*calc the size needed*/
fseek(fp, 0, SEEK_SET);
buffer = (unsigned char *) malloc(size);
fclose(fp);
/*problem : i have to sum the buffer address with that offset here*/
function = (void (*)())(buffer);
function();
return 0;
}
i think it's pretty much clear from the code what im trying to do .
here is a link that tries to do the same thing for meterpreter
(the idea is the same):
https://github.com/rsmudge/metasploit-loader/blob/master/src/main.c
i'd appreciate any help .
Thanks