BratokHR

D3D Hook all versions

Sep 26th, 2016
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.26 KB | None | 0 0
  1. /**
  2.  * Использоваение:
  3.  *
  4.  * HRESULT APIENTRY hEndScene( LPDIRECT3DDEVICE9 pDevice )
  5.  * {
  6.  *      return pEndScene(pDevice);
  7.  * }
  8.  *
  9.  * void __stdcall main()
  10.  * {
  11.  *      EndScene = hEndScene;
  12.  *      HOOK();
  13.  * }
  14.  *
  15.  * bool __stdcall DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
  16.  * {
  17.  *      if (fdwReason == DLL_PROCESS_ATTACH)
  18.  *          CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)main, NULL, NULL, NULL);
  19.  *
  20.  *      return true;
  21.  * }
  22.  
  23. /**
  24.  * d3dhook.cpp
  25.  */
  26. #include "d3dhook.h"
  27.  
  28. oDrawIndexedPrimitive pDrawIndexedPrimitive = NULL;
  29. oEndScene pEndScene = NULL;
  30. oSetStreamSource pSetStreamSource = NULL;
  31. oReset pReset = NULL;
  32.  
  33. void* DrawIndexedPrimitive = NULL;
  34. void* EndScene = NULL;
  35. void* SetStreamSource = NULL;
  36. void* Reset = NULL;
  37.  
  38. /**
  39.  * HOOK
  40.  */
  41. LRESULT CALLBACK MsgProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { return DefWindowProc(hwnd, uMsg, wParam, lParam); }
  42. void HOOK1(DWORD* table)
  43. {
  44.     WNDCLASSEX wc = {sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,GetModuleHandle(NULL),NULL,NULL,NULL,NULL,"DX",NULL};
  45.  
  46.     RegisterClassEx(&wc);
  47.  
  48.     HWND hWnd = CreateWindow("DX",NULL,WS_OVERLAPPEDWINDOW,100,100,300,300,GetDesktopWindow(),NULL,wc.hInstance,NULL);
  49.  
  50.     D3DPRESENT_PARAMETERS d3dpp;
  51.     ZeroMemory( &d3dpp, sizeof(d3dpp) );
  52.     d3dpp.Windowed = TRUE;
  53.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  54.     d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  55.  
  56.     LPDIRECT3DDEVICE9 pd3dDevice;
  57.     LPDIRECT3D9 pD3D = Direct3DCreate9( D3D_SDK_VERSION );
  58.  
  59.     pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pd3dDevice);
  60.  
  61.     pD3D->Release();
  62.  
  63.     DWORD* pVTable = (DWORD*)pd3dDevice;
  64.     pVTable = (DWORD*)pVTable[0];
  65.  
  66.     table[ES] = pVTable[42];                    //EndScene address
  67.     table[DIP] = pVTable[82];                   //DrawIndexedPrimitive address
  68.     table[RES] = pVTable[16];                   //Reset address
  69.     table[BS] = pVTable[41];                    //BeginScene address
  70.     table[SSS] = pVTable[100];                  //SetStreamSource
  71.  
  72.     DestroyWindow(hWnd);
  73. }
  74.  
  75. void HOOK2(DWORD* table)
  76. {
  77.     HMODULE hD3D9 = LoadLibraryA("d3d9.dll");
  78.     PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)hD3D9;
  79.  
  80.     if (dosHeader->e_magic == IMAGE_DOS_SIGNATURE) {
  81.         PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)((DWORD)dosHeader + dosHeader->e_lfanew);
  82.  
  83.         if (ntHeaders->Signature == IMAGE_NT_SIGNATURE) {
  84.  
  85.             DWORD NumberOfSections = ntHeaders->FileHeader.NumberOfSections;
  86.             PIMAGE_SECTION_HEADER SectionHeader = (PIMAGE_SECTION_HEADER)((DWORD)dosHeader + dosHeader->e_lfanew + sizeof(IMAGE_NT_HEADERS));
  87.  
  88.             for (unsigned int i = 0; i < NumberOfSections; i++) {
  89.                 if (strncmp((const char *)SectionHeader[i].Name, ".text", 5) == 0) {
  90.                     DWORD dwFrom = (DWORD)dosHeader + SectionHeader[i].VirtualAddress;
  91.                     DWORD dwTo = dwFrom + SectionHeader[i].Misc.VirtualSize;
  92.  
  93.                     //C7 06 ?? ?? ?? ?? 89 86 ?? ?? ?? ?? 89 86
  94.                     for (unsigned int i = dwFrom; i < dwTo; i++) {
  95.                         if (*(WORD*)(i) == 0x06C7)
  96.                         if (*(WORD*)(i + 6) == 0x8689)
  97.                         if (*(WORD*)(i + 12) == 0x8689) {
  98.                             PDWORD pVTable = *(PDWORD*)(i + 2);
  99.  
  100.                             table[ES] = pVTable[42];                    //EndScene address
  101.                             table[DIP] = pVTable[82];                   //DrawIndexedPrimitive address
  102.                             table[RES] = pVTable[16];                   //Reset address
  103.                             table[BS] = pVTable[41];                    //BeginScene address
  104.                             table[SSS] = pVTable[100];                  //SetStreamSource
  105.  
  106.                             return;
  107.                         }
  108.                     }
  109.                 }
  110.             }
  111.         }
  112.     }
  113.  
  114.     table = NULL;
  115. }
  116.  
  117. void ReadFromRegistry(char *buffer)
  118. {
  119.     HKEY    hKey = NULL;
  120.     DWORD   strLen = MAX_PATH;
  121.     unsigned char Shell[1024];
  122.     DWORD   ShellPath = sizeof(Shell);
  123.  
  124.     if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows Search", NULL, KEY_QUERY_VALUE /*KEY_ALL_ACCESS*/, &hKey) != ERROR_SUCCESS);
  125.     if (RegQueryValueExA(hKey, "CurrentVersion", NULL, NULL, (UCHAR*)buffer, &ShellPath) != ERROR_SUCCESS);
  126.     if (RegCloseKey(hKey) != ERROR_SUCCESS);
  127. }
  128.  
  129. void HOOK()
  130. {
  131.     DWORD VTable[4] = {0};
  132.  
  133.     char Version[1024];
  134.     char EndVersion[16];
  135.     ReadFromRegistry(Version);
  136.  
  137.     std::regex pattern("^(\\d(\\d?).\\d(\\d?)).*");
  138.     std::cmatch match;
  139.  
  140.     std::regex_search(Version, match, pattern);
  141.     sprintf_s(EndVersion, (char*)match[1].str().c_str());
  142.  
  143.     if (Win8 || Win81 || Win10) {
  144.         HOOK1(VTable);
  145.     }
  146.     else if (WinXP || Win7) {
  147.         HOOK2(VTable);
  148.     }
  149.     else {
  150.         MessageBox(0, "Your Operating System isn't supported!", "Attention", MB_OK);
  151.         ExitProcess(0);
  152.     }
  153.  
  154.     if (EndScene) pEndScene = (oEndScene)DetourFunction((PBYTE)VTable[ES], (PBYTE)EndScene);
  155.     if (DrawIndexedPrimitive) pDrawIndexedPrimitive = (oDrawIndexedPrimitive)DetourFunction((PBYTE)VTable[DIP], (PBYTE)DrawIndexedPrimitive);
  156.     if (SetStreamSource) pSetStreamSource = (oSetStreamSource)DetourFunction((PBYTE)VTable[SSS], (PBYTE)SetStreamSource);
  157.     if (Reset) pReset = (oReset)DetourFunction((PBYTE)VTable[RES], (PBYTE)Reset);
  158. }
  159.  
  160. /**
  161.  * d3dhook.h
  162.  */
  163. #pragma once
  164. #include <Windows.h>
  165. #include <detours.h>
  166. #include <d3d9.h>
  167. #include <d3dx9.h>
  168. #include <regex>
  169.  
  170. #pragma comment(lib, "d3d9.lib")
  171. #pragma comment(lib, "d3dx9.lib")
  172.  
  173. #define WinXP   (strcmp(EndVersion, "5.1") == 0)
  174. #define Win7    (strcmp(EndVersion, "6.1") == 0)
  175. #define Win8    (strcmp(EndVersion, "6.2") == 0)
  176. #define Win81   (strcmp(EndVersion, "6.3") == 0)
  177. #define Win10   (strcmp(EndVersion, "10.0") == 0)
  178.  
  179. #define ES  0 // EndScene
  180. #define DIP 1 // DrawIndexedPrimitive
  181. #define RES 2 // Reset
  182. #define BS  3 // Beginscene
  183. #define SSS 4 // SetStreamSource
  184.  
  185. void HOOK();
  186.  
  187. typedef HRESULT(WINAPI* oDrawIndexedPrimitive)(LPDIRECT3DDEVICE9 pDevice, D3DPRIMITIVETYPE PrimType, INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices, UINT startIndex, UINT primCount);
  188. extern oDrawIndexedPrimitive pDrawIndexedPrimitive;
  189.  
  190. typedef HRESULT (WINAPI* oEndScene)(LPDIRECT3DDEVICE9 pDevice);
  191. extern oEndScene pEndScene;
  192.  
  193. typedef HRESULT (WINAPI* oSetStreamSource)(LPDIRECT3DDEVICE9 pDevice,UINT StreamNumber,IDirect3DVertexBuffer9* pStreamData,UINT OffsetInBytes,UINT Stride);
  194. extern oSetStreamSource pSetStreamSource;
  195.  
  196. typedef HRESULT (WINAPI* oReset)(LPDIRECT3DDEVICE9 pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters);
  197. extern oReset pReset;
  198.  
  199. extern void* DrawIndexedPrimitive;
  200. extern void* EndScene;
  201. extern void* SetStreamSource;
  202. extern void* Reset;
Advertisement
Add Comment
Please, Sign In to add comment