2024-04-15 19:30:28 +00:00
|
|
|
#include <assert.h>
|
2023-07-13 23:00:28 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include "hook/table.h"
|
2024-05-12 17:36:08 +00:00
|
|
|
#include "hook/procaddr.h"
|
|
|
|
#include "hook/iohook.h"
|
|
|
|
|
|
|
|
#include "hooklib/dll.h"
|
2023-07-13 23:00:28 +00:00
|
|
|
#include "hooklib/path.h"
|
2024-05-12 17:36:08 +00:00
|
|
|
#include "hooklib/printer.h"
|
|
|
|
#include "hooklib/reg.h"
|
|
|
|
#include "hooklib/touch.h"
|
|
|
|
#include "hooklib/serial.h"
|
|
|
|
|
2023-07-13 23:00:28 +00:00
|
|
|
#include "util/dprintf.h"
|
|
|
|
|
2024-04-15 19:30:28 +00:00
|
|
|
#include "doorstop.h"
|
|
|
|
#include "hook.h"
|
|
|
|
|
|
|
|
static bool unity_hook_initted;
|
|
|
|
static struct unity_config unity_config;
|
|
|
|
|
|
|
|
static const wchar_t *target_modules[] = {
|
|
|
|
L"mono.dll",
|
|
|
|
L"mono-2.0-bdwgc.dll",
|
|
|
|
L"cri_ware_unity.dll",
|
2024-06-08 22:50:54 +00:00
|
|
|
L"amdaemon_api.dll",
|
2024-05-12 17:36:08 +00:00
|
|
|
L"SerialPortAPI.dll",
|
|
|
|
L"C300usb.dll",
|
|
|
|
L"C300FWDLusb.dll",
|
|
|
|
L"apmled.dll",
|
2024-06-08 22:50:54 +00:00
|
|
|
L"HKBSys_api.dll",
|
|
|
|
L"amptw.dll"
|
2024-04-15 19:30:28 +00:00
|
|
|
};
|
2024-05-12 17:36:08 +00:00
|
|
|
|
2024-04-15 19:30:28 +00:00
|
|
|
static const size_t target_modules_len = _countof(target_modules);
|
|
|
|
|
2023-07-13 23:00:28 +00:00
|
|
|
static void dll_hook_insert_hooks(HMODULE target);
|
|
|
|
|
2024-05-12 17:36:08 +00:00
|
|
|
static HMODULE WINAPI hook_LoadLibraryW(const wchar_t *name);
|
2023-07-13 23:00:28 +00:00
|
|
|
static HMODULE (WINAPI *next_LoadLibraryW)(const wchar_t *name);
|
2024-05-12 17:36:08 +00:00
|
|
|
static HMODULE WINAPI hook_LoadLibraryExW(const wchar_t *name, HANDLE hFile, DWORD dwFlags);
|
|
|
|
static HMODULE (WINAPI *next_LoadLibraryExW)(const wchar_t *name, HANDLE hFile, DWORD dwFlags);
|
2023-07-13 23:00:28 +00:00
|
|
|
|
|
|
|
static const struct hook_symbol unity_kernel32_syms[] = {
|
|
|
|
{
|
|
|
|
.name = "LoadLibraryW",
|
2024-05-12 17:36:08 +00:00
|
|
|
.patch = hook_LoadLibraryW,
|
2023-07-13 23:00:28 +00:00
|
|
|
.link = (void **) &next_LoadLibraryW,
|
2024-05-12 17:36:08 +00:00
|
|
|
}, {
|
|
|
|
.name = "LoadLibraryExW",
|
|
|
|
.patch = hook_LoadLibraryExW,
|
|
|
|
.link = (void **) &next_LoadLibraryExW,
|
|
|
|
}
|
2023-07-13 23:00:28 +00:00
|
|
|
};
|
|
|
|
|
2024-05-12 17:36:08 +00:00
|
|
|
|
2024-04-15 19:30:28 +00:00
|
|
|
void unity_hook_init(const struct unity_config *cfg, HINSTANCE self) {
|
|
|
|
assert(cfg != NULL);
|
|
|
|
|
|
|
|
if (!cfg->enable) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-13 23:00:28 +00:00
|
|
|
|
2024-04-15 19:30:28 +00:00
|
|
|
if (unity_hook_initted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&unity_config, cfg, sizeof(*cfg));
|
2023-07-13 23:00:28 +00:00
|
|
|
dll_hook_insert_hooks(NULL);
|
2024-04-15 19:30:28 +00:00
|
|
|
|
|
|
|
unity_hook_initted = true;
|
|
|
|
dprintf("Unity: Hook enabled.\n");
|
2023-07-13 23:00:28 +00:00
|
|
|
}
|
|
|
|
|
2024-04-15 19:30:28 +00:00
|
|
|
static void dll_hook_insert_hooks(HMODULE target) {
|
2023-07-13 23:00:28 +00:00
|
|
|
hook_table_apply(
|
2024-04-15 19:30:28 +00:00
|
|
|
target,
|
|
|
|
"kernel32.dll",
|
|
|
|
unity_kernel32_syms,
|
|
|
|
_countof(unity_kernel32_syms));
|
2023-07-13 23:00:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-12 17:36:08 +00:00
|
|
|
static HMODULE WINAPI hook_LoadLibraryExW(const wchar_t *name, HANDLE hFile, DWORD dwFlags)
|
|
|
|
{
|
|
|
|
// dprintf("Unity: LoadLibraryExW %ls\n", name);
|
|
|
|
return hook_LoadLibraryW(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HMODULE WINAPI hook_LoadLibraryW(const wchar_t *name)
|
|
|
|
{
|
2023-07-13 23:00:28 +00:00
|
|
|
const wchar_t *name_end;
|
|
|
|
const wchar_t *target_module;
|
|
|
|
bool already_loaded;
|
|
|
|
HMODULE result;
|
|
|
|
size_t name_len;
|
|
|
|
size_t target_module_len;
|
|
|
|
|
|
|
|
if (name == NULL) {
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the module is already loaded
|
|
|
|
already_loaded = GetModuleHandleW(name) != NULL;
|
|
|
|
|
|
|
|
// Must call the next handler so the DLL reference count is incremented
|
|
|
|
result = next_LoadLibraryW(name);
|
|
|
|
|
|
|
|
if (!already_loaded && result != NULL) {
|
|
|
|
name_len = wcslen(name);
|
|
|
|
|
2024-04-15 19:30:28 +00:00
|
|
|
// mono entrypoint for injecting target_assembly
|
|
|
|
if (GetProcAddress(result, "mono_jit_init_version")) {
|
|
|
|
doorstop_mono_hook_init(&unity_config, result);
|
|
|
|
}
|
|
|
|
|
2023-07-13 23:00:28 +00:00
|
|
|
for (size_t i = 0; i < target_modules_len; i++) {
|
|
|
|
target_module = target_modules[i];
|
|
|
|
target_module_len = wcslen(target_module);
|
|
|
|
|
|
|
|
// Check if the newly loaded library is at least the length of
|
|
|
|
// the name of the target module
|
|
|
|
if (name_len < target_module_len) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
name_end = &name[name_len - target_module_len];
|
|
|
|
|
|
|
|
// Check if the name of the newly loaded library is one of the
|
|
|
|
// modules the path hooks should be injected into
|
|
|
|
if (_wcsicmp(name_end, target_module) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
dprintf("Unity: Loaded %S\n", target_module);
|
|
|
|
|
|
|
|
dll_hook_insert_hooks(result);
|
|
|
|
path_hook_insert_hooks(result);
|
2024-05-12 17:36:08 +00:00
|
|
|
|
|
|
|
reg_hook_insert_hooks(result);
|
|
|
|
proc_addr_insert_hooks(result);
|
2024-08-05 18:49:47 +00:00
|
|
|
|
|
|
|
// Not needed?
|
|
|
|
// serial_hook_apply_hooks(result);
|
|
|
|
// Unity will crash during option loading when we hook this twice
|
|
|
|
// iohook_apply_hooks(result);
|
2023-07-13 23:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|