2023-09-18 10:05:22 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include "hook/table.h"
|
2023-12-08 19:29:37 +01:00
|
|
|
#include "hook/iohook.h"
|
2023-09-18 10:05:22 +02:00
|
|
|
|
|
|
|
#include "hooklib/dll.h"
|
|
|
|
#include "hooklib/path.h"
|
2023-12-08 19:29:37 +01:00
|
|
|
#include "hooklib/serial.h"
|
2023-12-11 00:48:47 +01:00
|
|
|
#include "hooklib/reg.h"
|
2024-01-10 02:21:51 +01:00
|
|
|
#include "hook/procaddr.h"
|
2023-12-08 19:29:37 +01:00
|
|
|
|
2023-09-18 10:05:22 +02:00
|
|
|
#include "amcus/amcus.h"
|
|
|
|
#include "board/usio.h"
|
2023-12-11 00:48:47 +01:00
|
|
|
#include "platform/epay.h"
|
2024-08-20 16:16:18 +02:00
|
|
|
#include "platform/clock.h"
|
2023-09-18 10:05:22 +02:00
|
|
|
|
|
|
|
#include "util/dprintf.h"
|
|
|
|
|
|
|
|
static void dll_hook_insert_hooks(HMODULE target);
|
|
|
|
|
|
|
|
static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name);
|
|
|
|
static HMODULE (WINAPI *next_LoadLibraryW)(const wchar_t *name);
|
2024-04-23 03:11:03 +02:00
|
|
|
static HMODULE WINAPI my_LoadLibraryExW(const wchar_t *name, HANDLE hFile, DWORD dwFlags);
|
|
|
|
static HMODULE (WINAPI *next_LoadLibraryExW)(const wchar_t *name, HANDLE hFile, DWORD dwFlags);
|
2023-09-18 10:05:22 +02:00
|
|
|
|
|
|
|
static const struct hook_symbol unity_kernel32_syms[] = {
|
|
|
|
{
|
|
|
|
.name = "LoadLibraryW",
|
|
|
|
.patch = my_LoadLibraryW,
|
|
|
|
.link = (void **) &next_LoadLibraryW,
|
2024-04-23 03:11:03 +02:00
|
|
|
},{
|
|
|
|
.name = "LoadLibraryExW",
|
|
|
|
.patch = my_LoadLibraryExW,
|
|
|
|
.link = (void **) &next_LoadLibraryExW,
|
|
|
|
}
|
2023-09-18 10:05:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const wchar_t *target_modules[] = {
|
|
|
|
L"mono.dll",
|
|
|
|
L"cri_ware_unity.dll",
|
|
|
|
L"bnAMPF.dll",
|
|
|
|
L"bnReader.dll",
|
|
|
|
};
|
2023-12-11 00:48:47 +01:00
|
|
|
|
|
|
|
static const wchar_t *dep_hooks[] = {
|
|
|
|
L"libamw.dll",
|
|
|
|
};
|
|
|
|
|
2023-09-18 10:05:22 +02:00
|
|
|
static const size_t target_modules_len = _countof(target_modules);
|
2023-12-11 00:48:47 +01:00
|
|
|
static const size_t dep_hooks_len = _countof(dep_hooks);
|
2023-09-18 10:05:22 +02:00
|
|
|
|
|
|
|
void unity_hook_init(void)
|
|
|
|
{
|
|
|
|
dll_hook_insert_hooks(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dll_hook_insert_hooks(HMODULE target)
|
|
|
|
{
|
|
|
|
hook_table_apply(
|
|
|
|
target,
|
|
|
|
"kernel32.dll",
|
|
|
|
unity_kernel32_syms,
|
|
|
|
_countof(unity_kernel32_syms));
|
|
|
|
}
|
|
|
|
|
2024-04-23 03:11:03 +02:00
|
|
|
static HMODULE WINAPI my_LoadLibraryExW(const wchar_t *name, HANDLE hFile, DWORD dwFlags)
|
|
|
|
{
|
|
|
|
//dprintf("Unity: LoadLibraryExW %ls\n", name);
|
|
|
|
return my_LoadLibraryW(name);
|
|
|
|
}
|
|
|
|
|
2023-09-18 10:05:22 +02:00
|
|
|
static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name)
|
|
|
|
{
|
|
|
|
const wchar_t *name_end;
|
|
|
|
const wchar_t *target_module;
|
2023-12-11 00:48:47 +01:00
|
|
|
const wchar_t *target_dep;
|
2023-09-18 10:05:22 +02:00
|
|
|
bool already_loaded;
|
|
|
|
HMODULE result;
|
2023-12-11 00:48:47 +01:00
|
|
|
HMODULE dep_mod;
|
2023-09-18 10:05:22 +02:00
|
|
|
size_t name_len;
|
|
|
|
size_t target_module_len;
|
2023-12-11 00:48:47 +01:00
|
|
|
size_t dep_len;
|
2023-09-18 10:05:22 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
amcus_insert_hooks(result);
|
2023-12-11 00:48:47 +01:00
|
|
|
reg_hook_apply_hooks(result);
|
2024-08-20 16:16:18 +02:00
|
|
|
clock_hook_insert_hooks(result);
|
2023-09-18 10:05:22 +02:00
|
|
|
usio_hook_proc_addr(result);
|
2023-12-11 00:48:47 +01:00
|
|
|
proc_addr_insert_hooks(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < dep_hooks_len; i++) {
|
|
|
|
target_dep = dep_hooks[i];
|
|
|
|
|
|
|
|
dep_mod = GetModuleHandleW(target_dep);
|
|
|
|
if (dep_mod != NULL) {
|
|
|
|
dprintf("Unity: Hook dependency %ls\n", target_dep);
|
|
|
|
iohook_apply_hooks(dep_mod);
|
|
|
|
serial_hook_apply_hooks(dep_mod);
|
|
|
|
reg_hook_apply_hooks(dep_mod);
|
|
|
|
epay_insert_hook(dep_mod);
|
2024-08-20 16:16:18 +02:00
|
|
|
clock_hook_insert_hooks(dep_mod);
|
2023-12-11 00:48:47 +01:00
|
|
|
}
|
2023-09-18 10:05:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|