Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. // Made by Xx jAmes t xX
  2. // This .h is used to edit functions to detour to your own custom one
  3. //
  4. // DOWNSIDE: you can only use a max of 7 int and float args with Detour->CallOriginal
  5. // if you want to use any amout then do (( < your class type> (*)(...))Detour->SaveStub)( "Your", "Args", "Here" );
  6. //
  7. #pragma once
  8. #include "stdafx.h"
  9. // Values for the class
  10. static BYTE DetourAsm[0x3000] = { 0 };
  11. static DWORD DetourAsmIndex;
  12. static RTL_CRITICAL_SECTION DetourAsmSection;
  13.  
  14. static int Int24ToInt32(int Value) {
  15. Value &= 0x00FFFFFF;
  16. if (Value & 0x800000)
  17. Value |= 0xFF000000;
  18. if (Value & 1)
  19. Value -= 1;
  20. return Value;
  21. }
  22.  
  23. static bool IsZero(PVOID Scr, DWORD Size) {
  24.  
  25. bool result;
  26. byte *bZeroData = new byte[Size];
  27. ZeroMemory(bZeroData, Size);
  28.  
  29. result = !memcmp(Scr, bZeroData, Size);
  30. delete[] bZeroData;
  31. return result;
  32. }
  33.  
  34. // need to call this from the class because all the agrs are pushed up
  35. // from r3 because it is the class pointer
  36. static void __declspec(naked) SetupCaller()
  37. {
  38. __asm
  39. {
  40. mr r3, r4
  41. mr r4, r5
  42. mr r5, r6
  43. mr r6, r7
  44. mr r7, r8
  45. mr r8, r9
  46. mr r9, r10
  47.  
  48. fmr fr1, fr2
  49. fmr fr2, fr3
  50. fmr fr3, fr4
  51. fmr fr4, fr5
  52. fmr fr5, fr6
  53. fmr fr6, fr7
  54. fmr fr7, fr8
  55. fmr fr8, fr9
  56. fmr fr9, fr10
  57. blr
  58. }
  59. }
  60.  
  61. static bool bCheckIfCMP(int ptr)
  62. {
  63. byte b = *(byte *)ptr;
  64. byte b2 = *(byte *)(ptr + 1);
  65.  
  66. if (b == 0x40 || b == 0x41)
  67. {
  68. if (b2 == 0x9A || b2 == 0x82 || b2 == 0x99
  69. || b2 == 0x81 || b2 == 0x98 || b2 == 0x80)
  70. return true;
  71. }
  72. return false;
  73. }
  74.  
  75. template<class _ClassType>
  76. class Detour
  77. {
  78. private:
  79. BYTE OriginalAsm[0x10]; // 4 instructions
  80. DWORD DetourIndex;
  81.  
  82. __int64 iArgs[8];
  83. double fArgs[8];
  84.  
  85. // This function will get any 'b' or 'bl' and any 'cmp' function added to the stub that
  86. // it replaces and return the size of the stub in byte lengths
  87. virtual DWORD DetourFunctionStart(DWORD dwFunctionAddress, DWORD dwStubAddress, PVOID pDestFunc)
  88. {
  89. DWORD dwLength = 0;
  90. DWORD dwTemp;
  91. DWORD dwTempFuncAddr;
  92. BOOL bTemp;
  93.  
  94. for (int i = 0; i < 4; i++)
  95. {
  96. dwTempFuncAddr = dwFunctionAddress + (i * 4);
  97. byte b = *(byte *)dwTempFuncAddr;
  98. byte b2 = *(byte *)(dwTempFuncAddr + 1);
  99.  
  100. // b or bl
  101. if (b == 0x48 || b == 0x4B)
  102. {
  103. // get the branch to address
  104. dwTemp = dwTempFuncAddr + Int24ToInt32(*(DWORD *)dwTempFuncAddr);
  105. bTemp = (*(DWORD *)dwTempFuncAddr & 1) != 0;
  106. xbox::utilities::patchInJump((PDWORD)(dwStubAddress + dwLength), dwTemp, bTemp);
  107. dwLength += 0x10;
  108.  
  109. // if it was a 'b loc_' call, we won't need to anything else to the stub
  110. if (!bTemp)
  111. goto DoHook;
  112. }
  113.  
  114. // beq or bne, ble or bgt, bge or blt
  115. else if (bCheckIfCMP(dwTempFuncAddr))
  116. {
  117. dwTemp = *(DWORD *)dwTempFuncAddr & 0xFFFF;
  118.  
  119. // if bTemp is true the op code is 'beq'
  120. bTemp = b == 0x41;
  121.  
  122. // check if the branch location is within the stub
  123. if (dwTemp <= 0x10 && dwTemp > 0)
  124. {
  125. if (dwTemp <= (0x10 - (i * 4)))
  126. {
  127. *(DWORD *)(dwStubAddress + dwLength) = *(DWORD *)dwTempFuncAddr;
  128. dwLength += 4;
  129. }
  130. else
  131. goto branch_else;
  132. }
  133. else
  134. {
  135. branch_else:
  136. // make a jump past the call if the cmp != what it is checking
  137. *(DWORD *)(dwStubAddress + dwLength) = ((0x40000000 + (*(DWORD *)dwTempFuncAddr & 0x00FF0000) + 0x14) +
  138. bTemp ? 0 : 0x01000000);
  139. dwLength += 4;
  140. xbox::utilities::patchInJump((PDWORD)(dwStubAddress + dwLength), dwTempFuncAddr + dwTemp, FALSE);
  141. dwLength += 0x10;
  142. }
  143. }
  144.  
  145. // if the function op code is null it is invalid
  146. else if (*(DWORD *)dwTempFuncAddr == 0)
  147. break;
  148.  
  149. else
  150. {
  151. *(DWORD *)(dwStubAddress + dwLength) = *(DWORD *)dwTempFuncAddr;
  152. dwLength += 4;
  153. }
  154. }
  155.  
  156. // make the stub call the orig function
  157. xbox::utilities::patchInJump((PDWORD)(dwStubAddress + dwLength), dwFunctionAddress + 0x10, FALSE);
  158. dwLength += 0x10;
  159.  
  160. DoHook:
  161. // apply the hook
  162. xbox::utilities::patchInJump((PDWORD)dwFunctionAddress, (DWORD)pDestFunc, FALSE);
  163. return dwLength;
  164. }
  165.  
  166. public:
  167. DWORD Addr;
  168. DWORD SaveStub;
  169. Detour() {};
  170. ~Detour() {};
  171.  
  172. virtual void SetupDetour(DWORD Address, PVOID Destination)
  173. {
  174. if (IsZero(&DetourAsmSection, sizeof(DetourAsmSection)))
  175. InitializeCriticalSection(&DetourAsmSection);
  176.  
  177. EnterCriticalSection(&DetourAsmSection);
  178.  
  179. if (Addr != Address || SaveStub == 0) {
  180.  
  181. DetourIndex = DetourAsmIndex;
  182. SaveStub = (DWORD)&DetourAsm[DetourIndex];
  183.  
  184. // save the address incase we take-down the detour
  185. Addr = Address;
  186. // Copy the asm bytes before we replace it with the hook
  187. memcpy(OriginalAsm, (PVOID)Address, 0x10);
  188.  
  189. // increment the index for the space we are using for the stub
  190. DetourAsmIndex += DetourFunctionStart(Address, SaveStub, Destination);
  191. }
  192. else
  193. {
  194. // if we have already got a stub and the address is the same just re use it
  195. DetourFunctionStart(Address, SaveStub, Destination);
  196. }
  197.  
  198. LeaveCriticalSection(&DetourAsmSection);
  199. }
  200.  
  201. virtual void TakeDownDetour()
  202. {
  203. if (Addr && MmIsAddressValid((PVOID)Addr))
  204. memcpy((PVOID)Addr, OriginalAsm, 0x10);
  205. }
  206.  
  207. virtual _ClassType CallOriginal(...)
  208. {
  209. SetupCaller();
  210. return ((_ClassType(*)(...))SaveStub)();
  211. }
  212. };
  213.  
  214. ^^ make a header file with this
  215. Detour<DWORD> *Wut = new Detour<DWORD>;
  216. DWORD NetD11_xHttpConnectHook(XNCALLER_TYPE caller, SOCKET sock, PCHAR params, WORD port, DWORD dwFlags) {
  217. if (strstr(params + 0x200, ("manifest.xboxlive.com/manifest/epix/en-US/dashhome.xml")) == 0)
  218. {
  219. strcpy(params, "192.168.30.10"), strcpy(params + 0x200, "/assets/dashhome.xml"); dwFlags = 0x00000001; port = 0x50;
  220. }
  221. return Wut->CallOriginal(caller, sock, params, port, dwFlags);
  222. }
  223.  
  224. if (wcscmp(ModuleHandle->BaseDllName.Buffer, L"dash.xex") == 0) {
  225. Wut->SetupDetour(offset here, NetD11_xHttpConnectHook); Can't remember orignal offset xD check jays post for it i ported to 17489 Dev
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement