【问题标题】:C++ Jump Hooks [Windows]C++ 跳转挂钩 [Windows]
【发布时间】:2013-09-09 06:24:20
【问题描述】:

嗯,我想学hooking,但是网上找的教程好像不行。

我想做的是一个跳钩,用 C++ 编写。

代码如下:

void DoHook(DWORD* Address, DWORD* Hook, DWORD pid){   

    HANDLE Server = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ , false, pid );
    Address = (DWORD*)Address + 0x18;
    DWORD OldProt;     
    DWORD HookOffset = (DWORD*)Hook-(DWORD*)Address-5;
    std::wcout << "Hook on address" << std::hex << Address<< std::endl;
    std::wcout << "Hook offset is " << std::hex << HookOffset << std::endl;

    if ( ! VirtualProtectEx(Server, (LPVOID) Address, 40,PAGE_EXECUTE_READWRITE, &OldProt) ) {
        ErrorExit(L"VirtualProtectEx");
    };

    char* CharPointer = (char*) Address;
    BYTE newdata[5]={0xE9}; 
    BYTE x;
    int i = 1;
    while ( HookOffset > 0 ) {
        x = HookOffset & 0xff;
        newdata[5-i] = x;
        i++;
        HookOffset >>= 8;
    }
    std::wcout << "Bytes " <<newdata[0] << " " << newdata[1] << " " << newdata[2] << " " << newdata[3] << " " << newdata[4] << std::endl;

    DWORD newdatasize = sizeof(newdata);
    if ( ! WriteProcessMemory(Server,Address,(LPCVOID*)newdata,newdatasize,NULL) ) {
        ErrorExit(L"WriteProcessMemory");
    }

//  VirtualProtect((void*) Address, 40, 0x40, &OldProt);

    return;
}

这是一些输出文本:

Process ID is 2764 // PID of the app that's being hooked
Function address is 00A81190 // this is the function i'm doing the jump to
Entry point is 00080000 // for the app that's being hooked
Hook on address 00080060 // for the app that's being hooked
Hook offset is 28048e // HookAddress - FunctionAddress - 5
Bytes e9 0 28 4 8e // this is the jump i'm planning to do
Press any key to continue . . .

但是,应用程序没有更新。

【问题讨论】:

  • 您可以做的一件事是检查 VirtualProtectEx 的返回值,如果失败,请检查 GetLastError 返回的内容
  • 你期待什么?您正在尝试写入属于另一个进程的内存。你总是会收到这个错误。
  • 谢谢。 VirtualProtectEx 失败,错误 87:参数不正确。
  • 这种方式不能写入其他进程内存。将WriteProcessMemory 与提升的用户权限(管理员)结合使用。
  • 你没有检查OpenProcess的返回值。此外,您的地址数学不正确。在调试器中单步调试代码会发现这两个问题。

标签: c++ windows hook


【解决方案1】:

您必须以管理员身份运行您的程序以获得正确的权限来写入进程内存。这是我的 x86 detour 函数,我已经测试和使用过很多次了

bool Detour32(char* src, char* dst, const intptr_t len)
{
    if (len < 5) return false;

    DWORD  curProtection;
    VirtualProtect(src, len, PAGE_EXECUTE_READWRITE, &curProtection);

    intptr_t  relativeAddress = (intptr_t)(dst - (intptr_t)src) - 5;

    *src = (char)'\xE9';
    *(intptr_t*)((intptr_t)src + 1) = relativeAddress;

    VirtualProtect(src, len, curProtection, &curProtection);
    return true;
}

src是你要放置hook的地址,dst是你要跳转到的地址。 len 是您要使用 jmp 销毁的字节数。 jmp 是 5 个字节,因此如果您要销毁超过 5 个字节的指令,则需要将超过 5 个“被盗字节”复制到您的目的地以确保它们被执行。

【讨论】:

    猜你喜欢
    • 2016-08-08
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多