现在的位置: 首页 > 综合 > 正文

hook杂记(二)

2011年01月03日 ⁄ 综合 ⁄ 共 1217字 ⁄ 字号 评论关闭
修改自https://forum.eviloctal.com/thread-28859-1-1.html
在VC6编译通过

#include <stdio.h>
#include <windows.h>
// 保存原始的6个字节代码
BYTE orig_code[6] = {0x90, 0x90, 0x90, 0x90, 0x90,0x90};
// JMP 0xXXXXXXXX
BYTE hook_code[5] = { 0xe9, 0, 0, 0, 0 };
BYTE jmp_orig_code[5] = { 0xe9, 0, 0, 0, 0};
int func();
int fake_func();
void hook_func();
int jmp_back();

int main(int argc, char **argv)
{
    int ret;
    hook_func();
    ret = func();
    return ret;
}
int func()
{
    printf("func()\n");
    return 0;
}
void hook_func()
{
    DWORD dwOldProtect;
    if(!VirtualProtect(jmp_back, 12, PAGE_EXECUTE_READWRITE, &dwOldProtect))
    {
        printf("VirtualProtect error!\r\n");
        return;
    }
    // 保存原始操作码
    memcpy(orig_code, (BYTE *)func, 6);
    // 计算fack_func地址
    *((ULONG*)(hook_code+1) ) = (ULONG)fake_func - (ULONG)func - 5;
    // 修改原始入口
    memcpy((BYTE *)func, hook_code, 5);
    // 计算跳回地址
    *( (ULONG*)(jmp_orig_code+1) ) = (ULONG)func - (ULONG)jmp_back -5;
    // 填充jmp_back
    memcpy((BYTE *)jmp_back, orig_code, 6);
    memcpy((BYTE *)jmp_back+6, jmp_orig_code, 5);
}
__declspec(naked) int jmp_back()
{
    __asm
    {
        _emit 0x90
        _emit 0x90
        _emit 0x90
        _emit 0x90
        _emit 0x90
        _emit 0x90
        _emit 0x90
        _emit 0x90
        _emit 0x90
        _emit 0x90
    }
}
int fake_func()
{
    int ret;
    printf("fake_func()\n");
    ret = jmp_back();
    return ret;
}

抱歉!评论已关闭.