-1

Let's say that we have function A and function B, as shown below :

#include <stdio.h>
#include <stdlib.h>

void  a( )
{
    printf("Hello from function a!\n");

    return;
}

void  b( )
{
    printf("Hello from function b!\n");

    return;
}

int main( )
{

    printf( "a = %p, b = %p\n", a, b );
    a( );
    b( );

    return 0;
}

Which in this case gives the output of :

a = 00401334, b = 00401349
Hello from function a!
Hello from function b!

Is there a way to programatically( using C and the winapi ) switch those 2 function calls from an external program to have the call to a( ) print out b's message and b( ) print out a's message with a hook of some type? If so, how would I write such a program, assuming my program is running long enough to switch the functions, and the functions aren't inlined.

4

2 回答 2

0

如果你想学习钩子,自己试验,你必须意识到修补/钩子是一个庞大而复杂的主题:-)

  1. 你想在磁盘上打补丁,还是在应用程序运行时打补丁?
  2. 您是否控制目标应用程序的来源?
  3. 目标函数是位于 EXE 中还是 DLL 中?
  4. 在 DLL 的情况下,函数是否被导出?

作为初学者,我会推荐:

尝试在您自己的 EXE 中修补一个函数,当它正在运行时,从它本身(不涉及外部进程)

  1. 获取函数的地址(纯 C/C++)
  2. 通过 VirtualAlloc 分配可执行内存
  3. 在可执行内存中写入二进制操作码
  4. 通过跳转到新代码覆盖函数的开头(保存被覆盖的部分)

警告:您必须了解一些关于操作码、堆栈、调用约定等的基础知识……特别是,函数开头的覆盖必须保持其余部分的操作码完整性(如果您打算跳回去) ,这可能很棘手。

一些链接:

x86 API 挂钩揭秘

MinHook - 简约的 x86/x64 API 挂钩库

EasyHook - Windows API Hooking 的再发明

于 2013-11-06T15:32:17.810 回答
0

听起来最好的办法就是在程序中加入一些逻辑,根据当前情况决定调用哪个函数。您需要在代码中构建一些东西,让它以某种受控方式响应外部程序。

公开或操作任何较低级别的内容(例如修改二进制文件中的调用指令)将很难安全/可靠地完成。

于 2013-11-06T00:38:55.510 回答