使用微软的HOOK库

Detours是微软开发的一个函数库,可用于捕获系统API。在用其进行程序开发之前,得做一些准备工作:
一.下载Detours
在http://research.microsoft.com/sn/detours 可免费下载Detours,当前的最新版本是
· Detours Express 2.1 is available for immediate download under a no-fee, click-through license for research, non-commercial, and non-production use on 32-bit code.
· Detours Professional 2.1 includes a license for use in production environments and the right to distribute detour functions in products. In addition to support for 32-bit x86 code, Detours Professional 2.1 includes support for 64-bit code on x64 and IA64 processors. For information on licensing Detours Professional 2.1 visit Microsoft's IP Licensing Group at www.microsoft.com/iplicensing and search under Detours.
由于我只是用于研究学习,所以下载的是第一个版本。
二.安装Detours
从网上下载的是DetoursExpress.msi,安装该文件

三.生成Detours库
在安装后的文件夹下找不到直接可以拿来用的LIB文件,但是却有SRC文件(在**\Microsoft Research\Detours Express 2.1\src下)。该文件夹下还有Makefile,可以直接用来生成库。
安装后的文件夹目录

 具体生成库的做法是:
 1.将Detours路径下的SCR文件夹拷贝到**\Microsoft Visual Studio\VC98路径下,注意是整个文件夹
 2.运行**\Microsoft Visual Studio\VC98\Bin下VCVARS32.BAT文件.
    在开始->运行里面输入CMD命令,在出来的命令行窗口里,将路径换至**\Microsoft Visual Studio\VC98\Bin,再将VCVARS32.BAT文件拖进命令行窗口里就行
   
 
 3.运行NMAKE命令
     在命令行窗口里将路径换到**\Microsoft Visual Studio\VC98\SRC,然后输入..\bin\nmake指令,回车
    

  4.待此命令运行完后,在**\Microsoft Visual Studio\VC98\Lib文件下就能找到detoured.lib与detours.lib文件.

// HOOK_使用微软的HOOK库.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
//1. 添加HOOK库的头文件.
#include "detours/detours.h"

//2. 包含静态库
#ifdef _X64
#pragma comment(lib,"detours/lib.X64/detours.lib")
#else
#pragma comment(lib,"detours/lib.X86/detours.lib")
#endif // _X64

// 3. 定义想要HOOK的API的函数原型.
// 定义函数类型
typedef DWORD( WINAPI *fnMessageBox )( DWORD , TCHAR* , TCHAR* , DWORD );

// 4. 定义一个函数指针变量, 用于保存原始版本的API(没有被HOOK的API)
fnMessageBox g_pSrcMessageBoxW ;

// 5. 盗版的MessageBox
DWORD WINAPI MyMessageBox( HWND hWnd , TCHAR* pText , TCHAR* pTitle , DWORD type ) {
        
    // 调用原版函数
    g_pSrcMessageBoxW( 0 , L"在盗版的MessageBox中弹出此框" , L"提示" , 0 );

    return 0;
}

/*!
 * \brief : hookFunction
 * \return: LPVOID
 * \param : LPVOID pSrcFcuntion 要被HOOK的API函数地址
 * \param : LPVOID pDesFcuntion 要安装的钩子函数.
 */
LPVOID hookFunction( LPVOID pSrcFcuntion , LPVOID pDesFcuntion ) {

    // 
    DetourTransactionBegin( );
    DetourUpdateThread( GetCurrentThread( ) );

    // 拦截函数,并把被钩住的的函数输出.
    // 作用: 
    // 将pSrcFcuntion所保存的函数地址进行HOOK
    // 并将被HOOK前的API输出到pSrcFcuntion
    DetourAttach( &pSrcFcuntion , pDesFcuntion );

    // 提交所有的更改(所有的HOOK)
    if( DetourTransactionCommit( ) == NO_ERROR )
        return pSrcFcuntion;// 返回原始版本的API地址
    return NULL;
}



int _tmain(int argc, _TCHAR* argv[])
{
    // detours使用步骤:
    // 1. 初始化detours
    // 2. 更新进行detours的线程
    // 3. 设置钩子
    // 4. 检查钩子是否设置错误

    MessageBoxW( 0 , L"正版函数" , L"提示" , 0 );


    g_pSrcMessageBoxW = 
        (fnMessageBox)hookFunction( &MessageBoxW , &MyMessageBox );



    MessageBoxW( 0 , L"正版函数" , L"提示" , 0 );


    DetourTransactionBegin( );
    DetourUpdateThread( GetCurrentThread( ) );

    // 卸载钩子
    DetourDetach( (LPVOID*)&g_pSrcMessageBoxW , &MyMessageBox );
    DetourTransactionCommit( );
    
    MessageBoxW( 0 , L"正版函数" , L"提示" , 0 );


    return 0;
}

=======================

你可能感兴趣的:(使用微软的HOOK库)