zoukankan      html  css  js  c++  java
  • Detours的作用和实例

    Detours 可以用来拦截Win32的API函数,从而让程序按照我们自定义的方式进行处理,而不是Windows默认的。

    Detours 也是通过Dll的方式,拦截Api函数。

    为什么是修改API的前5个字节?

      现在NewCode[]里的指令相当于Jmp MyMessageBoxW
      既然已经获取到了Jmp MyMessageBoxW
      现在该是将Jmp MyMessageBoxW写入原API入口前5个字节的时候了
      //知道为什么是5个字节吗?
      //Jmp指令相当于0xe9,占一个字节的内存空间
      //MyMessageBoxW是一个地址,其实是一个整数,占4个字节的内存空间
      //int n=0x123;   n占4个字节和MyMessageBoxW占4个字节是一样的
      //1+4=5,知道为什么是5个字节了吧

    例:拦截Win32的MessageBoxA()函数

    1.先新建一个Dll工程

    #include "detours.h"
    #pragma comment(lib,"detours.lib") //导入detours.h和detours.lib文件
    
    
    //static BOOL (WINAPI* Real_Messagebox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)=MessageBoxA;
    int (WINAPI *Real_Messagebox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)= MessageBoxA; //注意是MessageBoxA函数
    extern "C" _declspec(dllexport) BOOL WINAPI MessageBox_Mine(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
    {
    	CString temp= lpText;
    	temp+="该MessageBox已被Detours截获!";
    	return Real_Messagebox(hWnd,temp,lpCaption,uType);
    }
    
    
    //dll入口函数
    BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
    {
    	if (DLL_PROCESS_ATTACH == fdwReason)
    	{
    		DetourTransactionBegin();
    		DetourUpdateThread(GetCurrentThread()); //当前线程
    		DetourAttach(&(PVOID&)Real_Messagebox,MessageBox_Mine); //设置detour
    		DetourTransactionCommit();
    	}
    	else if (DLL_PROCESS_DETACH == fdwReason)
    	{
    		DetourTransactionBegin();
    		DetourUpdateThread(GetCurrentThread());
    		DetourDetach(&(PVOID&)Real_Messagebox,MessageBox_Mine);//卸载detour
    		DetourTransactionCommit();
    	}
    	return TRUE;
    
    }
    特别要注意的就是MessageBox和MessageBoxA是不同的,我刚开始以为detours不会把它们区分,结果一直无法拦截成功。

    2.新建一个MFC工程(在按扭响应函数中添加 MessageBoxA())

    在其OnInitDialog()函数中 添加::LoadLibrary(L"messageDll.dll"); //注意路径问题

    然后在按钮响应函数中添加 ::MessageBoxA(NULL,"4444443","23",0);

    这样当成功后 点击按钮就可以拦截到MessageBox函数

    效果如图:




    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    [悟]你为什么想创业
    [悟] 因上努力,果上随缘
    自己写个多任务多线程断点下载框架
    大道甚夷,而人好径
    [经验帖]外包如何定价
    python 基础语法
    python 中文编码问题
    python的运行机制和版本区别
    [转]linux 调用动态库so文件
    shell join详解
  • 原文地址:https://www.cnblogs.com/lovelyx/p/4867104.html
Copyright © 2011-2022 走看看