I wrote a program that writes a function to a file, then loads said file and executes the function. This code works without a problem when I compile for 32bit, but when I set my compiler to 64bit the program crashes.
#include <stdio.h>
#include <windows.h>
#include <Filez.h>
void shellc(FARPROC p,char* x)
{
(int (WINAPI *)(HWND,LPCSTR,LPCSTR,UINT))p(NULL,x,x,MB_OK);
}
void stub()
{
}
int main(int argc, char **argv)
{
int size = stub - shellc;
CryptMemoryToFile(shellc,size,"somepassword","shellcodefile");
int sz;
char * x = DeCryptFileToMemory("shellcodefile","somepassword",&sz);
void (*shellcode)(FARPROC,char*) = x;
FARPROC p = GetProcAddress(LoadLibraryA("user32.dll"),"MessageBoxA");
shellcode(p,"test");
getchar();
}
The 2 (De)Cryption functions that are defined in Filez.h
char * DeCryptFileToMemory(char * File,char * Pw,int * Size)
{
int PWL = strlen(Pw);
char * Data = LoadFile(File,Size);
if (Data == NULL) return 0;
int y = 0;
for (int x = 0; x <= *Size;x++)
{
Data[x] = Data[x] - Pw[y];
y++;
if (y > PWL) y = 0;
}
return Data;
}
int CryptMemoryToFile(char * Memory,int Size,char * Pw,char * File)
{
int PWL = strlen(Pw);
char * Memory2 = malloc(Size);
memcpy(Memory2,Memory,Size);
if (Memory2 == NULL) return 0;
int y = 0;
for (int x = 0; x <= Size;x++)
{
Memory2[x] = Memory2[x] + Pw[y];
y++;
if (y > PWL) y = 0;
}
FILE * f = fopen(File,"wb");
if (f == NULL) return 0;
int w = fwrite(Memory2,1,Size,f);
if (w != Size) return 0;
fclose(f);
free(Memory2);
return 1;
}