|
| 1 | +#include <Windows.h> |
| 2 | +#include <detours.h> |
| 3 | +#include <random> |
| 4 | +#include <psapi.h> |
| 5 | +#include <winnt.h> |
| 6 | +#include <dbghelp.h> |
| 7 | + |
| 8 | +// Original function pointer |
| 9 | +typedef int(WINAPI *OriginalRandFunc)(); |
| 10 | + |
| 11 | +// Pointer to the original rand() function |
| 12 | +OriginalRandFunc pOriginalRand = NULL; |
| 13 | + |
| 14 | +std::random_device rd; // Non-deterministic random number generator |
| 15 | +std::uniform_int_distribution<> dist(0, RAND_MAX); // Distribute results between 0 and RAND_MAX |
| 16 | +std::mt19937 gen(rd()); // Seed Mersenne Twister generator |
| 17 | + |
| 18 | +// Typedef for the original function |
| 19 | +typedef int (*OriginalFunc)(int); |
| 20 | + |
| 21 | +// Pointer to the original function |
| 22 | +OriginalFunc pOriginalFunction = nullptr; |
| 23 | + |
| 24 | +// Hooked function for rand() |
| 25 | +__declspec(dllexport) int WINAPI HookedRand() |
| 26 | +{ |
| 27 | + // Call the custom implementation instead of the original function |
| 28 | + return dist(gen); |
| 29 | +} |
| 30 | + |
| 31 | +bool MatchPattern(const unsigned char* data, const unsigned char* pattern, int length) |
| 32 | +{ |
| 33 | + for (int i = 0; i < length; ++i) |
| 34 | + { |
| 35 | + // Skip certain characters in the pattern |
| 36 | + if (pattern[i] == '.') |
| 37 | + continue; |
| 38 | + |
| 39 | + // If the characters don't match, return false |
| 40 | + if (data[i] != pattern[i]) |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + return true; |
| 45 | +} |
| 46 | + |
| 47 | +// Find the offset of a function within a module using a pattern |
| 48 | +LPBYTE FindFunctionOffset(HMODULE hModule, const unsigned char *pattern, int length) |
| 49 | +{ |
| 50 | + // Get the base address of the module |
| 51 | + auto moduleBase = reinterpret_cast<DWORD_PTR>(hModule); |
| 52 | + |
| 53 | + // Get the module's PE header |
| 54 | + auto ntHeader = ImageNtHeader(hModule); |
| 55 | + if (!ntHeader) |
| 56 | + return 0; // Invalid PE header |
| 57 | + |
| 58 | + // Get the image section header |
| 59 | + auto sectionHeader = IMAGE_FIRST_SECTION(ntHeader); |
| 60 | + if (!sectionHeader) |
| 61 | + return 0; // Invalid section header |
| 62 | + |
| 63 | + // Search within the module's code section |
| 64 | + IMAGE_SECTION_HEADER *codeSectionHeader = nullptr; |
| 65 | + for (int i = 0; i < ntHeader->FileHeader.NumberOfSections; ++i) |
| 66 | + { |
| 67 | + if (sectionHeader->Characteristics & IMAGE_SCN_MEM_EXECUTE) |
| 68 | + { |
| 69 | + codeSectionHeader = sectionHeader; |
| 70 | + break; |
| 71 | + } |
| 72 | + sectionHeader++; |
| 73 | + } |
| 74 | + |
| 75 | + if (!codeSectionHeader) |
| 76 | + return 0x0; // Code section not found |
| 77 | + |
| 78 | + // Search for the pattern within the code section |
| 79 | + auto codeSectionMemory = reinterpret_cast<const unsigned char *>(moduleBase + codeSectionHeader->VirtualAddress); |
| 80 | + auto endAddress = codeSectionMemory + codeSectionHeader->Misc.VirtualSize - 2; |
| 81 | + |
| 82 | + for (; codeSectionMemory < endAddress; codeSectionMemory++) |
| 83 | + { |
| 84 | + if (MatchPattern(codeSectionMemory, pattern, length)) |
| 85 | + // if (memcmp(codeSectionMemory, pattern, length) == 0) |
| 86 | + return (LPBYTE)codeSectionMemory; |
| 87 | + } |
| 88 | + |
| 89 | + // Pattern not found |
| 90 | + return 0x0; |
| 91 | +} |
| 92 | + |
| 93 | +// Function to initialize the hook |
| 94 | +void InitHook() |
| 95 | +{ |
| 96 | + // Get the address of the target function |
| 97 | + auto hModule = GetModuleHandle(nullptr); |
| 98 | + |
| 99 | + LPBYTE pFunctionAddress = 0x0; |
| 100 | + |
| 101 | + // Find the offset of the target function using a specific pattern |
| 102 | + |
| 103 | + // Baldur's Gate EE, Baldur's Gate 2 EE, Icewind Dale EE, |
| 104 | + unsigned char pattern1[] = "\x48\x83..\xE8.......\xFD\x43\x03\x00"; |
| 105 | + if (pFunctionAddress == 0x0) |
| 106 | + pFunctionAddress = FindFunctionOffset(hModule, pattern1, sizeof(pattern1) - 1); |
| 107 | + |
| 108 | + // Planescape EE |
| 109 | + unsigned char pattern2[] = "\xE8.......\xFD\x43\x03\x00"; |
| 110 | + if (pFunctionAddress == 0x0) |
| 111 | + pFunctionAddress = FindFunctionOffset(hModule, pattern2, sizeof(pattern2) - 1); |
| 112 | + |
| 113 | + // Baldur's Gate, Baldur's Gate 2, Icewind Dale, Planescape Torment |
| 114 | + unsigned char pattern3[] = "\xE8....\x8B....\xFD\x43\x03\x00"; |
| 115 | + if (pFunctionAddress == 0x0) |
| 116 | + pFunctionAddress = FindFunctionOffset(hModule, pattern3, sizeof(pattern3) - 1); |
| 117 | + |
| 118 | + if (pFunctionAddress == 0x0) |
| 119 | + { |
| 120 | + MessageBoxA(NULL, "original rand() method not found!", "betterrand.dll Error", MB_OK); |
| 121 | + throw("Error: original rand() method not found!"); |
| 122 | + } |
| 123 | + |
| 124 | + SYSTEM_INFO systemInfo; |
| 125 | + GetSystemInfo(&systemInfo); |
| 126 | + |
| 127 | + auto baseAddress = systemInfo.lpMinimumApplicationAddress; |
| 128 | + auto scanSize = reinterpret_cast<SIZE_T>(systemInfo.lpMaximumApplicationAddress) - reinterpret_cast<SIZE_T>(systemInfo.lpMinimumApplicationAddress); |
| 129 | + |
| 130 | + // Get module info |
| 131 | + MODULEINFO modinfo = {NULL}; |
| 132 | + GetModuleInformation(GetCurrentProcess(), hModule, &modinfo, sizeof(modinfo)); |
| 133 | + |
| 134 | + // Create a trampoline for the original function |
| 135 | + auto pOriginalFunction = reinterpret_cast<OriginalRandFunc>(pFunctionAddress); |
| 136 | + |
| 137 | + // Detour the function with the custom implementation |
| 138 | + DetourTransactionBegin(); |
| 139 | + DetourUpdateThread(GetCurrentThread()); |
| 140 | + DetourAttach(&(PVOID &)pOriginalFunction, HookedRand); |
| 141 | + DetourTransactionCommit(); |
| 142 | +} |
| 143 | + |
| 144 | +// Function to remove the hook |
| 145 | +void RemoveHook() |
| 146 | +{ |
| 147 | + // Detach the hook |
| 148 | + DetourTransactionBegin(); |
| 149 | + DetourUpdateThread(GetCurrentThread()); |
| 150 | + DetourDetach(&(PVOID &)pOriginalRand, HookedRand); |
| 151 | + DetourTransactionCommit(); |
| 152 | +} |
| 153 | + |
| 154 | +// Entry point of the DLL |
| 155 | +BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved) |
| 156 | +{ |
| 157 | + if (DetourIsHelperProcess()) |
| 158 | + { |
| 159 | + return TRUE; |
| 160 | + } |
| 161 | + |
| 162 | + if (dwReason == DLL_PROCESS_ATTACH) |
| 163 | + { |
| 164 | + // Initialize the hook when the DLL is loaded |
| 165 | + InitHook(); |
| 166 | + } |
| 167 | + else if (dwReason == DLL_PROCESS_DETACH) |
| 168 | + { |
| 169 | + // Remove the hook when the DLL is unloaded |
| 170 | + RemoveHook(); |
| 171 | + } |
| 172 | + |
| 173 | + return TRUE; |
| 174 | +} |
0 commit comments