关于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.mak和Makefile复制到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。
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.mak和Makefile复制到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。
相关文章推荐
- ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
- linux c编程实现cd目录切换功能(带空格的目录名问题)
- 取得键盘每个按键的keyCode的值
- c++ 多态
- SD-Tax set up
- 英语总结——五月
- android 自学中的散乱笔记
- hdu 5246(模拟)
- 年轻程序员需要学习的5大经验
- nginx rewrite例子
- ScrollView 与 Autolayout
- 母函数
- onSaveInstanceState和onRestoreInstanceState触发的时机
- Android编译环境——ubuntu12.04上android2.3.4编译错误以及解决
- fzu 1692(矩阵快速幂)
- 凯云水利水电工程造价系统 (三) 材料单价 (2)
- [转]ubuntu bits/predefs.h:没有那个文件或目录
- 【Flume】【源码分析】深入flume-ng的三大组件——source,channel,sink
- erlang otp中的socket参数设置
- 珠三角实施无人工厂,用工将减少七成