From a7a9eda6c43e0d6de8ed8e183b769a8317f192cb Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Sun, 31 Oct 2004 12:29:52 +0000 Subject: [PATCH] - removed parameter "size" from "MemoryLoadLibrary" as it wasn't used - updated import table loading code, now also works with Delphi .bpl files - updated section copying code for uninitialized sections --- MemoryModule.c | 27 +++++++++++++++------------ MemoryModule.h | 2 +- example/DllLoader/DllLoader.cpp | 2 +- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/MemoryModule.c b/MemoryModule.c index 6dcfc31..78b3fc9 100644 --- a/MemoryModule.c +++ b/MemoryModule.c @@ -73,10 +73,7 @@ CopySections(const unsigned char *data, PIMAGE_NT_HEADERS old_headers, PMEMORYMO { // section doesn't contain data in the dll itself, but may define // uninitialized data - int initialized = section->Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA; - int uninitialized = section->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA; - - size = initialized ? old_headers->OptionalHeader.SizeOfInitializedData : old_headers->OptionalHeader.SizeOfUninitializedData; + size = old_headers->OptionalHeader.SectionAlignment; if (size > 0) { dest = (unsigned char *)VirtualAlloc(codeBase + section->VirtualAddress, @@ -85,8 +82,7 @@ CopySections(const unsigned char *data, PIMAGE_NT_HEADERS old_headers, PMEMORYMO PAGE_READWRITE); section->Misc.PhysicalAddress = (DWORD)dest; - if (initialized) - memset(dest, 0, size); + memset(dest, 0, size); } // section is empty @@ -222,7 +218,7 @@ BuildImportTable(PMEMORYMODULE module) if (directory->Size > 0) { PIMAGE_IMPORT_DESCRIPTOR importDesc = (PIMAGE_IMPORT_DESCRIPTOR)(codeBase + directory->VirtualAddress); - for (; importDesc->Characteristics != 0; importDesc++) + for (; !IsBadReadPtr(importDesc, sizeof(IMAGE_IMPORT_DESCRIPTOR)) && importDesc->Name; importDesc++) { DWORD *thunkRef, *funcRef; HMODULE handle = LoadLibrary((LPCSTR)(codeBase + importDesc->Name)); @@ -243,8 +239,15 @@ BuildImportTable(PMEMORYMODULE module) } module->modules[module->numModules++] = handle; - thunkRef = (DWORD *)(codeBase + importDesc->OriginalFirstThunk); - funcRef = (DWORD *)(codeBase + importDesc->FirstThunk); + if (importDesc->OriginalFirstThunk) + { + thunkRef = (DWORD *)(codeBase + importDesc->OriginalFirstThunk); + funcRef = (DWORD *)(codeBase + importDesc->FirstThunk); + } else { + // no hint table + thunkRef = (DWORD *)(codeBase + importDesc->FirstThunk); + funcRef = (DWORD *)(codeBase + importDesc->FirstThunk); + } for (; *thunkRef; thunkRef++, funcRef++) { PIMAGE_IMPORT_BY_NAME thunkData = (PIMAGE_IMPORT_BY_NAME)(codeBase + *thunkRef); @@ -264,7 +267,7 @@ BuildImportTable(PMEMORYMODULE module) return result; } -HMEMORYMODULE MemoryLoadLibrary(const void *data, const size_t size) +HMEMORYMODULE MemoryLoadLibrary(const void *data) { PMEMORYMODULE result=NULL; PIMAGE_DOS_HEADER dos_header; @@ -333,8 +336,8 @@ HMEMORYMODULE MemoryLoadLibrary(const void *data, const size_t size) PAGE_READWRITE); // copy PE header to code - memcpy(headers, old_header, old_header->OptionalHeader.SizeOfHeaders); - result->headers = (PIMAGE_NT_HEADERS)headers; + memcpy(headers, dos_header, dos_header->e_lfanew + old_header->OptionalHeader.SizeOfHeaders); + result->headers = (PIMAGE_NT_HEADERS)&((const unsigned char *)(headers))[dos_header->e_lfanew]; // update position result->headers->OptionalHeader.ImageBase = (DWORD)code; diff --git a/MemoryModule.h b/MemoryModule.h index a3e627d..1cdec5c 100644 --- a/MemoryModule.h +++ b/MemoryModule.h @@ -32,7 +32,7 @@ typedef void *HMEMORYMODULE; extern "C" { #endif -HMEMORYMODULE MemoryLoadLibrary(const void *, const size_t); +HMEMORYMODULE MemoryLoadLibrary(const void *); FARPROC MemoryGetProcAddress(HMEMORYMODULE, const char *); diff --git a/example/DllLoader/DllLoader.cpp b/example/DllLoader/DllLoader.cpp index e3e8c44..4fa8ca3 100644 --- a/example/DllLoader/DllLoader.cpp +++ b/example/DllLoader/DllLoader.cpp @@ -44,7 +44,7 @@ void LoadFromMemory(void) fread(data, 1, size, fp); fclose(fp); - module = MemoryLoadLibrary(data, size); + module = MemoryLoadLibrary(data); if (module == NULL) { printf("Can't load library from memory.\n");