From 23fee5fd8d6f47e63c1393b7533ee3bd66207b33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anil=20D=C3=B6kmetas?= <111908128+JohnnyTheCoder1@users.noreply.github.com> Date: Fri, 24 Oct 2025 17:29:17 +0000 Subject: [PATCH 01/12] Alien Isolation: startup hardening, logging guard, safe hooks, and Windows CI workflow\n\n- Add early, idempotent logging and SEH guard around RunCT\n- Validate offsets/pointers and wait for DX11 device/swapchain\n- Guard glow patch and timescale writes to module range\n- Validate hook target addresses; guard Present VTable hook\n- Add IsPtrReadable/IsAddressInModule helpers\n- Link Psapi for GetModuleInformation\n- Add GitHub Actions workflow to build Release (Win32/x64) and upload artifacts --- .github/workflows/windows-build.yml | 52 +++++++++++++ Alien Isolation/DllMain.cpp | 17 +++- Alien Isolation/Main.cpp | 116 ++++++++++++++++++++++++---- Alien Isolation/Util/Hooks.cpp | 37 +++++++-- Alien Isolation/Util/Log.cpp | 1 + Alien Isolation/Util/Util.cpp | 42 ++++++++++ Alien Isolation/Util/Util.h | 6 ++ 7 files changed, 246 insertions(+), 25 deletions(-) create mode 100644 .github/workflows/windows-build.yml diff --git a/.github/workflows/windows-build.yml b/.github/workflows/windows-build.yml new file mode 100644 index 0000000..3c0918f --- /dev/null +++ b/.github/workflows/windows-build.yml @@ -0,0 +1,52 @@ +name: Build CT_AlienIsolation (Windows) + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + workflow_dispatch: + +jobs: + build: + runs-on: windows-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup MSBuild + uses: microsoft/setup-msbuild@v2 + + - name: Setup NuGet + uses: NuGet/setup-nuget@v2 + + - name: Restore NuGet packages + run: nuget restore "Alien Isolation/CT_AlienIsolation.sln" + + - name: Build Win32 Release + run: msbuild "Alien Isolation/CT_AlienIsolation.sln" /t:Build /p:Configuration=Release /p:Platform=Win32 /m + + - name: Build x64 Release + run: msbuild "Alien Isolation/CT_AlienIsolation.sln" /t:Build /p:Configuration=Release /p:Platform=x64 /m + + - name: Collect artifacts + shell: pwsh + run: | + New-Item -ItemType Directory -Force -Path artifacts | Out-Null + $patterns = @( + 'Build/*.dll', + 'Build/*.pdb', + 'Alien Isolation/**/Release/*.dll', + 'Alien Isolation/**/Release/*.pdb', + 'Alien Isolation/**/x64/Release/*.dll', + 'Alien Isolation/**/x64/Release/*.pdb' + ) + foreach ($p in $patterns) { Get-ChildItem -Path $p -ErrorAction SilentlyContinue | Copy-Item -Destination artifacts -Force } + Get-ChildItem artifacts | Format-Table -AutoSize + + - name: Upload artifacts + uses: actions/upload-artifact@v4 + with: + name: CT_AlienIsolation-binaries + path: artifacts + if-no-files-found: warn diff --git a/Alien Isolation/DllMain.cpp b/Alien Isolation/DllMain.cpp index 5a172f7..0439ece 100644 --- a/Alien Isolation/DllMain.cpp +++ b/Alien Isolation/DllMain.cpp @@ -1,11 +1,22 @@ #include "Main.h" +#include "Util/Util.h" #include DWORD WINAPI RunCT(LPVOID arg) { - g_mainHandle = new Main(); - if (g_mainHandle->Initialize()) - g_mainHandle->Run(); + util::log::Init(); + util::log::Write("CT_AlienIsolation injected. Spawning main loop..."); + + __try + { + g_mainHandle = new Main(); + if (g_mainHandle->Initialize()) + g_mainHandle->Run(); + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + util::log::Error("Unhandled exception in RunCT thread. Exception code=0x%X", GetExceptionCode()); + } delete g_mainHandle; diff --git a/Alien Isolation/Main.cpp b/Alien Isolation/Main.cpp index 96fc602..0fec6da 100644 --- a/Alien Isolation/Main.cpp +++ b/Alien Isolation/Main.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#pragma comment(lib, "Psapi.lib") static const char* g_gameName = "Alien: Isolation"; static const char* g_moduleName = "AI.exe"; @@ -62,7 +64,17 @@ bool Main::Initialize() util::log::Write("Cinematic Tools for %s\n", g_gameName); // Needed for ImGui + other functionality - g_gameHwnd = FindWindowA(g_className, NULL); + // Window and module may not be ready immediately after injection; wait up to a few seconds + { + const int maxTries = 200; // ~10 seconds + int tries = 0; + while (tries++ < maxTries) + { + g_gameHwnd = FindWindowA(g_className, NULL); + if (g_gameHwnd) break; + Sleep(50); + } + } if (g_gameHwnd == NULL) { util::log::Error("Failed to retrieve window handle, GetLastError 0x%X", GetLastError()); @@ -70,33 +82,105 @@ bool Main::Initialize() } // Used for relative offsets - g_gameHandle = GetModuleHandleA(g_moduleName); + { + const int maxTries = 200; // ~10 seconds + int tries = 0; + while (tries++ < maxTries) + { + g_gameHandle = GetModuleHandleA(g_moduleName); + if (g_gameHandle) break; + Sleep(50); + } + } if (g_gameHandle == NULL) { util::log::Error("Failed to retrieve module handle, GetLastError 0x%X", GetLastError()); return false; } - g_dxgiSwapChain = CATHODE::D3D::Singleton()->m_pSwapChain;//fb::DxRenderer::Singleton()->m_pScreen->m_pSwapChain; // Fetch SwapChain - g_d3d11Device = CATHODE::D3D::Singleton()->m_pDevice;// fb::DxRenderer::Singleton()->m_pDevice; // Fetch ID3D11Device - if (g_d3d11Device) - g_d3d11Device->GetImmediateContext(&g_d3d11Context); - - if (!g_d3d11Context || !g_d3d11Device || !g_dxgiSwapChain) + // Safely resolve D3D device/swapchain from game's singleton { - util::log::Error("Failed to retrieve Dx11 interfaces"); - util::log::Error("Device 0x%X DeviceContext 0x%X SwapChain 0x%X", g_d3d11Device, g_d3d11Context, g_dxgiSwapChain); - return false; + MODULEINFO modInfo{ 0 }; + if (!GetModuleInformation(GetCurrentProcess(), g_gameHandle, &modInfo, sizeof(modInfo))) + util::log::Warning("GetModuleInformation failed, GetLastError 0x%X", GetLastError()); + + int d3dSingletonAddr = util::offsets::GetOffset("OFFSET_D3D"); + if (!util::IsAddressInModule(g_gameHandle, (void*)d3dSingletonAddr, sizeof(void*))) + { + util::log::Error("OFFSET_D3D (0x%X) is outside module image. Likely version mismatch.", d3dSingletonAddr); + return false; + } + + CATHODE::D3D* pD3D = nullptr; + __try + { + pD3D = *(CATHODE::D3D**)d3dSingletonAddr; + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + util::log::Error("Access violation while reading D3D singleton at 0x%X. Offsets likely outdated.", d3dSingletonAddr); + return false; + } + + if (!util::IsPtrReadable(pD3D, sizeof(void*))) + { + util::log::Error("D3D singleton pointer is not readable: 0x%p", pD3D); + return false; + } + + // Wait until the device/swapchain are initialized by the game (up to ~10s) + const int maxTries = 200; + int tries = 0; + while (tries++ < maxTries) + { + g_d3d11Device = pD3D->m_pDevice; + g_dxgiSwapChain = pD3D->m_pSwapChain; + if (g_d3d11Device && g_dxgiSwapChain) + break; + Sleep(50); + } + + if (g_d3d11Device) + g_d3d11Device->GetImmediateContext(&g_d3d11Context); + + if (!g_d3d11Context || !g_d3d11Device || !g_dxgiSwapChain) + { + util::log::Error("Failed to retrieve Dx11 interfaces"); + util::log::Error("Device 0x%X DeviceContext 0x%X SwapChain 0x%X", g_d3d11Device, g_d3d11Context, g_dxgiSwapChain); + return false; + } } // This disables the object glow thing - BYTE GlowPatch[7] = { 0x80, 0xB9, 0x65, 0x70, 0x02, 0x00, 0x01 }; - util::WriteMemory((int)g_gameHandle + 0x3A3494, GlowPatch, 7); + // Apply small byte patch, but only if target lies within module image + { + BYTE GlowPatch[7] = { 0x80, 0xB9, 0x65, 0x70, 0x02, 0x00, 0x01 }; + void* patchAddr = (void*)((int)g_gameHandle + 0x3A3494); + if (util::IsAddressInModule(g_gameHandle, patchAddr, sizeof(GlowPatch))) + { + if (!util::WriteMemory((DWORD_PTR)patchAddr, GlowPatch, (DWORD)sizeof(GlowPatch))) + util::log::Warning("Glow patch VirtualProtect/WriteMemory failed at %p", patchAddr); + } + else + { + util::log::Warning("Skipping glow patch: address %p outside module image (possible version mismatch)", patchAddr); + } + } // Make timescale writable - DWORD dwOld = 0; - if (!VirtualProtect(reinterpret_cast(util::offsets::GetOffset("OFFSET_TIMESCALE")), sizeof(double), PAGE_READWRITE, &dwOld)) - util::log::Warning("Could not get write permissions to timescale"); + { + int tsAddr = util::offsets::GetOffset("OFFSET_TIMESCALE"); + if (util::IsAddressInModule(g_gameHandle, (void*)tsAddr, sizeof(double))) + { + DWORD dwOld = 0; + if (!VirtualProtect(reinterpret_cast(tsAddr), sizeof(double), PAGE_READWRITE, &dwOld)) + util::log::Warning("Could not get write permissions to timescale (addr 0x%X)", tsAddr); + } + else + { + util::log::Warning("Skipping timescale protect: address 0x%X outside module image", tsAddr); + } + } // Retrieve game version and make a const variable for whatever version // the tools support. If versions mismatch, scan for offsets. diff --git a/Alien Isolation/Util/Hooks.cpp b/Alien Isolation/Util/Hooks.cpp index 6c1a11f..d37facd 100644 --- a/Alien Isolation/Util/Hooks.cpp +++ b/Alien Isolation/Util/Hooks.cpp @@ -221,16 +221,41 @@ void util::hooks::Init() if (status != MH_OK) util::log::Error("Failed to initialize MinHook, MH_STATUS 0x%X", status); - CreateHook("CameraUpdate", util::offsets::GetOffset("OFFSET_CAMERAUPDATE"), hCameraUpdate, &oCameraUpdate); - //CreateHook("InputUpdate", util::offsets::GetOffset("OFFSET_INPUTUPDATE"), hInputUpdate, &oInputUpdate); - CreateHook("GamepadUpdate", util::offsets::GetOffset("OFFSET_GAMEPADUPDATE"), hGamepadUpdate, &oGamepadUpdate); - CreateHook("PostProcessUpdate", util::offsets::GetOffset("OFFSET_POSTPROCESSUPDATE"), hPostProcessUpdate, &oPostProcessUpdate); - CreateHook("TonemapUpdate", util::offsets::GetOffset("OFFSET_TONEMAPUPDATE"), hTonemapSettings, &oTonemapUpdate); + auto safeCreate = [](const char* name, const char* key, auto hook, auto original) + { + int addr = util::offsets::GetOffset(key); + if (!util::IsAddressInModule(g_gameHandle, (void*)addr, 16)) + { + util::log::Warning("Skipping hook %s: address 0x%X outside module image", name, addr); + return; + } + CreateHook(name, addr, hook, original); + }; + + safeCreate("CameraUpdate", "OFFSET_CAMERAUPDATE", hCameraUpdate, &oCameraUpdate); + //safeCreate("InputUpdate", "OFFSET_INPUTUPDATE", hInputUpdate, &oInputUpdate); + safeCreate("GamepadUpdate", "OFFSET_GAMEPADUPDATE", hGamepadUpdate, &oGamepadUpdate); + safeCreate("PostProcessUpdate", "OFFSET_POSTPROCESSUPDATE", hPostProcessUpdate, &oPostProcessUpdate); + safeCreate("TonemapUpdate", "OFFSET_TONEMAPUPDATE", hTonemapSettings, &oTonemapUpdate); //CreateHook("AICombatManagerUpdate", util::offsets::GetOffset("OFFSET_COMBATMANAGERUPDATE"), hCombatManagerUpdate, &oCombatManagerUpdate); CreateHook("SetCursorPos", (int)GetProcAddress(GetModuleHandleA("user32.dll"), "SetCursorPos"), hSetCursorPos, &oSetCursorPos); - CreateVTableHook("SwapChainPresent", (PDWORD*)g_dxgiSwapChain, hIDXGISwapChain_Present, 8, &oIDXGISwapChain_Present); + if (g_dxgiSwapChain) + { + __try + { + CreateVTableHook("SwapChainPresent", (PDWORD*)g_dxgiSwapChain, hIDXGISwapChain_Present, 8, &oIDXGISwapChain_Present); + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + util::log::Error("Access violation while creating VTable hook on SwapChain. Skipping Present hook."); + } + } + else + { + util::log::Warning("SwapChain is null; skipping Present VTable hook."); + } } // In some cases it's useful or even required to disable all hooks or just certain ones diff --git a/Alien Isolation/Util/Log.cpp b/Alien Isolation/Util/Log.cpp index dbab5e8..f455bc3 100644 --- a/Alien Isolation/Util/Log.cpp +++ b/Alien Isolation/Util/Log.cpp @@ -40,6 +40,7 @@ namespace void log::Init() { + if (pfileout) return; // already initialized AllocConsole(); freopen_s(&pfstdout, "CONOUT$", "w", stdout); freopen_s(&pfstdin, "CONIN$", "r", stdin); diff --git a/Alien Isolation/Util/Util.cpp b/Alien Isolation/Util/Util.cpp index 5d58f0e..78b9580 100644 --- a/Alien Isolation/Util/Util.cpp +++ b/Alien Isolation/Util/Util.cpp @@ -3,6 +3,17 @@ #include #include +#include +#pragma comment(lib, "Psapi.lib") + +namespace { + // Helper to check if a memory page has readable protections + static bool IsReadableProtection(DWORD prot) + { + return (prot & PAGE_READONLY) || (prot & PAGE_READWRITE) || (prot & PAGE_WRITECOPY) || + (prot & PAGE_EXECUTE_READ) || (prot & PAGE_EXECUTE_READWRITE) || (prot & PAGE_EXECUTE_WRITECOPY); + } +} // Loads resource data from the .dll file based on resource IDs in resource.h bool util::GetResource(int ID, void* &pData, DWORD& size) @@ -127,3 +138,34 @@ BOOL util::WriteMemory(DWORD_PTR dwAddress, const void* cpvPatch, DWORD dwSize) return VirtualProtect((void*)dwAddress, dwSize, dwProtect, new DWORD); //Reprotect the memory } +bool util::IsPtrReadable(const void* ptr, size_t bytes) +{ + if (!ptr) return false; + MEMORY_BASIC_INFORMATION mbi{ 0 }; + if (!VirtualQuery(ptr, &mbi, sizeof(mbi))) + return false; + if (mbi.State != MEM_COMMIT) + return false; + if (!IsReadableProtection(mbi.Protect)) + return false; + // Ensure the requested size fits in this region + const uintptr_t begin = reinterpret_cast(ptr); + const uintptr_t end = begin + bytes; + const uintptr_t regionEnd = reinterpret_cast(mbi.BaseAddress) + mbi.RegionSize; + return end <= regionEnd; +} + +bool util::IsAddressInModule(HMODULE hModule, const void* addr, size_t size) +{ + if (!hModule || !addr) return false; + MODULEINFO info{ 0 }; + if (!GetModuleInformation(GetCurrentProcess(), hModule, &info, sizeof(info))) + return false; + + const uintptr_t base = reinterpret_cast(info.lpBaseOfDll); + const uintptr_t end = base + info.SizeOfImage; + const uintptr_t a = reinterpret_cast(addr); + const uintptr_t b = a + size; + return (a >= base) && (b <= end); +} + diff --git a/Alien Isolation/Util/Util.h b/Alien Isolation/Util/Util.h index 5b9133f..6bd0d41 100644 --- a/Alien Isolation/Util/Util.h +++ b/Alien Isolation/Util/Util.h @@ -68,6 +68,12 @@ namespace util BOOL WriteMemory(DWORD_PTR, const void*, DWORD); + // Memory safety helpers + // Returns true if the pointer appears to be readable for at least `bytes` bytes. + bool IsPtrReadable(const void* ptr, size_t bytes = 1); + // Returns true if [addr, addr+size) lies within the specified module's image range. + bool IsAddressInModule(HMODULE hModule, const void* addr, size_t size); + namespace math { float CatmullRomInterpolate(float y0, float y1, float y2, float y3, float mu); From 96b833cd6f14073c0647b3d029411e2ea03828fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anil=20D=C3=B6kmetas?= <111908128+JohnnyTheCoder1@users.noreply.github.com> Date: Fri, 24 Oct 2025 17:40:41 +0000 Subject: [PATCH 02/12] CI: build vcxproj directly (fix invalid Release|Win32 solution config); add x64 build via project file --- .github/workflows/windows-build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/windows-build.yml b/.github/workflows/windows-build.yml index 3c0918f..b3f53e9 100644 --- a/.github/workflows/windows-build.yml +++ b/.github/workflows/windows-build.yml @@ -24,10 +24,10 @@ jobs: run: nuget restore "Alien Isolation/CT_AlienIsolation.sln" - name: Build Win32 Release - run: msbuild "Alien Isolation/CT_AlienIsolation.sln" /t:Build /p:Configuration=Release /p:Platform=Win32 /m + run: msbuild "Alien Isolation/CT_AlienIsolation.vcxproj" /t:Build /p:Configuration=Release /p:Platform=Win32 /m - name: Build x64 Release - run: msbuild "Alien Isolation/CT_AlienIsolation.sln" /t:Build /p:Configuration=Release /p:Platform=x64 /m + run: msbuild "Alien Isolation/CT_AlienIsolation.vcxproj" /t:Build /p:Configuration=Release /p:Platform=x64 /m - name: Collect artifacts shell: pwsh From 6314f7541cd156e2e146e85605e442140e7b556d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anil=20D=C3=B6kmetas?= <111908128+JohnnyTheCoder1@users.noreply.github.com> Date: Fri, 24 Oct 2025 17:49:39 +0000 Subject: [PATCH 03/12] Retarget to Windows 10 SDK (default) and MSVC v143 toolset for CI compatibility --- Alien Isolation/CT_AlienIsolation.vcxproj | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Alien Isolation/CT_AlienIsolation.vcxproj b/Alien Isolation/CT_AlienIsolation.vcxproj index aea247e..91477a0 100644 --- a/Alien Isolation/CT_AlienIsolation.vcxproj +++ b/Alien Isolation/CT_AlienIsolation.vcxproj @@ -22,32 +22,32 @@ 15.0 {A83381F5-6D18-43C8-8B95-030F3B2BE6C5} CTAlienIsolation - 10.0.18362.0 + 10.0 DynamicLibrary true - v140 + v143 MultiByte DynamicLibrary false - v140 + v143 true MultiByte DynamicLibrary true - v141 + v143 MultiByte DynamicLibrary false - v141 + v143 true MultiByte From d1376b9b308ed3138d1214aaae39ba79d0c38ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anil=20D=C3=B6kmetas?= <111908128+JohnnyTheCoder1@users.noreply.github.com> Date: Fri, 24 Oct 2025 17:55:53 +0000 Subject: [PATCH 04/12] Fix CI build: remove SEH __try blocks (C2712) and replace with pointer readability checks for safe init/hooking --- Alien Isolation/DllMain.cpp | 13 +++---------- Alien Isolation/Main.cpp | 10 +++------- Alien Isolation/Util/Hooks.cpp | 12 ++++++++---- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/Alien Isolation/DllMain.cpp b/Alien Isolation/DllMain.cpp index 0439ece..bcb378b 100644 --- a/Alien Isolation/DllMain.cpp +++ b/Alien Isolation/DllMain.cpp @@ -7,16 +7,9 @@ DWORD WINAPI RunCT(LPVOID arg) util::log::Init(); util::log::Write("CT_AlienIsolation injected. Spawning main loop..."); - __try - { - g_mainHandle = new Main(); - if (g_mainHandle->Initialize()) - g_mainHandle->Run(); - } - __except (EXCEPTION_EXECUTE_HANDLER) - { - util::log::Error("Unhandled exception in RunCT thread. Exception code=0x%X", GetExceptionCode()); - } + g_mainHandle = new Main(); + if (g_mainHandle->Initialize()) + g_mainHandle->Run(); delete g_mainHandle; diff --git a/Alien Isolation/Main.cpp b/Alien Isolation/Main.cpp index 0fec6da..b729eec 100644 --- a/Alien Isolation/Main.cpp +++ b/Alien Isolation/Main.cpp @@ -111,17 +111,13 @@ bool Main::Initialize() return false; } - CATHODE::D3D* pD3D = nullptr; - __try + if (!util::IsPtrReadable((void*)d3dSingletonAddr, sizeof(void*))) { - pD3D = *(CATHODE::D3D**)d3dSingletonAddr; - } - __except (EXCEPTION_EXECUTE_HANDLER) - { - util::log::Error("Access violation while reading D3D singleton at 0x%X. Offsets likely outdated.", d3dSingletonAddr); + util::log::Error("D3D singleton address not readable: 0x%X", d3dSingletonAddr); return false; } + CATHODE::D3D* pD3D = *(CATHODE::D3D**)d3dSingletonAddr; if (!util::IsPtrReadable(pD3D, sizeof(void*))) { util::log::Error("D3D singleton pointer is not readable: 0x%p", pD3D); diff --git a/Alien Isolation/Util/Hooks.cpp b/Alien Isolation/Util/Hooks.cpp index d37facd..5ebc14d 100644 --- a/Alien Isolation/Util/Hooks.cpp +++ b/Alien Isolation/Util/Hooks.cpp @@ -243,13 +243,17 @@ void util::hooks::Init() if (g_dxgiSwapChain) { - __try + if (util::IsPtrReadable(g_dxgiSwapChain, sizeof(void*))) { - CreateVTableHook("SwapChainPresent", (PDWORD*)g_dxgiSwapChain, hIDXGISwapChain_Present, 8, &oIDXGISwapChain_Present); + void** vtbl = *(void***)g_dxgiSwapChain; + if (util::IsPtrReadable(vtbl, (8 + 1) * sizeof(void*))) + CreateVTableHook("SwapChainPresent", (PDWORD*)g_dxgiSwapChain, hIDXGISwapChain_Present, 8, &oIDXGISwapChain_Present); + else + util::log::Error("SwapChain vtable not readable; skipping Present hook"); } - __except (EXCEPTION_EXECUTE_HANDLER) + else { - util::log::Error("Access violation while creating VTable hook on SwapChain. Skipping Present hook."); + util::log::Error("SwapChain pointer not readable; skipping Present hook"); } } else From ae69a3f2c3ac10c4fba521e71c1ed7586eca8d45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anil=20D=C3=B6kmetas?= <111908128+JohnnyTheCoder1@users.noreply.github.com> Date: Fri, 24 Oct 2025 18:01:02 +0000 Subject: [PATCH 05/12] CI: link against NuGet MinHook (libMinHook.x86/x64.lib) and NuGet DirectXTK lib dirs; remove stale local paths --- Alien Isolation/CT_AlienIsolation.vcxproj | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Alien Isolation/CT_AlienIsolation.vcxproj b/Alien Isolation/CT_AlienIsolation.vcxproj index 91477a0..dcec6c6 100644 --- a/Alien Isolation/CT_AlienIsolation.vcxproj +++ b/Alien Isolation/CT_AlienIsolation.vcxproj @@ -81,11 +81,12 @@ Disabled true true - ../../../DirectXTK/Inc;../../../minhook-master/include;../../../boost_1_64_0-bin;%(AdditionalIncludeDirectories) + %(AdditionalIncludeDirectories) MultiThreadedDebugDLL - ../../../minhook-master/build/VC15/lib/Debug;../../../DirectXTK/Bin/Desktop_2017/Win32/Debug;../../../boost_1_64_0-bin/lib32-msvc-14.1;%(AdditionalLibraryDirectories) + $(ProjectDir)packages\minhook.1.3.3\lib\native;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\Win32\Debug;%(AdditionalLibraryDirectories) + libMinHook.x86.lib;%(AdditionalDependencies) @@ -96,8 +97,8 @@ true - - + $(ProjectDir)packages\minhook.1.3.3\lib\native;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\x64\Debug;%(AdditionalLibraryDirectories) + libMinHook.x64.lib;%(AdditionalDependencies) @@ -108,14 +109,15 @@ true true true - ../../../DirectXTK/Inc;../../../minhook-master/include;../../../boost_1_64_0-bin;%(AdditionalIncludeDirectories) + %(AdditionalIncludeDirectories) MultiThreadedDLL WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions) true true - ../../../minhook-master/build/VC15/lib/Release;../../../DirectXTK/Bin/Desktop_2017/Win32/Release;../../../boost_1_64_0-bin/lib32-msvc-14.1;%(AdditionalLibraryDirectories) + $(ProjectDir)packages\minhook.1.3.3\lib\native;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\Win32\Release;%(AdditionalLibraryDirectories) + libMinHook.x86.lib;%(AdditionalDependencies) @@ -130,8 +132,8 @@ true true - - + $(ProjectDir)packages\minhook.1.3.3\lib\native;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\x64\Release;%(AdditionalLibraryDirectories) + libMinHook.x64.lib;%(AdditionalDependencies) From 00c92ebcaee80e712c6b6aa53776be7f779659c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anil=20D=C3=B6kmetas?= <111908128+JohnnyTheCoder1@users.noreply.github.com> Date: Fri, 24 Oct 2025 18:07:43 +0000 Subject: [PATCH 06/12] CI: stop importing NuGet minhook.targets (adds bad lib path); set MinHook include dirs and link libs explicitly --- Alien Isolation/CT_AlienIsolation.vcxproj | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Alien Isolation/CT_AlienIsolation.vcxproj b/Alien Isolation/CT_AlienIsolation.vcxproj index dcec6c6..2b194f8 100644 --- a/Alien Isolation/CT_AlienIsolation.vcxproj +++ b/Alien Isolation/CT_AlienIsolation.vcxproj @@ -81,7 +81,7 @@ Disabled true true - %(AdditionalIncludeDirectories) + $(ProjectDir)packages\minhook.1.3.3\lib\native\include;%(AdditionalIncludeDirectories) MultiThreadedDebugDLL @@ -95,6 +95,7 @@ Disabled true true + $(ProjectDir)packages\minhook.1.3.3\lib\native\include;%(AdditionalIncludeDirectories) $(ProjectDir)packages\minhook.1.3.3\lib\native;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\x64\Debug;%(AdditionalLibraryDirectories) @@ -109,7 +110,7 @@ true true true - %(AdditionalIncludeDirectories) + $(ProjectDir)packages\minhook.1.3.3\lib\native\include;%(AdditionalIncludeDirectories) MultiThreadedDLL WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions) @@ -128,6 +129,7 @@ true true true + $(ProjectDir)packages\minhook.1.3.3\lib\native\include;%(AdditionalIncludeDirectories) true @@ -197,7 +199,6 @@ - From 55d5adb8b5f74b7b1e8919c5c3d1540041867608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anil=20D=C3=B6kmetas?= <111908128+JohnnyTheCoder1@users.noreply.github.com> Date: Fri, 24 Oct 2025 18:19:19 +0000 Subject: [PATCH 07/12] CI: use MinHook v141 libs per config (md/mdd) and point linker to lib/native/lib --- Alien Isolation/CT_AlienIsolation.vcxproj | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Alien Isolation/CT_AlienIsolation.vcxproj b/Alien Isolation/CT_AlienIsolation.vcxproj index 2b194f8..06aec59 100644 --- a/Alien Isolation/CT_AlienIsolation.vcxproj +++ b/Alien Isolation/CT_AlienIsolation.vcxproj @@ -85,8 +85,8 @@ MultiThreadedDebugDLL - $(ProjectDir)packages\minhook.1.3.3\lib\native;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\Win32\Debug;%(AdditionalLibraryDirectories) - libMinHook.x86.lib;%(AdditionalDependencies) + $(ProjectDir)packages\minhook.1.3.3\lib\native\lib;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\Win32\Debug;%(AdditionalLibraryDirectories) + libMinHook-x86-v141-mdd.lib;%(AdditionalDependencies) @@ -98,8 +98,8 @@ $(ProjectDir)packages\minhook.1.3.3\lib\native\include;%(AdditionalIncludeDirectories) - $(ProjectDir)packages\minhook.1.3.3\lib\native;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\x64\Debug;%(AdditionalLibraryDirectories) - libMinHook.x64.lib;%(AdditionalDependencies) + $(ProjectDir)packages\minhook.1.3.3\lib\native\lib;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\x64\Debug;%(AdditionalLibraryDirectories) + libMinHook-x64-v141-mdd.lib;%(AdditionalDependencies) @@ -117,8 +117,8 @@ true true - $(ProjectDir)packages\minhook.1.3.3\lib\native;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\Win32\Release;%(AdditionalLibraryDirectories) - libMinHook.x86.lib;%(AdditionalDependencies) + $(ProjectDir)packages\minhook.1.3.3\lib\native\lib;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\Win32\Release;%(AdditionalLibraryDirectories) + libMinHook-x86-v141-md.lib;%(AdditionalDependencies) @@ -134,8 +134,8 @@ true true - $(ProjectDir)packages\minhook.1.3.3\lib\native;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\x64\Release;%(AdditionalLibraryDirectories) - libMinHook.x64.lib;%(AdditionalDependencies) + $(ProjectDir)packages\minhook.1.3.3\lib\native\lib;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\x64\Release;%(AdditionalLibraryDirectories) + libMinHook-x64-v141-md.lib;%(AdditionalDependencies) From a1e3817513a7390ede6773a76daf6f9578be7ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anil=20D=C3=B6kmetas?= <111908128+JohnnyTheCoder1@users.noreply.github.com> Date: Fri, 24 Oct 2025 18:39:47 +0000 Subject: [PATCH 08/12] Vendor MinHook and update build guidance --- Alien Isolation/CT_AlienIsolation.vcxproj | 38 +- .../CT_AlienIsolation.vcxproj.filters | 57 ++ .../ThirdParty/MinHook/LICENSE.txt | 81 ++ Alien Isolation/ThirdParty/MinHook/README.md | 5 + .../ThirdParty/MinHook/include/MinHook.h | 186 ++++ .../ThirdParty/MinHook/src/buffer.c | 312 ++++++ .../ThirdParty/MinHook/src/buffer.h | 42 + .../ThirdParty/MinHook/src/hde/hde32.c | 326 +++++++ .../ThirdParty/MinHook/src/hde/hde32.h | 105 +++ .../ThirdParty/MinHook/src/hde/hde64.c | 337 +++++++ .../ThirdParty/MinHook/src/hde/hde64.h | 112 +++ .../ThirdParty/MinHook/src/hde/pstdint.h | 39 + .../ThirdParty/MinHook/src/hde/table32.h | 73 ++ .../ThirdParty/MinHook/src/hde/table64.h | 74 ++ Alien Isolation/ThirdParty/MinHook/src/hook.c | 889 ++++++++++++++++++ .../ThirdParty/MinHook/src/trampoline.c | 316 +++++++ .../ThirdParty/MinHook/src/trampoline.h | 105 +++ Alien Isolation/packages.config | 1 - README.md | 20 +- 19 files changed, 3102 insertions(+), 16 deletions(-) create mode 100644 Alien Isolation/ThirdParty/MinHook/LICENSE.txt create mode 100644 Alien Isolation/ThirdParty/MinHook/README.md create mode 100644 Alien Isolation/ThirdParty/MinHook/include/MinHook.h create mode 100644 Alien Isolation/ThirdParty/MinHook/src/buffer.c create mode 100644 Alien Isolation/ThirdParty/MinHook/src/buffer.h create mode 100644 Alien Isolation/ThirdParty/MinHook/src/hde/hde32.c create mode 100644 Alien Isolation/ThirdParty/MinHook/src/hde/hde32.h create mode 100644 Alien Isolation/ThirdParty/MinHook/src/hde/hde64.c create mode 100644 Alien Isolation/ThirdParty/MinHook/src/hde/hde64.h create mode 100644 Alien Isolation/ThirdParty/MinHook/src/hde/pstdint.h create mode 100644 Alien Isolation/ThirdParty/MinHook/src/hde/table32.h create mode 100644 Alien Isolation/ThirdParty/MinHook/src/hde/table64.h create mode 100644 Alien Isolation/ThirdParty/MinHook/src/hook.c create mode 100644 Alien Isolation/ThirdParty/MinHook/src/trampoline.c create mode 100644 Alien Isolation/ThirdParty/MinHook/src/trampoline.h diff --git a/Alien Isolation/CT_AlienIsolation.vcxproj b/Alien Isolation/CT_AlienIsolation.vcxproj index 06aec59..f1614e5 100644 --- a/Alien Isolation/CT_AlienIsolation.vcxproj +++ b/Alien Isolation/CT_AlienIsolation.vcxproj @@ -81,12 +81,12 @@ Disabled true true - $(ProjectDir)packages\minhook.1.3.3\lib\native\include;%(AdditionalIncludeDirectories) + $(ProjectDir)ThirdParty\MinHook\include;%(AdditionalIncludeDirectories) MultiThreadedDebugDLL - $(ProjectDir)packages\minhook.1.3.3\lib\native\lib;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\Win32\Debug;%(AdditionalLibraryDirectories) - libMinHook-x86-v141-mdd.lib;%(AdditionalDependencies) + $(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\Win32\Debug;%(AdditionalLibraryDirectories) + %(AdditionalDependencies) @@ -95,11 +95,11 @@ Disabled true true - $(ProjectDir)packages\minhook.1.3.3\lib\native\include;%(AdditionalIncludeDirectories) + $(ProjectDir)ThirdParty\MinHook\include;%(AdditionalIncludeDirectories) - $(ProjectDir)packages\minhook.1.3.3\lib\native\lib;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\x64\Debug;%(AdditionalLibraryDirectories) - libMinHook-x64-v141-mdd.lib;%(AdditionalDependencies) + $(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\x64\Debug;%(AdditionalLibraryDirectories) + %(AdditionalDependencies) @@ -110,15 +110,15 @@ true true true - $(ProjectDir)packages\minhook.1.3.3\lib\native\include;%(AdditionalIncludeDirectories) + $(ProjectDir)ThirdParty\MinHook\include;%(AdditionalIncludeDirectories) MultiThreadedDLL WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions) true true - $(ProjectDir)packages\minhook.1.3.3\lib\native\lib;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\Win32\Release;%(AdditionalLibraryDirectories) - libMinHook-x86-v141-md.lib;%(AdditionalDependencies) + $(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\Win32\Release;%(AdditionalLibraryDirectories) + %(AdditionalDependencies) @@ -129,13 +129,13 @@ true true true - $(ProjectDir)packages\minhook.1.3.3\lib\native\include;%(AdditionalIncludeDirectories) + $(ProjectDir)ThirdParty\MinHook\include;%(AdditionalIncludeDirectories) true true - $(ProjectDir)packages\minhook.1.3.3\lib\native\lib;$(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\x64\Release;%(AdditionalLibraryDirectories) - libMinHook-x64-v141-md.lib;%(AdditionalDependencies) + $(ProjectDir)packages\directxtk_desktop_2015.2019.5.31.1\lib\x64\Release;%(AdditionalLibraryDirectories) + %(AdditionalDependencies) @@ -152,6 +152,11 @@ + + + + + @@ -181,6 +186,14 @@ + + + + + + + + @@ -209,7 +222,6 @@ - diff --git a/Alien Isolation/CT_AlienIsolation.vcxproj.filters b/Alien Isolation/CT_AlienIsolation.vcxproj.filters index 159894c..252a6c1 100644 --- a/Alien Isolation/CT_AlienIsolation.vcxproj.filters +++ b/Alien Isolation/CT_AlienIsolation.vcxproj.filters @@ -37,6 +37,24 @@ {d5489722-4e47-49db-b8be-1231dbc72f8d} + + {0f8c4dbe-bdec-4fcb-84a3-2aa9edccd925} + + + {dd0d13fa-4c6e-47e3-8ae5-b38be2b9b47c} + + + {fc8d5f32-cae1-46d5-9f6a-6f0a7fe2f667} + + + {f06d5d0e-e3fb-4210-823d-85416b97a3a4} + + + {c54ce0d5-850f-4205-9ad2-47a554a05898} + + + {6ad90022-211d-4a62-8d5e-058f61548f26} + @@ -96,6 +114,21 @@ Source Files\Rendering + + Source Files\ThirdParty\MinHook + + + Source Files\ThirdParty\MinHook + + + Source Files\ThirdParty\MinHook + + + Source Files\ThirdParty\MinHook\hde + + + Source Files\ThirdParty\MinHook\hde + Source Files\Tools @@ -170,6 +203,30 @@ Source Files\Rendering + + Header Files\ThirdParty\MinHook + + + Header Files\ThirdParty\MinHook + + + Header Files\ThirdParty\MinHook + + + Header Files\ThirdParty\MinHook\hde + + + Header Files\ThirdParty\MinHook\hde + + + Header Files\ThirdParty\MinHook\hde + + + Header Files\ThirdParty\MinHook\hde + + + Header Files\ThirdParty\MinHook\hde + Source Files\Tools diff --git a/Alien Isolation/ThirdParty/MinHook/LICENSE.txt b/Alien Isolation/ThirdParty/MinHook/LICENSE.txt new file mode 100644 index 0000000..74dea27 --- /dev/null +++ b/Alien Isolation/ThirdParty/MinHook/LICENSE.txt @@ -0,0 +1,81 @@ +MinHook - The Minimalistic API Hooking Library for x64/x86 +Copyright (C) 2009-2017 Tsuda Kageyu. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER +OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +================================================================================ +Portions of this software are Copyright (c) 2008-2009, Vyacheslav Patkov. +================================================================================ +Hacker Disassembler Engine 32 C +Copyright (c) 2008-2009, Vyacheslav Patkov. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +------------------------------------------------------------------------------- +Hacker Disassembler Engine 64 C +Copyright (c) 2008-2009, Vyacheslav Patkov. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Alien Isolation/ThirdParty/MinHook/README.md b/Alien Isolation/ThirdParty/MinHook/README.md new file mode 100644 index 0000000..44a30ee --- /dev/null +++ b/Alien Isolation/ThirdParty/MinHook/README.md @@ -0,0 +1,5 @@ +# MinHook + +This directory contains an unmodified copy of [MinHook](https://github.com/TsudaKageyu/minhook) version 1.3.3, vendored so the project can build with consistent toolsets. + +See `LICENSE.txt` for the library's licensing terms. diff --git a/Alien Isolation/ThirdParty/MinHook/include/MinHook.h b/Alien Isolation/ThirdParty/MinHook/include/MinHook.h new file mode 100644 index 0000000..15c0a87 --- /dev/null +++ b/Alien Isolation/ThirdParty/MinHook/include/MinHook.h @@ -0,0 +1,186 @@ +/* + * MinHook - The Minimalistic API Hooking Library for x64/x86 + * Copyright (C) 2009-2017 Tsuda Kageyu. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#if !(defined _M_IX86) && !(defined _M_X64) && !(defined __i386__) && !(defined __x86_64__) + #error MinHook supports only x86 and x64 systems. +#endif + +#include + +// MinHook Error Codes. +typedef enum MH_STATUS +{ + // Unknown error. Should not be returned. + MH_UNKNOWN = -1, + + // Successful. + MH_OK = 0, + + // MinHook is already initialized. + MH_ERROR_ALREADY_INITIALIZED, + + // MinHook is not initialized yet, or already uninitialized. + MH_ERROR_NOT_INITIALIZED, + + // The hook for the specified target function is already created. + MH_ERROR_ALREADY_CREATED, + + // The hook for the specified target function is not created yet. + MH_ERROR_NOT_CREATED, + + // The hook for the specified target function is already enabled. + MH_ERROR_ENABLED, + + // The hook for the specified target function is not enabled yet, or already + // disabled. + MH_ERROR_DISABLED, + + // The specified pointer is invalid. It points the address of non-allocated + // and/or non-executable region. + MH_ERROR_NOT_EXECUTABLE, + + // The specified target function cannot be hooked. + MH_ERROR_UNSUPPORTED_FUNCTION, + + // Failed to allocate memory. + MH_ERROR_MEMORY_ALLOC, + + // Failed to change the memory protection. + MH_ERROR_MEMORY_PROTECT, + + // The specified module is not loaded. + MH_ERROR_MODULE_NOT_FOUND, + + // The specified function is not found. + MH_ERROR_FUNCTION_NOT_FOUND +} +MH_STATUS; + +// Can be passed as a parameter to MH_EnableHook, MH_DisableHook, +// MH_QueueEnableHook or MH_QueueDisableHook. +#define MH_ALL_HOOKS NULL + +#ifdef __cplusplus +extern "C" { +#endif + + // Initialize the MinHook library. You must call this function EXACTLY ONCE + // at the beginning of your program. + MH_STATUS WINAPI MH_Initialize(VOID); + + // Uninitialize the MinHook library. You must call this function EXACTLY + // ONCE at the end of your program. + MH_STATUS WINAPI MH_Uninitialize(VOID); + + // Creates a Hook for the specified target function, in disabled state. + // Parameters: + // pTarget [in] A pointer to the target function, which will be + // overridden by the detour function. + // pDetour [in] A pointer to the detour function, which will override + // the target function. + // ppOriginal [out] A pointer to the trampoline function, which will be + // used to call the original target function. + // This parameter can be NULL. + MH_STATUS WINAPI MH_CreateHook(LPVOID pTarget, LPVOID pDetour, LPVOID *ppOriginal); + + // Creates a Hook for the specified API function, in disabled state. + // Parameters: + // pszModule [in] A pointer to the loaded module name which contains the + // target function. + // pszTarget [in] A pointer to the target function name, which will be + // overridden by the detour function. + // pDetour [in] A pointer to the detour function, which will override + // the target function. + // ppOriginal [out] A pointer to the trampoline function, which will be + // used to call the original target function. + // This parameter can be NULL. + MH_STATUS WINAPI MH_CreateHookApi( + LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, LPVOID *ppOriginal); + + // Creates a Hook for the specified API function, in disabled state. + // Parameters: + // pszModule [in] A pointer to the loaded module name which contains the + // target function. + // pszTarget [in] A pointer to the target function name, which will be + // overridden by the detour function. + // pDetour [in] A pointer to the detour function, which will override + // the target function. + // ppOriginal [out] A pointer to the trampoline function, which will be + // used to call the original target function. + // This parameter can be NULL. + // ppTarget [out] A pointer to the target function, which will be used + // with other functions. + // This parameter can be NULL. + MH_STATUS WINAPI MH_CreateHookApiEx( + LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, LPVOID *ppOriginal, LPVOID *ppTarget); + + // Removes an already created hook. + // Parameters: + // pTarget [in] A pointer to the target function. + MH_STATUS WINAPI MH_RemoveHook(LPVOID pTarget); + + // Enables an already created hook. + // Parameters: + // pTarget [in] A pointer to the target function. + // If this parameter is MH_ALL_HOOKS, all created hooks are + // enabled in one go. + MH_STATUS WINAPI MH_EnableHook(LPVOID pTarget); + + // Disables an already created hook. + // Parameters: + // pTarget [in] A pointer to the target function. + // If this parameter is MH_ALL_HOOKS, all created hooks are + // disabled in one go. + MH_STATUS WINAPI MH_DisableHook(LPVOID pTarget); + + // Queues to enable an already created hook. + // Parameters: + // pTarget [in] A pointer to the target function. + // If this parameter is MH_ALL_HOOKS, all created hooks are + // queued to be enabled. + MH_STATUS WINAPI MH_QueueEnableHook(LPVOID pTarget); + + // Queues to disable an already created hook. + // Parameters: + // pTarget [in] A pointer to the target function. + // If this parameter is MH_ALL_HOOKS, all created hooks are + // queued to be disabled. + MH_STATUS WINAPI MH_QueueDisableHook(LPVOID pTarget); + + // Applies all queued changes in one go. + MH_STATUS WINAPI MH_ApplyQueued(VOID); + + // Translates the MH_STATUS to its name as a string. + const char * WINAPI MH_StatusToString(MH_STATUS status); + +#ifdef __cplusplus +} +#endif + diff --git a/Alien Isolation/ThirdParty/MinHook/src/buffer.c b/Alien Isolation/ThirdParty/MinHook/src/buffer.c new file mode 100644 index 0000000..8f9fbce --- /dev/null +++ b/Alien Isolation/ThirdParty/MinHook/src/buffer.c @@ -0,0 +1,312 @@ +/* + * MinHook - The Minimalistic API Hooking Library for x64/x86 + * Copyright (C) 2009-2017 Tsuda Kageyu. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "buffer.h" + +// Size of each memory block. (= page size of VirtualAlloc) +#define MEMORY_BLOCK_SIZE 0x1000 + +// Max range for seeking a memory block. (= 1024MB) +#define MAX_MEMORY_RANGE 0x40000000 + +// Memory protection flags to check the executable address. +#define PAGE_EXECUTE_FLAGS \ + (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY) + +// Memory slot. +typedef struct _MEMORY_SLOT +{ + union + { + struct _MEMORY_SLOT *pNext; + UINT8 buffer[MEMORY_SLOT_SIZE]; + }; +} MEMORY_SLOT, *PMEMORY_SLOT; + +// Memory block info. Placed at the head of each block. +typedef struct _MEMORY_BLOCK +{ + struct _MEMORY_BLOCK *pNext; + PMEMORY_SLOT pFree; // First element of the free slot list. + UINT usedCount; +} MEMORY_BLOCK, *PMEMORY_BLOCK; + +//------------------------------------------------------------------------- +// Global Variables: +//------------------------------------------------------------------------- + +// First element of the memory block list. +PMEMORY_BLOCK g_pMemoryBlocks; + +//------------------------------------------------------------------------- +VOID InitializeBuffer(VOID) +{ + // Nothing to do for now. +} + +//------------------------------------------------------------------------- +VOID UninitializeBuffer(VOID) +{ + PMEMORY_BLOCK pBlock = g_pMemoryBlocks; + g_pMemoryBlocks = NULL; + + while (pBlock) + { + PMEMORY_BLOCK pNext = pBlock->pNext; + VirtualFree(pBlock, 0, MEM_RELEASE); + pBlock = pNext; + } +} + +//------------------------------------------------------------------------- +#if defined(_M_X64) || defined(__x86_64__) +static LPVOID FindPrevFreeRegion(LPVOID pAddress, LPVOID pMinAddr, DWORD dwAllocationGranularity) +{ + ULONG_PTR tryAddr = (ULONG_PTR)pAddress; + + // Round down to the allocation granularity. + tryAddr -= tryAddr % dwAllocationGranularity; + + // Start from the previous allocation granularity multiply. + tryAddr -= dwAllocationGranularity; + + while (tryAddr >= (ULONG_PTR)pMinAddr) + { + MEMORY_BASIC_INFORMATION mbi; + if (VirtualQuery((LPVOID)tryAddr, &mbi, sizeof(mbi)) == 0) + break; + + if (mbi.State == MEM_FREE) + return (LPVOID)tryAddr; + + if ((ULONG_PTR)mbi.AllocationBase < dwAllocationGranularity) + break; + + tryAddr = (ULONG_PTR)mbi.AllocationBase - dwAllocationGranularity; + } + + return NULL; +} +#endif + +//------------------------------------------------------------------------- +#if defined(_M_X64) || defined(__x86_64__) +static LPVOID FindNextFreeRegion(LPVOID pAddress, LPVOID pMaxAddr, DWORD dwAllocationGranularity) +{ + ULONG_PTR tryAddr = (ULONG_PTR)pAddress; + + // Round down to the allocation granularity. + tryAddr -= tryAddr % dwAllocationGranularity; + + // Start from the next allocation granularity multiply. + tryAddr += dwAllocationGranularity; + + while (tryAddr <= (ULONG_PTR)pMaxAddr) + { + MEMORY_BASIC_INFORMATION mbi; + if (VirtualQuery((LPVOID)tryAddr, &mbi, sizeof(mbi)) == 0) + break; + + if (mbi.State == MEM_FREE) + return (LPVOID)tryAddr; + + tryAddr = (ULONG_PTR)mbi.BaseAddress + mbi.RegionSize; + + // Round up to the next allocation granularity. + tryAddr += dwAllocationGranularity - 1; + tryAddr -= tryAddr % dwAllocationGranularity; + } + + return NULL; +} +#endif + +//------------------------------------------------------------------------- +static PMEMORY_BLOCK GetMemoryBlock(LPVOID pOrigin) +{ + PMEMORY_BLOCK pBlock; +#if defined(_M_X64) || defined(__x86_64__) + ULONG_PTR minAddr; + ULONG_PTR maxAddr; + + SYSTEM_INFO si; + GetSystemInfo(&si); + minAddr = (ULONG_PTR)si.lpMinimumApplicationAddress; + maxAddr = (ULONG_PTR)si.lpMaximumApplicationAddress; + + // pOrigin ± 512MB + if ((ULONG_PTR)pOrigin > MAX_MEMORY_RANGE && minAddr < (ULONG_PTR)pOrigin - MAX_MEMORY_RANGE) + minAddr = (ULONG_PTR)pOrigin - MAX_MEMORY_RANGE; + + if (maxAddr > (ULONG_PTR)pOrigin + MAX_MEMORY_RANGE) + maxAddr = (ULONG_PTR)pOrigin + MAX_MEMORY_RANGE; + + // Make room for MEMORY_BLOCK_SIZE bytes. + maxAddr -= MEMORY_BLOCK_SIZE - 1; +#endif + + // Look the registered blocks for a reachable one. + for (pBlock = g_pMemoryBlocks; pBlock != NULL; pBlock = pBlock->pNext) + { +#if defined(_M_X64) || defined(__x86_64__) + // Ignore the blocks too far. + if ((ULONG_PTR)pBlock < minAddr || (ULONG_PTR)pBlock >= maxAddr) + continue; +#endif + // The block has at least one unused slot. + if (pBlock->pFree != NULL) + return pBlock; + } + +#if defined(_M_X64) || defined(__x86_64__) + // Alloc a new block above if not found. + { + LPVOID pAlloc = pOrigin; + while ((ULONG_PTR)pAlloc >= minAddr) + { + pAlloc = FindPrevFreeRegion(pAlloc, (LPVOID)minAddr, si.dwAllocationGranularity); + if (pAlloc == NULL) + break; + + pBlock = (PMEMORY_BLOCK)VirtualAlloc( + pAlloc, MEMORY_BLOCK_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); + if (pBlock != NULL) + break; + } + } + + // Alloc a new block below if not found. + if (pBlock == NULL) + { + LPVOID pAlloc = pOrigin; + while ((ULONG_PTR)pAlloc <= maxAddr) + { + pAlloc = FindNextFreeRegion(pAlloc, (LPVOID)maxAddr, si.dwAllocationGranularity); + if (pAlloc == NULL) + break; + + pBlock = (PMEMORY_BLOCK)VirtualAlloc( + pAlloc, MEMORY_BLOCK_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); + if (pBlock != NULL) + break; + } + } +#else + // In x86 mode, a memory block can be placed anywhere. + pBlock = (PMEMORY_BLOCK)VirtualAlloc( + NULL, MEMORY_BLOCK_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); +#endif + + if (pBlock != NULL) + { + // Build a linked list of all the slots. + PMEMORY_SLOT pSlot = (PMEMORY_SLOT)pBlock + 1; + pBlock->pFree = NULL; + pBlock->usedCount = 0; + do + { + pSlot->pNext = pBlock->pFree; + pBlock->pFree = pSlot; + pSlot++; + } while ((ULONG_PTR)pSlot - (ULONG_PTR)pBlock <= MEMORY_BLOCK_SIZE - MEMORY_SLOT_SIZE); + + pBlock->pNext = g_pMemoryBlocks; + g_pMemoryBlocks = pBlock; + } + + return pBlock; +} + +//------------------------------------------------------------------------- +LPVOID AllocateBuffer(LPVOID pOrigin) +{ + PMEMORY_SLOT pSlot; + PMEMORY_BLOCK pBlock = GetMemoryBlock(pOrigin); + if (pBlock == NULL) + return NULL; + + // Remove an unused slot from the list. + pSlot = pBlock->pFree; + pBlock->pFree = pSlot->pNext; + pBlock->usedCount++; +#ifdef _DEBUG + // Fill the slot with INT3 for debugging. + memset(pSlot, 0xCC, sizeof(MEMORY_SLOT)); +#endif + return pSlot; +} + +//------------------------------------------------------------------------- +VOID FreeBuffer(LPVOID pBuffer) +{ + PMEMORY_BLOCK pBlock = g_pMemoryBlocks; + PMEMORY_BLOCK pPrev = NULL; + ULONG_PTR pTargetBlock = ((ULONG_PTR)pBuffer / MEMORY_BLOCK_SIZE) * MEMORY_BLOCK_SIZE; + + while (pBlock != NULL) + { + if ((ULONG_PTR)pBlock == pTargetBlock) + { + PMEMORY_SLOT pSlot = (PMEMORY_SLOT)pBuffer; +#ifdef _DEBUG + // Clear the released slot for debugging. + memset(pSlot, 0x00, sizeof(*pSlot)); +#endif + // Restore the released slot to the list. + pSlot->pNext = pBlock->pFree; + pBlock->pFree = pSlot; + pBlock->usedCount--; + + // Free if unused. + if (pBlock->usedCount == 0) + { + if (pPrev) + pPrev->pNext = pBlock->pNext; + else + g_pMemoryBlocks = pBlock->pNext; + + VirtualFree(pBlock, 0, MEM_RELEASE); + } + + break; + } + + pPrev = pBlock; + pBlock = pBlock->pNext; + } +} + +//------------------------------------------------------------------------- +BOOL IsExecutableAddress(LPVOID pAddress) +{ + MEMORY_BASIC_INFORMATION mi; + VirtualQuery(pAddress, &mi, sizeof(mi)); + + return (mi.State == MEM_COMMIT && (mi.Protect & PAGE_EXECUTE_FLAGS)); +} diff --git a/Alien Isolation/ThirdParty/MinHook/src/buffer.h b/Alien Isolation/ThirdParty/MinHook/src/buffer.h new file mode 100644 index 0000000..204d551 --- /dev/null +++ b/Alien Isolation/ThirdParty/MinHook/src/buffer.h @@ -0,0 +1,42 @@ +/* + * MinHook - The Minimalistic API Hooking Library for x64/x86 + * Copyright (C) 2009-2017 Tsuda Kageyu. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +// Size of each memory slot. +#if defined(_M_X64) || defined(__x86_64__) + #define MEMORY_SLOT_SIZE 64 +#else + #define MEMORY_SLOT_SIZE 32 +#endif + +VOID InitializeBuffer(VOID); +VOID UninitializeBuffer(VOID); +LPVOID AllocateBuffer(LPVOID pOrigin); +VOID FreeBuffer(LPVOID pBuffer); +BOOL IsExecutableAddress(LPVOID pAddress); diff --git a/Alien Isolation/ThirdParty/MinHook/src/hde/hde32.c b/Alien Isolation/ThirdParty/MinHook/src/hde/hde32.c new file mode 100644 index 0000000..08fa25b --- /dev/null +++ b/Alien Isolation/ThirdParty/MinHook/src/hde/hde32.c @@ -0,0 +1,326 @@ +/* + * Hacker Disassembler Engine 32 C + * Copyright (c) 2008-2009, Vyacheslav Patkov. + * All rights reserved. + * + */ + +#if defined(_M_IX86) || defined(__i386__) + +#include "hde32.h" +#include "table32.h" + +unsigned int hde32_disasm(const void *code, hde32s *hs) +{ + uint8_t x, c, *p = (uint8_t *)code, cflags, opcode, pref = 0; + uint8_t *ht = hde32_table, m_mod, m_reg, m_rm, disp_size = 0; + + // Avoid using memset to reduce the footprint. +#ifndef _MSC_VER + memset((LPBYTE)hs, 0, sizeof(hde32s)); +#else + __stosb((LPBYTE)hs, 0, sizeof(hde32s)); +#endif + + for (x = 16; x; x--) + switch (c = *p++) { + case 0xf3: + hs->p_rep = c; + pref |= PRE_F3; + break; + case 0xf2: + hs->p_rep = c; + pref |= PRE_F2; + break; + case 0xf0: + hs->p_lock = c; + pref |= PRE_LOCK; + break; + case 0x26: case 0x2e: case 0x36: + case 0x3e: case 0x64: case 0x65: + hs->p_seg = c; + pref |= PRE_SEG; + break; + case 0x66: + hs->p_66 = c; + pref |= PRE_66; + break; + case 0x67: + hs->p_67 = c; + pref |= PRE_67; + break; + default: + goto pref_done; + } + pref_done: + + hs->flags = (uint32_t)pref << 23; + + if (!pref) + pref |= PRE_NONE; + + if ((hs->opcode = c) == 0x0f) { + hs->opcode2 = c = *p++; + ht += DELTA_OPCODES; + } else if (c >= 0xa0 && c <= 0xa3) { + if (pref & PRE_67) + pref |= PRE_66; + else + pref &= ~PRE_66; + } + + opcode = c; + cflags = ht[ht[opcode / 4] + (opcode % 4)]; + + if (cflags == C_ERROR) { + hs->flags |= F_ERROR | F_ERROR_OPCODE; + cflags = 0; + if ((opcode & -3) == 0x24) + cflags++; + } + + x = 0; + if (cflags & C_GROUP) { + uint16_t t; + t = *(uint16_t *)(ht + (cflags & 0x7f)); + cflags = (uint8_t)t; + x = (uint8_t)(t >> 8); + } + + if (hs->opcode2) { + ht = hde32_table + DELTA_PREFIXES; + if (ht[ht[opcode / 4] + (opcode % 4)] & pref) + hs->flags |= F_ERROR | F_ERROR_OPCODE; + } + + if (cflags & C_MODRM) { + hs->flags |= F_MODRM; + hs->modrm = c = *p++; + hs->modrm_mod = m_mod = c >> 6; + hs->modrm_rm = m_rm = c & 7; + hs->modrm_reg = m_reg = (c & 0x3f) >> 3; + + if (x && ((x << m_reg) & 0x80)) + hs->flags |= F_ERROR | F_ERROR_OPCODE; + + if (!hs->opcode2 && opcode >= 0xd9 && opcode <= 0xdf) { + uint8_t t = opcode - 0xd9; + if (m_mod == 3) { + ht = hde32_table + DELTA_FPU_MODRM + t*8; + t = ht[m_reg] << m_rm; + } else { + ht = hde32_table + DELTA_FPU_REG; + t = ht[t] << m_reg; + } + if (t & 0x80) + hs->flags |= F_ERROR | F_ERROR_OPCODE; + } + + if (pref & PRE_LOCK) { + if (m_mod == 3) { + hs->flags |= F_ERROR | F_ERROR_LOCK; + } else { + uint8_t *table_end, op = opcode; + if (hs->opcode2) { + ht = hde32_table + DELTA_OP2_LOCK_OK; + table_end = ht + DELTA_OP_ONLY_MEM - DELTA_OP2_LOCK_OK; + } else { + ht = hde32_table + DELTA_OP_LOCK_OK; + table_end = ht + DELTA_OP2_LOCK_OK - DELTA_OP_LOCK_OK; + op &= -2; + } + for (; ht != table_end; ht++) + if (*ht++ == op) { + if (!((*ht << m_reg) & 0x80)) + goto no_lock_error; + else + break; + } + hs->flags |= F_ERROR | F_ERROR_LOCK; + no_lock_error: + ; + } + } + + if (hs->opcode2) { + switch (opcode) { + case 0x20: case 0x22: + m_mod = 3; + if (m_reg > 4 || m_reg == 1) + goto error_operand; + else + goto no_error_operand; + case 0x21: case 0x23: + m_mod = 3; + if (m_reg == 4 || m_reg == 5) + goto error_operand; + else + goto no_error_operand; + } + } else { + switch (opcode) { + case 0x8c: + if (m_reg > 5) + goto error_operand; + else + goto no_error_operand; + case 0x8e: + if (m_reg == 1 || m_reg > 5) + goto error_operand; + else + goto no_error_operand; + } + } + + if (m_mod == 3) { + uint8_t *table_end; + if (hs->opcode2) { + ht = hde32_table + DELTA_OP2_ONLY_MEM; + table_end = ht + sizeof(hde32_table) - DELTA_OP2_ONLY_MEM; + } else { + ht = hde32_table + DELTA_OP_ONLY_MEM; + table_end = ht + DELTA_OP2_ONLY_MEM - DELTA_OP_ONLY_MEM; + } + for (; ht != table_end; ht += 2) + if (*ht++ == opcode) { + if (*ht++ & pref && !((*ht << m_reg) & 0x80)) + goto error_operand; + else + break; + } + goto no_error_operand; + } else if (hs->opcode2) { + switch (opcode) { + case 0x50: case 0xd7: case 0xf7: + if (pref & (PRE_NONE | PRE_66)) + goto error_operand; + break; + case 0xd6: + if (pref & (PRE_F2 | PRE_F3)) + goto error_operand; + break; + case 0xc5: + goto error_operand; + } + goto no_error_operand; + } else + goto no_error_operand; + + error_operand: + hs->flags |= F_ERROR | F_ERROR_OPERAND; + no_error_operand: + + c = *p++; + if (m_reg <= 1) { + if (opcode == 0xf6) + cflags |= C_IMM8; + else if (opcode == 0xf7) + cflags |= C_IMM_P66; + } + + switch (m_mod) { + case 0: + if (pref & PRE_67) { + if (m_rm == 6) + disp_size = 2; + } else + if (m_rm == 5) + disp_size = 4; + break; + case 1: + disp_size = 1; + break; + case 2: + disp_size = 2; + if (!(pref & PRE_67)) + disp_size <<= 1; + } + + if (m_mod != 3 && m_rm == 4 && !(pref & PRE_67)) { + hs->flags |= F_SIB; + p++; + hs->sib = c; + hs->sib_scale = c >> 6; + hs->sib_index = (c & 0x3f) >> 3; + if ((hs->sib_base = c & 7) == 5 && !(m_mod & 1)) + disp_size = 4; + } + + p--; + switch (disp_size) { + case 1: + hs->flags |= F_DISP8; + hs->disp.disp8 = *p; + break; + case 2: + hs->flags |= F_DISP16; + hs->disp.disp16 = *(uint16_t *)p; + break; + case 4: + hs->flags |= F_DISP32; + hs->disp.disp32 = *(uint32_t *)p; + } + p += disp_size; + } else if (pref & PRE_LOCK) + hs->flags |= F_ERROR | F_ERROR_LOCK; + + if (cflags & C_IMM_P66) { + if (cflags & C_REL32) { + if (pref & PRE_66) { + hs->flags |= F_IMM16 | F_RELATIVE; + hs->imm.imm16 = *(uint16_t *)p; + p += 2; + goto disasm_done; + } + goto rel32_ok; + } + if (pref & PRE_66) { + hs->flags |= F_IMM16; + hs->imm.imm16 = *(uint16_t *)p; + p += 2; + } else { + hs->flags |= F_IMM32; + hs->imm.imm32 = *(uint32_t *)p; + p += 4; + } + } + + if (cflags & C_IMM16) { + if (hs->flags & F_IMM32) { + hs->flags |= F_IMM16; + hs->disp.disp16 = *(uint16_t *)p; + } else if (hs->flags & F_IMM16) { + hs->flags |= F_2IMM16; + hs->disp.disp16 = *(uint16_t *)p; + } else { + hs->flags |= F_IMM16; + hs->imm.imm16 = *(uint16_t *)p; + } + p += 2; + } + if (cflags & C_IMM8) { + hs->flags |= F_IMM8; + hs->imm.imm8 = *p++; + } + + if (cflags & C_REL32) { + rel32_ok: + hs->flags |= F_IMM32 | F_RELATIVE; + hs->imm.imm32 = *(uint32_t *)p; + p += 4; + } else if (cflags & C_REL8) { + hs->flags |= F_IMM8 | F_RELATIVE; + hs->imm.imm8 = *p++; + } + + disasm_done: + + if ((hs->len = (uint8_t)(p-(uint8_t *)code)) > 15) { + hs->flags |= F_ERROR | F_ERROR_LENGTH; + hs->len = 15; + } + + return (unsigned int)hs->len; +} + +#endif // defined(_M_IX86) || defined(__i386__) diff --git a/Alien Isolation/ThirdParty/MinHook/src/hde/hde32.h b/Alien Isolation/ThirdParty/MinHook/src/hde/hde32.h new file mode 100644 index 0000000..1112450 --- /dev/null +++ b/Alien Isolation/ThirdParty/MinHook/src/hde/hde32.h @@ -0,0 +1,105 @@ +/* + * Hacker Disassembler Engine 32 + * Copyright (c) 2006-2009, Vyacheslav Patkov. + * All rights reserved. + * + * hde32.h: C/C++ header file + * + */ + +#ifndef _HDE32_H_ +#define _HDE32_H_ + +/* stdint.h - C99 standard header + * http://en.wikipedia.org/wiki/stdint.h + * + * if your compiler doesn't contain "stdint.h" header (for + * example, Microsoft Visual C++), you can download file: + * http://www.azillionmonkeys.com/qed/pstdint.h + * and change next line to: + * #include "pstdint.h" + */ +#include "pstdint.h" + +#define F_MODRM 0x00000001 +#define F_SIB 0x00000002 +#define F_IMM8 0x00000004 +#define F_IMM16 0x00000008 +#define F_IMM32 0x00000010 +#define F_DISP8 0x00000020 +#define F_DISP16 0x00000040 +#define F_DISP32 0x00000080 +#define F_RELATIVE 0x00000100 +#define F_2IMM16 0x00000800 +#define F_ERROR 0x00001000 +#define F_ERROR_OPCODE 0x00002000 +#define F_ERROR_LENGTH 0x00004000 +#define F_ERROR_LOCK 0x00008000 +#define F_ERROR_OPERAND 0x00010000 +#define F_PREFIX_REPNZ 0x01000000 +#define F_PREFIX_REPX 0x02000000 +#define F_PREFIX_REP 0x03000000 +#define F_PREFIX_66 0x04000000 +#define F_PREFIX_67 0x08000000 +#define F_PREFIX_LOCK 0x10000000 +#define F_PREFIX_SEG 0x20000000 +#define F_PREFIX_ANY 0x3f000000 + +#define PREFIX_SEGMENT_CS 0x2e +#define PREFIX_SEGMENT_SS 0x36 +#define PREFIX_SEGMENT_DS 0x3e +#define PREFIX_SEGMENT_ES 0x26 +#define PREFIX_SEGMENT_FS 0x64 +#define PREFIX_SEGMENT_GS 0x65 +#define PREFIX_LOCK 0xf0 +#define PREFIX_REPNZ 0xf2 +#define PREFIX_REPX 0xf3 +#define PREFIX_OPERAND_SIZE 0x66 +#define PREFIX_ADDRESS_SIZE 0x67 + +#pragma pack(push,1) + +typedef struct { + uint8_t len; + uint8_t p_rep; + uint8_t p_lock; + uint8_t p_seg; + uint8_t p_66; + uint8_t p_67; + uint8_t opcode; + uint8_t opcode2; + uint8_t modrm; + uint8_t modrm_mod; + uint8_t modrm_reg; + uint8_t modrm_rm; + uint8_t sib; + uint8_t sib_scale; + uint8_t sib_index; + uint8_t sib_base; + union { + uint8_t imm8; + uint16_t imm16; + uint32_t imm32; + } imm; + union { + uint8_t disp8; + uint16_t disp16; + uint32_t disp32; + } disp; + uint32_t flags; +} hde32s; + +#pragma pack(pop) + +#ifdef __cplusplus +extern "C" { +#endif + +/* __cdecl */ +unsigned int hde32_disasm(const void *code, hde32s *hs); + +#ifdef __cplusplus +} +#endif + +#endif /* _HDE32_H_ */ diff --git a/Alien Isolation/ThirdParty/MinHook/src/hde/hde64.c b/Alien Isolation/ThirdParty/MinHook/src/hde/hde64.c new file mode 100644 index 0000000..c23e2fc --- /dev/null +++ b/Alien Isolation/ThirdParty/MinHook/src/hde/hde64.c @@ -0,0 +1,337 @@ +/* + * Hacker Disassembler Engine 64 C + * Copyright (c) 2008-2009, Vyacheslav Patkov. + * All rights reserved. + * + */ + +#if defined(_M_X64) || defined(__x86_64__) + +#include "hde64.h" +#include "table64.h" + +unsigned int hde64_disasm(const void *code, hde64s *hs) +{ + uint8_t x, c, *p = (uint8_t *)code, cflags, opcode, pref = 0; + uint8_t *ht = hde64_table, m_mod, m_reg, m_rm, disp_size = 0; + uint8_t op64 = 0; + + // Avoid using memset to reduce the footprint. +#ifndef _MSC_VER + memset((LPBYTE)hs, 0, sizeof(hde64s)); +#else + __stosb((LPBYTE)hs, 0, sizeof(hde64s)); +#endif + + for (x = 16; x; x--) + switch (c = *p++) { + case 0xf3: + hs->p_rep = c; + pref |= PRE_F3; + break; + case 0xf2: + hs->p_rep = c; + pref |= PRE_F2; + break; + case 0xf0: + hs->p_lock = c; + pref |= PRE_LOCK; + break; + case 0x26: case 0x2e: case 0x36: + case 0x3e: case 0x64: case 0x65: + hs->p_seg = c; + pref |= PRE_SEG; + break; + case 0x66: + hs->p_66 = c; + pref |= PRE_66; + break; + case 0x67: + hs->p_67 = c; + pref |= PRE_67; + break; + default: + goto pref_done; + } + pref_done: + + hs->flags = (uint32_t)pref << 23; + + if (!pref) + pref |= PRE_NONE; + + if ((c & 0xf0) == 0x40) { + hs->flags |= F_PREFIX_REX; + if ((hs->rex_w = (c & 0xf) >> 3) && (*p & 0xf8) == 0xb8) + op64++; + hs->rex_r = (c & 7) >> 2; + hs->rex_x = (c & 3) >> 1; + hs->rex_b = c & 1; + if (((c = *p++) & 0xf0) == 0x40) { + opcode = c; + goto error_opcode; + } + } + + if ((hs->opcode = c) == 0x0f) { + hs->opcode2 = c = *p++; + ht += DELTA_OPCODES; + } else if (c >= 0xa0 && c <= 0xa3) { + op64++; + if (pref & PRE_67) + pref |= PRE_66; + else + pref &= ~PRE_66; + } + + opcode = c; + cflags = ht[ht[opcode / 4] + (opcode % 4)]; + + if (cflags == C_ERROR) { + error_opcode: + hs->flags |= F_ERROR | F_ERROR_OPCODE; + cflags = 0; + if ((opcode & -3) == 0x24) + cflags++; + } + + x = 0; + if (cflags & C_GROUP) { + uint16_t t; + t = *(uint16_t *)(ht + (cflags & 0x7f)); + cflags = (uint8_t)t; + x = (uint8_t)(t >> 8); + } + + if (hs->opcode2) { + ht = hde64_table + DELTA_PREFIXES; + if (ht[ht[opcode / 4] + (opcode % 4)] & pref) + hs->flags |= F_ERROR | F_ERROR_OPCODE; + } + + if (cflags & C_MODRM) { + hs->flags |= F_MODRM; + hs->modrm = c = *p++; + hs->modrm_mod = m_mod = c >> 6; + hs->modrm_rm = m_rm = c & 7; + hs->modrm_reg = m_reg = (c & 0x3f) >> 3; + + if (x && ((x << m_reg) & 0x80)) + hs->flags |= F_ERROR | F_ERROR_OPCODE; + + if (!hs->opcode2 && opcode >= 0xd9 && opcode <= 0xdf) { + uint8_t t = opcode - 0xd9; + if (m_mod == 3) { + ht = hde64_table + DELTA_FPU_MODRM + t*8; + t = ht[m_reg] << m_rm; + } else { + ht = hde64_table + DELTA_FPU_REG; + t = ht[t] << m_reg; + } + if (t & 0x80) + hs->flags |= F_ERROR | F_ERROR_OPCODE; + } + + if (pref & PRE_LOCK) { + if (m_mod == 3) { + hs->flags |= F_ERROR | F_ERROR_LOCK; + } else { + uint8_t *table_end, op = opcode; + if (hs->opcode2) { + ht = hde64_table + DELTA_OP2_LOCK_OK; + table_end = ht + DELTA_OP_ONLY_MEM - DELTA_OP2_LOCK_OK; + } else { + ht = hde64_table + DELTA_OP_LOCK_OK; + table_end = ht + DELTA_OP2_LOCK_OK - DELTA_OP_LOCK_OK; + op &= -2; + } + for (; ht != table_end; ht++) + if (*ht++ == op) { + if (!((*ht << m_reg) & 0x80)) + goto no_lock_error; + else + break; + } + hs->flags |= F_ERROR | F_ERROR_LOCK; + no_lock_error: + ; + } + } + + if (hs->opcode2) { + switch (opcode) { + case 0x20: case 0x22: + m_mod = 3; + if (m_reg > 4 || m_reg == 1) + goto error_operand; + else + goto no_error_operand; + case 0x21: case 0x23: + m_mod = 3; + if (m_reg == 4 || m_reg == 5) + goto error_operand; + else + goto no_error_operand; + } + } else { + switch (opcode) { + case 0x8c: + if (m_reg > 5) + goto error_operand; + else + goto no_error_operand; + case 0x8e: + if (m_reg == 1 || m_reg > 5) + goto error_operand; + else + goto no_error_operand; + } + } + + if (m_mod == 3) { + uint8_t *table_end; + if (hs->opcode2) { + ht = hde64_table + DELTA_OP2_ONLY_MEM; + table_end = ht + sizeof(hde64_table) - DELTA_OP2_ONLY_MEM; + } else { + ht = hde64_table + DELTA_OP_ONLY_MEM; + table_end = ht + DELTA_OP2_ONLY_MEM - DELTA_OP_ONLY_MEM; + } + for (; ht != table_end; ht += 2) + if (*ht++ == opcode) { + if (*ht++ & pref && !((*ht << m_reg) & 0x80)) + goto error_operand; + else + break; + } + goto no_error_operand; + } else if (hs->opcode2) { + switch (opcode) { + case 0x50: case 0xd7: case 0xf7: + if (pref & (PRE_NONE | PRE_66)) + goto error_operand; + break; + case 0xd6: + if (pref & (PRE_F2 | PRE_F3)) + goto error_operand; + break; + case 0xc5: + goto error_operand; + } + goto no_error_operand; + } else + goto no_error_operand; + + error_operand: + hs->flags |= F_ERROR | F_ERROR_OPERAND; + no_error_operand: + + c = *p++; + if (m_reg <= 1) { + if (opcode == 0xf6) + cflags |= C_IMM8; + else if (opcode == 0xf7) + cflags |= C_IMM_P66; + } + + switch (m_mod) { + case 0: + if (pref & PRE_67) { + if (m_rm == 6) + disp_size = 2; + } else + if (m_rm == 5) + disp_size = 4; + break; + case 1: + disp_size = 1; + break; + case 2: + disp_size = 2; + if (!(pref & PRE_67)) + disp_size <<= 1; + } + + if (m_mod != 3 && m_rm == 4) { + hs->flags |= F_SIB; + p++; + hs->sib = c; + hs->sib_scale = c >> 6; + hs->sib_index = (c & 0x3f) >> 3; + if ((hs->sib_base = c & 7) == 5 && !(m_mod & 1)) + disp_size = 4; + } + + p--; + switch (disp_size) { + case 1: + hs->flags |= F_DISP8; + hs->disp.disp8 = *p; + break; + case 2: + hs->flags |= F_DISP16; + hs->disp.disp16 = *(uint16_t *)p; + break; + case 4: + hs->flags |= F_DISP32; + hs->disp.disp32 = *(uint32_t *)p; + } + p += disp_size; + } else if (pref & PRE_LOCK) + hs->flags |= F_ERROR | F_ERROR_LOCK; + + if (cflags & C_IMM_P66) { + if (cflags & C_REL32) { + if (pref & PRE_66) { + hs->flags |= F_IMM16 | F_RELATIVE; + hs->imm.imm16 = *(uint16_t *)p; + p += 2; + goto disasm_done; + } + goto rel32_ok; + } + if (op64) { + hs->flags |= F_IMM64; + hs->imm.imm64 = *(uint64_t *)p; + p += 8; + } else if (!(pref & PRE_66)) { + hs->flags |= F_IMM32; + hs->imm.imm32 = *(uint32_t *)p; + p += 4; + } else + goto imm16_ok; + } + + + if (cflags & C_IMM16) { + imm16_ok: + hs->flags |= F_IMM16; + hs->imm.imm16 = *(uint16_t *)p; + p += 2; + } + if (cflags & C_IMM8) { + hs->flags |= F_IMM8; + hs->imm.imm8 = *p++; + } + + if (cflags & C_REL32) { + rel32_ok: + hs->flags |= F_IMM32 | F_RELATIVE; + hs->imm.imm32 = *(uint32_t *)p; + p += 4; + } else if (cflags & C_REL8) { + hs->flags |= F_IMM8 | F_RELATIVE; + hs->imm.imm8 = *p++; + } + + disasm_done: + + if ((hs->len = (uint8_t)(p-(uint8_t *)code)) > 15) { + hs->flags |= F_ERROR | F_ERROR_LENGTH; + hs->len = 15; + } + + return (unsigned int)hs->len; +} + +#endif // defined(_M_X64) || defined(__x86_64__) diff --git a/Alien Isolation/ThirdParty/MinHook/src/hde/hde64.h b/Alien Isolation/ThirdParty/MinHook/src/hde/hde64.h new file mode 100644 index 0000000..ecbf4df --- /dev/null +++ b/Alien Isolation/ThirdParty/MinHook/src/hde/hde64.h @@ -0,0 +1,112 @@ +/* + * Hacker Disassembler Engine 64 + * Copyright (c) 2008-2009, Vyacheslav Patkov. + * All rights reserved. + * + * hde64.h: C/C++ header file + * + */ + +#ifndef _HDE64_H_ +#define _HDE64_H_ + +/* stdint.h - C99 standard header + * http://en.wikipedia.org/wiki/stdint.h + * + * if your compiler doesn't contain "stdint.h" header (for + * example, Microsoft Visual C++), you can download file: + * http://www.azillionmonkeys.com/qed/pstdint.h + * and change next line to: + * #include "pstdint.h" + */ +#include "pstdint.h" + +#define F_MODRM 0x00000001 +#define F_SIB 0x00000002 +#define F_IMM8 0x00000004 +#define F_IMM16 0x00000008 +#define F_IMM32 0x00000010 +#define F_IMM64 0x00000020 +#define F_DISP8 0x00000040 +#define F_DISP16 0x00000080 +#define F_DISP32 0x00000100 +#define F_RELATIVE 0x00000200 +#define F_ERROR 0x00001000 +#define F_ERROR_OPCODE 0x00002000 +#define F_ERROR_LENGTH 0x00004000 +#define F_ERROR_LOCK 0x00008000 +#define F_ERROR_OPERAND 0x00010000 +#define F_PREFIX_REPNZ 0x01000000 +#define F_PREFIX_REPX 0x02000000 +#define F_PREFIX_REP 0x03000000 +#define F_PREFIX_66 0x04000000 +#define F_PREFIX_67 0x08000000 +#define F_PREFIX_LOCK 0x10000000 +#define F_PREFIX_SEG 0x20000000 +#define F_PREFIX_REX 0x40000000 +#define F_PREFIX_ANY 0x7f000000 + +#define PREFIX_SEGMENT_CS 0x2e +#define PREFIX_SEGMENT_SS 0x36 +#define PREFIX_SEGMENT_DS 0x3e +#define PREFIX_SEGMENT_ES 0x26 +#define PREFIX_SEGMENT_FS 0x64 +#define PREFIX_SEGMENT_GS 0x65 +#define PREFIX_LOCK 0xf0 +#define PREFIX_REPNZ 0xf2 +#define PREFIX_REPX 0xf3 +#define PREFIX_OPERAND_SIZE 0x66 +#define PREFIX_ADDRESS_SIZE 0x67 + +#pragma pack(push,1) + +typedef struct { + uint8_t len; + uint8_t p_rep; + uint8_t p_lock; + uint8_t p_seg; + uint8_t p_66; + uint8_t p_67; + uint8_t rex; + uint8_t rex_w; + uint8_t rex_r; + uint8_t rex_x; + uint8_t rex_b; + uint8_t opcode; + uint8_t opcode2; + uint8_t modrm; + uint8_t modrm_mod; + uint8_t modrm_reg; + uint8_t modrm_rm; + uint8_t sib; + uint8_t sib_scale; + uint8_t sib_index; + uint8_t sib_base; + union { + uint8_t imm8; + uint16_t imm16; + uint32_t imm32; + uint64_t imm64; + } imm; + union { + uint8_t disp8; + uint16_t disp16; + uint32_t disp32; + } disp; + uint32_t flags; +} hde64s; + +#pragma pack(pop) + +#ifdef __cplusplus +extern "C" { +#endif + +/* __cdecl */ +unsigned int hde64_disasm(const void *code, hde64s *hs); + +#ifdef __cplusplus +} +#endif + +#endif /* _HDE64_H_ */ diff --git a/Alien Isolation/ThirdParty/MinHook/src/hde/pstdint.h b/Alien Isolation/ThirdParty/MinHook/src/hde/pstdint.h new file mode 100644 index 0000000..84d82a0 --- /dev/null +++ b/Alien Isolation/ThirdParty/MinHook/src/hde/pstdint.h @@ -0,0 +1,39 @@ +/* + * MinHook - The Minimalistic API Hooking Library for x64/x86 + * Copyright (C) 2009-2017 Tsuda Kageyu. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include + +// Integer types for HDE. +typedef INT8 int8_t; +typedef INT16 int16_t; +typedef INT32 int32_t; +typedef INT64 int64_t; +typedef UINT8 uint8_t; +typedef UINT16 uint16_t; +typedef UINT32 uint32_t; +typedef UINT64 uint64_t; diff --git a/Alien Isolation/ThirdParty/MinHook/src/hde/table32.h b/Alien Isolation/ThirdParty/MinHook/src/hde/table32.h new file mode 100644 index 0000000..7b3e12e --- /dev/null +++ b/Alien Isolation/ThirdParty/MinHook/src/hde/table32.h @@ -0,0 +1,73 @@ +/* + * Hacker Disassembler Engine 32 C + * Copyright (c) 2008-2009, Vyacheslav Patkov. + * All rights reserved. + * + */ + +#define C_NONE 0x00 +#define C_MODRM 0x01 +#define C_IMM8 0x02 +#define C_IMM16 0x04 +#define C_IMM_P66 0x10 +#define C_REL8 0x20 +#define C_REL32 0x40 +#define C_GROUP 0x80 +#define C_ERROR 0xff + +#define PRE_ANY 0x00 +#define PRE_NONE 0x01 +#define PRE_F2 0x02 +#define PRE_F3 0x04 +#define PRE_66 0x08 +#define PRE_67 0x10 +#define PRE_LOCK 0x20 +#define PRE_SEG 0x40 +#define PRE_ALL 0xff + +#define DELTA_OPCODES 0x4a +#define DELTA_FPU_REG 0xf1 +#define DELTA_FPU_MODRM 0xf8 +#define DELTA_PREFIXES 0x130 +#define DELTA_OP_LOCK_OK 0x1a1 +#define DELTA_OP2_LOCK_OK 0x1b9 +#define DELTA_OP_ONLY_MEM 0x1cb +#define DELTA_OP2_ONLY_MEM 0x1da + +unsigned char hde32_table[] = { + 0xa3,0xa8,0xa3,0xa8,0xa3,0xa8,0xa3,0xa8,0xa3,0xa8,0xa3,0xa8,0xa3,0xa8,0xa3, + 0xa8,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xac,0xaa,0xb2,0xaa,0x9f,0x9f, + 0x9f,0x9f,0xb5,0xa3,0xa3,0xa4,0xaa,0xaa,0xba,0xaa,0x96,0xaa,0xa8,0xaa,0xc3, + 0xc3,0x96,0x96,0xb7,0xae,0xd6,0xbd,0xa3,0xc5,0xa3,0xa3,0x9f,0xc3,0x9c,0xaa, + 0xaa,0xac,0xaa,0xbf,0x03,0x7f,0x11,0x7f,0x01,0x7f,0x01,0x3f,0x01,0x01,0x90, + 0x82,0x7d,0x97,0x59,0x59,0x59,0x59,0x59,0x7f,0x59,0x59,0x60,0x7d,0x7f,0x7f, + 0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x9a,0x88,0x7d, + 0x59,0x50,0x50,0x50,0x50,0x59,0x59,0x59,0x59,0x61,0x94,0x61,0x9e,0x59,0x59, + 0x85,0x59,0x92,0xa3,0x60,0x60,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59,0x59, + 0x59,0x59,0x9f,0x01,0x03,0x01,0x04,0x03,0xd5,0x03,0xcc,0x01,0xbc,0x03,0xf0, + 0x10,0x10,0x10,0x10,0x50,0x50,0x50,0x50,0x14,0x20,0x20,0x20,0x20,0x01,0x01, + 0x01,0x01,0xc4,0x02,0x10,0x00,0x00,0x00,0x00,0x01,0x01,0xc0,0xc2,0x10,0x11, + 0x02,0x03,0x11,0x03,0x03,0x04,0x00,0x00,0x14,0x00,0x02,0x00,0x00,0xc6,0xc8, + 0x02,0x02,0x02,0x02,0x00,0x00,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xca, + 0x01,0x01,0x01,0x00,0x06,0x00,0x04,0x00,0xc0,0xc2,0x01,0x01,0x03,0x01,0xff, + 0xff,0x01,0x00,0x03,0xc4,0xc4,0xc6,0x03,0x01,0x01,0x01,0xff,0x03,0x03,0x03, + 0xc8,0x40,0x00,0x0a,0x00,0x04,0x00,0x00,0x00,0x00,0x7f,0x00,0x33,0x01,0x00, + 0x00,0x00,0x00,0x00,0x00,0xff,0xbf,0xff,0xff,0x00,0x00,0x00,0x00,0x07,0x00, + 0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0xff,0xff,0x00,0x00,0x00,0xbf,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x7f,0x00,0x00,0xff,0x4a,0x4a,0x4a,0x4a,0x4b,0x52,0x4a,0x4a,0x4a,0x4a,0x4f, + 0x4c,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x55,0x45,0x40,0x4a,0x4a,0x4a, + 0x45,0x59,0x4d,0x46,0x4a,0x5d,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a,0x4a, + 0x4a,0x4a,0x4a,0x4a,0x4a,0x61,0x63,0x67,0x4e,0x4a,0x4a,0x6b,0x6d,0x4a,0x4a, + 0x45,0x6d,0x4a,0x4a,0x44,0x45,0x4a,0x4a,0x00,0x00,0x00,0x02,0x0d,0x06,0x06, + 0x06,0x06,0x0e,0x00,0x00,0x00,0x00,0x06,0x06,0x06,0x00,0x06,0x06,0x02,0x06, + 0x00,0x0a,0x0a,0x07,0x07,0x06,0x02,0x05,0x05,0x02,0x02,0x00,0x00,0x04,0x04, + 0x04,0x04,0x00,0x00,0x00,0x0e,0x05,0x06,0x06,0x06,0x01,0x06,0x00,0x00,0x08, + 0x00,0x10,0x00,0x18,0x00,0x20,0x00,0x28,0x00,0x30,0x00,0x80,0x01,0x82,0x01, + 0x86,0x00,0xf6,0xcf,0xfe,0x3f,0xab,0x00,0xb0,0x00,0xb1,0x00,0xb3,0x00,0xba, + 0xf8,0xbb,0x00,0xc0,0x00,0xc1,0x00,0xc7,0xbf,0x62,0xff,0x00,0x8d,0xff,0x00, + 0xc4,0xff,0x00,0xc5,0xff,0x00,0xff,0xff,0xeb,0x01,0xff,0x0e,0x12,0x08,0x00, + 0x13,0x09,0x00,0x16,0x08,0x00,0x17,0x09,0x00,0x2b,0x09,0x00,0xae,0xff,0x07, + 0xb2,0xff,0x00,0xb4,0xff,0x00,0xb5,0xff,0x00,0xc3,0x01,0x00,0xc7,0xff,0xbf, + 0xe7,0x08,0x00,0xf0,0x02,0x00 +}; diff --git a/Alien Isolation/ThirdParty/MinHook/src/hde/table64.h b/Alien Isolation/ThirdParty/MinHook/src/hde/table64.h new file mode 100644 index 0000000..01d4541 --- /dev/null +++ b/Alien Isolation/ThirdParty/MinHook/src/hde/table64.h @@ -0,0 +1,74 @@ +/* + * Hacker Disassembler Engine 64 C + * Copyright (c) 2008-2009, Vyacheslav Patkov. + * All rights reserved. + * + */ + +#define C_NONE 0x00 +#define C_MODRM 0x01 +#define C_IMM8 0x02 +#define C_IMM16 0x04 +#define C_IMM_P66 0x10 +#define C_REL8 0x20 +#define C_REL32 0x40 +#define C_GROUP 0x80 +#define C_ERROR 0xff + +#define PRE_ANY 0x00 +#define PRE_NONE 0x01 +#define PRE_F2 0x02 +#define PRE_F3 0x04 +#define PRE_66 0x08 +#define PRE_67 0x10 +#define PRE_LOCK 0x20 +#define PRE_SEG 0x40 +#define PRE_ALL 0xff + +#define DELTA_OPCODES 0x4a +#define DELTA_FPU_REG 0xfd +#define DELTA_FPU_MODRM 0x104 +#define DELTA_PREFIXES 0x13c +#define DELTA_OP_LOCK_OK 0x1ae +#define DELTA_OP2_LOCK_OK 0x1c6 +#define DELTA_OP_ONLY_MEM 0x1d8 +#define DELTA_OP2_ONLY_MEM 0x1e7 + +unsigned char hde64_table[] = { + 0xa5,0xaa,0xa5,0xb8,0xa5,0xaa,0xa5,0xaa,0xa5,0xb8,0xa5,0xb8,0xa5,0xb8,0xa5, + 0xb8,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xac,0xc0,0xcc,0xc0,0xa1,0xa1, + 0xa1,0xa1,0xb1,0xa5,0xa5,0xa6,0xc0,0xc0,0xd7,0xda,0xe0,0xc0,0xe4,0xc0,0xea, + 0xea,0xe0,0xe0,0x98,0xc8,0xee,0xf1,0xa5,0xd3,0xa5,0xa5,0xa1,0xea,0x9e,0xc0, + 0xc0,0xc2,0xc0,0xe6,0x03,0x7f,0x11,0x7f,0x01,0x7f,0x01,0x3f,0x01,0x01,0xab, + 0x8b,0x90,0x64,0x5b,0x5b,0x5b,0x5b,0x5b,0x92,0x5b,0x5b,0x76,0x90,0x92,0x92, + 0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x6a,0x73,0x90, + 0x5b,0x52,0x52,0x52,0x52,0x5b,0x5b,0x5b,0x5b,0x77,0x7c,0x77,0x85,0x5b,0x5b, + 0x70,0x5b,0x7a,0xaf,0x76,0x76,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b, + 0x5b,0x5b,0x86,0x01,0x03,0x01,0x04,0x03,0xd5,0x03,0xd5,0x03,0xcc,0x01,0xbc, + 0x03,0xf0,0x03,0x03,0x04,0x00,0x50,0x50,0x50,0x50,0xff,0x20,0x20,0x20,0x20, + 0x01,0x01,0x01,0x01,0xc4,0x02,0x10,0xff,0xff,0xff,0x01,0x00,0x03,0x11,0xff, + 0x03,0xc4,0xc6,0xc8,0x02,0x10,0x00,0xff,0xcc,0x01,0x01,0x01,0x00,0x00,0x00, + 0x00,0x01,0x01,0x03,0x01,0xff,0xff,0xc0,0xc2,0x10,0x11,0x02,0x03,0x01,0x01, + 0x01,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0xff,0xff,0xff,0xff,0x10, + 0x10,0x10,0x10,0x02,0x10,0x00,0x00,0xc6,0xc8,0x02,0x02,0x02,0x02,0x06,0x00, + 0x04,0x00,0x02,0xff,0x00,0xc0,0xc2,0x01,0x01,0x03,0x03,0x03,0xca,0x40,0x00, + 0x0a,0x00,0x04,0x00,0x00,0x00,0x00,0x7f,0x00,0x33,0x01,0x00,0x00,0x00,0x00, + 0x00,0x00,0xff,0xbf,0xff,0xff,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0xff,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff, + 0x00,0x00,0x00,0xbf,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x00,0x00, + 0xff,0x40,0x40,0x40,0x40,0x41,0x49,0x40,0x40,0x40,0x40,0x4c,0x42,0x40,0x40, + 0x40,0x40,0x40,0x40,0x40,0x40,0x4f,0x44,0x53,0x40,0x40,0x40,0x44,0x57,0x43, + 0x5c,0x40,0x60,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40, + 0x40,0x40,0x64,0x66,0x6e,0x6b,0x40,0x40,0x6a,0x46,0x40,0x40,0x44,0x46,0x40, + 0x40,0x5b,0x44,0x40,0x40,0x00,0x00,0x00,0x00,0x06,0x06,0x06,0x06,0x01,0x06, + 0x06,0x02,0x06,0x06,0x00,0x06,0x00,0x0a,0x0a,0x00,0x00,0x00,0x02,0x07,0x07, + 0x06,0x02,0x0d,0x06,0x06,0x06,0x0e,0x05,0x05,0x02,0x02,0x00,0x00,0x04,0x04, + 0x04,0x04,0x05,0x06,0x06,0x06,0x00,0x00,0x00,0x0e,0x00,0x00,0x08,0x00,0x10, + 0x00,0x18,0x00,0x20,0x00,0x28,0x00,0x30,0x00,0x80,0x01,0x82,0x01,0x86,0x00, + 0xf6,0xcf,0xfe,0x3f,0xab,0x00,0xb0,0x00,0xb1,0x00,0xb3,0x00,0xba,0xf8,0xbb, + 0x00,0xc0,0x00,0xc1,0x00,0xc7,0xbf,0x62,0xff,0x00,0x8d,0xff,0x00,0xc4,0xff, + 0x00,0xc5,0xff,0x00,0xff,0xff,0xeb,0x01,0xff,0x0e,0x12,0x08,0x00,0x13,0x09, + 0x00,0x16,0x08,0x00,0x17,0x09,0x00,0x2b,0x09,0x00,0xae,0xff,0x07,0xb2,0xff, + 0x00,0xb4,0xff,0x00,0xb5,0xff,0x00,0xc3,0x01,0x00,0xc7,0xff,0xbf,0xe7,0x08, + 0x00,0xf0,0x02,0x00 +}; diff --git a/Alien Isolation/ThirdParty/MinHook/src/hook.c b/Alien Isolation/ThirdParty/MinHook/src/hook.c new file mode 100644 index 0000000..ce65e57 --- /dev/null +++ b/Alien Isolation/ThirdParty/MinHook/src/hook.c @@ -0,0 +1,889 @@ +/* + * MinHook - The Minimalistic API Hooking Library for x64/x86 + * Copyright (C) 2009-2017 Tsuda Kageyu. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +#include "../include/MinHook.h" +#include "buffer.h" +#include "trampoline.h" + +#ifndef ARRAYSIZE + #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0])) +#endif + +// Initial capacity of the HOOK_ENTRY buffer. +#define INITIAL_HOOK_CAPACITY 32 + +// Initial capacity of the thread IDs buffer. +#define INITIAL_THREAD_CAPACITY 128 + +// Special hook position values. +#define INVALID_HOOK_POS UINT_MAX +#define ALL_HOOKS_POS UINT_MAX + +// Freeze() action argument defines. +#define ACTION_DISABLE 0 +#define ACTION_ENABLE 1 +#define ACTION_APPLY_QUEUED 2 + +// Thread access rights for suspending/resuming threads. +#define THREAD_ACCESS \ + (THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION | THREAD_SET_CONTEXT) + +// Hook information. +typedef struct _HOOK_ENTRY +{ + LPVOID pTarget; // Address of the target function. + LPVOID pDetour; // Address of the detour or relay function. + LPVOID pTrampoline; // Address of the trampoline function. + UINT8 backup[8]; // Original prologue of the target function. + + UINT8 patchAbove : 1; // Uses the hot patch area. + UINT8 isEnabled : 1; // Enabled. + UINT8 queueEnable : 1; // Queued for enabling/disabling when != isEnabled. + + UINT nIP : 4; // Count of the instruction boundaries. + UINT8 oldIPs[8]; // Instruction boundaries of the target function. + UINT8 newIPs[8]; // Instruction boundaries of the trampoline function. +} HOOK_ENTRY, *PHOOK_ENTRY; + +// Suspended threads for Freeze()/Unfreeze(). +typedef struct _FROZEN_THREADS +{ + LPDWORD pItems; // Data heap + UINT capacity; // Size of allocated data heap, items + UINT size; // Actual number of data items +} FROZEN_THREADS, *PFROZEN_THREADS; + +//------------------------------------------------------------------------- +// Global Variables: +//------------------------------------------------------------------------- + +// Spin lock flag for EnterSpinLock()/LeaveSpinLock(). +volatile LONG g_isLocked = FALSE; + +// Private heap handle. If not NULL, this library is initialized. +HANDLE g_hHeap = NULL; + +// Hook entries. +struct +{ + PHOOK_ENTRY pItems; // Data heap + UINT capacity; // Size of allocated data heap, items + UINT size; // Actual number of data items +} g_hooks; + +//------------------------------------------------------------------------- +// Returns INVALID_HOOK_POS if not found. +static UINT FindHookEntry(LPVOID pTarget) +{ + UINT i; + for (i = 0; i < g_hooks.size; ++i) + { + if ((ULONG_PTR)pTarget == (ULONG_PTR)g_hooks.pItems[i].pTarget) + return i; + } + + return INVALID_HOOK_POS; +} + +//------------------------------------------------------------------------- +static PHOOK_ENTRY AddHookEntry() +{ + if (g_hooks.pItems == NULL) + { + g_hooks.capacity = INITIAL_HOOK_CAPACITY; + g_hooks.pItems = (PHOOK_ENTRY)HeapAlloc( + g_hHeap, 0, g_hooks.capacity * sizeof(HOOK_ENTRY)); + if (g_hooks.pItems == NULL) + return NULL; + } + else if (g_hooks.size >= g_hooks.capacity) + { + PHOOK_ENTRY p = (PHOOK_ENTRY)HeapReAlloc( + g_hHeap, 0, g_hooks.pItems, (g_hooks.capacity * 2) * sizeof(HOOK_ENTRY)); + if (p == NULL) + return NULL; + + g_hooks.capacity *= 2; + g_hooks.pItems = p; + } + + return &g_hooks.pItems[g_hooks.size++]; +} + +//------------------------------------------------------------------------- +static void DeleteHookEntry(UINT pos) +{ + if (pos < g_hooks.size - 1) + g_hooks.pItems[pos] = g_hooks.pItems[g_hooks.size - 1]; + + g_hooks.size--; + + if (g_hooks.capacity / 2 >= INITIAL_HOOK_CAPACITY && g_hooks.capacity / 2 >= g_hooks.size) + { + PHOOK_ENTRY p = (PHOOK_ENTRY)HeapReAlloc( + g_hHeap, 0, g_hooks.pItems, (g_hooks.capacity / 2) * sizeof(HOOK_ENTRY)); + if (p == NULL) + return; + + g_hooks.capacity /= 2; + g_hooks.pItems = p; + } +} + +//------------------------------------------------------------------------- +static DWORD_PTR FindOldIP(PHOOK_ENTRY pHook, DWORD_PTR ip) +{ + UINT i; + + if (pHook->patchAbove && ip == ((DWORD_PTR)pHook->pTarget - sizeof(JMP_REL))) + return (DWORD_PTR)pHook->pTarget; + + for (i = 0; i < pHook->nIP; ++i) + { + if (ip == ((DWORD_PTR)pHook->pTrampoline + pHook->newIPs[i])) + return (DWORD_PTR)pHook->pTarget + pHook->oldIPs[i]; + } + +#if defined(_M_X64) || defined(__x86_64__) + // Check relay function. + if (ip == (DWORD_PTR)pHook->pDetour) + return (DWORD_PTR)pHook->pTarget; +#endif + + return 0; +} + +//------------------------------------------------------------------------- +static DWORD_PTR FindNewIP(PHOOK_ENTRY pHook, DWORD_PTR ip) +{ + UINT i; + for (i = 0; i < pHook->nIP; ++i) + { + if (ip == ((DWORD_PTR)pHook->pTarget + pHook->oldIPs[i])) + return (DWORD_PTR)pHook->pTrampoline + pHook->newIPs[i]; + } + + return 0; +} + +//------------------------------------------------------------------------- +static void ProcessThreadIPs(HANDLE hThread, UINT pos, UINT action) +{ + // If the thread suspended in the overwritten area, + // move IP to the proper address. + + CONTEXT c; +#if defined(_M_X64) || defined(__x86_64__) + DWORD64 *pIP = &c.Rip; +#else + DWORD *pIP = &c.Eip; +#endif + UINT count; + + c.ContextFlags = CONTEXT_CONTROL; + if (!GetThreadContext(hThread, &c)) + return; + + if (pos == ALL_HOOKS_POS) + { + pos = 0; + count = g_hooks.size; + } + else + { + count = pos + 1; + } + + for (; pos < count; ++pos) + { + PHOOK_ENTRY pHook = &g_hooks.pItems[pos]; + BOOL enable; + DWORD_PTR ip; + + switch (action) + { + case ACTION_DISABLE: + enable = FALSE; + break; + + case ACTION_ENABLE: + enable = TRUE; + break; + + default: // ACTION_APPLY_QUEUED + enable = pHook->queueEnable; + break; + } + if (pHook->isEnabled == enable) + continue; + + if (enable) + ip = FindNewIP(pHook, *pIP); + else + ip = FindOldIP(pHook, *pIP); + + if (ip != 0) + { + *pIP = ip; + SetThreadContext(hThread, &c); + } + } +} + +//------------------------------------------------------------------------- +static VOID EnumerateThreads(PFROZEN_THREADS pThreads) +{ + HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); + if (hSnapshot != INVALID_HANDLE_VALUE) + { + THREADENTRY32 te; + te.dwSize = sizeof(THREADENTRY32); + if (Thread32First(hSnapshot, &te)) + { + do + { + if (te.dwSize >= (FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(DWORD)) + && te.th32OwnerProcessID == GetCurrentProcessId() + && te.th32ThreadID != GetCurrentThreadId()) + { + if (pThreads->pItems == NULL) + { + pThreads->capacity = INITIAL_THREAD_CAPACITY; + pThreads->pItems + = (LPDWORD)HeapAlloc(g_hHeap, 0, pThreads->capacity * sizeof(DWORD)); + if (pThreads->pItems == NULL) + break; + } + else if (pThreads->size >= pThreads->capacity) + { + LPDWORD p = (LPDWORD)HeapReAlloc( + g_hHeap, 0, pThreads->pItems, (pThreads->capacity * 2) * sizeof(DWORD)); + if (p == NULL) + break; + + pThreads->capacity *= 2; + pThreads->pItems = p; + } + pThreads->pItems[pThreads->size++] = te.th32ThreadID; + } + + te.dwSize = sizeof(THREADENTRY32); + } while (Thread32Next(hSnapshot, &te)); + } + CloseHandle(hSnapshot); + } +} + +//------------------------------------------------------------------------- +static VOID Freeze(PFROZEN_THREADS pThreads, UINT pos, UINT action) +{ + pThreads->pItems = NULL; + pThreads->capacity = 0; + pThreads->size = 0; + EnumerateThreads(pThreads); + + if (pThreads->pItems != NULL) + { + UINT i; + for (i = 0; i < pThreads->size; ++i) + { + HANDLE hThread = OpenThread(THREAD_ACCESS, FALSE, pThreads->pItems[i]); + if (hThread != NULL) + { + SuspendThread(hThread); + ProcessThreadIPs(hThread, pos, action); + CloseHandle(hThread); + } + } + } +} + +//------------------------------------------------------------------------- +static VOID Unfreeze(PFROZEN_THREADS pThreads) +{ + if (pThreads->pItems != NULL) + { + UINT i; + for (i = 0; i < pThreads->size; ++i) + { + HANDLE hThread = OpenThread(THREAD_ACCESS, FALSE, pThreads->pItems[i]); + if (hThread != NULL) + { + ResumeThread(hThread); + CloseHandle(hThread); + } + } + + HeapFree(g_hHeap, 0, pThreads->pItems); + } +} + +//------------------------------------------------------------------------- +static MH_STATUS EnableHookLL(UINT pos, BOOL enable) +{ + PHOOK_ENTRY pHook = &g_hooks.pItems[pos]; + DWORD oldProtect; + SIZE_T patchSize = sizeof(JMP_REL); + LPBYTE pPatchTarget = (LPBYTE)pHook->pTarget; + + if (pHook->patchAbove) + { + pPatchTarget -= sizeof(JMP_REL); + patchSize += sizeof(JMP_REL_SHORT); + } + + if (!VirtualProtect(pPatchTarget, patchSize, PAGE_EXECUTE_READWRITE, &oldProtect)) + return MH_ERROR_MEMORY_PROTECT; + + if (enable) + { + PJMP_REL pJmp = (PJMP_REL)pPatchTarget; + pJmp->opcode = 0xE9; + pJmp->operand = (UINT32)((LPBYTE)pHook->pDetour - (pPatchTarget + sizeof(JMP_REL))); + + if (pHook->patchAbove) + { + PJMP_REL_SHORT pShortJmp = (PJMP_REL_SHORT)pHook->pTarget; + pShortJmp->opcode = 0xEB; + pShortJmp->operand = (UINT8)(0 - (sizeof(JMP_REL_SHORT) + sizeof(JMP_REL))); + } + } + else + { + if (pHook->patchAbove) + memcpy(pPatchTarget, pHook->backup, sizeof(JMP_REL) + sizeof(JMP_REL_SHORT)); + else + memcpy(pPatchTarget, pHook->backup, sizeof(JMP_REL)); + } + + VirtualProtect(pPatchTarget, patchSize, oldProtect, &oldProtect); + + // Just-in-case measure. + FlushInstructionCache(GetCurrentProcess(), pPatchTarget, patchSize); + + pHook->isEnabled = enable; + pHook->queueEnable = enable; + + return MH_OK; +} + +//------------------------------------------------------------------------- +static MH_STATUS EnableAllHooksLL(BOOL enable) +{ + MH_STATUS status = MH_OK; + UINT i, first = INVALID_HOOK_POS; + + for (i = 0; i < g_hooks.size; ++i) + { + if (g_hooks.pItems[i].isEnabled != enable) + { + first = i; + break; + } + } + + if (first != INVALID_HOOK_POS) + { + FROZEN_THREADS threads; + Freeze(&threads, ALL_HOOKS_POS, enable ? ACTION_ENABLE : ACTION_DISABLE); + + for (i = first; i < g_hooks.size; ++i) + { + if (g_hooks.pItems[i].isEnabled != enable) + { + status = EnableHookLL(i, enable); + if (status != MH_OK) + break; + } + } + + Unfreeze(&threads); + } + + return status; +} + +//------------------------------------------------------------------------- +static VOID EnterSpinLock(VOID) +{ + SIZE_T spinCount = 0; + + // Wait until the flag is FALSE. + while (InterlockedCompareExchange(&g_isLocked, TRUE, FALSE) != FALSE) + { + // No need to generate a memory barrier here, since InterlockedCompareExchange() + // generates a full memory barrier itself. + + // Prevent the loop from being too busy. + if (spinCount < 32) + Sleep(0); + else + Sleep(1); + + spinCount++; + } +} + +//------------------------------------------------------------------------- +static VOID LeaveSpinLock(VOID) +{ + // No need to generate a memory barrier here, since InterlockedExchange() + // generates a full memory barrier itself. + + InterlockedExchange(&g_isLocked, FALSE); +} + +//------------------------------------------------------------------------- +MH_STATUS WINAPI MH_Initialize(VOID) +{ + MH_STATUS status = MH_OK; + + EnterSpinLock(); + + if (g_hHeap == NULL) + { + g_hHeap = HeapCreate(0, 0, 0); + if (g_hHeap != NULL) + { + // Initialize the internal function buffer. + InitializeBuffer(); + } + else + { + status = MH_ERROR_MEMORY_ALLOC; + } + } + else + { + status = MH_ERROR_ALREADY_INITIALIZED; + } + + LeaveSpinLock(); + + return status; +} + +//------------------------------------------------------------------------- +MH_STATUS WINAPI MH_Uninitialize(VOID) +{ + MH_STATUS status = MH_OK; + + EnterSpinLock(); + + if (g_hHeap != NULL) + { + status = EnableAllHooksLL(FALSE); + if (status == MH_OK) + { + // Free the internal function buffer. + + // HeapFree is actually not required, but some tools detect a false + // memory leak without HeapFree. + + UninitializeBuffer(); + + HeapFree(g_hHeap, 0, g_hooks.pItems); + HeapDestroy(g_hHeap); + + g_hHeap = NULL; + + g_hooks.pItems = NULL; + g_hooks.capacity = 0; + g_hooks.size = 0; + } + } + else + { + status = MH_ERROR_NOT_INITIALIZED; + } + + LeaveSpinLock(); + + return status; +} + +//------------------------------------------------------------------------- +MH_STATUS WINAPI MH_CreateHook(LPVOID pTarget, LPVOID pDetour, LPVOID *ppOriginal) +{ + MH_STATUS status = MH_OK; + + EnterSpinLock(); + + if (g_hHeap != NULL) + { + if (IsExecutableAddress(pTarget) && IsExecutableAddress(pDetour)) + { + UINT pos = FindHookEntry(pTarget); + if (pos == INVALID_HOOK_POS) + { + LPVOID pBuffer = AllocateBuffer(pTarget); + if (pBuffer != NULL) + { + TRAMPOLINE ct; + + ct.pTarget = pTarget; + ct.pDetour = pDetour; + ct.pTrampoline = pBuffer; + if (CreateTrampolineFunction(&ct)) + { + PHOOK_ENTRY pHook = AddHookEntry(); + if (pHook != NULL) + { + pHook->pTarget = ct.pTarget; +#if defined(_M_X64) || defined(__x86_64__) + pHook->pDetour = ct.pRelay; +#else + pHook->pDetour = ct.pDetour; +#endif + pHook->pTrampoline = ct.pTrampoline; + pHook->patchAbove = ct.patchAbove; + pHook->isEnabled = FALSE; + pHook->queueEnable = FALSE; + pHook->nIP = ct.nIP; + memcpy(pHook->oldIPs, ct.oldIPs, ARRAYSIZE(ct.oldIPs)); + memcpy(pHook->newIPs, ct.newIPs, ARRAYSIZE(ct.newIPs)); + + // Back up the target function. + + if (ct.patchAbove) + { + memcpy( + pHook->backup, + (LPBYTE)pTarget - sizeof(JMP_REL), + sizeof(JMP_REL) + sizeof(JMP_REL_SHORT)); + } + else + { + memcpy(pHook->backup, pTarget, sizeof(JMP_REL)); + } + + if (ppOriginal != NULL) + *ppOriginal = pHook->pTrampoline; + } + else + { + status = MH_ERROR_MEMORY_ALLOC; + } + } + else + { + status = MH_ERROR_UNSUPPORTED_FUNCTION; + } + + if (status != MH_OK) + { + FreeBuffer(pBuffer); + } + } + else + { + status = MH_ERROR_MEMORY_ALLOC; + } + } + else + { + status = MH_ERROR_ALREADY_CREATED; + } + } + else + { + status = MH_ERROR_NOT_EXECUTABLE; + } + } + else + { + status = MH_ERROR_NOT_INITIALIZED; + } + + LeaveSpinLock(); + + return status; +} + +//------------------------------------------------------------------------- +MH_STATUS WINAPI MH_RemoveHook(LPVOID pTarget) +{ + MH_STATUS status = MH_OK; + + EnterSpinLock(); + + if (g_hHeap != NULL) + { + UINT pos = FindHookEntry(pTarget); + if (pos != INVALID_HOOK_POS) + { + if (g_hooks.pItems[pos].isEnabled) + { + FROZEN_THREADS threads; + Freeze(&threads, pos, ACTION_DISABLE); + + status = EnableHookLL(pos, FALSE); + + Unfreeze(&threads); + } + + if (status == MH_OK) + { + FreeBuffer(g_hooks.pItems[pos].pTrampoline); + DeleteHookEntry(pos); + } + } + else + { + status = MH_ERROR_NOT_CREATED; + } + } + else + { + status = MH_ERROR_NOT_INITIALIZED; + } + + LeaveSpinLock(); + + return status; +} + +//------------------------------------------------------------------------- +static MH_STATUS EnableHook(LPVOID pTarget, BOOL enable) +{ + MH_STATUS status = MH_OK; + + EnterSpinLock(); + + if (g_hHeap != NULL) + { + if (pTarget == MH_ALL_HOOKS) + { + status = EnableAllHooksLL(enable); + } + else + { + FROZEN_THREADS threads; + UINT pos = FindHookEntry(pTarget); + if (pos != INVALID_HOOK_POS) + { + if (g_hooks.pItems[pos].isEnabled != enable) + { + Freeze(&threads, pos, ACTION_ENABLE); + + status = EnableHookLL(pos, enable); + + Unfreeze(&threads); + } + else + { + status = enable ? MH_ERROR_ENABLED : MH_ERROR_DISABLED; + } + } + else + { + status = MH_ERROR_NOT_CREATED; + } + } + } + else + { + status = MH_ERROR_NOT_INITIALIZED; + } + + LeaveSpinLock(); + + return status; +} + +//------------------------------------------------------------------------- +MH_STATUS WINAPI MH_EnableHook(LPVOID pTarget) +{ + return EnableHook(pTarget, TRUE); +} + +//------------------------------------------------------------------------- +MH_STATUS WINAPI MH_DisableHook(LPVOID pTarget) +{ + return EnableHook(pTarget, FALSE); +} + +//------------------------------------------------------------------------- +static MH_STATUS QueueHook(LPVOID pTarget, BOOL queueEnable) +{ + MH_STATUS status = MH_OK; + + EnterSpinLock(); + + if (g_hHeap != NULL) + { + if (pTarget == MH_ALL_HOOKS) + { + UINT i; + for (i = 0; i < g_hooks.size; ++i) + g_hooks.pItems[i].queueEnable = queueEnable; + } + else + { + UINT pos = FindHookEntry(pTarget); + if (pos != INVALID_HOOK_POS) + { + g_hooks.pItems[pos].queueEnable = queueEnable; + } + else + { + status = MH_ERROR_NOT_CREATED; + } + } + } + else + { + status = MH_ERROR_NOT_INITIALIZED; + } + + LeaveSpinLock(); + + return status; +} + +//------------------------------------------------------------------------- +MH_STATUS WINAPI MH_QueueEnableHook(LPVOID pTarget) +{ + return QueueHook(pTarget, TRUE); +} + +//------------------------------------------------------------------------- +MH_STATUS WINAPI MH_QueueDisableHook(LPVOID pTarget) +{ + return QueueHook(pTarget, FALSE); +} + +//------------------------------------------------------------------------- +MH_STATUS WINAPI MH_ApplyQueued(VOID) +{ + MH_STATUS status = MH_OK; + UINT i, first = INVALID_HOOK_POS; + + EnterSpinLock(); + + if (g_hHeap != NULL) + { + for (i = 0; i < g_hooks.size; ++i) + { + if (g_hooks.pItems[i].isEnabled != g_hooks.pItems[i].queueEnable) + { + first = i; + break; + } + } + + if (first != INVALID_HOOK_POS) + { + FROZEN_THREADS threads; + Freeze(&threads, ALL_HOOKS_POS, ACTION_APPLY_QUEUED); + + for (i = first; i < g_hooks.size; ++i) + { + PHOOK_ENTRY pHook = &g_hooks.pItems[i]; + if (pHook->isEnabled != pHook->queueEnable) + { + status = EnableHookLL(i, pHook->queueEnable); + if (status != MH_OK) + break; + } + } + + Unfreeze(&threads); + } + } + else + { + status = MH_ERROR_NOT_INITIALIZED; + } + + LeaveSpinLock(); + + return status; +} + +//------------------------------------------------------------------------- +MH_STATUS WINAPI MH_CreateHookApiEx( + LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, + LPVOID *ppOriginal, LPVOID *ppTarget) +{ + HMODULE hModule; + LPVOID pTarget; + + hModule = GetModuleHandleW(pszModule); + if (hModule == NULL) + return MH_ERROR_MODULE_NOT_FOUND; + + pTarget = (LPVOID)GetProcAddress(hModule, pszProcName); + if (pTarget == NULL) + return MH_ERROR_FUNCTION_NOT_FOUND; + + if(ppTarget != NULL) + *ppTarget = pTarget; + + return MH_CreateHook(pTarget, pDetour, ppOriginal); +} + +//------------------------------------------------------------------------- +MH_STATUS WINAPI MH_CreateHookApi( + LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, LPVOID *ppOriginal) +{ + return MH_CreateHookApiEx(pszModule, pszProcName, pDetour, ppOriginal, NULL); +} + +//------------------------------------------------------------------------- +const char * WINAPI MH_StatusToString(MH_STATUS status) +{ +#define MH_ST2STR(x) \ + case x: \ + return #x; + + switch (status) { + MH_ST2STR(MH_UNKNOWN) + MH_ST2STR(MH_OK) + MH_ST2STR(MH_ERROR_ALREADY_INITIALIZED) + MH_ST2STR(MH_ERROR_NOT_INITIALIZED) + MH_ST2STR(MH_ERROR_ALREADY_CREATED) + MH_ST2STR(MH_ERROR_NOT_CREATED) + MH_ST2STR(MH_ERROR_ENABLED) + MH_ST2STR(MH_ERROR_DISABLED) + MH_ST2STR(MH_ERROR_NOT_EXECUTABLE) + MH_ST2STR(MH_ERROR_UNSUPPORTED_FUNCTION) + MH_ST2STR(MH_ERROR_MEMORY_ALLOC) + MH_ST2STR(MH_ERROR_MEMORY_PROTECT) + MH_ST2STR(MH_ERROR_MODULE_NOT_FOUND) + MH_ST2STR(MH_ERROR_FUNCTION_NOT_FOUND) + } + +#undef MH_ST2STR + + return "(unknown)"; +} diff --git a/Alien Isolation/ThirdParty/MinHook/src/trampoline.c b/Alien Isolation/ThirdParty/MinHook/src/trampoline.c new file mode 100644 index 0000000..ac37f0f --- /dev/null +++ b/Alien Isolation/ThirdParty/MinHook/src/trampoline.c @@ -0,0 +1,316 @@ +/* + * MinHook - The Minimalistic API Hooking Library for x64/x86 + * Copyright (C) 2009-2017 Tsuda Kageyu. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#ifndef ARRAYSIZE + #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0])) +#endif + +#if defined(_M_X64) || defined(__x86_64__) + #include "./hde/hde64.h" + typedef hde64s HDE; + #define HDE_DISASM(code, hs) hde64_disasm(code, hs) +#else + #include "./hde/hde32.h" + typedef hde32s HDE; + #define HDE_DISASM(code, hs) hde32_disasm(code, hs) +#endif + +#include "trampoline.h" +#include "buffer.h" + +// Maximum size of a trampoline function. +#if defined(_M_X64) || defined(__x86_64__) + #define TRAMPOLINE_MAX_SIZE (MEMORY_SLOT_SIZE - sizeof(JMP_ABS)) +#else + #define TRAMPOLINE_MAX_SIZE MEMORY_SLOT_SIZE +#endif + +//------------------------------------------------------------------------- +static BOOL IsCodePadding(LPBYTE pInst, UINT size) +{ + UINT i; + + if (pInst[0] != 0x00 && pInst[0] != 0x90 && pInst[0] != 0xCC) + return FALSE; + + for (i = 1; i < size; ++i) + { + if (pInst[i] != pInst[0]) + return FALSE; + } + return TRUE; +} + +//------------------------------------------------------------------------- +BOOL CreateTrampolineFunction(PTRAMPOLINE ct) +{ +#if defined(_M_X64) || defined(__x86_64__) + CALL_ABS call = { + 0xFF, 0x15, 0x00000002, // FF15 00000002: CALL [RIP+8] + 0xEB, 0x08, // EB 08: JMP +10 + 0x0000000000000000ULL // Absolute destination address + }; + JMP_ABS jmp = { + 0xFF, 0x25, 0x00000000, // FF25 00000000: JMP [RIP+6] + 0x0000000000000000ULL // Absolute destination address + }; + JCC_ABS jcc = { + 0x70, 0x0E, // 7* 0E: J** +16 + 0xFF, 0x25, 0x00000000, // FF25 00000000: JMP [RIP+6] + 0x0000000000000000ULL // Absolute destination address + }; +#else + CALL_REL call = { + 0xE8, // E8 xxxxxxxx: CALL +5+xxxxxxxx + 0x00000000 // Relative destination address + }; + JMP_REL jmp = { + 0xE9, // E9 xxxxxxxx: JMP +5+xxxxxxxx + 0x00000000 // Relative destination address + }; + JCC_REL jcc = { + 0x0F, 0x80, // 0F8* xxxxxxxx: J** +6+xxxxxxxx + 0x00000000 // Relative destination address + }; +#endif + + UINT8 oldPos = 0; + UINT8 newPos = 0; + ULONG_PTR jmpDest = 0; // Destination address of an internal jump. + BOOL finished = FALSE; // Is the function completed? +#if defined(_M_X64) || defined(__x86_64__) + UINT8 instBuf[16]; +#endif + + ct->patchAbove = FALSE; + ct->nIP = 0; + + do + { + HDE hs; + UINT copySize; + LPVOID pCopySrc; + ULONG_PTR pOldInst = (ULONG_PTR)ct->pTarget + oldPos; + ULONG_PTR pNewInst = (ULONG_PTR)ct->pTrampoline + newPos; + + copySize = HDE_DISASM((LPVOID)pOldInst, &hs); + if (hs.flags & F_ERROR) + return FALSE; + + pCopySrc = (LPVOID)pOldInst; + if (oldPos >= sizeof(JMP_REL)) + { + // The trampoline function is long enough. + // Complete the function with the jump to the target function. +#if defined(_M_X64) || defined(__x86_64__) + jmp.address = pOldInst; +#else + jmp.operand = (UINT32)(pOldInst - (pNewInst + sizeof(jmp))); +#endif + pCopySrc = &jmp; + copySize = sizeof(jmp); + + finished = TRUE; + } +#if defined(_M_X64) || defined(__x86_64__) + else if ((hs.modrm & 0xC7) == 0x05) + { + // Instructions using RIP relative addressing. (ModR/M = 00???101B) + + // Modify the RIP relative address. + PUINT32 pRelAddr; + + // Avoid using memcpy to reduce the footprint. +#ifndef _MSC_VER + memcpy(instBuf, (LPBYTE)pOldInst, copySize); +#else + __movsb(instBuf, (LPBYTE)pOldInst, copySize); +#endif + pCopySrc = instBuf; + + // Relative address is stored at (instruction length - immediate value length - 4). + pRelAddr = (PUINT32)(instBuf + hs.len - ((hs.flags & 0x3C) >> 2) - 4); + *pRelAddr + = (UINT32)((pOldInst + hs.len + (INT32)hs.disp.disp32) - (pNewInst + hs.len)); + + // Complete the function if JMP (FF /4). + if (hs.opcode == 0xFF && hs.modrm_reg == 4) + finished = TRUE; + } +#endif + else if (hs.opcode == 0xE8) + { + // Direct relative CALL + ULONG_PTR dest = pOldInst + hs.len + (INT32)hs.imm.imm32; +#if defined(_M_X64) || defined(__x86_64__) + call.address = dest; +#else + call.operand = (UINT32)(dest - (pNewInst + sizeof(call))); +#endif + pCopySrc = &call; + copySize = sizeof(call); + } + else if ((hs.opcode & 0xFD) == 0xE9) + { + // Direct relative JMP (EB or E9) + ULONG_PTR dest = pOldInst + hs.len; + + if (hs.opcode == 0xEB) // isShort jmp + dest += (INT8)hs.imm.imm8; + else + dest += (INT32)hs.imm.imm32; + + // Simply copy an internal jump. + if ((ULONG_PTR)ct->pTarget <= dest + && dest < ((ULONG_PTR)ct->pTarget + sizeof(JMP_REL))) + { + if (jmpDest < dest) + jmpDest = dest; + } + else + { +#if defined(_M_X64) || defined(__x86_64__) + jmp.address = dest; +#else + jmp.operand = (UINT32)(dest - (pNewInst + sizeof(jmp))); +#endif + pCopySrc = &jmp; + copySize = sizeof(jmp); + + // Exit the function If it is not in the branch + finished = (pOldInst >= jmpDest); + } + } + else if ((hs.opcode & 0xF0) == 0x70 + || (hs.opcode & 0xFC) == 0xE0 + || (hs.opcode2 & 0xF0) == 0x80) + { + // Direct relative Jcc + ULONG_PTR dest = pOldInst + hs.len; + + if ((hs.opcode & 0xF0) == 0x70 // Jcc + || (hs.opcode & 0xFC) == 0xE0) // LOOPNZ/LOOPZ/LOOP/JECXZ + dest += (INT8)hs.imm.imm8; + else + dest += (INT32)hs.imm.imm32; + + // Simply copy an internal jump. + if ((ULONG_PTR)ct->pTarget <= dest + && dest < ((ULONG_PTR)ct->pTarget + sizeof(JMP_REL))) + { + if (jmpDest < dest) + jmpDest = dest; + } + else if ((hs.opcode & 0xFC) == 0xE0) + { + // LOOPNZ/LOOPZ/LOOP/JCXZ/JECXZ to the outside are not supported. + return FALSE; + } + else + { + UINT8 cond = ((hs.opcode != 0x0F ? hs.opcode : hs.opcode2) & 0x0F); +#if defined(_M_X64) || defined(__x86_64__) + // Invert the condition in x64 mode to simplify the conditional jump logic. + jcc.opcode = 0x71 ^ cond; + jcc.address = dest; +#else + jcc.opcode1 = 0x80 | cond; + jcc.operand = (UINT32)(dest - (pNewInst + sizeof(jcc))); +#endif + pCopySrc = &jcc; + copySize = sizeof(jcc); + } + } + else if ((hs.opcode & 0xFE) == 0xC2) + { + // RET (C2 or C3) + + // Complete the function if not in a branch. + finished = (pOldInst >= jmpDest); + } + + // Can't alter the instruction length in a branch. + if (pOldInst < jmpDest && copySize != hs.len) + return FALSE; + + // Trampoline function is too large. + if ((newPos + copySize) > TRAMPOLINE_MAX_SIZE) + return FALSE; + + // Trampoline function has too many instructions. + if (ct->nIP >= ARRAYSIZE(ct->oldIPs)) + return FALSE; + + ct->oldIPs[ct->nIP] = oldPos; + ct->newIPs[ct->nIP] = newPos; + ct->nIP++; + + // Avoid using memcpy to reduce the footprint. +#ifndef _MSC_VER + memcpy((LPBYTE)ct->pTrampoline + newPos, pCopySrc, copySize); +#else + __movsb((LPBYTE)ct->pTrampoline + newPos, pCopySrc, copySize); +#endif + newPos += copySize; + oldPos += hs.len; + } + while (!finished); + + // Is there enough place for a long jump? + if (oldPos < sizeof(JMP_REL) + && !IsCodePadding((LPBYTE)ct->pTarget + oldPos, sizeof(JMP_REL) - oldPos)) + { + // Is there enough place for a short jump? + if (oldPos < sizeof(JMP_REL_SHORT) + && !IsCodePadding((LPBYTE)ct->pTarget + oldPos, sizeof(JMP_REL_SHORT) - oldPos)) + { + return FALSE; + } + + // Can we place the long jump above the function? + if (!IsExecutableAddress((LPBYTE)ct->pTarget - sizeof(JMP_REL))) + return FALSE; + + if (!IsCodePadding((LPBYTE)ct->pTarget - sizeof(JMP_REL), sizeof(JMP_REL))) + return FALSE; + + ct->patchAbove = TRUE; + } + +#if defined(_M_X64) || defined(__x86_64__) + // Create a relay function. + jmp.address = (ULONG_PTR)ct->pDetour; + + ct->pRelay = (LPBYTE)ct->pTrampoline + newPos; + memcpy(ct->pRelay, &jmp, sizeof(jmp)); +#endif + + return TRUE; +} diff --git a/Alien Isolation/ThirdParty/MinHook/src/trampoline.h b/Alien Isolation/ThirdParty/MinHook/src/trampoline.h new file mode 100644 index 0000000..bdffdac --- /dev/null +++ b/Alien Isolation/ThirdParty/MinHook/src/trampoline.h @@ -0,0 +1,105 @@ +/* + * MinHook - The Minimalistic API Hooking Library for x64/x86 + * Copyright (C) 2009-2017 Tsuda Kageyu. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#pragma pack(push, 1) + +// Structs for writing x86/x64 instructions. + +// 8-bit relative jump. +typedef struct _JMP_REL_SHORT +{ + UINT8 opcode; // EB xx: JMP +2+xx + UINT8 operand; +} JMP_REL_SHORT, *PJMP_REL_SHORT; + +// 32-bit direct relative jump/call. +typedef struct _JMP_REL +{ + UINT8 opcode; // E9/E8 xxxxxxxx: JMP/CALL +5+xxxxxxxx + UINT32 operand; // Relative destination address +} JMP_REL, *PJMP_REL, CALL_REL; + +// 64-bit indirect absolute jump. +typedef struct _JMP_ABS +{ + UINT8 opcode0; // FF25 00000000: JMP [+6] + UINT8 opcode1; + UINT32 dummy; + UINT64 address; // Absolute destination address +} JMP_ABS, *PJMP_ABS; + +// 64-bit indirect absolute call. +typedef struct _CALL_ABS +{ + UINT8 opcode0; // FF15 00000002: CALL [+6] + UINT8 opcode1; + UINT32 dummy0; + UINT8 dummy1; // EB 08: JMP +10 + UINT8 dummy2; + UINT64 address; // Absolute destination address +} CALL_ABS; + +// 32-bit direct relative conditional jumps. +typedef struct _JCC_REL +{ + UINT8 opcode0; // 0F8* xxxxxxxx: J** +6+xxxxxxxx + UINT8 opcode1; + UINT32 operand; // Relative destination address +} JCC_REL; + +// 64bit indirect absolute conditional jumps that x64 lacks. +typedef struct _JCC_ABS +{ + UINT8 opcode; // 7* 0E: J** +16 + UINT8 dummy0; + UINT8 dummy1; // FF25 00000000: JMP [+6] + UINT8 dummy2; + UINT32 dummy3; + UINT64 address; // Absolute destination address +} JCC_ABS; + +#pragma pack(pop) + +typedef struct _TRAMPOLINE +{ + LPVOID pTarget; // [In] Address of the target function. + LPVOID pDetour; // [In] Address of the detour function. + LPVOID pTrampoline; // [In] Buffer address for the trampoline and relay function. + +#if defined(_M_X64) || defined(__x86_64__) + LPVOID pRelay; // [Out] Address of the relay function. +#endif + BOOL patchAbove; // [Out] Should use the hot patch area? + UINT nIP; // [Out] Number of the instruction boundaries. + UINT8 oldIPs[8]; // [Out] Instruction boundaries of the target function. + UINT8 newIPs[8]; // [Out] Instruction boundaries of the trampoline function. +} TRAMPOLINE, *PTRAMPOLINE; + +BOOL CreateTrampolineFunction(PTRAMPOLINE ct); diff --git a/Alien Isolation/packages.config b/Alien Isolation/packages.config index e53f03d..aae931f 100644 --- a/Alien Isolation/packages.config +++ b/Alien Isolation/packages.config @@ -5,5 +5,4 @@ - \ No newline at end of file diff --git a/README.md b/README.md index 56e1165..99f3ef6 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,26 @@ This is a continuation of the Alien: Isolation Cinematic Tools by [Hattiwatti](h The project must be built in release mode, for x86 systems. This is the default project configuration. -Use Visual Studio VC 140 to compile, and make sure to have restored all Nuget packages. +Use Visual Studio 2022 (MSVC v143 toolset) to compile, and make sure to restore the required NuGet packages (Boost and DirectXTK). MinHook is vendored under `Alien Isolation/ThirdParty/MinHook`, so no separate package restore is needed for it. + +Alternatively, a GitHub Actions workflow builds Release binaries on Windows and publishes artifacts on every PR to master. See `.github/workflows/windows-build.yml`. + +Artifacts include the built `CT_AlienIsolation.dll` for Win32 and (if configured) x64. ### How to use To hook the cinematic tools into Alien: Isolation, run the game, and then launch "inject.bat" in the root of the project. -This utilises [Injector](https://github.com/nefarius/Injector) - a project by [Benjamin Höglinger](https://github.com/nefarius) (thanks!). \ No newline at end of file +This utilises [Injector](https://github.com/nefarius/Injector) - a project by [Benjamin Höglinger](https://github.com/nefarius) (thanks!). + +### Testing and injection tips + +- Start Alien: Isolation and wait at least until the main menu (or load into a level) before injecting. +- Use the provided `Build/inject.bat` or your preferred injector targeting `AI.exe`. +- After injection, a log file is created at `Cinematic Tools/CT.log`. Check it for initialization steps, warnings, or errors. +- If injection fails safely, the tools will log the reason (e.g., invalid offsets, swapchain not ready) and exit without crashing the game. + +### Troubleshooting + +- If the game version differs from the hardcoded offsets, the tools will skip dangerous operations and log which offsets look invalid. Please share `Cinematic Tools/CT.log` so offsets can be updated or replaced with signature scans. +- If you see linking issues for `GetModuleInformation`, ensure `Psapi.lib` is linked (already handled in source by `#pragma comment(lib, "Psapi.lib")`). \ No newline at end of file From f40fe18e695a8b1e81cd5ad60a9945e5c5c20a91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anil=20D=C3=B6kmetas?= <111908128+JohnnyTheCoder1@users.noreply.github.com> Date: Fri, 24 Oct 2025 18:48:52 +0000 Subject: [PATCH 09/12] Retarget Boost NuGet packages to vc142 --- Alien Isolation/CT_AlienIsolation.vcxproj | 12 ++++++------ Alien Isolation/packages.config | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Alien Isolation/CT_AlienIsolation.vcxproj b/Alien Isolation/CT_AlienIsolation.vcxproj index f1614e5..1b08936 100644 --- a/Alien Isolation/CT_AlienIsolation.vcxproj +++ b/Alien Isolation/CT_AlienIsolation.vcxproj @@ -212,9 +212,9 @@ - - - + + + @@ -222,8 +222,8 @@ - - - + + + \ No newline at end of file diff --git a/Alien Isolation/packages.config b/Alien Isolation/packages.config index aae931f..d9bd0b2 100644 --- a/Alien Isolation/packages.config +++ b/Alien Isolation/packages.config @@ -1,8 +1,8 @@  - - - + + + \ No newline at end of file From 43c46e1cdd7857fd85371d369064d7b3d409b349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anil=20D=C3=B6kmetas?= <111908128+JohnnyTheCoder1@users.noreply.github.com> Date: Fri, 24 Oct 2025 19:08:16 +0000 Subject: [PATCH 10/12] Preserve Win32 build artifact --- .github/workflows/windows-build.yml | 35 ++++++++++++++++++++--------- README.md | 2 +- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/.github/workflows/windows-build.yml b/.github/workflows/windows-build.yml index b3f53e9..e522198 100644 --- a/.github/workflows/windows-build.yml +++ b/.github/workflows/windows-build.yml @@ -32,17 +32,32 @@ jobs: - name: Collect artifacts shell: pwsh run: | - New-Item -ItemType Directory -Force -Path artifacts | Out-Null - $patterns = @( - 'Build/*.dll', - 'Build/*.pdb', - 'Alien Isolation/**/Release/*.dll', - 'Alien Isolation/**/Release/*.pdb', - 'Alien Isolation/**/x64/Release/*.dll', - 'Alien Isolation/**/x64/Release/*.pdb' + $artifactDir = 'artifacts' + New-Item -ItemType Directory -Force -Path $artifactDir | Out-Null + + $copies = @( + @{ Source = 'Build/CT_AlienIsolation.dll'; Destination = 'CT_AlienIsolation.Win32.dll' }, + @{ Source = 'Build/CT_AlienIsolation.pdb'; Destination = 'CT_AlienIsolation.Win32.pdb' }, + @{ SourcePattern = 'CT_Alien.*\Release\vc143.pdb'; Destination = 'vc143.Win32.pdb' }, + @{ Source = 'Alien Isolation/x64/Release/CT_AlienIsolation.dll'; Destination = 'CT_AlienIsolation.x64.dll' }, + @{ Source = 'Alien Isolation/x64/Release/CT_AlienIsolation.pdb'; Destination = 'CT_AlienIsolation.x64.pdb' }, + @{ SourcePattern = 'CT_Alien.*\x64\Release\vc143.pdb'; Destination = 'vc143.x64.pdb' } ) - foreach ($p in $patterns) { Get-ChildItem -Path $p -ErrorAction SilentlyContinue | Copy-Item -Destination artifacts -Force } - Get-ChildItem artifacts | Format-Table -AutoSize + + foreach ($item in $copies) { + if ($item.ContainsKey('Source') -and (Test-Path $item.Source)) { + Copy-Item -Path $item.Source -Destination (Join-Path $artifactDir $item.Destination) -Force + } + + if ($item.ContainsKey('SourcePattern')) { + $match = Get-ChildItem -Path $item.SourcePattern -ErrorAction SilentlyContinue | Select-Object -First 1 + if ($null -ne $match) { + Copy-Item -Path $match.FullName -Destination (Join-Path $artifactDir $item.Destination) -Force + } + } + } + + Get-ChildItem $artifactDir | Format-Table -AutoSize - name: Upload artifacts uses: actions/upload-artifact@v4 diff --git a/README.md b/README.md index 99f3ef6..01d6bb5 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Use Visual Studio 2022 (MSVC v143 toolset) to compile, and make sure to restore Alternatively, a GitHub Actions workflow builds Release binaries on Windows and publishes artifacts on every PR to master. See `.github/workflows/windows-build.yml`. -Artifacts include the built `CT_AlienIsolation.dll` for Win32 and (if configured) x64. +Artifacts now include architecture-specific filenames (`CT_AlienIsolation.Win32.dll` and, for experimental coverage, `CT_AlienIsolation.x64.dll`). Use the Win32 build for the shipping game; the x64 binary is non-functional without significant offset work. ### How to use From db0632c6cfabfad2b0ec9f3e66f4d312be35890c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anil=20D=C3=B6kmetas?= <111908128+JohnnyTheCoder1@users.noreply.github.com> Date: Fri, 24 Oct 2025 19:20:29 +0000 Subject: [PATCH 11/12] Wait for D3D singleton before hooking --- Alien Isolation/Main.cpp | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/Alien Isolation/Main.cpp b/Alien Isolation/Main.cpp index b729eec..2ba784e 100644 --- a/Alien Isolation/Main.cpp +++ b/Alien Isolation/Main.cpp @@ -111,13 +111,38 @@ bool Main::Initialize() return false; } - if (!util::IsPtrReadable((void*)d3dSingletonAddr, sizeof(void*))) + CATHODE::D3D** ppD3D = reinterpret_cast(d3dSingletonAddr); + CATHODE::D3D* pD3D = nullptr; + + const int maxPtrTries = 200; // ~10 seconds total wait + int ptrTries = 0; + while (ptrTries++ < maxPtrTries) + { + if (!util::IsPtrReadable(ppD3D, sizeof(*ppD3D))) + { + Sleep(50); + continue; + } + + pD3D = *ppD3D; + if (pD3D && util::IsPtrReadable(pD3D, sizeof(void*))) + break; + + Sleep(50); + } + + if (!util::IsPtrReadable(ppD3D, sizeof(*ppD3D))) + { + util::log::Error("D3D singleton address not readable after waiting: 0x%X", d3dSingletonAddr); + return false; + } + + if (!pD3D) { - util::log::Error("D3D singleton address not readable: 0x%X", d3dSingletonAddr); + util::log::Error("D3D singleton pointer stayed null (addr 0x%X) after waiting", d3dSingletonAddr); return false; } - CATHODE::D3D* pD3D = *(CATHODE::D3D**)d3dSingletonAddr; if (!util::IsPtrReadable(pD3D, sizeof(void*))) { util::log::Error("D3D singleton pointer is not readable: 0x%p", pD3D); From 4d63bf3b6accc32bed8c9a431f2433b9e7256d7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anil=20D=C3=B6kmetas?= <111908128+JohnnyTheCoder1@users.noreply.github.com> Date: Fri, 24 Oct 2025 19:36:05 +0000 Subject: [PATCH 12/12] Fix D3D offset usage for relocated module --- Alien Isolation/Main.cpp | 9 ++++++--- Alien Isolation/Util/Offsets.cpp | 22 +++++++++++++++++++++- Alien Isolation/Util/Util.h | 3 ++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Alien Isolation/Main.cpp b/Alien Isolation/Main.cpp index 2ba784e..a89e323 100644 --- a/Alien Isolation/Main.cpp +++ b/Alien Isolation/Main.cpp @@ -104,14 +104,17 @@ bool Main::Initialize() if (!GetModuleInformation(GetCurrentProcess(), g_gameHandle, &modInfo, sizeof(modInfo))) util::log::Warning("GetModuleInformation failed, GetLastError 0x%X", GetLastError()); - int d3dSingletonAddr = util::offsets::GetOffset("OFFSET_D3D"); - if (!util::IsAddressInModule(g_gameHandle, (void*)d3dSingletonAddr, sizeof(void*))) + int d3dSingletonAddr = util::offsets::GetRelOffset("OFFSET_D3D"); + uintptr_t d3dSingletonAbs = (uintptr_t)g_gameHandle + (uintptr_t)d3dSingletonAddr; + + if (!util::IsAddressInModule(g_gameHandle, + reinterpret_cast(d3dSingletonAbs), sizeof(void*))) { util::log::Error("OFFSET_D3D (0x%X) is outside module image. Likely version mismatch.", d3dSingletonAddr); return false; } - CATHODE::D3D** ppD3D = reinterpret_cast(d3dSingletonAddr); + CATHODE::D3D** ppD3D = reinterpret_cast(d3dSingletonAbs); CATHODE::D3D* pD3D = nullptr; const int maxPtrTries = 200; // ~10 seconds total wait diff --git a/Alien Isolation/Util/Offsets.cpp b/Alien Isolation/Util/Offsets.cpp index 23401ff..5b88751 100644 --- a/Alien Isolation/Util/Offsets.cpp +++ b/Alien Isolation/Util/Offsets.cpp @@ -200,4 +200,24 @@ int util::offsets::GetOffset(std::string const& name) util::log::Error("Offset %s does not exist", name.c_str()); return 0; -} \ No newline at end of file +} + +int util::offsets::GetRelOffset(std::string const& name) +{ + if (m_UseScannedResults) + { + auto result = m_Signatures.find(name); + if (result != m_Signatures.end()) + { + if (result->second.Result) + return result->second.Result - (int)g_gameHandle; + } + } + + auto hardcodedResult = m_HardcodedOffsets.find(name); + if (hardcodedResult != m_HardcodedOffsets.end()) + return hardcodedResult->second; + + util::log::Error("Relative offset %s does not exist", name.c_str()); + return 0; +} diff --git a/Alien Isolation/Util/Util.h b/Alien Isolation/Util/Util.h index 6bd0d41..bc10c95 100644 --- a/Alien Isolation/Util/Util.h +++ b/Alien Isolation/Util/Util.h @@ -46,7 +46,7 @@ namespace util { BYTE* Pattern{ nullptr }; // The pattern to search std::string Mask; // Which bytes should be evaluated (x = evaluate, ? = skip) - + bool HasReference{ false }; // Interpret the offset from the assembly reference int ReferenceOffset{ 0 }; // How far in the signature is the assembly reference int ReferenceSize{ 0 }; // How many bytes is the assembly reference (usually 4, obsolete?) @@ -59,6 +59,7 @@ namespace util void Scan(); int GetOffset(std::string const& name); + int GetRelOffset(std::string const& name); } bool GetResource(int, void*&, DWORD&);