Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Использоваение:
- *
- * HRESULT APIENTRY hEndScene( LPDIRECT3DDEVICE9 pDevice )
- * {
- * return pEndScene(pDevice);
- * }
- *
- * void __stdcall main()
- * {
- * EndScene = hEndScene;
- * HOOK();
- * }
- *
- * bool __stdcall DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
- * {
- * if (fdwReason == DLL_PROCESS_ATTACH)
- * CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)main, NULL, NULL, NULL);
- *
- * return true;
- * }
- /**
- * d3dhook.cpp
- */
- #include "d3dhook.h"
- oDrawIndexedPrimitive pDrawIndexedPrimitive = NULL;
- oEndScene pEndScene = NULL;
- oSetStreamSource pSetStreamSource = NULL;
- oReset pReset = NULL;
- void* DrawIndexedPrimitive = NULL;
- void* EndScene = NULL;
- void* SetStreamSource = NULL;
- void* Reset = NULL;
- /**
- * HOOK
- */
- LRESULT CALLBACK MsgProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { return DefWindowProc(hwnd, uMsg, wParam, lParam); }
- void HOOK1(DWORD* table)
- {
- WNDCLASSEX wc = {sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,GetModuleHandle(NULL),NULL,NULL,NULL,NULL,"DX",NULL};
- RegisterClassEx(&wc);
- HWND hWnd = CreateWindow("DX",NULL,WS_OVERLAPPEDWINDOW,100,100,300,300,GetDesktopWindow(),NULL,wc.hInstance,NULL);
- D3DPRESENT_PARAMETERS d3dpp;
- ZeroMemory( &d3dpp, sizeof(d3dpp) );
- d3dpp.Windowed = TRUE;
- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
- LPDIRECT3DDEVICE9 pd3dDevice;
- LPDIRECT3D9 pD3D = Direct3DCreate9( D3D_SDK_VERSION );
- pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pd3dDevice);
- pD3D->Release();
- DWORD* pVTable = (DWORD*)pd3dDevice;
- pVTable = (DWORD*)pVTable[0];
- table[ES] = pVTable[42]; //EndScene address
- table[DIP] = pVTable[82]; //DrawIndexedPrimitive address
- table[RES] = pVTable[16]; //Reset address
- table[BS] = pVTable[41]; //BeginScene address
- table[SSS] = pVTable[100]; //SetStreamSource
- DestroyWindow(hWnd);
- }
- void HOOK2(DWORD* table)
- {
- HMODULE hD3D9 = LoadLibraryA("d3d9.dll");
- PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)hD3D9;
- if (dosHeader->e_magic == IMAGE_DOS_SIGNATURE) {
- PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)((DWORD)dosHeader + dosHeader->e_lfanew);
- if (ntHeaders->Signature == IMAGE_NT_SIGNATURE) {
- DWORD NumberOfSections = ntHeaders->FileHeader.NumberOfSections;
- PIMAGE_SECTION_HEADER SectionHeader = (PIMAGE_SECTION_HEADER)((DWORD)dosHeader + dosHeader->e_lfanew + sizeof(IMAGE_NT_HEADERS));
- for (unsigned int i = 0; i < NumberOfSections; i++) {
- if (strncmp((const char *)SectionHeader[i].Name, ".text", 5) == 0) {
- DWORD dwFrom = (DWORD)dosHeader + SectionHeader[i].VirtualAddress;
- DWORD dwTo = dwFrom + SectionHeader[i].Misc.VirtualSize;
- //C7 06 ?? ?? ?? ?? 89 86 ?? ?? ?? ?? 89 86
- for (unsigned int i = dwFrom; i < dwTo; i++) {
- if (*(WORD*)(i) == 0x06C7)
- if (*(WORD*)(i + 6) == 0x8689)
- if (*(WORD*)(i + 12) == 0x8689) {
- PDWORD pVTable = *(PDWORD*)(i + 2);
- table[ES] = pVTable[42]; //EndScene address
- table[DIP] = pVTable[82]; //DrawIndexedPrimitive address
- table[RES] = pVTable[16]; //Reset address
- table[BS] = pVTable[41]; //BeginScene address
- table[SSS] = pVTable[100]; //SetStreamSource
- return;
- }
- }
- }
- }
- }
- }
- table = NULL;
- }
- void ReadFromRegistry(char *buffer)
- {
- HKEY hKey = NULL;
- DWORD strLen = MAX_PATH;
- unsigned char Shell[1024];
- DWORD ShellPath = sizeof(Shell);
- if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows Search", NULL, KEY_QUERY_VALUE /*KEY_ALL_ACCESS*/, &hKey) != ERROR_SUCCESS);
- if (RegQueryValueExA(hKey, "CurrentVersion", NULL, NULL, (UCHAR*)buffer, &ShellPath) != ERROR_SUCCESS);
- if (RegCloseKey(hKey) != ERROR_SUCCESS);
- }
- void HOOK()
- {
- DWORD VTable[4] = {0};
- char Version[1024];
- char EndVersion[16];
- ReadFromRegistry(Version);
- std::regex pattern("^(\\d(\\d?).\\d(\\d?)).*");
- std::cmatch match;
- std::regex_search(Version, match, pattern);
- sprintf_s(EndVersion, (char*)match[1].str().c_str());
- if (Win8 || Win81 || Win10) {
- HOOK1(VTable);
- }
- else if (WinXP || Win7) {
- HOOK2(VTable);
- }
- else {
- MessageBox(0, "Your Operating System isn't supported!", "Attention", MB_OK);
- ExitProcess(0);
- }
- if (EndScene) pEndScene = (oEndScene)DetourFunction((PBYTE)VTable[ES], (PBYTE)EndScene);
- if (DrawIndexedPrimitive) pDrawIndexedPrimitive = (oDrawIndexedPrimitive)DetourFunction((PBYTE)VTable[DIP], (PBYTE)DrawIndexedPrimitive);
- if (SetStreamSource) pSetStreamSource = (oSetStreamSource)DetourFunction((PBYTE)VTable[SSS], (PBYTE)SetStreamSource);
- if (Reset) pReset = (oReset)DetourFunction((PBYTE)VTable[RES], (PBYTE)Reset);
- }
- /**
- * d3dhook.h
- */
- #pragma once
- #include <Windows.h>
- #include <detours.h>
- #include <d3d9.h>
- #include <d3dx9.h>
- #include <regex>
- #pragma comment(lib, "d3d9.lib")
- #pragma comment(lib, "d3dx9.lib")
- #define WinXP (strcmp(EndVersion, "5.1") == 0)
- #define Win7 (strcmp(EndVersion, "6.1") == 0)
- #define Win8 (strcmp(EndVersion, "6.2") == 0)
- #define Win81 (strcmp(EndVersion, "6.3") == 0)
- #define Win10 (strcmp(EndVersion, "10.0") == 0)
- #define ES 0 // EndScene
- #define DIP 1 // DrawIndexedPrimitive
- #define RES 2 // Reset
- #define BS 3 // Beginscene
- #define SSS 4 // SetStreamSource
- void HOOK();
- typedef HRESULT(WINAPI* oDrawIndexedPrimitive)(LPDIRECT3DDEVICE9 pDevice, D3DPRIMITIVETYPE PrimType, INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices, UINT startIndex, UINT primCount);
- extern oDrawIndexedPrimitive pDrawIndexedPrimitive;
- typedef HRESULT (WINAPI* oEndScene)(LPDIRECT3DDEVICE9 pDevice);
- extern oEndScene pEndScene;
- typedef HRESULT (WINAPI* oSetStreamSource)(LPDIRECT3DDEVICE9 pDevice,UINT StreamNumber,IDirect3DVertexBuffer9* pStreamData,UINT OffsetInBytes,UINT Stride);
- extern oSetStreamSource pSetStreamSource;
- typedef HRESULT (WINAPI* oReset)(LPDIRECT3DDEVICE9 pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters);
- extern oReset pReset;
- extern void* DrawIndexedPrimitive;
- extern void* EndScene;
- extern void* SetStreamSource;
- extern void* Reset;
Advertisement
Add Comment
Please, Sign In to add comment