Hook技术之API拦截,Detours

本文结合姊妹篇观看更加合适:Detour 原理分析-CSDN博客文章浏览阅读96次,点赞3次,收藏3次。Inline Hookhttps://blog.csdn.net/weixin_55030700/article/details/135632966?spm=1001.2014.3001.5502

Detour作为微软官方的Hook库,其代码是非常稳定的,并且多线程安全。(当然了没有绝对的安全,日常使用够了)

Hook MessageBox 示例:

#include <iostream>
#include <windows.h>

#include "detours.h"
#pragma comment (lib,"detours.lib")

typedef int (WINAPI* MESSAGEBOXW_)(_In_opt_ HWND hWnd, _In_opt_ LPCWSTR lpText, _In_opt_ LPCWSTR lpCaption, _In_ UINT uType);
MESSAGEBOXW_ messagebox = 0;

//保存函数原型
static int (WINAPI* OLD_MessageBoxW)(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType) = MessageBoxW;

int WINAPI New_MessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCation, UINT uType) {
    int ret = OLD_MessageBoxW(hWnd, L"输入的参数已修改", L"[测试]", uType);
    return ret;
}

void HOOK() {
    DetourRestoreAfterWith();
    DetourTransactionBegin();

    //更新所有线程,确保所有拦截点都能正确更新
    DetourUpdateThread(GetCurrentThread());

    // 这里可以连续多次调用DetourAttach,表明HOOK多个函数
    // 目标函数指针的地址和指向DetourFunction函数的指针。目标函数未作为参数提供,因为它必须已经存储在目标指针中。
    DetourAttach(&(PVOID&)OLD_MessageBoxW, New_MessageBoxW);

    DetourTransactionCommit();
}

void UnHook()
 {
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());

    //这里可以连续多次调用DetourDetach,表明撤销多个函数HOOK
    DetourDetach(&(PVOID&)OLD_MessageBoxW, New_MessageBoxW);

    DetourTransactionCommit();
}

void main()
 {
    ::MessageBoxW(NULL, L"正常消息框", L"测试", MB_OK);
    HOOK();
    ::MessageBoxW(NULL, L"正常消息框", L"测试", MB_OK);
    UnHook();
    return;
}

静态库如上,快来下载资源吧

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值