• 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函数

    效果如图:




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

  • 相关阅读:
    去除空格
    常见的Js
    无法访问 ASP 兼容性模式
    asp.net mvc 笔记一
    PowerDesigner如何将设计的表更新到数据库中
    微信小程序基于第三方websocket的服务器端部署
    C# Linq GroupBy 分组过滤求和
    一步一步教你用c# entity framework6 连接 sqlite 实现增删改查
    执行指定iframe页面的脚本
    vs2017 x64 ibatis.net 平台调用 Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342 x64
  • 原文地址:https://www.cnblogs.com/lovelyx/p/4867104.html
Copyright © 2020-2023  润新知