From 658c8d46cf19055e2d405f77caa2986f7b7b8e55 Mon Sep 17 00:00:00 2001 From: Alessandro <66976091+alessandromrc@users.noreply.github.com> Date: Mon, 4 Sep 2023 16:27:59 +0200 Subject: [PATCH] Automatically load up reshade --- LibraryHijacker/dllmain.cpp | 76 +++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 42 deletions(-) diff --git a/LibraryHijacker/dllmain.cpp b/LibraryHijacker/dllmain.cpp index 6d2a47f..740e9a0 100644 --- a/LibraryHijacker/dllmain.cpp +++ b/LibraryHijacker/dllmain.cpp @@ -6,14 +6,16 @@ #include #include #include "../MinHook/include/MinHook.h" +#include "pattern_sc.hpp" -bool hijacked = false; +bool hijacked_d3d11 = false; +bool hijacked_dxgi = false; typedef HMODULE(WINAPI* TYPE_LoadLibraryA)(LPCSTR); TYPE_LoadLibraryA g_loadLibraryA_original = NULL; HMODULE WINAPI LoadLibraryA_replacement(_In_ LPCSTR lpFileName) { - if (hijacked == false) // avoid doing it again as our wrapper module might actually load real d3d11.dll! + if (hijacked_d3d11== false) // avoid doing it again as our wrapper module might actually load real d3d11.dll! { if (strstr(lpFileName, "D3D11") != nullptr || strstr(lpFileName, "d3d11")) { @@ -28,14 +30,41 @@ HMODULE WINAPI LoadLibraryA_replacement(_In_ LPCSTR lpFileName) *lastSlash = '\0'; strcat_s(dllPath, "\\d3d11.dll"); lpFileName = dllPath; - hijacked = true; + hijacked_d3d11 = true; } } } } + + if (hijacked_dxgi == false) // avoid doing it again as our wrapper module might actually load real d3d11.dll! + { + if (strstr(lpFileName, "dxgi") != nullptr || strstr(lpFileName, "DXGI")) + { + char path[MAX_PATH]; + if (GetModuleFileNameA(NULL, path, MAX_PATH) != 0) + { + char dllPath[MAX_PATH]; + strcpy_s(dllPath, path); + char* lastSlash = strrchr(dllPath, '\\'); + if (lastSlash != NULL) + { + *lastSlash = '\0'; + strcat_s(dllPath, "\\dxgi.dll"); + if (PathFileExistsA(dllPath)) // check if dxgi.dll exists + { + lpFileName = dllPath; + hijacked_dxgi = true; + } + } + } + } + } + return g_loadLibraryA_original(lpFileName); } +using namespace memory; + bool installHooks() { // Initialize MinHook. @@ -59,34 +88,6 @@ bool installHooks() return true; } -const char* mutexDetect = "hijackmutex1337"; -HANDLE hMutex; - -bool IsAlreadyInjected() -{ - hMutex = OpenMutexA(MUTEX_ALL_ACCESS, FALSE, mutexDetect); - if (hMutex) - { - CloseHandle(hMutex); - return true; - } - - // The mutex does not exist, indicating the DLL is not injected yet - // Create the mutex to prevent further injections - HANDLE hNewMutex = CreateMutexA(NULL, TRUE, mutexDetect); - if (!hNewMutex) - { - // Failed to create mutex - // Handle the error appropriately - } - - if (hNewMutex != 0) - CloseHandle(hNewMutex); - - return false; -} - - BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved @@ -96,21 +97,14 @@ BOOL APIENTRY DllMain(HMODULE hModule, switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: - - if (IsAlreadyInjected()) - { - MessageBoxA(NULL, "Do you want to crash your game? Seems like you like desktop!", "Injection Error", MB_ICONERROR); - return FALSE; - } installHooks(); std::thread([]() { - if (hijacked) + if (hijacked_d3d11 || hijacked_dxgi) { if (MH_DisableHook(MH_ALL_HOOKS) == MH_OK) { MH_Uninitialize(); HMODULE hModule = GetModuleHandle(NULL); - ReleaseMutex(hMutex); FreeLibraryAndExitThread(hModule, 0); } } @@ -118,10 +112,8 @@ BOOL APIENTRY DllMain(HMODULE hModule, break; case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: - ReleaseMutex(hMutex); case DLL_PROCESS_DETACH: - ReleaseMutex(hMutex); break; } return TRUE; -} \ No newline at end of file +}