From 40ce4d1bb3e792a7c63f7b7529b73afc7bb3f91d Mon Sep 17 00:00:00 2001 From: Kevin Trocolli Date: Wed, 29 Dec 2021 21:58:00 -0500 Subject: [PATCH] added setupapi hook to elisabeth --- hooklib/dll.c | 2 ++ hooklib/setupapi.c | 18 ++++++++++++------ hooklib/setupapi.h | 1 + mercuryhook/elisabeth.c | 34 ++++++++++++---------------------- 4 files changed, 27 insertions(+), 28 deletions(-) diff --git a/hooklib/dll.c b/hooklib/dll.c index 9400bb9..1eab2b7 100644 --- a/hooklib/dll.c +++ b/hooklib/dll.c @@ -284,6 +284,8 @@ static HMODULE WINAPI hook_LoadLibraryW(const wchar_t *name) { HMODULE result; + dprintf("hook_LoadLibraryW: Loading %S\n", name); + if (name == NULL) { SetLastError(ERROR_INVALID_PARAMETER); diff --git a/hooklib/setupapi.c b/hooklib/setupapi.c index eb50323..26a5b53 100644 --- a/hooklib/setupapi.c +++ b/hooklib/setupapi.c @@ -134,22 +134,27 @@ end: return hr; } -static void setupapi_hook_init(void) +static void setupapi_hook_init() { if (setupapi_initted) { return; } - hook_table_apply( - NULL, - "setupapi.dll", - setupapi_syms, - _countof(setupapi_syms)); + setupapi_hook_insert_hooks(NULL); InitializeCriticalSection(&setupapi_lock); setupapi_initted = true; } +void setupapi_hook_insert_hooks(HMODULE target) +{ + hook_table_apply( + target, + "setupapi.dll", + setupapi_syms, + _countof(setupapi_syms)); +} + static HDEVINFO WINAPI my_SetupDiGetClassDevsW( const GUID *ClassGuid, wchar_t *Enumerator, @@ -191,6 +196,7 @@ static BOOL WINAPI my_SetupDiEnumDeviceInterfaces( DWORD MemberIndex, SP_DEVICE_INTERFACE_DATA *DeviceInterfaceData) { + dprintf("my_SetupDiEnumDeviceInterfaces hit!\n"); const struct setupapi_class *class_; size_t i; diff --git a/hooklib/setupapi.h b/hooklib/setupapi.h index a09520c..94eea13 100644 --- a/hooklib/setupapi.h +++ b/hooklib/setupapi.h @@ -5,3 +5,4 @@ #include HRESULT setupapi_add_phantom_dev(const GUID *iface_class, const wchar_t *path); +void setupapi_hook_insert_hooks(HMODULE target); diff --git a/mercuryhook/elisabeth.c b/mercuryhook/elisabeth.c index 82d24d2..8dbc834 100644 --- a/mercuryhook/elisabeth.c +++ b/mercuryhook/elisabeth.c @@ -10,6 +10,7 @@ #include "hooklib/dll.h" #include "hooklib/path.h" +#include "hooklib/setupapi.h" #include "util/dprintf.h" @@ -21,7 +22,7 @@ static HMODULE (WINAPI *next_LoadLibraryW)(const wchar_t *name); static FARPROC WINAPI my_GetProcAddress(HMODULE hModule, const char *name); static FARPROC (WINAPI *next_GetProcAddress)(HMODULE hModule, const char *name); -static const struct hook_symbol elisabeth_hooks[] = { +static const struct hook_symbol win32_hooks[] = { { .name = "LoadLibraryW", .patch = my_LoadLibraryW, @@ -39,14 +40,7 @@ static const wchar_t *target_modules[] = { L"ftd2XX.dll", }; -static const char *target_functions[] = { - "FT_Read", - "FT_Write", - "USBIntLED_Init", -}; - static const size_t target_modules_len = _countof(target_modules); -static const size_t target_functions_len = _countof(target_functions); void elisabeth_hook_init() { @@ -58,8 +52,8 @@ static void dll_hook_insert_hooks(HMODULE target) hook_table_apply( target, "kernel32.dll", - elisabeth_hooks, - _countof(elisabeth_hooks)); + win32_hooks, + _countof(win32_hooks)); } static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name) @@ -71,6 +65,8 @@ static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name) size_t name_len; size_t target_module_len; + dprintf("Elisabeth: Trying to load %S\n", name); + if (name == NULL) { SetLastError(ERROR_INVALID_PARAMETER); @@ -107,7 +103,7 @@ static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name) dprintf("Elisabeth: Loaded %S\n", target_module); dll_hook_insert_hooks(result); - path_hook_insert_hooks(result); + setupapi_hook_insert_hooks(result); } } @@ -116,19 +112,13 @@ static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name) FARPROC WINAPI my_GetProcAddress(HMODULE hModule, const char *name) { - uintptr_t ordinal; + uintptr_t ordinal = (uintptr_t) name; - FARPROC result = next_GetProcAddress(hModule, name); + FARPROC result = next_GetProcAddress(hModule, name); - - for (size_t i = 0; i < target_functions_len; i++) { - ordinal = (uintptr_t) name; - - if (ordinal > 0xFFFF) { - /* Import by name */ - if (strcmp(target_functions[i], name) == 0) - dprintf("Elisabeth: GetProcAddress %s is %p\n", name, result); - } + if (ordinal > 0xFFFF) { + /* Import by name */ + dprintf("Elisabeth: GetProcAddress %s is %p\n", name, result); } return result;