利用detours实现API劫持

一直想玩黑客技术,今天正好找到一个detours的库,这是微软的出品的用来搞这类活动的库,下载下来,用了一下,把messagebox给劫持了,感觉各种好happy,23333333333
这里需要特别注意的一点是,一定要在release模式下使用,否则是显示不出效果的。

// ====================== 【API劫持】===========================
// @ author         :       zhyh2010
// @ date           :       20150530
// @ version        :       1.0
// @ description    :       劫持 系统API 这里以 MessageBox 为例
//                      注意 需要预先配置 detours 
// @ detours 配置 :   http://research.microsoft.com/en-us/projects/detours/
//              安装后, 使用 nmake 编译源文件,并将相应文件拷贝到工程目录即可
//                          【一定要用release模式编译 <^-^>】
// =================== 【end of API劫持】========================


#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "detours.h"
#include "detver.h"
#pragma comment(lib, "detours.lib")

static int (WINAPI *OldMessageBoxA)(
    HWND hWnd,
    LPCWSTR lpText,
    LPCWSTR lpCaption,
    UINT uType) = MessageBoxA;

int WINAPI NewMessageBoxA(
    HWND hWnd,
    LPCWSTR lpText,
    LPCWSTR lpCaption,
    UINT uType)
{
    printf("哈哈,你的函数被我劫持了哦<^_^>\n");
    return 0;
}

VOID Hook()
{
    DetourRestoreAfterWith();
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());

    //这里可以连续多次调用DetourAttach,表明HOOK多个函数
    DetourAttach((void **)&OldMessageBoxA, NewMessageBoxA);

    DetourTransactionCommit();
}

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

    //这里可以连续多次调用DetourDetach,表明撤销多个函数HOOK
    DetourDetach((void **)&OldMessageBoxA, NewMessageBoxA);

    DetourTransactionCommit();

}

void main()
{
    MessageBoxA(0, "锄禾日当午1", "学C真辛苦", 0);
    Hook();
    MessageBoxA(0, "锄禾日当午2", "学C真辛苦", 0);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值