您的位置:首页 > 其它

关于detours的用法

2015-05-30 19:05 323 查看
1.从http://research.microsoft.com/sn/detours下载detour
professional 3.0并且安装,记住你的安装目录。

2.生成detour.lib:安装detour后,目录中有src文件夹,把文件夹拷贝到\Microsoft Visual Studio 10.0\VC下(这个是你的vc目录,依据不同版本而定)

3.以管理员身份打开cmd进入Microsoft Visual Studio 10.0\VC\bin目录输入命令vcvars32.bat,运行后可以设置好编译环境
3.切换到\Microsoft Visual Studio10.0\VC\SRC,然后输入nmake指令(这里可能会有错误 未找到文件“..\system.mak”)
未找到文件“..\system.mak”解决方法:
将detours安装目录中的system.makMakefile复制到Microsoft Visual Studio
10.0\VC
目录就可以了

4.部署lib文件,上一步\Microsoft Visual Studio10.0\VC\lib中会生成lib文件,将\Microsoft Visual Studio10.0\VC\src中的detours.h复制到..\include目录下

整个部署完成了

detour的使用:

自己写的源码中添加如下内容就可以使用detour了:

#include <detours .h>

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

5.绑定方法

extern "C" BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)

{
LONG error;

(void)hinst;

(void)reserved;

if (DetourIsHelperProcess()) {

return TRUE;

}

if (dwReason == DLL_PROCESS_ATTACH) {

DetourRestoreAfterWith();

printf("simple" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"

" Starting.\n");

fflush(stdout);

}

DetourTransactionBegin();

DetourUpdateThread(GetCurrentThread());

DetourAttach(&(PVOID&)pCoCreateInstance, MyCoCreateInstance);//这个地方就是你要HOOK的函数

error = DetourTransactionCommit();

if (error == NO_ERROR) {

printf("simple" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"

" Detoured SleepEx().\n");

}

else {

printf("simple" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"

" Error detouring SleepEx(): %d\n", error);

}

}

else if (dwReason == DLL_PROCESS_DETACH) {

DetourTransactionBegin();

DetourUpdateThread(GetCurrentThread());

DetourDetach(&(PVOID&)pCoCreateInstance, MyCoCreateInstance);//这个地方就是你要HOOK的函数

error = DetourTransactionCommit();

fflush(stdout);

}

return TRUE;

}

6.函数的写法

假设我们hookCoCreateInstance函数,先查看CoCreateInstance原型,把参数照搬,参数一定要一模一样。
HRESULT (WINAPI *pCoCreateInstance)(__in REFCLSID rclsid,__in_opt LPUNKNOWN pUnkOuter,__in DWORD dwClsContext,__in REFIID riid,__deref_out LPVOID FAR*
ppv)=CoCreateInstance;//完成指针绑定

HRESULT WINAPI MyCoCreateInstance(__in REFCLSID rclsid,__in_opt LPUNKNOWN pUnkOuter,__in DWORD dwClsContext,__in REFIID riid,__deref_out LPVOID FAR* ppv)

{

OutputDebugString(L"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");//你可以在此处修改传入的参数

return pCoCreateInstance(rclsid,pUnkOuter,dwClsContext,riid,ppv);//如果你只是修改参数而不修改原逻辑,一定要返回pCoCreateInstance而不
//是CoCreateInstance否则会出错

}

把pCoCreateInstance和MyCoCreateInstance填入上一步中红色部分,就完成你的hook dll了。

7.最后将该dll注入到exe应用程序中

#include "stdafx.h"
#include "stdafx.h"

#undef UNICODE

#include <cstdio>

#include <windows.h>

#include <detours.h>

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

int _tmain(int argc, _TCHAR* argv[])

{

STARTUPINFO si;

PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));

ZeroMemory(&pi, sizeof(pi));

si.cb = sizeof(si);

DetourCreateProcessWithDll("C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe",NULL,
NULL,NULL, FALSE, CREATE_DEFAULT_ERROR_MODE,NULL,NULL,&si, &pi,"C:\\Users\\Administrator\\Desktop\\tx\\hookagl\\hookagl\\Release\\hookagl.dll",NULL);

return 0;

}

红色部分就是你的exe程序,但是切记,该程序必须是单进程的,ie的话要改成单进程模式运行。蓝色的部分就是上一步编译形成的dll。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: