1
0
mirror of https://github.com/FunkyFr3sh/cnc-ddraw.git synced 2025-03-21 08:22:12 +01:00
cnc-ddraw/src/hook.c

318 lines
13 KiB
C
Raw Normal View History

2020-10-13 09:20:52 +02:00
#define WIN32_LEAN_AND_MEAN
2019-03-19 06:57:49 +01:00
#include <windows.h>
#include <stdio.h>
2020-10-13 09:20:52 +02:00
#include "dd.h"
#include "winapi_hooks.h"
2019-03-19 06:57:49 +01:00
#include "hook.h"
2020-10-13 09:20:52 +02:00
#include "dllmain.h"
2019-03-19 06:57:49 +01:00
2019-08-07 12:45:40 +02:00
#ifdef _MSC_VER
#include "detours.h"
#endif
2020-10-13 09:20:52 +02:00
BOOL g_hook_active;
int g_hook_method = 1;
2019-03-19 06:57:49 +01:00
GETCURSORPOSPROC real_GetCursorPos = GetCursorPos;
CLIPCURSORPROC real_ClipCursor = ClipCursor;
SHOWCURSORPROC real_ShowCursor = ShowCursor;
SETCURSORPROC real_SetCursor = SetCursor;
GETWINDOWRECTPROC real_GetWindowRect = GetWindowRect;
GETCLIENTRECTPROC real_GetClientRect = GetClientRect;
CLIENTTOSCREENPROC real_ClientToScreen = ClientToScreen;
SCREENTOCLIENTPROC real_ScreenToClient = ScreenToClient;
SETCURSORPOSPROC real_SetCursorPos = SetCursorPos;
WINDOWFROMPOINTPROC real_WindowFromPoint = WindowFromPoint;
GETCLIPCURSORPROC real_GetClipCursor = GetClipCursor;
GETCURSORINFOPROC real_GetCursorInfo = GetCursorInfo;
GETSYSTEMMETRICSPROC real_GetSystemMetrics = GetSystemMetrics;
SETWINDOWPOSPROC real_SetWindowPos = SetWindowPos;
MOVEWINDOWPROC real_MoveWindow = MoveWindow;
SENDMESSAGEAPROC real_SendMessageA = SendMessageA;
SETWINDOWLONGAPROC real_SetWindowLongA = SetWindowLongA;
2019-08-06 04:37:06 +02:00
ENABLEWINDOWPROC real_EnableWindow = EnableWindow;
CREATEWINDOWEXAPROC real_CreateWindowExA = CreateWindowExA;
DESTROYWINDOWPROC real_DestroyWindow = DestroyWindow;
GETDEVICECAPSPROC real_GetDeviceCaps = GetDeviceCaps;
LOADLIBRARYAPROC real_LoadLibraryA = LoadLibraryA;
LOADLIBRARYWPROC real_LoadLibraryW = LoadLibraryW;
LOADLIBRARYEXAPROC real_LoadLibraryExA = LoadLibraryExA;
2020-09-19 11:23:06 +02:00
LOADLIBRARYEXWPROC real_LoadLibraryExW = LoadLibraryExW;
2019-08-06 04:37:06 +02:00
2019-03-19 06:57:49 +01:00
2020-10-13 09:20:52 +02:00
void hook_patch_iat(HMODULE hmod, char *module_name, char *function_name, PROC new_function)
2019-03-19 06:57:49 +01:00
{
2020-10-13 09:20:52 +02:00
if (!hmod || hmod == INVALID_HANDLE_VALUE || !new_function)
2019-03-19 06:57:49 +01:00
return;
2020-09-22 14:04:49 +02:00
#ifdef _MSC_VER
__try
{
#endif
2020-10-13 09:20:52 +02:00
PIMAGE_DOS_HEADER dos_header = (PIMAGE_DOS_HEADER)hmod;
if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
2020-09-22 14:04:49 +02:00
return;
2019-03-19 06:57:49 +01:00
2020-10-13 09:20:52 +02:00
PIMAGE_NT_HEADERS nt_headers = (PIMAGE_NT_HEADERS)((DWORD)dos_header + (DWORD)dos_header->e_lfanew);
if (nt_headers->Signature != IMAGE_NT_SIGNATURE)
2020-09-22 14:04:49 +02:00
return;
2019-03-19 06:57:49 +01:00
2020-10-13 09:20:52 +02:00
PIMAGE_IMPORT_DESCRIPTOR import_desc = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD)dos_header +
(DWORD)(nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress));
2019-03-19 06:57:49 +01:00
2020-10-13 09:20:52 +02:00
if (import_desc == (PIMAGE_IMPORT_DESCRIPTOR)nt_headers)
2020-09-22 14:04:49 +02:00
return;
2019-03-19 06:57:49 +01:00
2020-10-13 09:20:52 +02:00
while (import_desc->FirstThunk)
2019-03-19 06:57:49 +01:00
{
2020-10-13 09:20:52 +02:00
char* imp_module_name = (char*)((DWORD)dos_header + (DWORD)(import_desc->Name));
2019-03-19 06:57:49 +01:00
2020-10-13 09:20:52 +02:00
if (_stricmp(imp_module_name, module_name) == 0)
2019-03-19 06:57:49 +01:00
{
2020-10-13 09:20:52 +02:00
PIMAGE_THUNK_DATA first_thunk =
(PIMAGE_THUNK_DATA)((DWORD)dos_header + (DWORD)import_desc->FirstThunk);
2019-03-19 06:57:49 +01:00
2020-10-13 09:20:52 +02:00
PIMAGE_THUNK_DATA original_first_thunk =
(PIMAGE_THUNK_DATA)((DWORD)dos_header + (DWORD)import_desc->OriginalFirstThunk);
2020-09-22 14:04:49 +02:00
2020-10-13 09:20:52 +02:00
while (first_thunk->u1.Function && original_first_thunk->u1.AddressOfData)
2019-03-19 06:57:49 +01:00
{
2020-10-13 09:20:52 +02:00
PIMAGE_IMPORT_BY_NAME import =
(PIMAGE_IMPORT_BY_NAME)((DWORD)dos_header + original_first_thunk->u1.AddressOfData);
2019-03-19 06:57:49 +01:00
2020-10-13 09:20:52 +02:00
if ((original_first_thunk->u1.Ordinal & IMAGE_ORDINAL_FLAG) == 0 &&
_stricmp((const char*)import->Name, function_name) == 0)
2019-03-19 06:57:49 +01:00
{
2020-10-13 09:20:52 +02:00
DWORD old_protect;
2020-09-22 14:04:49 +02:00
2020-10-13 09:20:52 +02:00
if (VirtualProtect(&first_thunk->u1.Function, sizeof(DWORD), PAGE_READWRITE, &old_protect))
2020-09-22 14:04:49 +02:00
{
2020-10-13 09:20:52 +02:00
first_thunk->u1.Function = (DWORD)new_function;
VirtualProtect(&first_thunk->u1.Function, sizeof(DWORD), old_protect, &old_protect);
2020-09-22 14:04:49 +02:00
}
break;
2019-03-19 06:57:49 +01:00
}
2020-10-13 09:20:52 +02:00
first_thunk++;
original_first_thunk++;
2019-03-19 06:57:49 +01:00
}
}
2020-10-13 09:20:52 +02:00
import_desc++;
2020-09-22 14:04:49 +02:00
}
#ifdef _MSC_VER
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
2019-03-19 06:57:49 +01:00
}
2020-09-22 14:04:49 +02:00
#endif
2019-03-19 06:57:49 +01:00
}
2020-10-13 09:20:52 +02:00
void hook_create(char *module_name, char *function_name, PROC new_function, PROC *function)
2019-03-19 06:57:49 +01:00
{
2019-08-07 12:45:40 +02:00
#ifdef _MSC_VER
2020-10-13 09:20:52 +02:00
if (g_hook_method == 2)
2019-03-20 05:07:28 +01:00
{
2019-08-07 12:45:40 +02:00
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
2020-10-13 09:20:52 +02:00
DetourAttach((PVOID *)function, (PVOID)new_function);
2019-08-07 12:45:40 +02:00
DetourTransactionCommit();
2019-03-20 05:07:28 +01:00
}
2020-09-19 11:23:06 +02:00
2020-10-13 09:20:52 +02:00
if (g_hook_method == 3 || g_hook_method == 4)
2020-09-19 11:23:06 +02:00
{
2020-10-13 09:20:52 +02:00
WCHAR game_exe_path[MAX_PATH] = { 0 };
WCHAR game_dir[MAX_PATH] = { 0 };
2020-09-19 11:23:06 +02:00
2020-10-13 09:20:52 +02:00
if (GetModuleFileNameW(NULL, game_exe_path, MAX_PATH))
2020-09-19 11:23:06 +02:00
{
2020-10-13 09:20:52 +02:00
_wsplitpath(game_exe_path, NULL, game_dir, NULL, NULL);
2020-09-19 11:23:06 +02:00
2020-10-13 09:20:52 +02:00
WCHAR mod_path[MAX_PATH] = { 0 };
WCHAR mod_dir[MAX_PATH] = { 0 };
HMODULE hmod = NULL;
2020-09-19 11:23:06 +02:00
2020-10-13 09:20:52 +02:00
while (hmod = DetourEnumerateModules(hmod))
2020-09-19 11:23:06 +02:00
{
2020-10-13 09:20:52 +02:00
if (hmod == g_ddraw_module)
2020-09-19 11:23:06 +02:00
continue;
2020-10-13 09:20:52 +02:00
if (GetModuleFileNameW(hmod, mod_path, MAX_PATH))
2020-09-19 11:23:06 +02:00
{
2020-10-13 09:20:52 +02:00
_wsplitpath(mod_path, NULL, mod_dir, NULL, NULL);
2020-09-19 11:23:06 +02:00
2020-10-13 09:20:52 +02:00
if (_wcsnicmp(game_dir, mod_dir, wcslen(game_dir)) == 0)
2020-09-19 11:23:06 +02:00
{
2020-10-13 09:20:52 +02:00
hook_patch_iat(hmod, module_name, function_name, new_function);
2020-09-19 11:23:06 +02:00
}
}
}
}
}
2019-08-07 12:45:40 +02:00
#endif
2019-03-20 05:07:28 +01:00
2020-10-13 09:20:52 +02:00
if (g_hook_method == 1)
2019-08-07 12:59:23 +02:00
{
2020-10-13 09:20:52 +02:00
hook_patch_iat(GetModuleHandle(NULL), module_name, function_name, new_function);
2019-08-07 12:59:23 +02:00
}
2019-03-19 06:57:49 +01:00
}
2020-10-13 09:20:52 +02:00
void hook_revert(char *module_name, char *function_name, PROC new_function, PROC *function)
2019-03-19 06:57:49 +01:00
{
2019-08-07 12:45:40 +02:00
#ifdef _MSC_VER
2020-10-13 09:20:52 +02:00
if (g_hook_method == 2)
2019-03-19 06:57:49 +01:00
{
2019-08-07 12:45:40 +02:00
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
2020-10-13 09:20:52 +02:00
DetourDetach((PVOID *)function, (PVOID)new_function);
2019-08-07 12:45:40 +02:00
DetourTransactionCommit();
2019-03-19 09:43:47 +01:00
}
2020-09-19 11:23:06 +02:00
2020-10-13 09:20:52 +02:00
if (g_hook_method == 3 || g_hook_method == 4)
2020-09-19 11:23:06 +02:00
{
2020-10-13 09:20:52 +02:00
WCHAR game_exe_path[MAX_PATH] = { 0 };
WCHAR game_dir[MAX_PATH] = { 0 };
2020-09-19 11:23:06 +02:00
2020-10-13 09:20:52 +02:00
if (GetModuleFileNameW(NULL, game_exe_path, MAX_PATH))
2020-09-19 11:23:06 +02:00
{
2020-10-13 09:20:52 +02:00
_wsplitpath(game_exe_path, NULL, game_dir, NULL, NULL);
2020-09-19 11:23:06 +02:00
2020-10-13 09:20:52 +02:00
WCHAR mod_path[MAX_PATH] = { 0 };
WCHAR mod_dir[MAX_PATH] = { 0 };
HMODULE hmod = NULL;
2020-09-19 11:23:06 +02:00
2020-10-13 09:20:52 +02:00
while (hmod = DetourEnumerateModules(hmod))
2020-09-19 11:23:06 +02:00
{
2020-10-13 09:20:52 +02:00
if (hmod == g_ddraw_module)
2020-09-19 11:23:06 +02:00
continue;
2020-10-13 09:20:52 +02:00
if (GetModuleFileNameW(hmod, mod_path, MAX_PATH))
2020-09-19 11:23:06 +02:00
{
2020-10-13 09:20:52 +02:00
_wsplitpath(mod_path, NULL, mod_dir, NULL, NULL);
2020-09-19 11:23:06 +02:00
2020-10-13 09:20:52 +02:00
if (_wcsnicmp(game_dir, mod_dir, wcslen(game_dir)) == 0)
2020-09-19 11:23:06 +02:00
{
2020-10-13 09:20:52 +02:00
hook_patch_iat(
hmod,
module_name,
function_name,
GetProcAddress(GetModuleHandle(module_name), function_name));
2020-09-19 11:23:06 +02:00
}
}
}
}
}
2019-08-07 12:45:40 +02:00
#endif
2019-08-06 04:37:06 +02:00
2020-10-13 09:20:52 +02:00
if (g_hook_method == 1)
2019-08-07 12:45:40 +02:00
{
2020-10-13 09:20:52 +02:00
hook_patch_iat(
2019-08-07 12:45:40 +02:00
GetModuleHandle(NULL),
2020-10-13 09:20:52 +02:00
module_name,
function_name,
GetProcAddress(GetModuleHandle(module_name), function_name));
2019-03-19 06:57:49 +01:00
}
}
2020-10-13 09:20:52 +02:00
void hook_init()
2019-03-19 06:57:49 +01:00
{
2020-10-13 09:20:52 +02:00
if (!g_hook_active || g_hook_method == 3 || g_hook_method == 4)
2019-03-19 06:57:49 +01:00
{
2020-09-22 14:04:49 +02:00
#ifdef _MSC_VER
2020-10-13 09:20:52 +02:00
if (!g_hook_active && g_hook_method == 3)
2020-09-19 11:23:06 +02:00
{
2020-09-19 12:05:49 +02:00
FARPROC proc = GetProcAddress(GetModuleHandle("kernelbase.dll"), "LoadLibraryExW");
if (proc)
2020-09-22 14:04:49 +02:00
real_LoadLibraryExW = (LOADLIBRARYEXWPROC)proc;
2020-09-19 12:05:49 +02:00
2020-09-19 11:23:06 +02:00
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)&real_LoadLibraryExW, (PVOID)fake_LoadLibraryExW);
DetourTransactionCommit();
}
2020-09-22 14:04:49 +02:00
#endif
2020-09-19 11:23:06 +02:00
2020-10-13 09:20:52 +02:00
g_hook_active = TRUE;
hook_create("user32.dll", "GetCursorPos", (PROC)fake_GetCursorPos, (PROC *)&real_GetCursorPos);
hook_create("user32.dll", "ClipCursor", (PROC)fake_ClipCursor, (PROC *)&real_ClipCursor);
hook_create("user32.dll", "ShowCursor", (PROC)fake_ShowCursor, (PROC *)&real_ShowCursor);
hook_create("user32.dll", "SetCursor", (PROC)fake_SetCursor, (PROC *)&real_SetCursor);
hook_create("user32.dll", "GetWindowRect", (PROC)fake_GetWindowRect, (PROC *)&real_GetWindowRect);
hook_create("user32.dll", "GetClientRect", (PROC)fake_GetClientRect, (PROC *)&real_GetClientRect);
hook_create("user32.dll", "ClientToScreen", (PROC)fake_ClientToScreen, (PROC *)&real_ClientToScreen);
hook_create("user32.dll", "ScreenToClient", (PROC)fake_ScreenToClient, (PROC *)&real_ScreenToClient);
hook_create("user32.dll", "SetCursorPos", (PROC)fake_SetCursorPos, (PROC *)&real_SetCursorPos);
hook_create("user32.dll", "GetClipCursor", (PROC)fake_GetClipCursor, (PROC *)&real_GetClipCursor);
hook_create("user32.dll", "WindowFromPoint", (PROC)fake_WindowFromPoint, (PROC *)&real_WindowFromPoint);
hook_create("user32.dll", "GetCursorInfo", (PROC)fake_GetCursorInfo, (PROC *)&real_GetCursorInfo);
hook_create("user32.dll", "GetSystemMetrics", (PROC)fake_GetSystemMetrics, (PROC *)&real_GetSystemMetrics);
hook_create("user32.dll", "SetWindowPos", (PROC)fake_SetWindowPos, (PROC *)&real_SetWindowPos);
hook_create("user32.dll", "MoveWindow", (PROC)fake_MoveWindow, (PROC *)&real_MoveWindow);
hook_create("user32.dll", "SendMessageA", (PROC)fake_SendMessageA, (PROC *)&real_SendMessageA);
hook_create("user32.dll", "SetWindowLongA", (PROC)fake_SetWindowLongA, (PROC *)&real_SetWindowLongA);
hook_create("user32.dll", "EnableWindow", (PROC)fake_EnableWindow, (PROC *)&real_EnableWindow);
hook_create("user32.dll", "CreateWindowExA", (PROC)fake_CreateWindowExA, (PROC *)&real_CreateWindowExA);
hook_create("user32.dll", "DestroyWindow", (PROC)fake_DestroyWindow, (PROC *)&real_DestroyWindow);
hook_create("gdi32.dll", "GetDeviceCaps", (PROC)fake_GetDeviceCaps, (PROC*)&real_GetDeviceCaps);
if (g_hook_method == 4)
{
2020-10-13 09:20:52 +02:00
hook_create("kernel32.dll", "LoadLibraryA", (PROC)fake_LoadLibraryA, (PROC*)&real_LoadLibraryA);
hook_create("kernel32.dll", "LoadLibraryW", (PROC)fake_LoadLibraryW, (PROC*)&real_LoadLibraryW);
hook_create("kernel32.dll", "LoadLibraryExA", (PROC)fake_LoadLibraryExA, (PROC*)&real_LoadLibraryExA);
hook_create("kernel32.dll", "LoadLibraryExW", (PROC)fake_LoadLibraryExW, (PROC*)&real_LoadLibraryExW);
}
2019-08-07 12:45:40 +02:00
}
}
2020-10-13 09:20:52 +02:00
void hook_exit()
2019-08-07 12:45:40 +02:00
{
2020-10-13 09:20:52 +02:00
if (g_hook_active)
2019-08-07 12:45:40 +02:00
{
2020-10-13 09:20:52 +02:00
g_hook_active = FALSE;
2019-08-07 12:45:40 +02:00
2020-09-22 14:04:49 +02:00
#ifdef _MSC_VER
2020-10-13 09:20:52 +02:00
if (g_hook_method == 3)
2020-09-19 11:23:06 +02:00
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach((PVOID*)&real_LoadLibraryExW, (PVOID)fake_LoadLibraryExW);
DetourTransactionCommit();
}
2020-09-22 14:04:49 +02:00
#endif
2020-09-19 11:23:06 +02:00
2020-10-13 09:20:52 +02:00
hook_revert("user32.dll", "GetCursorPos", (PROC)fake_GetCursorPos, (PROC *)&real_GetCursorPos);
hook_revert("user32.dll", "ClipCursor", (PROC)fake_ClipCursor, (PROC *)&real_ClipCursor);
hook_revert("user32.dll", "ShowCursor", (PROC)fake_ShowCursor, (PROC *)&real_ShowCursor);
hook_revert("user32.dll", "SetCursor", (PROC)fake_SetCursor, (PROC *)&real_SetCursor);
hook_revert("user32.dll", "GetWindowRect", (PROC)fake_GetWindowRect, (PROC *)&real_GetWindowRect);
hook_revert("user32.dll", "GetClientRect", (PROC)fake_GetClientRect, (PROC *)&real_GetClientRect);
hook_revert("user32.dll", "ClientToScreen", (PROC)fake_ClientToScreen, (PROC *)&real_ClientToScreen);
hook_revert("user32.dll", "ScreenToClient", (PROC)fake_ScreenToClient, (PROC *)&real_ScreenToClient);
hook_revert("user32.dll", "SetCursorPos", (PROC)fake_SetCursorPos, (PROC *)&real_SetCursorPos);
hook_revert("user32.dll", "GetClipCursor", (PROC)fake_GetClipCursor, (PROC *)&real_GetClipCursor);
hook_revert("user32.dll", "WindowFromPoint", (PROC)fake_WindowFromPoint, (PROC *)&real_WindowFromPoint);
hook_revert("user32.dll", "GetCursorInfo", (PROC)fake_GetCursorInfo, (PROC *)&real_GetCursorInfo);
hook_revert("user32.dll", "GetSystemMetrics", (PROC)fake_GetSystemMetrics, (PROC *)&real_GetSystemMetrics);
hook_revert("user32.dll", "SetWindowPos", (PROC)fake_SetWindowPos, (PROC *)&real_SetWindowPos);
hook_revert("user32.dll", "MoveWindow", (PROC)fake_MoveWindow, (PROC *)&real_MoveWindow);
hook_revert("user32.dll", "SendMessageA", (PROC)fake_SendMessageA, (PROC *)&real_SendMessageA);
hook_revert("user32.dll", "SetWindowLongA", (PROC)fake_SetWindowLongA, (PROC *)&real_SetWindowLongA);
hook_revert("user32.dll", "EnableWindow", (PROC)fake_EnableWindow, (PROC *)&real_EnableWindow);
hook_revert("user32.dll", "CreateWindowExA", (PROC)fake_CreateWindowExA, (PROC *)&real_CreateWindowExA);
hook_revert("user32.dll", "DestroyWindow", (PROC)fake_DestroyWindow, (PROC *)&real_DestroyWindow);
hook_revert("gdi32.dll", "GetDeviceCaps", (PROC)fake_GetDeviceCaps, (PROC*)&real_GetDeviceCaps);
if (g_hook_method == 4)
{
2020-10-13 09:20:52 +02:00
hook_revert("kernel32.dll", "LoadLibraryA", (PROC)fake_LoadLibraryA, (PROC*)&real_LoadLibraryA);
hook_revert("kernel32.dll", "LoadLibraryW", (PROC)fake_LoadLibraryW, (PROC*)&real_LoadLibraryW);
hook_revert("kernel32.dll", "LoadLibraryExA", (PROC)fake_LoadLibraryExA, (PROC*)&real_LoadLibraryExA);
hook_revert("kernel32.dll", "LoadLibraryExW", (PROC)fake_LoadLibraryExW, (PROC*)&real_LoadLibraryExW);
}
2019-03-19 06:57:49 +01:00
}
}