C ++和绕行

时间:2019-03-21 13:34:49

标签: c++ reverse-engineering

所以最近几天我开始学习c ++,因为我想参与一些游戏黑客和逆向工程。我通过基本的绕道方法尝试了运气,只需在目标程序内存中的功能中插入一个jmp即可。但是,我想开始使用Microsoft的绕行库并尝试一下。我现在唯一的目标是每次调用该函数时都要注册。

由于某种原因,我的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,但是我几乎没有经验能够验证任何此信息,因此将不胜感激。

1 个答案:

答案 0 :(得分:0)

当您使用MS Detours时,我认为它没有为jmp写错跳转地址。您的代码是正确的,除了我有2个小问题

您正在使用PVOID *,这应该不会引起任何问题,但是它是多余的。 PVOID被定义为void *,因此,当您编写PVOID *时,实际上是在做void **,这没有任何意义。

与我通常使用MS Detours的方式有一些小的语法差异

这就是我使用它时的样子:

typedef int (__cdecl* tFunc)(int x);
tFunc oFunc = nullptr;

int hkFunc(int x)
{
//stuff
}

oFunc = (tFunc)(DWORD)GetModuleHandleA("Borderlands2.exe") + 0xA18940;
DetourAttach(&(PVOID &)oFunc, hkFunc);

如果这些更改不能解决您的问题,我认为您正在使用的调用约定(cdecl)是错误的。您不能总是信任IDA Pro,有时必须手动确认调用约定的参数和返回类型。

如果需要其他帮助,请发布更多信息,我们需要在通话前后查看10条说明,以及被叫方的前10条和后10条指令。

相关问题