mirror of
https://github.com/fancycode/MemoryModule
synced 2026-06-06 15:44:28 +00:00
Support loading (and running) EXE files (fixes #7).
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
|
||||
#include <windows.h>
|
||||
#include <tchar.h>
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#include "../../MemoryModule.h"
|
||||
|
||||
#define EXE_FILE TEXT("DllLoader.exe")
|
||||
|
||||
int RunFromMemory(void)
|
||||
{
|
||||
FILE *fp;
|
||||
unsigned char *data=NULL;
|
||||
size_t size;
|
||||
HMEMORYMODULE handle;
|
||||
int result = -1;
|
||||
|
||||
fp = _tfopen(EXE_FILE, _T("rb"));
|
||||
if (fp == NULL)
|
||||
{
|
||||
_tprintf(_T("Can't open executable \"%s\"."), EXE_FILE);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
size = ftell(fp);
|
||||
data = (unsigned char *)malloc(size);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
fread(data, 1, size, fp);
|
||||
fclose(fp);
|
||||
|
||||
handle = MemoryLoadLibrary(data);
|
||||
if (handle == NULL)
|
||||
{
|
||||
_tprintf(_T("Can't load library from memory.\n"));
|
||||
goto exit;
|
||||
}
|
||||
|
||||
result = MemoryCallEntryPoint(handle);
|
||||
if (result < 0) {
|
||||
_tprintf(_T("Could not execute entry point: %d\n"), result);
|
||||
}
|
||||
MemoryFreeLibrary(handle);
|
||||
|
||||
exit:
|
||||
if (data)
|
||||
free(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
return RunFromMemory();
|
||||
}
|
||||
|
||||
@@ -22,9 +22,15 @@ CFLAGS += -DUNICODE -D_UNICODE
|
||||
endif
|
||||
|
||||
OBJ = DllLoader.o ../../MemoryModule.o
|
||||
OBJ_LOADER = DllLoaderLoader.o ../../MemoryModule.o
|
||||
|
||||
all: DllLoader.exe DllLoaderLoader.exe
|
||||
|
||||
DllLoader.exe: $(OBJ)
|
||||
$(CC) $(LDFLAGS) -o DllLoader.exe $(OBJ)
|
||||
$(CC) $(LDFLAGS) -Wl,--image-base -Wl,0x20000000 -o DllLoader.exe $(OBJ)
|
||||
|
||||
DllLoaderLoader.exe: $(OBJ_LOADER)
|
||||
$(CC) $(LDFLAGS) -Wl,--image-base -Wl,0x10000000 -o DllLoaderLoader.exe $(OBJ_LOADER)
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) $(CFLAGS) -c $<
|
||||
@@ -33,4 +39,4 @@ DllLoader.exe: $(OBJ)
|
||||
$(CC) $(CFLAGS) -c $<
|
||||
|
||||
clean:
|
||||
$(RM) -rf $(OBJ) DllLoader.exe
|
||||
$(RM) -rf $(OBJ) $(OBJ_LOADER) DllLoader.exe DllLoaderLoader.exe
|
||||
|
||||
Reference in New Issue
Block a user