当前位置:编程学习 > C/C++ >>

Windows下的函数hook技术

都是很成熟的东西了,这几天看了看,总结一下而已。
讨论了Windows下hook函数的几种方法。提供了一个hook TextOutA的完整例子。通过CreateRemoteThread的方法把hook dll注入到一个普通的应用程序中。Hooking Imported Functions by name调用 imported functions时的步骤/实现
在程序中调用从其它模块引入的函数的方法和普通的函数调用有所不同。对于普通的函数调用,直接使用
call address来调用即可,但是对于
imported functions
,在编译的时候
compiler/link
并不知道实际的函数实现会被加载到那个地址,函数实现在那个地址在运行的时候才会确定。对于
imported functions
,首先是
call
引入表中的一个函数,在运行时再初始化引入表,使用
jmp
跳转到真实的函数实现。


引入表:
The PE file IMAGE_IMPORT_DESCRIPTOR structure, which holds all the information about functions imported from a specific DLL, has pointers to two arrays in the executable. These arrays are called import address tables (IATs), or sometimes thunk data arrays. The first pointer references the real IAT, which the program loader fixes up when the executable is loaded. The second pointer references the original IAT, which is untouched by the loader and lists the imported functions.


实现原理

  • 找到
    PE
    文件的
    Image_Import_Descriptor
    结构
  • 找到
    Original LAT

    Real LAT.
  • 通过要
    hook
    的函数的名字在
    Original LAT
    找到要
    hook

    imported function
    在数组中的
    index.
  • 保存并修改
    Real LAT
    在相应
    index

    function address

(refer to John Robbins, BugsLayerUtil.dll)

Hooking Imported Functions by ordinal

原理和
Hook Imported functions by name
一样,只是是通过要
hook
的函数的
ordinal

original LAT
中找到
index.

Hooking a function in this dll

当一个
DLL
是通过
LoadLibrary
载入的时候,我们无法通过
hook imported function
的方法的
hook
它中的
function
。有两种可能的办法处理这种情况:
第一种方法,遍历进程空间,发现
call
指定函数的地方替换为
call hookFunction.
太麻烦,而且不安全。


第二种方法,改写要
hook
的函数
FuncA
。比较好的方法

  1. 实现
    HookFuncA
    ,最后的实现垫入
    n

    nop.
  2. 找到要
    hook
    的函数
    FuncA
    的绝对地址,改写前
    5
    个字节为
    jmp hookFuncA(
    假定前
    5
    个字节为
    n
    个完整的指令
    )


FuncA
的前
5
个字节拷贝到
hookFuncA
的后面,在加上一条指令 jmp funcA+5.
----Code of HookDLL.dll, 可以通过CreateRemoteThread的方法把hook dll注入到一个普通的应用程序中。
// HookDLL.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "HookDLL.h"
#include "Log.h"
//forward declare.
LRESULT WINAPI InstallTextoutHook();
LRESULT WINAPI UninstallTextoutHook();
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
if (InstallTextoutHook())
{
WriteLog("Install hook success. ");
}else
{
WriteLog("Intall hook failed. ");
}
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
if (UninstallTextoutHook())
{
WriteLog("Uninstall hook success. ");
}else
{
WriteLog("Unintall hook failed. ");
}
break;
}
return TRUE;
}
#define DWORD_PTR DWORD*
#define __LOCAL_SIZE 40h
#define NAKED_PROLOG()
DWORD_PTR dwRet ;
DWORD_PTR dwESI ;
{
__asm PUSH EBP /* Set up the standard frame.*/
__asm MOV EBP , ESP
__asm SUB ESP , __LOCAL_SIZE /* Save room for the local */
/* variables. */
__asm MOV EAX , EBP /* EBP has the stack coming */
/* into the fn. in it. */
__asm ADD EAX , 4 /* Account for PUSH EBP */
__asm MOV EAX , [EAX] /* Get return address. */
__asm MOV [dwRet] , EAX /* Save return address. */
__asm MOV [dwESI] , ESI /* Save ESI so chkesp in dbg */
/* builds works. */
}// The common epilog part that can be shared between the stdcall and
// cdecl hook functions.
#define EPILOG_COMMON()
{
__asm MOV ESI , [dwESI] /* Restore ESI. */
__asm ADD ESP , __LOCAL_SIZE /* Take away local var space */
__asm MOV ESP, EBP /* Restore standard frame. */
__asm POP EBP
}
#define COPY_CODE_LENGTH 5
BYTE g_abOriCode[COPY_CODE_LENGTH];
BYTE g_abJmpCode[COPY_CODE_LENGTH];
PROC g_oriTextout;
BOOL g_blHooked = FALSE;
LRESULT WINAPI InstallTextoutHook()
{
if (g_blHooked)
return TRUE;
//Get TextOutAs address.
HMODULE hGdi32 = ::LoadLibrary(_T("Gdi32.dll"));
g_oriTextout = GetProcAddress(hGdi32, _T("TextOutA"));
if (NULL == g_oriTextout)
return FALSE;
//Get the hooka address.
HMODULE hModule = GetModuleHandle(_T("HookDLL.dll"));
if (NULL == hModule)
return FALSE;
DWORD dwHookAddr = NULL;
__asm
{
mov esi, offset HookLabel;
mov edi, 0x10000000;//0x10000000 is the dlls base address.
sub esi, edi;
add esi, hModule;
mov [dwHookAddr], esi;
}
//Get the NOPs address.
DWORD dwNOPAddr = NULL;
__asm
{
mov esi, offset NOPLabel;
mov edi, 0x10000000;//0x10000000 is the dlls base address.
sub esi, edi;
add esi, hModule;
mov [dwNOPAddr], esi;
}
//Save the first 5 byte of TextOutA to g_abOriCode
__asm
{
mov esi, g_oriTextout;
lea edi, g_abOriCode;
cld;
movsd;
movsb;
}
//Generate the jmp Hook function.
g_abJmpCode[0] = 0xe9;
__asm
{
mov eax, dwHookAddr;
mov ebx, g_oriTextout;
add ebx, 5;
sub eax, ebx;
mov dword ptr[g_abJmpCode+1], eax;
}
//Write the jump instruction to the textoutA.
DWORD dwProcessId = GetCurrentProcessId();
HANDLE hProcess = OpenProcess (PROCESS_ALL_ACCESS,
FALSE, dwProcessId);
if (NULL == hProcess)
return FALSE;
DWORD dwOldFlag;
VirtualProtectEx(hProcess, g_oriTextout, 5, PAGE_READWRITE, &dwOldFlag);
WriteProcessMemory(hProcess, g_oriTextout, g_abJmpCode, sizeof(g_abJmpCode), NULL

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,