From d6c2ccfd724105a089903bb2befc9d550538d9a6 Mon Sep 17 00:00:00 2001 From: ThirteenAG Date: Mon, 1 Jul 2024 16:56:00 +0800 Subject: [PATCH] change sh to minhook for overload --- external/injector | 2 +- premake5.lua | 16 +++--- source/dllmain.cpp | 130 +++++++++++++++++++++++++-------------------- 3 files changed, 82 insertions(+), 66 deletions(-) diff --git a/external/injector b/external/injector index 9da9f94..1391267 160000 --- a/external/injector +++ b/external/injector @@ -1 +1 @@ -Subproject commit 9da9f948b5e87e7577decf2776eeeb3cb8d6fcec +Subproject commit 139126791f4529ecf0929bbf807de1afa715d7a6 diff --git a/premake5.lua b/premake5.lua index 56c4e61..1ecd7ee 100644 --- a/premake5.lua +++ b/premake5.lua @@ -51,10 +51,10 @@ project "Ultimate-ASI-Loader-Win32" includedirs { "source" } includedirs { "external" } - includedirs { "external/injector/safetyhook/include" } - files { "external/injector/safetyhook/include/**.hpp", "external/injector/safetyhook/src/**.cpp" } - includedirs { "external/injector/zydis" } - files { "external/injector/zydis/**.h", "external/injector/zydis/**.c" } + includedirs { "external/injector/minhook/include" } + files { "external/injector/minhook/include/*.h", "external/injector/minhook/src/**.h", "external/injector/minhook/src/**.c" } + includedirs { "external/injector/utility" } + files { "external/injector/utility/FunctionHookMinHook.hpp", "external/injector/utility/FunctionHookMinHook.cpp" } files { "source/dllmain.h", "source/dllmain.cpp" } files { "source/x86.def" } @@ -200,10 +200,10 @@ project "Ultimate-ASI-Loader-x64" includedirs { "source" } includedirs { "external" } - includedirs { "external/injector/safetyhook/include" } - files { "external/injector/safetyhook/include/**.hpp", "external/injector/safetyhook/src/**.cpp" } - includedirs { "external/injector/zydis" } - files { "external/injector/zydis/**.h", "external/injector/zydis/**.c" } + includedirs { "external/injector/minhook/include" } + files { "external/injector/minhook/include/*.h", "external/injector/minhook/src/**.h", "external/injector/minhook/src/**.c" } + includedirs { "external/injector/utility" } + files { "external/injector/utility/FunctionHookMinHook.hpp", "external/injector/utility/FunctionHookMinHook.cpp" } files { "source/dllmain.h", "source/dllmain.cpp" } files { "source/x64.def" } diff --git a/source/dllmain.cpp b/source/dllmain.cpp index b2fae78..2a45be4 100644 --- a/source/dllmain.cpp +++ b/source/dllmain.cpp @@ -2,7 +2,8 @@ #include "exception.hpp" #include #include -#include +#include +#include #if !X64 #include @@ -322,20 +323,20 @@ size_t OLE32Data[OLE32ExportsNamesCount][Kernel32ExportsDataCount]; namespace OverloadFromFolder { - SafetyHookInline shLoadLibraryExA = {}; - SafetyHookInline shLoadLibraryExW = {}; - SafetyHookInline shCreateFileA = {}; - SafetyHookInline shCreateFileW = {}; - SafetyHookInline shGetFileAttributesA = {}; - SafetyHookInline shGetFileAttributesW = {}; - SafetyHookInline shGetFileAttributesExA = {}; - SafetyHookInline shGetFileAttributesExW = {}; - SafetyHookInline shFindFirstFileA = {}; - SafetyHookInline shFindNextFileA = {}; - SafetyHookInline shFindFirstFileW = {}; - SafetyHookInline shFindNextFileW = {}; - SafetyHookInline shFindFirstFileExA = {}; - SafetyHookInline shFindFirstFileExW = {}; + std::unique_ptr mhLoadLibraryExA = { nullptr }; + std::unique_ptr mhLoadLibraryExW = { nullptr }; + std::unique_ptr mhCreateFileA = { nullptr }; + std::unique_ptr mhCreateFileW = { nullptr }; + std::unique_ptr mhGetFileAttributesA = { nullptr }; + std::unique_ptr mhGetFileAttributesW = { nullptr }; + std::unique_ptr mhGetFileAttributesExA = { nullptr }; + std::unique_ptr mhGetFileAttributesExW = { nullptr }; + std::unique_ptr mhFindFirstFileA = { nullptr }; + std::unique_ptr mhFindNextFileA = { nullptr }; + std::unique_ptr mhFindFirstFileW = { nullptr }; + std::unique_ptr mhFindNextFileW = { nullptr }; + std::unique_ptr mhFindFirstFileExA = { nullptr }; + std::unique_ptr mhFindFirstFileExW = { nullptr }; void HookAPIForOverload(); } @@ -1322,63 +1323,63 @@ namespace OverloadFromFolder { auto raddr = _ReturnAddress(); auto r = GetFilePathForOverload(lpLibFileName, isRecursive(raddr)); - return shLoadLibraryExA.unsafe_stdcall>(value_orA(r, lpLibFileName), hFile, dwFlags); + return mhLoadLibraryExA->get_original()(value_orA(r, lpLibFileName), hFile, dwFlags); } HMODULE WINAPI shCustomLoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags) { auto raddr = _ReturnAddress(); auto r = GetFilePathForOverload(lpLibFileName, isRecursive(raddr)); - return shLoadLibraryExW.unsafe_stdcall>(value_orW(r, lpLibFileName), hFile, dwFlags); + return mhLoadLibraryExW->get_original()(value_orW(r, lpLibFileName), hFile, dwFlags); } HANDLE WINAPI shCustomCreateFileA(LPCSTR lpFileName, DWORD dwAccess, DWORD dwSharing, LPSECURITY_ATTRIBUTES saAttributes, DWORD dwCreation, DWORD dwAttributes, HANDLE hTemplate) { auto raddr = _ReturnAddress(); auto r = GetFilePathForOverload(lpFileName, isRecursive(raddr)); - return shCreateFileA.unsafe_stdcall>(value_orA(r, lpFileName), dwAccess, dwSharing, saAttributes, dwCreation, dwAttributes, hTemplate); + return mhCreateFileA->get_original()(value_orA(r, lpFileName), dwAccess, dwSharing, saAttributes, dwCreation, dwAttributes, hTemplate); } HANDLE WINAPI shCustomCreateFileW(LPCWSTR lpFileName, DWORD dwAccess, DWORD dwSharing, LPSECURITY_ATTRIBUTES saAttributes, DWORD dwCreation, DWORD dwAttributes, HANDLE hTemplate) { auto raddr = _ReturnAddress(); auto r = GetFilePathForOverload(lpFileName, isRecursive(raddr)); - return shCreateFileW.unsafe_stdcall>(value_orW(r, lpFileName), dwAccess, dwSharing, saAttributes, dwCreation, dwAttributes, hTemplate); + return mhCreateFileW->get_original()(value_orW(r, lpFileName), dwAccess, dwSharing, saAttributes, dwCreation, dwAttributes, hTemplate); } DWORD WINAPI shCustomGetFileAttributesA(LPCSTR lpFileName) { auto raddr = _ReturnAddress(); auto r = GetFilePathForOverload(lpFileName, isRecursive(raddr)); - return shGetFileAttributesA.unsafe_stdcall>(value_orA(r, lpFileName)); + return mhGetFileAttributesA->get_original()(value_orA(r, lpFileName)); } DWORD WINAPI shCustomGetFileAttributesW(LPCWSTR lpFileName) { auto raddr = _ReturnAddress(); auto r = GetFilePathForOverload(lpFileName, isRecursive(raddr)); - return shGetFileAttributesW.unsafe_stdcall>(value_orW(r, lpFileName)); + return mhGetFileAttributesW->get_original()(value_orW(r, lpFileName)); } BOOL WINAPI shCustomGetFileAttributesExA(LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId, LPVOID lpFileInformation) { auto raddr = _ReturnAddress(); auto r = GetFilePathForOverload(lpFileName, isRecursive(raddr)); - return shGetFileAttributesExA.unsafe_stdcall>(value_orA(r, lpFileName), fInfoLevelId, lpFileInformation); + return mhGetFileAttributesExA->get_original()(value_orA(r, lpFileName), fInfoLevelId, lpFileInformation); } BOOL WINAPI shCustomGetFileAttributesExW(LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId, LPVOID lpFileInformation) { auto raddr = _ReturnAddress(); auto r = GetFilePathForOverload(lpFileName, isRecursive(raddr)); - return shGetFileAttributesExW.unsafe_stdcall>(value_orW(r, lpFileName), fInfoLevelId, lpFileInformation); + return mhGetFileAttributesExW->get_original()(value_orW(r, lpFileName), fInfoLevelId, lpFileInformation); } typedef HANDLE(WINAPI* tFindFirstFileA)(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData); HANDLE WINAPI shCustomFindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData) { auto raddr = _ReturnAddress(); - auto ret = shFindFirstFileA.unsafe_stdcall>(lpFileName, lpFindFileData); + auto ret = mhFindFirstFileA->get_original()(lpFileName, lpFindFileData); if (isRecursive(raddr)) return ret; @@ -1402,7 +1403,7 @@ namespace OverloadFromFolder BOOL WINAPI shCustomFindNextFileA(HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData) { auto raddr = _ReturnAddress(); - auto ret = shFindNextFileA.unsafe_stdcall>(hFindFile, lpFindFileData); + auto ret = mhFindNextFileA->get_original()(hFindFile, lpFindFileData); if (isRecursive(raddr)) return ret; @@ -1424,7 +1425,7 @@ namespace OverloadFromFolder HANDLE WINAPI shCustomFindFirstFileW(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFindFileData) { auto raddr = _ReturnAddress(); - auto ret = shFindFirstFileW.unsafe_stdcall>(lpFileName, lpFindFileData); + auto ret = mhFindFirstFileW->get_original()(lpFileName, lpFindFileData); if (isRecursive(raddr)) return ret; @@ -1448,7 +1449,7 @@ namespace OverloadFromFolder BOOL WINAPI shCustomFindNextFileW(HANDLE hFindFile, LPWIN32_FIND_DATAW lpFindFileData) { auto raddr = _ReturnAddress(); - auto ret = shFindNextFileW.unsafe_stdcall>(hFindFile, lpFindFileData); + auto ret = mhFindNextFileW->get_original()(hFindFile, lpFindFileData); if (isRecursive(raddr)) return ret; @@ -1469,7 +1470,7 @@ namespace OverloadFromFolder HANDLE WINAPI shCustomFindFirstFileExA(LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, WIN32_FIND_DATAA* lpFindFileData, FINDEX_SEARCH_OPS fSearchOp, LPVOID lpSearchFilter, DWORD dwAdditionalFlags) { auto raddr = _ReturnAddress(); - auto ret = shFindFirstFileExA.unsafe_stdcall>(lpFileName, fInfoLevelId, lpFindFileData, fSearchOp, lpSearchFilter, dwAdditionalFlags); + auto ret = mhFindFirstFileExA->get_original()(lpFileName, fInfoLevelId, lpFindFileData, fSearchOp, lpSearchFilter, dwAdditionalFlags); if (isRecursive(raddr)) return ret; @@ -1492,7 +1493,7 @@ namespace OverloadFromFolder HANDLE WINAPI shCustomFindFirstFileExW(LPCWSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, WIN32_FIND_DATAW* lpFindFileData, FINDEX_SEARCH_OPS fSearchOp, LPVOID lpSearchFilter, DWORD dwAdditionalFlags) { auto raddr = _ReturnAddress(); - auto ret = shFindFirstFileExW.unsafe_stdcall>(lpFileName, fInfoLevelId, lpFindFileData, fSearchOp, lpSearchFilter, dwAdditionalFlags); + auto ret = mhFindFirstFileExW->get_original()(lpFileName, fInfoLevelId, lpFindFileData, fSearchOp, lpSearchFilter, dwAdditionalFlags); if (isRecursive(raddr)) return ret; @@ -1514,20 +1515,35 @@ namespace OverloadFromFolder void HookAPIForOverload() { - shLoadLibraryExA = safetyhook::create_inline(LoadLibraryExA, shCustomLoadLibraryExA); - shLoadLibraryExW = safetyhook::create_inline(LoadLibraryExW, shCustomLoadLibraryExW); - shCreateFileA = safetyhook::create_inline(CreateFileA, shCustomCreateFileA); - shCreateFileW = safetyhook::create_inline(CreateFileW, shCustomCreateFileW); - shGetFileAttributesA = safetyhook::create_inline(GetFileAttributesA, shCustomGetFileAttributesA); - shGetFileAttributesW = safetyhook::create_inline(GetFileAttributesW, shCustomGetFileAttributesW); - shGetFileAttributesExA = safetyhook::create_inline(GetFileAttributesExA, shCustomGetFileAttributesExA); - shGetFileAttributesExW = safetyhook::create_inline(GetFileAttributesExW, shCustomGetFileAttributesExW); - shFindFirstFileA = safetyhook::create_inline(FindFirstFileA, shCustomFindFirstFileA); - shFindNextFileA = safetyhook::create_inline(FindNextFileA, shCustomFindNextFileA); - shFindFirstFileW = safetyhook::create_inline(FindFirstFileW, shCustomFindFirstFileW); - shFindNextFileW = safetyhook::create_inline(FindNextFileW, shCustomFindNextFileW); - shFindFirstFileExA = safetyhook::create_inline(FindFirstFileExA, shCustomFindFirstFileExA); - shFindFirstFileExW = safetyhook::create_inline(FindFirstFileExW, shCustomFindFirstFileExW); + mhLoadLibraryExA = std::make_unique((uintptr_t)LoadLibraryExA, (uintptr_t)shCustomLoadLibraryExA); + mhLoadLibraryExW = std::make_unique((uintptr_t)LoadLibraryExW, (uintptr_t)shCustomLoadLibraryExW); + mhCreateFileA = std::make_unique((uintptr_t)CreateFileA, (uintptr_t)shCustomCreateFileA); + mhCreateFileW = std::make_unique((uintptr_t)CreateFileW, (uintptr_t)shCustomCreateFileW); + mhGetFileAttributesA = std::make_unique((uintptr_t)GetFileAttributesA, (uintptr_t)shCustomGetFileAttributesA); + mhGetFileAttributesW = std::make_unique((uintptr_t)GetFileAttributesW, (uintptr_t)shCustomGetFileAttributesW); + mhGetFileAttributesExA = std::make_unique((uintptr_t)GetFileAttributesExA, (uintptr_t)shCustomGetFileAttributesExA); + mhGetFileAttributesExW = std::make_unique((uintptr_t)GetFileAttributesExW, (uintptr_t)shCustomGetFileAttributesExW); + mhFindFirstFileA = std::make_unique((uintptr_t)FindFirstFileA, (uintptr_t)shCustomFindFirstFileA); + mhFindNextFileA = std::make_unique((uintptr_t)FindNextFileA, (uintptr_t)shCustomFindNextFileA); + mhFindFirstFileW = std::make_unique((uintptr_t)FindFirstFileW, (uintptr_t)shCustomFindFirstFileW); + mhFindNextFileW = std::make_unique((uintptr_t)FindNextFileW, (uintptr_t)shCustomFindNextFileW); + mhFindFirstFileExA = std::make_unique((uintptr_t)FindFirstFileExA, (uintptr_t)shCustomFindFirstFileExA); + mhFindFirstFileExW = std::make_unique((uintptr_t)FindFirstFileExW, (uintptr_t)shCustomFindFirstFileExW); + + mhLoadLibraryExA->create(); + mhLoadLibraryExW->create(); + mhCreateFileA->create(); + mhCreateFileW->create(); + mhGetFileAttributesA->create(); + mhGetFileAttributesW->create(); + mhGetFileAttributesExA->create(); + mhGetFileAttributesExW->create(); + mhFindFirstFileA->create(); + mhFindNextFileA->create(); + mhFindFirstFileW->create(); + mhFindNextFileW->create(); + mhFindFirstFileExA->create(); + mhFindFirstFileExW->create(); } } @@ -2561,20 +2577,20 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID /*lpReserved*/) { using namespace OverloadFromFolder; - shCreateFileA = {}; - shCreateFileW = {}; - shLoadLibraryExA = {}; - shLoadLibraryExW = {}; - shGetFileAttributesA = {}; - shGetFileAttributesW = {}; - shGetFileAttributesExA = {}; - shGetFileAttributesExW = {}; - shFindFirstFileA = {}; - shFindNextFileA = {}; - shFindFirstFileW = {}; - shFindNextFileW = {}; - shFindFirstFileExA = {}; - shFindFirstFileExW = {}; + mhCreateFileA = {}; + mhCreateFileW = {}; + mhLoadLibraryExA = {}; + mhLoadLibraryExW = {}; + mhGetFileAttributesA = {}; + mhGetFileAttributesW = {}; + mhGetFileAttributesExA = {}; + mhGetFileAttributesExW = {}; + mhFindFirstFileA = {}; + mhFindNextFileA = {}; + mhFindFirstFileW = {}; + mhFindNextFileW = {}; + mhFindFirstFileExA = {}; + mhFindFirstFileExW = {}; } } return TRUE;