2012-07-02 22:55:23 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
#ifndef __GNUC__
|
|
|
|
// disable warnings about pointer <-> DWORD conversions
|
|
|
|
# pragma warning(disable : 4311 4312)
|
|
|
|
#endif
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
#ifdef _WIN64
|
|
|
|
# define POINTER_TYPE ULONGLONG
|
|
|
|
#else
|
|
|
|
# define POINTER_TYPE DWORD
|
|
|
|
#endif
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
#include <windows.h>
|
|
|
|
#include <winnt.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#ifdef DEBUG_OUTPUT
|
|
|
|
# include <stdio.h>
|
|
|
|
#endif
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
#include "nsWindowsHelpers.h"
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
typedef const unsigned char* FileView;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
template <>
|
|
|
|
class nsAutoRefTraits<FileView> {
|
|
|
|
public:
|
|
|
|
typedef FileView RawRef;
|
|
|
|
static FileView Void() { return nullptr; }
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
static void Release(RawRef aView) {
|
|
|
|
if (nullptr != aView) UnmapViewOfFile(aView);
|
|
|
|
}
|
|
|
|
};
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
#ifndef IMAGE_SIZEOF_BASE_RELOCATION
|
|
|
|
// Vista SDKs no longer define IMAGE_SIZEOF_BASE_RELOCATION!?
|
|
|
|
# define IMAGE_SIZEOF_BASE_RELOCATION (sizeof(IMAGE_BASE_RELOCATION))
|
|
|
|
#endif
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
#include "LoadLibraryRemote.h"
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
typedef struct {
|
|
|
|
PIMAGE_NT_HEADERS headers;
|
2019-05-01 11:47:10 +03:00
|
|
|
unsigned char* localCodeBase;
|
|
|
|
unsigned char* remoteCodeBase;
|
|
|
|
HMODULE* modules;
|
2012-07-02 22:55:23 +04:00
|
|
|
int numModules;
|
|
|
|
} MEMORYMODULE, *PMEMORYMODULE;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
typedef BOOL(WINAPI* DllEntryProc)(HINSTANCE hinstDLL, DWORD fdwReason,
|
2012-07-02 22:55:23 +04:00
|
|
|
LPVOID lpReserved);
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
#define GET_HEADER_DICTIONARY(module, idx) \
|
|
|
|
&(module)->headers->OptionalHeader.DataDirectory[idx]
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
#ifdef DEBUG_OUTPUT
|
2019-05-01 11:47:10 +03:00
|
|
|
static void OutputLastError(const char* msg) {
|
|
|
|
char* tmp;
|
|
|
|
char* tmpmsg;
|
2012-07-02 22:55:23 +04:00
|
|
|
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
2013-10-11 00:39:09 +04:00
|
|
|
nullptr, GetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&tmp, 0,
|
|
|
|
nullptr);
|
2019-05-01 11:47:10 +03:00
|
|
|
tmpmsg = (char*)LocalAlloc(LPTR, strlen(msg) + strlen(tmp) + 3);
|
2012-07-02 22:55:23 +04:00
|
|
|
sprintf(tmpmsg, "%s: %s", msg, tmp);
|
|
|
|
OutputDebugStringA(tmpmsg);
|
|
|
|
LocalFree(tmpmsg);
|
|
|
|
LocalFree(tmp);
|
|
|
|
}
|
|
|
|
#endif
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
static void CopySections(const unsigned char* data,
|
2012-07-02 22:55:23 +04:00
|
|
|
PIMAGE_NT_HEADERS old_headers, PMEMORYMODULE module) {
|
|
|
|
int i;
|
2019-05-01 11:47:10 +03:00
|
|
|
unsigned char* codeBase = module->localCodeBase;
|
|
|
|
unsigned char* dest;
|
2012-07-02 22:55:23 +04:00
|
|
|
PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(module->headers);
|
|
|
|
for (i = 0; i < module->headers->FileHeader.NumberOfSections;
|
|
|
|
i++, section++) {
|
|
|
|
dest = codeBase + section->VirtualAddress;
|
|
|
|
memset(dest, 0, section->Misc.VirtualSize);
|
|
|
|
if (section->SizeOfRawData) {
|
|
|
|
memcpy(dest, data + section->PointerToRawData, section->SizeOfRawData);
|
|
|
|
}
|
2018-11-29 13:30:46 +03:00
|
|
|
// section->Misc.PhysicalAddress = (POINTER_TYPE) module->remoteCodeBase +
|
|
|
|
// section->VirtualAddress;
|
2012-07-02 22:55:23 +04:00
|
|
|
}
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
static bool CopyRegion(HANDLE hRemoteProcess, void* remoteAddress,
|
|
|
|
void* localAddress, DWORD size, DWORD protect) {
|
2014-10-22 00:01:16 +04:00
|
|
|
if (size > 0) {
|
|
|
|
// Copy the data from local->remote and set the memory protection
|
|
|
|
if (!VirtualAllocEx(hRemoteProcess, remoteAddress, size, MEM_COMMIT,
|
|
|
|
PAGE_READWRITE))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!WriteProcessMemory(hRemoteProcess, remoteAddress, localAddress, size,
|
|
|
|
nullptr)) {
|
|
|
|
#ifdef DEBUG_OUTPUT
|
|
|
|
OutputLastError("Error writing remote memory.\n");
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD oldProtect;
|
|
|
|
if (VirtualProtectEx(hRemoteProcess, remoteAddress, size, protect,
|
|
|
|
&oldProtect) == 0) {
|
|
|
|
#ifdef DEBUG_OUTPUT
|
|
|
|
OutputLastError("Error protecting memory page");
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-02 22:55:23 +04:00
|
|
|
// Protection flags for memory pages (Executable, Readable, Writeable)
|
|
|
|
static int ProtectionFlags[2][2][2] = {
|
|
|
|
{
|
|
|
|
// not executable
|
|
|
|
{PAGE_NOACCESS, PAGE_WRITECOPY},
|
|
|
|
{PAGE_READONLY, PAGE_READWRITE},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// executable
|
|
|
|
{PAGE_EXECUTE, PAGE_EXECUTE_WRITECOPY},
|
|
|
|
{PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE},
|
|
|
|
},
|
|
|
|
};
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
static bool FinalizeSections(PMEMORYMODULE module, HANDLE hRemoteProcess) {
|
|
|
|
#ifdef DEBUG_OUTPUT
|
|
|
|
fprintf(stderr, "Finalizing sections: local base %p, remote base %p\n",
|
|
|
|
module->localCodeBase, module->remoteCodeBase);
|
|
|
|
#endif
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
int i;
|
2014-10-22 00:01:16 +04:00
|
|
|
int numSections = module->headers->FileHeader.NumberOfSections;
|
|
|
|
|
|
|
|
if (numSections < 1) return false;
|
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(module->headers);
|
2014-10-22 00:01:16 +04:00
|
|
|
|
|
|
|
// Copy any data before the first section (i.e. the image header)
|
|
|
|
if (!CopyRegion(hRemoteProcess, module->remoteCodeBase, module->localCodeBase,
|
|
|
|
section->VirtualAddress, PAGE_READONLY))
|
|
|
|
return false;
|
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
// loop through all sections and change access flags
|
2014-10-22 00:01:16 +04:00
|
|
|
for (i = 0; i < numSections; i++, section++) {
|
|
|
|
DWORD protect, size;
|
2012-07-02 22:55:23 +04:00
|
|
|
int executable = (section->Characteristics & IMAGE_SCN_MEM_EXECUTE) != 0;
|
|
|
|
int readable = (section->Characteristics & IMAGE_SCN_MEM_READ) != 0;
|
|
|
|
int writeable = (section->Characteristics & IMAGE_SCN_MEM_WRITE) != 0;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
// determine protection flags based on characteristics
|
|
|
|
protect = ProtectionFlags[executable][readable][writeable];
|
|
|
|
if (section->Characteristics & IMAGE_SCN_MEM_NOT_CACHED) {
|
|
|
|
protect |= PAGE_NOCACHE;
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void* remoteAddress = module->remoteCodeBase + section->VirtualAddress;
|
|
|
|
void* localAddress = module->localCodeBase + section->VirtualAddress;
|
2014-10-22 00:01:16 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
// determine size of region
|
|
|
|
size = section->Misc.VirtualSize;
|
|
|
|
#ifdef DEBUG_OUTPUT
|
|
|
|
fprintf(stderr,
|
|
|
|
"Copying section %s to %p, size %x, executable %i readable %i "
|
|
|
|
"writeable %i\n",
|
|
|
|
section->Name, remoteAddress, size, executable, readable,
|
|
|
|
writeable);
|
|
|
|
#endif
|
2014-10-22 00:01:16 +04:00
|
|
|
if (!CopyRegion(hRemoteProcess, remoteAddress, localAddress, size, protect))
|
|
|
|
return false;
|
2012-07-02 22:55:23 +04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
static void PerformBaseRelocation(PMEMORYMODULE module, SIZE_T delta) {
|
|
|
|
DWORD i;
|
2019-05-01 11:47:10 +03:00
|
|
|
unsigned char* codeBase = module->localCodeBase;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
PIMAGE_DATA_DIRECTORY directory =
|
|
|
|
GET_HEADER_DICTIONARY(module, IMAGE_DIRECTORY_ENTRY_BASERELOC);
|
|
|
|
if (directory->Size > 0) {
|
|
|
|
PIMAGE_BASE_RELOCATION relocation =
|
|
|
|
(PIMAGE_BASE_RELOCATION)(codeBase + directory->VirtualAddress);
|
|
|
|
for (; relocation->VirtualAddress > 0;) {
|
2019-05-01 11:47:10 +03:00
|
|
|
unsigned char* dest = codeBase + relocation->VirtualAddress;
|
|
|
|
unsigned short* relInfo = (unsigned short*)((unsigned char*)relocation +
|
|
|
|
IMAGE_SIZEOF_BASE_RELOCATION);
|
2012-07-02 22:55:23 +04:00
|
|
|
for (i = 0;
|
|
|
|
i < ((relocation->SizeOfBlock - IMAGE_SIZEOF_BASE_RELOCATION) / 2);
|
|
|
|
i++, relInfo++) {
|
2019-05-01 11:47:10 +03:00
|
|
|
DWORD* patchAddrHL;
|
2012-07-02 22:55:23 +04:00
|
|
|
#ifdef _WIN64
|
2019-05-01 11:47:10 +03:00
|
|
|
ULONGLONG* patchAddr64;
|
2012-07-02 22:55:23 +04:00
|
|
|
#endif
|
|
|
|
int type, offset;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
// the upper 4 bits define the type of relocation
|
|
|
|
type = *relInfo >> 12;
|
|
|
|
// the lower 12 bits define the offset
|
|
|
|
offset = *relInfo & 0xfff;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
switch (type) {
|
|
|
|
case IMAGE_REL_BASED_ABSOLUTE:
|
|
|
|
// skip relocation
|
|
|
|
break;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
case IMAGE_REL_BASED_HIGHLOW:
|
|
|
|
// change complete 32 bit address
|
2019-05-01 11:47:10 +03:00
|
|
|
patchAddrHL = (DWORD*)(dest + offset);
|
2012-07-02 22:55:23 +04:00
|
|
|
*patchAddrHL += delta;
|
|
|
|
break;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
#ifdef _WIN64
|
|
|
|
case IMAGE_REL_BASED_DIR64:
|
2019-05-01 11:47:10 +03:00
|
|
|
patchAddr64 = (ULONGLONG*)(dest + offset);
|
2012-07-02 22:55:23 +04:00
|
|
|
*patchAddr64 += delta;
|
|
|
|
break;
|
|
|
|
#endif
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
default:
|
|
|
|
// printf("Unknown relocation: %d\n", type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
// advance to next relocation block
|
2019-05-01 11:47:10 +03:00
|
|
|
relocation = (PIMAGE_BASE_RELOCATION)(((char*)relocation) +
|
2012-07-02 22:55:23 +04:00
|
|
|
relocation->SizeOfBlock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
static int BuildImportTable(PMEMORYMODULE module) {
|
|
|
|
int result = 1;
|
2019-05-01 11:47:10 +03:00
|
|
|
unsigned char* codeBase = module->localCodeBase;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
PIMAGE_DATA_DIRECTORY directory =
|
|
|
|
GET_HEADER_DICTIONARY(module, IMAGE_DIRECTORY_ENTRY_IMPORT);
|
|
|
|
if (directory->Size > 0) {
|
|
|
|
PIMAGE_IMPORT_DESCRIPTOR importDesc =
|
|
|
|
(PIMAGE_IMPORT_DESCRIPTOR)(codeBase + directory->VirtualAddress);
|
|
|
|
PIMAGE_IMPORT_DESCRIPTOR importEnd = (PIMAGE_IMPORT_DESCRIPTOR)(
|
|
|
|
codeBase + directory->VirtualAddress + directory->Size);
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
for (; importDesc < importEnd && importDesc->Name; importDesc++) {
|
2019-05-01 11:47:10 +03:00
|
|
|
POINTER_TYPE* thunkRef;
|
|
|
|
FARPROC* funcRef;
|
2012-07-02 22:55:23 +04:00
|
|
|
HMODULE handle = GetModuleHandleA((LPCSTR)(codeBase + importDesc->Name));
|
2013-10-11 00:39:09 +04:00
|
|
|
if (handle == nullptr) {
|
2012-07-02 22:55:23 +04:00
|
|
|
#if DEBUG_OUTPUT
|
|
|
|
OutputLastError("Can't load library");
|
|
|
|
#endif
|
|
|
|
result = 0;
|
|
|
|
break;
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
module->modules = (HMODULE*)realloc(
|
2012-07-02 22:55:23 +04:00
|
|
|
module->modules, (module->numModules + 1) * (sizeof(HMODULE)));
|
2013-10-11 00:39:09 +04:00
|
|
|
if (module->modules == nullptr) {
|
2012-07-02 22:55:23 +04:00
|
|
|
result = 0;
|
|
|
|
break;
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
module->modules[module->numModules++] = handle;
|
|
|
|
if (importDesc->OriginalFirstThunk) {
|
2019-05-01 11:47:10 +03:00
|
|
|
thunkRef = (POINTER_TYPE*)(codeBase + importDesc->OriginalFirstThunk);
|
|
|
|
funcRef = (FARPROC*)(codeBase + importDesc->FirstThunk);
|
2012-07-02 22:55:23 +04:00
|
|
|
} else {
|
|
|
|
// no hint table
|
2019-05-01 11:47:10 +03:00
|
|
|
thunkRef = (POINTER_TYPE*)(codeBase + importDesc->FirstThunk);
|
|
|
|
funcRef = (FARPROC*)(codeBase + importDesc->FirstThunk);
|
2012-07-02 22:55:23 +04:00
|
|
|
}
|
|
|
|
for (; *thunkRef; thunkRef++, funcRef++) {
|
|
|
|
if (IMAGE_SNAP_BY_ORDINAL(*thunkRef)) {
|
|
|
|
*funcRef =
|
|
|
|
(FARPROC)GetProcAddress(handle, (LPCSTR)IMAGE_ORDINAL(*thunkRef));
|
|
|
|
} else {
|
|
|
|
PIMAGE_IMPORT_BY_NAME thunkData =
|
|
|
|
(PIMAGE_IMPORT_BY_NAME)(codeBase + (*thunkRef));
|
|
|
|
*funcRef = (FARPROC)GetProcAddress(handle, (LPCSTR)&thunkData->Name);
|
|
|
|
}
|
|
|
|
if (*funcRef == 0) {
|
|
|
|
result = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
if (!result) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
return result;
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
static void* MemoryGetProcAddress(PMEMORYMODULE module, const char* name);
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void* LoadRemoteLibraryAndGetAddress(HANDLE hRemoteProcess,
|
|
|
|
const WCHAR* library, const char* symbol) {
|
2012-07-02 22:55:23 +04:00
|
|
|
// Map the DLL into memory
|
2013-10-11 00:39:09 +04:00
|
|
|
nsAutoHandle hLibrary(CreateFile(library, GENERIC_READ, FILE_SHARE_READ,
|
|
|
|
nullptr, OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL, nullptr));
|
2012-07-02 22:55:23 +04:00
|
|
|
if (INVALID_HANDLE_VALUE == hLibrary) {
|
|
|
|
#if DEBUG_OUTPUT
|
|
|
|
OutputLastError("Couldn't CreateFile the library.\n");
|
|
|
|
#endif
|
2013-10-11 00:39:09 +04:00
|
|
|
return nullptr;
|
2012-07-02 22:55:23 +04:00
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
nsAutoHandle hMapping(
|
2013-10-11 00:39:09 +04:00
|
|
|
CreateFileMapping(hLibrary, nullptr, PAGE_READONLY, 0, 0, nullptr));
|
2012-07-02 22:55:23 +04:00
|
|
|
if (!hMapping) {
|
|
|
|
#if DEBUG_OUTPUT
|
|
|
|
OutputLastError("Couldn't CreateFileMapping.\n");
|
|
|
|
#endif
|
2013-10-11 00:39:09 +04:00
|
|
|
return nullptr;
|
2012-07-02 22:55:23 +04:00
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
nsAutoRef<FileView> data(
|
2019-05-01 11:47:10 +03:00
|
|
|
(const unsigned char*)MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0));
|
2012-07-02 22:55:23 +04:00
|
|
|
if (!data) {
|
|
|
|
#if DEBUG_OUTPUT
|
|
|
|
OutputLastError("Couldn't MapViewOfFile.\n");
|
|
|
|
#endif
|
2013-10-11 00:39:09 +04:00
|
|
|
return nullptr;
|
2012-07-02 22:55:23 +04:00
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
SIZE_T locationDelta;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
PIMAGE_DOS_HEADER dos_header = (PIMAGE_DOS_HEADER)data.get();
|
|
|
|
if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) {
|
|
|
|
#if DEBUG_OUTPUT
|
|
|
|
OutputDebugStringA("Not a valid executable file.\n");
|
|
|
|
#endif
|
2013-10-11 00:39:09 +04:00
|
|
|
return nullptr;
|
2012-07-02 22:55:23 +04:00
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
PIMAGE_NT_HEADERS old_header =
|
|
|
|
(PIMAGE_NT_HEADERS)(data + dos_header->e_lfanew);
|
|
|
|
if (old_header->Signature != IMAGE_NT_SIGNATURE) {
|
|
|
|
#if DEBUG_OUTPUT
|
|
|
|
OutputDebugStringA("No PE header found.\n");
|
|
|
|
#endif
|
2013-10-11 00:39:09 +04:00
|
|
|
return nullptr;
|
2012-07-02 22:55:23 +04:00
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
// reserve memory for image of library in this process and the target process
|
2019-05-01 11:47:10 +03:00
|
|
|
unsigned char* localCode = (unsigned char*)VirtualAlloc(
|
2012-07-02 22:55:23 +04:00
|
|
|
nullptr, old_header->OptionalHeader.SizeOfImage, MEM_RESERVE | MEM_COMMIT,
|
|
|
|
PAGE_READWRITE);
|
|
|
|
if (!localCode) {
|
|
|
|
#if DEBUG_OUTPUT
|
|
|
|
OutputLastError("Can't reserve local memory.");
|
|
|
|
#endif
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
unsigned char* remoteCode = (unsigned char*)VirtualAllocEx(
|
2012-07-02 22:55:23 +04:00
|
|
|
hRemoteProcess, nullptr, old_header->OptionalHeader.SizeOfImage,
|
|
|
|
MEM_RESERVE, PAGE_EXECUTE_READ);
|
|
|
|
if (!remoteCode) {
|
|
|
|
#if DEBUG_OUTPUT
|
|
|
|
OutputLastError("Can't reserve remote memory.");
|
|
|
|
#endif
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
MEMORYMODULE result;
|
|
|
|
result.localCodeBase = localCode;
|
|
|
|
result.remoteCodeBase = remoteCode;
|
|
|
|
result.numModules = 0;
|
2013-10-11 00:39:09 +04:00
|
|
|
result.modules = nullptr;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
// copy PE header to code
|
|
|
|
memcpy(localCode, dos_header,
|
|
|
|
dos_header->e_lfanew + old_header->OptionalHeader.SizeOfHeaders);
|
|
|
|
result.headers =
|
|
|
|
reinterpret_cast<PIMAGE_NT_HEADERS>(localCode + dos_header->e_lfanew);
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
// update position
|
|
|
|
result.headers->OptionalHeader.ImageBase = (POINTER_TYPE)remoteCode;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
// copy sections from DLL file block to new memory location
|
|
|
|
CopySections(data, old_header, &result);
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
// adjust base address of imported data
|
|
|
|
locationDelta = (SIZE_T)(remoteCode - old_header->OptionalHeader.ImageBase);
|
|
|
|
if (locationDelta != 0) {
|
|
|
|
PerformBaseRelocation(&result, locationDelta);
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
// load required dlls and adjust function table of imports
|
|
|
|
if (!BuildImportTable(&result)) {
|
2013-10-11 00:39:09 +04:00
|
|
|
return nullptr;
|
2012-07-02 22:55:23 +04:00
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
// mark memory pages depending on section headers and release
|
|
|
|
// sections that are marked as "discardable"
|
|
|
|
if (!FinalizeSections(&result, hRemoteProcess)) {
|
2013-10-11 00:39:09 +04:00
|
|
|
return nullptr;
|
2012-07-02 22:55:23 +04:00
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
return MemoryGetProcAddress(&result, symbol);
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
static void* MemoryGetProcAddress(PMEMORYMODULE module, const char* name) {
|
|
|
|
unsigned char* localCodeBase = module->localCodeBase;
|
2012-07-02 22:55:23 +04:00
|
|
|
int idx = -1;
|
|
|
|
DWORD i, *nameRef;
|
2019-05-01 11:47:10 +03:00
|
|
|
WORD* ordinal;
|
2012-07-02 22:55:23 +04:00
|
|
|
PIMAGE_EXPORT_DIRECTORY exports;
|
|
|
|
PIMAGE_DATA_DIRECTORY directory =
|
|
|
|
GET_HEADER_DICTIONARY(module, IMAGE_DIRECTORY_ENTRY_EXPORT);
|
|
|
|
if (directory->Size == 0) {
|
|
|
|
// no export table found
|
2013-10-11 00:39:09 +04:00
|
|
|
return nullptr;
|
2012-07-02 22:55:23 +04:00
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
exports =
|
|
|
|
(PIMAGE_EXPORT_DIRECTORY)(localCodeBase + directory->VirtualAddress);
|
|
|
|
if (exports->NumberOfNames == 0 || exports->NumberOfFunctions == 0) {
|
|
|
|
// DLL doesn't export anything
|
2013-10-11 00:39:09 +04:00
|
|
|
return nullptr;
|
2012-07-02 22:55:23 +04:00
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
// search function name in list of exported names
|
2019-05-01 11:47:10 +03:00
|
|
|
nameRef = (DWORD*)(localCodeBase + exports->AddressOfNames);
|
|
|
|
ordinal = (WORD*)(localCodeBase + exports->AddressOfNameOrdinals);
|
2012-07-02 22:55:23 +04:00
|
|
|
for (i = 0; i < exports->NumberOfNames; i++, nameRef++, ordinal++) {
|
2019-05-01 11:47:10 +03:00
|
|
|
if (stricmp(name, (const char*)(localCodeBase + (*nameRef))) == 0) {
|
2012-07-02 22:55:23 +04:00
|
|
|
idx = *ordinal;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
if (idx == -1) {
|
|
|
|
// exported symbol not found
|
2013-10-11 00:39:09 +04:00
|
|
|
return nullptr;
|
2012-07-02 22:55:23 +04:00
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
if ((DWORD)idx > exports->NumberOfFunctions) {
|
|
|
|
// name <-> ordinal number don't match
|
2013-10-11 00:39:09 +04:00
|
|
|
return nullptr;
|
2012-07-02 22:55:23 +04:00
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2012-07-02 22:55:23 +04:00
|
|
|
// AddressOfFunctions contains the RVAs to the "real" functions
|
2013-04-05 13:29:50 +04:00
|
|
|
return module->remoteCodeBase +
|
2019-05-01 11:47:10 +03:00
|
|
|
(*(DWORD*)(localCodeBase + exports->AddressOfFunctions + (idx * 4)));
|
2012-07-02 22:55:23 +04:00
|
|
|
}
|