钩/绕道 d3d9 (现在/结束场景) - 似乎调用我的函数然后崩溃
Hooking/Detouring d3d9 (Present/EndScene) - Seems to call my function then crashes
正如标题所说,我正在尝试挂钩DirectX 9V-Table 并在屏幕上显示一些信息,我已经研究了几天的弯路和挂钩,我认为我在一定程度上理解了它,但现在我不太确定如何调试这个问题。
我的钩子正在调用另一个函数ShowMsg()
,它最终将是一个绘制函数,但现在它只显示一个消息框。
我的ShowMsg()
函数被调用,但随后程序崩溃。
我正在使用DirectX 2010 年 2 月的"SimpleSample">应用程序。
这是我的代码,注释的部分来自之前从另一个测试应用程序挂钩函数的测试。
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include "VirtualTable.h"
#include <d3d9.h>
#pragma comment(lib, "d3d9.lib")
#include <d3dx9.h>
#pragma comment(lib, "d3dx9.lib")
#include <detours.h>
#pragma comment(lib, "detours.lib")
using namespace std;
typedef void(__thiscall* Present)(IDirect3DDevice9* device);
Present g_org_Present;
void ShowMsg()
{
MessageBoxA(0, "This function was called.", "", 0);
}
void __fastcall hk_Present(IDirect3DDevice9* device)
{
ShowMsg();
//call the original function
g_org_Present(device);
}
void InitiateHooks()
{
HWND game_window = FindWindowA(NULL, "SimpleSample");
auto d3dpp = D3DPRESENT_PARAMETERS{};
auto d3d = Direct3DCreate9(D3D_SDK_VERSION);
IDirect3DDevice9* device;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = game_window;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
d3dpp.BackBufferFormat = D3DFMT_R5G6B5;
d3dpp.Windowed = TRUE;
if (SUCCEEDED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, game_window, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device)))
{
void** vmt = *(void***)device;
DWORD oldProtection;
VirtualProtect(&vmt[17], 4, PAGE_EXECUTE_READWRITE, &oldProtection);
g_org_Present = (Present)vmt[17];
vmt[17] = &hk_Present;
VirtualProtect(&vmt[17], 4, oldProtection, 0);
device->Present(NULL, NULL, NULL, NULL);
}
// VirtualTable* myTable = new VirtualTable();
//get the pointer to the actual virtual method table from our pointer to our class instance
// void** base = *(void***)myTable;
// DWORD oldProtection;
//one way to remove page protection(not the best but this is an example only)
// VirtualProtect(&base[1], 4, PAGE_EXECUTE_READWRITE, &oldProtection);
//save the original function
// g_org_VirtualFunction01 = (VirtualFunction01_t)base[1];
//overwrite
// base[1] = &hk_VirtualFunction01;
//restore page protection
// VirtualProtect(&base[1], 4, oldProtection, 0);
//call the virtual function (now hooked) from our class instance
// myTable->VirtualFunction01();
}
#pragma endregion
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
CreateThread(0, 0x1000, (LPTHREAD_START_ROUTINE)InitiateHooks, 0, 0, NULL);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
有人可以告诉我我可能在这里做错了什么,以便我可以解决这个问题,并供将来参考。
这是更新的代码,我意识到我不应该直接调用该函数,所以我更改了它,还将其更改为尝试钩子/绕道 EndScene,也使用 MS 绕道而不是我认为是 V-Table 修补的其他方法,似乎我的 EndScene 钩子正在被调用,因为 MessageBox 被连续调用。但是,如果我注释掉消息框,程序仍然崩溃。
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <intrin.h>
#include <tchar.h>
#include <tlhelp32.h>
#include <Psapi.h>
#include <winsock2.h>
#include <vector>
#include <ws2tcpip.h>
#pragma comment( lib, "Ws2_32.lib" )
#include <d3d9.h>
#pragma comment(lib, "d3d9.lib")
#include <d3dx9.h>
#pragma comment(lib, "d3dx9.lib")
#include <detours.h>
#pragma comment(lib, "detours.lib")
using namespace std;
D3DCOLOR RED = D3DCOLOR_ARGB(255, 255, 0, 0);
typedef HRESULT(__stdcall* EndScene) (IDirect3DDevice9*);
EndScene EndScene_orig;
HRESULT __stdcall EndScene_hook(IDirect3DDevice9* pDevice)
{
// D3DRECT rec = { 100,100,200,200 };
// pDevice->Clear(1, &rec, D3DCLEAR_TARGET, RED, 0, 0);
// MessageBoxA(0, "We made it here...2", "", 0); // <<<<----- This function is called over and over when not commented.
return EndScene_orig(pDevice);
}
void InitHook()
{
HWND game_window = FindWindow(NULL, _T("Skinned Mesh"));
auto d3dpp = D3DPRESENT_PARAMETERS{};
auto d3d = Direct3DCreate9(D3D_SDK_VERSION);
if (d3d)
{
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = game_window;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
d3dpp.BackBufferFormat = D3DFMT_R5G6B5;
d3dpp.Windowed = TRUE;
IDirect3DDevice9* Device;
if (SUCCEEDED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, game_window, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &Device)))
{
// MessageBoxA(0, "We made it here...", "", 0);
DWORD* pVTable = *reinterpret_cast<DWORD**>(Device);
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
EndScene_orig = (EndScene)pVTable[42];
DetourAttach(&(LPVOID&)pVTable[42], (PBYTE)EndScene_hook);
DetourTransactionCommit();
}
}
}
void SetupConsole()
{
AllocConsole();
freopen("CONOUT$", "wb", stdout);
freopen("CONOUT$", "wb", stderr);
freopen("CONIN$", "rb", stdin);
SetConsoleTitle("CSGOHAX");
}
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
SetupConsole();
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)InitHook, 0, 0, NULL);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
它必须是一些简单的事情,我只是不能认为是一个问题。
我想通了,反转了IDA中的二进制文件,找到了模块地址和EndScene函数及其地址,计算了偏移量。然后使用 ollydbg 再次找到该功能,从中签名,现在我可以使用签名扫描功能动态找到它。
所以我可以用这个签名获取函数地址。
DWORD dwEndScene = FindPattern("d3d9.dll",
"x6Ax18xB8x00x00x00x00xE8x00x00x00x00x8Bx7Dx08x8BxDFx8Dx47x04xF7xDBx1BxDBx23xD8x89x5DxE0x33xF6x89x75xE4x39x73x18x75x73",
"xxx????x????xxxxxxxxxxxxxxxxxxxxxxxxxxx");
然后我就绕道函数
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
EndScene_orig = (oEndScene)(dwEndScene);
DetourAttach(&(LPVOID&)EndScene_orig, EndScene_hook);
这比像以前那样尝试使用虚拟设备在 V-Table 中找到函数要容易得多。
相关文章:
- 为什么我的运算符 + 重载尽管是通过引用传递的,但仍调用我的复制构造函数?
- 什么时候可以使用常量装饰调用我的重载函数?
- 为什么当我做复制和交换习语时不调用我的复制构造函数?
- 钩/绕道 d3d9 (现在/结束场景) - 似乎调用我的函数然后崩溃
- 为什么在这种情况下不调用我的虚拟函数实现?
- 在linux中,mono调用我的.so-lib返回System.EntryPointNotFoundException
- 当我从头文件和实现文件调用我的函数到我的主文件时,我没有得到任何输出
- 为什么没有调用我的回调?
- 如何制作指针来调用我的成员函数
- 如何在Android Studio中编写CMakelists来调用我的C 项目(JNI)的功能
- 为什么在这种情况下不调用我的移动构造函数?(首先分配 r 值引用并创建对象)
- 我如何调用我的3个数据成员中每个人的设置方法,然后显示由我的设置方法设置的值
- 为什么没有调用我的覆盖方法
- 我的类中有方法的指针数组,但我不能调用我的方法.代码如下
- C++模板:无法从 main.cpp 调用我的函数
- 为什么我继承的构造函数调用我的基本默认构造函数
- 我如何在C#GUI中调用我的C 程序
- SetWindowsHookEx没有调用我的回调
- c++ Glut 显示需要调用我的绘制函数,但我无法传递任何参数
- 如何从对象中调用我的set回调函数