inline hook【002】
来源:互联网 发布:淘宝从哪里看消费总额 编辑:程序博客网 时间:2024/06/01 10:28
用inline hook 去hook dll中的某个函数的小例子:
制作Dll的代码如下:
下载
#include <windows.h>#ifdef __cplusplusextern "C" {#endif__declspec(dllexport) int two(void){return 2;}__declspec(dllexport) int func(void){return two();}BOOL APIENTRY DllMain( HMODULE hModule,DWORD ul_reason_for_call,LPVOID lpReserved){switch (ul_reason_for_call){case DLL_PROCESS_ATTACH:case DLL_THREAD_ATTACH:case DLL_THREAD_DETACH:case DLL_PROCESS_DETACH:break;}return TRUE;}#ifdef __cplusplus}#endif
编译生成dll:
导出函数:
调用Dll的代码:
下载
// exe.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>#include <iomanip>#include <WTypes.h>using namespace std;//#define func MyfuncBYTE OldBytes[5]={0};//不一定是5,还有更好的:http://blog.csdn.net/beanjoy/article/details/8497307BYTE JmpAddress[5]={0xE9,0,0,0,0}; //跳转到HOOK函数的地址BYTE OldCode[5],NewCode[5]; BOOL bHook=false;void HookOn() ;void HookOff() ;FARPROC func;typedef int (*Mytwo)(void);Mytwo two=NULL;int Myfunc(void){HookOff(); int num = func();cout <<"Myfunc 中unhook 后: "<< num << endl;HookOn(); return 2*two();}void HookOn() { HANDLE handle; DWORD cid = ::GetCurrentProcessId();handle = OpenProcess(PROCESS_ALL_ACCESS,0,cid); VirtualProtectEx(handle,func,5,PAGE_READWRITE,&cid); WriteProcessMemory(handle,func,NewCode,5,0); VirtualProtectEx(handle,func,5,cid,&cid); }void HookOff() { HANDLE handle; DWORD cid = ::GetCurrentProcessId();handle = OpenProcess(PROCESS_ALL_ACCESS,0,cid); VirtualProtectEx(handle,func,5,PAGE_READWRITE,&cid); WriteProcessMemory(handle,func,OldCode,5,0); VirtualProtectEx(handle,func,5,cid,&cid); bHook = false; } int _tmain(int argc, _TCHAR* argv[]){HMODULE hModule=LoadLibrary(L"TestDll.dll"); func = NULL;two=NULL;func = GetProcAddress(hModule,"func"); two = (Mytwo)GetProcAddress(hModule,"two"); if(func==NULL || two == NULL) return false; //将被hook的函数的入口数据5字节拷贝出来_asm { pushad lea edi,OldCode // mov edi, 内存地址2 ;(要移动到目的处的地址) mov esi,func// mov esi, 内存地址1 ;(也就是存放字符串的内存地址)cld //cld是来控制重复移动时候的esi 和edi的递增方式。 cld是将方向标志位DF设置为0,每次rep循环的时候,esi和edi自动+1。 std是将方向标志位DF设置为1,每次rep循环的时候,esi和edi自动-1。movsd //dword,4字节movsb //byte,1字节popad }NewCode[0] = 0xe9; // jmp func的相对地址的指令 _asm { lea eax,Myfunc mov ebx,func sub eax,ebx sub eax,5 mov dword ptr [NewCode + 1],eax } cout <<"hook前:"<< func() <<endl;HookOn();//cout <<"hook后:"<< func() <<endl;HookOff() ;cout <<"hook前:"<< func() <<endl;return 0;}运行结果:
这个HOOK前,和HOOK后很清晰
遇到的问题:
不能将本进程中的函数HOOK,否则会出现类似于这个的权限问题
0 0
- inline hook【002】
- Inline Hook
- Inline Hook
- inline hook
- inline hook
- Inline Hook
- Inline Hook
- inline hook
- IAT HOOK、EAT HOOK和Inline Hook
- 应用层InLine Hook
- 通用Inline Hook代码
- fsd inline hook
- Inline Hook NtQueryDirectoryFile
- INLINE HOOK RING0
- 通用的inline hook,
- INLINE HOOK RING0
- inline hook KiFastCallEntry
- ring3 inline hook例子
- dia的安装过程
- (C++Primer)[C++]多行注释标记原则
- 两个栈实现队列
- centos 5.3中文系统乱码问题
- 日期
- inline hook【002】
- c#项目中用到的两种接口 webservice 和wcf
- 简单谈谈对目前项目组组织形态的感受
- Python Learning——第一天
- Android Lint工具学习
- 编程之美2.13 子数组的最大乘积
- 将一个js对象装换为一个JSON串
- Erlang中的内存分配算法
- c++字符串大小写转换