【发布时间】:2019-08-12 09:06:32
【问题描述】:
所以最近几天我开始学习 c++,因为我想了解一些游戏黑客和逆向工程。我通过在我的目标程序内存中插入一个 jmp 到我的函数来尝试使用基本的迂回方法来试试运气,这很好。但是我想开始使用 Microsoft 的 detour 库并试一试。我现在唯一的目标是在每次调用该函数时进行注册。
由于某种原因,我的 DLL 将错误的地址写入内存以进行跳转,从而导致程序崩溃。
The jump, the address attached leads to empty memory
它写入正确的地址,但没有跳转到正确的位置。我对 c++ 很陌生,所以这可能是我的错误,但我无法让它工作。
#include "stdafx.h"
#include <detours.h>
#include <windows.h>
#include <iostream>
DWORD AddressOfFunc;
typedef int (__cdecl* func)(int x);
int hookFunc(int x) {
std::cout << "TRIGGERED!" << std::endl;
func originalFunc = (func)AddressOfFunc;
return originalFunc(x);
}
BOOL WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) {
AddressOfFunc = (DWORD)GetModuleHandleA("Borderlands2.exe") + 0xA18940;
if (dwReason = DLL_PROCESS_ATTACH) {
AllocConsole();
FILE* fp;
freopen_s(&fp, "CONOUT$", "w", stdout);
std::cout << "Injected!" << std::endl;
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)&AddressOfFunc, &hookFunc);
DetourTransactionCommit();
}
else if (dwReason == DLL_PROCESS_DETACH) {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(LPVOID&)AddressOfFunc, &hookFunc);
DetourTransactionCommit();
}
}
IdaPro 告诉我该函数需要一个 int 并返回一个 int,但我需要一点经验才能验证这些信息中的任何一个,因此我们将不胜感激。
【问题讨论】:
-
我不知道你在做什么,但从严格的 C++ 角度来看,它应该是
DetourAttach((PVOID*)&AddressOfFunc, (PVOID)hookFunc);。 -
你应该检查从
DetourAttach返回的错误代码。 -
不要发布照片的链接,而是您的书面代码
-
@john 我将其更改为 (PVOID*) 但结果是一样的,它正确地写入了跳转,但写入了没有指令的内存地址。
-
无意冒犯,但这是我见过的最糟糕的 C++ 学习方法。
标签: c++ reverse-engineering detours