Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/LoadBlob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,27 @@ typedef struct BlobSection_s

typedef struct BlobImportEntry_s
{
BlobImportEntry_s(ULONG_PTR* a1, HMODULE a2, const char* a3, const char* a4) : ThunkFunction(a1), hProcDll(a2), DllName(a3), FunctionName(a4)
BlobImportEntry_s(ULONG_PTR* a1, HMODULE a2, const char* a3, const char* a4, FARPROC original) :
ThunkFunction(a1), hProcDll(a2), DllName(a3), FunctionName(a4 ? a4 : ""), OriginalFunction(original)
{
};
bool MatchesFunction(const char* name) const
{
if (!name || IS_INTRESOURCE(name))
return false;
if (!FunctionName.empty())
return !stricmp(FunctionName.c_str(), name);
// Ordinal imports have no name in the BLOB. Resolve the requested name
// against the same DLL and compare the original export, not the IAT
// slot that an earlier hook may already have overwritten.
const auto address = GetProcAddress(hProcDll, name);
return address && address == OriginalFunction;
}
ULONG_PTR* ThunkFunction;
HMODULE hProcDll;
std::string DllName;
std::string FunctionName;
FARPROC OriginalFunction;
}BlobImportEntry_t;

typedef struct BlobModule_s
Expand Down Expand Up @@ -388,12 +402,10 @@ BlobHandle_t LoadBlobFromBuffer(BYTE* pBuffer, DWORD dwBufferSize, PVOID BlobSec
return NULL;
}

if (!bIsLoadByOrdinal)
{
pBlobModule->ImportEntries.emplace_back(&pThunk->u1.AddressOfData, hProcDll, pszDllName, pszProcName);
}

pThunk->u1.AddressOfData = (DWORD)GetProcAddress(hProcDll, pszProcName);
const auto original = GetProcAddress(hProcDll, pszProcName);
pBlobModule->ImportEntries.emplace_back(&pThunk->u1.AddressOfData, hProcDll, pszDllName,
bIsLoadByOrdinal ? NULL : pszProcName, original);
pThunk->u1.AddressOfData = (DWORD)original;

pThunk++;

Expand Down Expand Up @@ -551,7 +563,7 @@ hook_t* MH_BlobIATHook(BlobHandle_t hBlob, const char* pszModuleName, const char

for (const auto& entry : pBlobModule->ImportEntries)
{
if (!stricmp(entry.DllName.c_str(), pszModuleName) && !stricmp(entry.FunctionName.c_str(), pszFuncName))
if (!stricmp(entry.DllName.c_str(), pszModuleName) && entry.MatchesFunction(pszFuncName))
{
return MH_CreateIATHook(NULL, hBlob, pszModuleName, pszFuncName, pNewFuncAddr, pOrginalCall, entry.ThunkFunction);
}
Expand Down Expand Up @@ -581,11 +593,11 @@ bool MH_BlobHasImportEx(BlobHandle_t hBlob, const char* pszModuleName, const cha

for (const auto& entry : pBlobModule->ImportEntries)
{
if (!stricmp(entry.DllName.c_str(), pszModuleName) && !stricmp(entry.FunctionName.c_str(), pszFuncName))
if (!stricmp(entry.DllName.c_str(), pszModuleName) && entry.MatchesFunction(pszFuncName))
{
return true;
}
}

return false;
}
}
10 changes: 10 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ target_include_directories(gamedata_alias_tests PRIVATE
"${METAHOOK_ROOT}/thirdparty/Chocobo1Hash/src")
add_dependencies(gamedata_alias_tests client_orig client_org)
add_test(NAME gamedata_alias COMMAND gamedata_alias_tests)

add_executable(blob_import_tests blob_import_tests.cpp)
target_compile_definitions(blob_import_tests PRIVATE METAHOOK_BLOB_SUPPORT)
target_compile_options(blob_import_tests PRIVATE /wd4996)
target_include_directories(blob_import_tests PRIVATE
"${METAHOOK_ROOT}/include" "${METAHOOK_ROOT}/include/Interface"
"${METAHOOK_ROOT}/include/HLSDK/common" "${METAHOOK_ROOT}/include/HLSDK/cl_dll"
"${METAHOOK_ROOT}/include/HLSDK/engine" "${METAHOOK_ROOT}/include/HLSDK/pm_shared"
"${METAHOOK_ROOT}/include/HLSDK/public")
add_test(NAME blob_imports COMMAND blob_import_tests)
88 changes: 88 additions & 0 deletions tests/blob_import_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Exercise the real BLOB loader with an in-memory fixture and real Win32
// ordinal/name exports. Only the hook allocation sink and unused host services
// are stubbed; this test does not duplicate the loader's matching algorithm.
#include "../src/LoadBlob.cpp"
#include <cstdio>
#include <cstdlib>

#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #x); std::abort(); } } while (0)

IFileSystem* g_pFileSystem = NULL;
IFileSystem_HL25* g_pFileSystem_HL25 = NULL;
PVOID MH_GetSectionByName(PVOID, const char*, ULONG*) { return NULL; }
void* MH_SearchPattern(void*, DWORD, const char*, DWORD) { return NULL; }
void* MH_SearchPatternNoWildCard(void*, DWORD, const char*, DWORD) { return NULL; }
void MH_SysError(const char*, ...) { std::abort(); }
static ULONG_PTR* hookedSlot = NULL;
hook_t* MH_CreateIATHook(HMODULE, BlobHandle_t, const char*, const char*, void* replacement, void** original, ULONG_PTR* slot)
{
hookedSlot = slot;
if (original) *original = (void*)*slot;
*slot = (ULONG_PTR)replacement;
return (hook_t*)slot;
}
static BOOL WINAPI FixtureDllMain(HINSTANCE, DWORD, void*) { return TRUE; }
static void Replacement() {}

int main()
{
const DWORD imageSize = 4096;
auto image = (BYTE*)VirtualAlloc(NULL, imageSize * 2, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
CHECK(image != NULL);
const DWORD importOffset = 256, ordinalThunkOffset = 384, namedThunkOffset = 400;
const DWORD socketNameOffset = 512, kernelNameOffset = 544, sleepNameOffset = 576;
const size_t dataOffset = sizeof(BlobInfo_t) + sizeof(BlobHeader_t) + sizeof(BlobSection_t);
std::vector<BYTE> fixture(dataOffset + imageSize, 0);
auto header = (BlobHeader_t*)(fixture.data() + sizeof(BlobInfo_t));
header->m_dwImageBase = (DWORD)image ^ 0x49C042D1;
header->m_dwImportTable = ((DWORD)image + importOffset) ^ 0x872C3D47;
header->m_dwEntryPoint = (DWORD)FixtureDllMain + 12;
header->m_dwExportPoint = 0x7A32BC85;
// The BLOB format's section count is the last section index.
header->m_wSectionCount = 0;
auto section = (BlobSection_t*)(fixture.data() + sizeof(BlobInfo_t) + sizeof(BlobHeader_t));
section->m_dwVirtualAddress = (DWORD)image;
section->m_dwVirtualSize = imageSize;
section->m_dwDataSize = imageSize;
section->m_dwDataAddress = (DWORD)dataOffset;
auto data = fixture.data() + dataOffset;
auto imports = (IMAGE_IMPORT_DESCRIPTOR*)(data + importOffset);
imports[0].Name = socketNameOffset;
imports[0].FirstThunk = ordinalThunkOffset;
imports[1].Name = kernelNameOffset;
imports[1].FirstThunk = namedThunkOffset;
std::memcpy(data + socketNameOffset, "wsock32.dll", sizeof("wsock32.dll"));
std::memcpy(data + kernelNameOffset, "kernel32.dll", sizeof("kernel32.dll"));
std::memcpy(data + sleepNameOffset + sizeof(WORD), "Sleep", sizeof("Sleep"));
// Winsock's documented ordinal for select, deliberately loaded without a name.
*(DWORD*)(data + ordinalThunkOffset) = IMAGE_ORDINAL_FLAG32 | 18;
*(DWORD*)(data + namedThunkOffset) = sleepNameOffset;
BYTE key = 0x57;
for (size_t i = sizeof(BlobInfo_t); i < fixture.size(); ++i)
{
const BYTE plain = fixture[i];
fixture[i] = plain ^ key;
key += plain + 0x57;
}
auto blob = LoadBlobFromBuffer(fixture.data(), (DWORD)fixture.size(), image, imageSize * 2);
CHECK(blob != NULL);
CHECK(MH_BlobHasImport(blob, "WSOCK32.dll"));
CHECK(MH_BlobHasImportEx(blob, "wsock32.dll", "select"));
CHECK(MH_BlobHasImportEx(blob, "kernel32.dll", "Sleep"));
CHECK(!MH_BlobHasImportEx(blob, "kernel32.dll", "select"));
CHECK(!MH_BlobHasImportEx(blob, "wsock32.dll", "not_an_export"));
void* original = NULL;
CHECK(MH_BlobIATHook(blob, "wsock32.dll", "select", Replacement, &original));
CHECK(hookedSlot == (ULONG_PTR*)(image + ordinalThunkOffset));
CHECK(original == (void*)GetProcAddress(GetModuleHandleA("wsock32.dll"), "select"));
// Matching is based on the retained import, even after its slot is hooked.
CHECK(MH_BlobHasImportEx(blob, "wsock32.dll", "select"));
CHECK(MH_BlobIATHook(blob, "wsock32.dll", "select", original, NULL));
CHECK(*(void**)(image + ordinalThunkOffset) == original);
CHECK(MH_BlobIATHook(blob, "kernel32.dll", "Sleep", Replacement, &original));
CHECK(hookedSlot == (ULONG_PTR*)(image + namedThunkOffset));
CHECK(MH_BlobHasImportEx(blob, "kernel32.dll", "Sleep"));
FreeBlobModule(blob);
CHECK(VirtualFree(image, 0, MEM_RELEASE));
std::puts("PASS real BLOB loader: ordinal/name discovery, hook routing, retained identity and cleanup");
}