Guest User

DevModeForBLNDVW

a guest
Nov 26th, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. #include "stdafx.h"
  2.  
  3. #define MAX_HOOKS 1000
  4.  
  5. typedef struct _HOOK_INFO
  6. {
  7. ULONG_PTR Function;
  8. ULONG_PTR Hook;
  9. ULONG_PTR OrigBytes;
  10. } HOOK_INFO, *PHOOK_INFO;
  11.  
  12. HOOK_INFO HookInfo[MAX_HOOKS];
  13. UINT NumberOfHooks = 0;
  14. bool hookFailed = false;
  15. BYTE *pOrigBytesBuffer = NULL;
  16.  
  17.  
  18.  
  19. HOOK_INFO *GetHookInfoFromFunction(ULONG_PTR OriginalFunction)
  20. {
  21. if (NumberOfHooks == 0)
  22. return NULL;
  23.  
  24. for (UINT x = 0; x < NumberOfHooks; x++)
  25. {
  26. if (HookInfo[x].Function == OriginalFunction)
  27. return &HookInfo[x];
  28. }
  29.  
  30. return NULL;
  31. }
  32.  
  33. void WriteJump(void *pAddress, ULONG_PTR JumpTo)
  34. {
  35. // be extra safe and overwrite memory with a single memcpy
  36. BYTE *pJumpInstructionBuffer = (BYTE *)malloc(14);
  37. BYTE *pCur = pJumpInstructionBuffer;
  38.  
  39. *pCur = 0xff; // jmp [rip+addr]
  40. *(++pCur) = 0x25;
  41. *((DWORD *) ++pCur) = 0; // addr = 0
  42. pCur += sizeof(DWORD);
  43. *((ULONG_PTR *)pCur) = JumpTo;
  44.  
  45. DWORD dwOldProtect = 0;
  46. VirtualProtect(pAddress, 14, PAGE_EXECUTE_READWRITE, &dwOldProtect);
  47.  
  48. memcpy(pAddress, pJumpInstructionBuffer, 14);
  49.  
  50. DWORD dwBuf = 0;
  51. VirtualProtect(pAddress, 14, dwOldProtect, &dwBuf);
  52.  
  53. free(pJumpInstructionBuffer);
  54. }
  55.  
  56. void *BackupOrigBytes(ULONG_PTR originalFunction)
  57. {
  58. if (pOrigBytesBuffer == NULL) {
  59. pOrigBytesBuffer = (BYTE *)malloc(MAX_HOOKS * 14);
  60. }
  61.  
  62. VOID *pOrigBytes = (VOID *)&pOrigBytesBuffer[NumberOfHooks * 14];
  63.  
  64. memcpy(&pOrigBytesBuffer[NumberOfHooks * 14], (void *)originalFunction, 14);
  65.  
  66. return pOrigBytes;
  67. }
  68.  
  69. bool hookNative(UINT64 hash, ULONG_PTR hookFunction = NULL)
  70. {
  71. auto originalFunction = GetNativeHandler(hash);
  72.  
  73. if (originalFunction == 0) {
  74. hookFailed = true;
  75. return false;
  76. }
  77.  
  78. HOOK_INFO *hinfo = GetHookInfoFromFunction((ULONG_PTR) originalFunction);
  79.  
  80. if (hinfo) {
  81. WriteJump(originalFunction, hinfo->Hook);
  82. }
  83. else
  84. {
  85. if (NumberOfHooks == (MAX_HOOKS - 1))
  86. return false;
  87.  
  88. VOID *pOrigBytes = BackupOrigBytes((ULONG_PTR)originalFunction);
  89.  
  90. HookInfo[NumberOfHooks].Function = (ULONG_PTR)originalFunction;
  91. HookInfo[NumberOfHooks].OrigBytes = (ULONG_PTR)pOrigBytes;
  92. HookInfo[NumberOfHooks].Hook = hookFunction;
  93.  
  94. NumberOfHooks++;
  95.  
  96. WriteJump(originalFunction, hookFunction);
  97. }
  98.  
  99. return true;
  100. }
  101.  
  102. void unhookNative(UINT64 hash) {
  103. auto originalFunction = GetNativeHandler(hash);
  104.  
  105. HOOK_INFO *hinfo = GetHookInfoFromFunction((ULONG_PTR)originalFunction);
  106.  
  107. DWORD dwOldProtect = 0;
  108.  
  109. VirtualProtect(originalFunction, 14, PAGE_EXECUTE_READWRITE, &dwOldProtect);
  110.  
  111. memcpy((void *)hinfo->Function, (void *)hinfo->OrigBytes, 14);
  112.  
  113. DWORD dwBuf = 0;
  114. VirtualProtect(originalFunction, 14, dwOldProtect, &dwBuf);
  115. }
  116.  
  117. void* __cdecl MY_IS_DLC_PRESENT(NativeContext *cxt)
  118. {
  119. Hash DlcHash = cxt->GetArgument<Hash>(0);
  120.  
  121. if (DlcHash == 2532323046) { // DEV
  122. // game game requested dev dlc -> return true;
  123. cxt->SetResult(0, true);
  124. return cxt;
  125. }
  126.  
  127. // restore original function
  128. unhookNative(0x2D6859674806FDCE);
  129.  
  130. // get result of original function
  131. BOOL result = DLC2::IS_DLC_PRESENT(cxt->GetArgument<Hash>(0));
  132. cxt->SetResult(0, result);
  133.  
  134. // hook us up again
  135. hookNative(0x2D6859674806FDCE);
  136.  
  137. return cxt;
  138. }
  139.  
  140. bool AttemptHookNatives() {
  141. static DWORD64 dwThreadCollectionPtr = 0;
  142.  
  143. if (!dwThreadCollectionPtr) {
  144. // scan for GTA Thread Pool
  145. dwThreadCollectionPtr = Pattern::Scan(g_MainModuleInfo, "48 8B 05 ? ? ? ? 8B CA 4C 8B 0C C8 45 39 51 08");
  146. }
  147.  
  148. if ( !dwThreadCollectionPtr
  149. || !Pattern::Scan(g_MainModuleInfo, "76 61 49 8B 7A 40 48 8D 0D") // scan for Native Registration Table
  150. ) {
  151. // too early. GetNativeHandler would log a fatal error and exit the process
  152. return false;
  153. }
  154.  
  155. hookFailed = false;
  156.  
  157. hookNative(0x2D6859674806FDCE, (ULONG_PTR)&MY_IS_DLC_PRESENT);
  158. // add more hooks here
  159.  
  160. return !hookFailed;
  161. }
  162.  
  163. DWORD WINAPI lpHookNatives(LPVOID lpParam) {
  164. while (!AttemptHookNatives()) {
  165. Sleep(100);
  166. }
  167.  
  168. return 0;
  169. }
  170.  
  171. void SpawnHookNatives() {
  172. CreateThread(0, 0, lpHookNatives, 0, 0, 0);
  173. }
Advertisement
Add Comment
Please, Sign In to add comment