hook api的问题
#include <windows.h>
#include <imagehlp.h>
#include "api.h"
HHOOK old;
int MessageMy(
HWND hWnd, // handle of owner window
LPCTSTR lpText, // address of text in message box
LPCTSTR lpCaption, // address of title of message box
UINT uType // style of message box
);
void FindApi();
void SetHook();
typedef int (*fnnew)(HWND hWnd, LPCTSTR lpText,LPCTSTR lpCaption, UINT uType);
int MessageMy(
HWND hWnd, // handle of owner window
LPCTSTR lpText, // address of text in message box
LPCTSTR lpCaption, // address of title of message box
UINT uType // style of message box
)
{
MessageBox(NULL,"我爱","外挂",0);
return 0;
}
fnnew fn=MessageMy;
void FindApi()
{
HMODULE h=GetModuleHandle("msg.exe");
PROC p=GetProcAddress(GetModuleHandle("user32.dll"),"MessageBoxA");
ULONG size;
PCSTR name="USER32.dll";
PIMAGE_IMPORT_DESCRIPTOR import=(PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryEntryToData(h,TRUE,IMAGE_DIRECTORY_ENTRY_IMPORT,&size);
for(;import->Name;import++)
{
PSTR name1=(PSTR)((PBYTE)h+import->Name);
if(lstrcmpiA(name,name1)==0)
break;
}
if(import->Name==0)
return;
PIMAGE_THUNK_DATA pThunk=(PIMAGE_THUNK_DATA) ((PBYTE)h+import->FirstThunk);
for(;pThunk->u1.Function;pThunk++)
{
PROC *p1=(PROC*) &pThunk->u1.Function;
BOOL found=(*p1==p);
if(found)
WriteProcessMemory(GetCurrentProcess(),(LPVOID)p1,(LPVOID)&fn,sizeof(fn),NULL);
}
}
LRESULT CALLBACK GetMsgProc(
int code, // hook code
WPARAM wParam, // removal flag
LPARAM lParam // address of structure with message
)
{
return CallNextHookEx(old,code,wParam,lParam);
}
void SetHook()
{
old=SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,GetModuleHandle("api.dll"),0);
FindApi();
}
我这个程序对msg.exe这个程序中的messageboxa进行挂接但是
有错误不知道那里错拉
还请高手帮我看下!谢谢拉
问题点数:20、回复次数:11Top
1 楼rageliu(天气好了就去长白山看水怪去了,嘿嘿...)回复于 2005-08-01 08:19:36 得分 0
upTop
2 楼5204711353(小文)回复于 2005-08-01 15:22:08 得分 0
upTop
3 楼5204711353(小文)回复于 2005-08-02 15:59:22 得分 0
现在找到问题是
HMODULE h=GetModuleHandle("msg.exe");返回是空但是msg.exe确实存在啊!
为什么是空呢?Top
4 楼5204711353(小文)回复于 2005-08-02 23:46:37 得分 0
经过修改代码还是有问题
问个hook api的问题
以下是dll的代码
#include <windows.h>
#include <imagehlp.h>
#include <iostream.h>
#include "api.h"
HHOOK old;int WINAPI MessageMy(
HWND hWnd, // handle of owner window
LPCTSTR lpText, // address of text in message box
LPCTSTR lpCaption, // address of title of message box
UINT uType // style of message box
);
void FindApi(HMODULE h);
void SetHook();
typedef int (WINAPI *fnnew)(HWND hWnd, LPCTSTR lpText,LPCTSTR lpCaption, UINT uType);
int WINAPI MessageMy(
HWND hWnd, // handle of owner window
LPCTSTR lpText, // address of text in message box
LPCTSTR lpCaption, // address of title of message box
UINT uType // style of message box
)
{
cout<<"123\n";
return 0;
}
fnnew fn=(fnnew)MessageMy;//指向自定义的函数
void FindApi(HMODULE h) //hook msg.exe进程的api
{
HMODULE h=GetModuleHandle("msg.exe");//获取要修改进程的模块
if(h==NULL)
MessageBox(NULL,"//","//",0);
PROC p=GetProcAddress(GetModuleHandle("user32.dll"),"MessageBoxA"); //获取调用系统的MessageBoxA的地址
ULONG size;
PCSTR name="USER32.dll";
PIMAGE_IMPORT_DESCRIPTOR import=(PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryEntryToData(h,TRUE,IMAGE_DIRECTORY_ENTRY_IMPORT,&size);//查找msg.exe进程的输入节并hook这个进程中的MessageBoxA函数
for(;import->Name;import++)
{
PSTR name1=(PSTR)((PBYTE)h+import->Name);
if(lstrcmpiA(name,name1)==0)
break;
}
if(import->Name==0)
return;
PIMAGE_THUNK_DATA pThunk=(PIMAGE_THUNK_DATA) ((PBYTE)h+import->FirstThunk);
for(;pThunk->u1.Function;pThunk++)
{
PROC *p1=(PROC*) &pThunk->u1.Function;
BOOL found=(*p1==p);
if(found)
pThunk->u1.Function=(PDWORD)fn;
}
}
HMODULE x;
LRESULT CALLBACK GetMsgProc(
int code, // hook code
WPARAM wParam, // removal flag
LPARAM lParam // address of structure with message
)
{
x=GetModuleHandle("msg.exe");
if(x!=NULL)
FindApi(x);
return CallNextHookEx(old,code,wParam,lParam);
}
void SetHook()
{
old=SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,GetModuleHandle("api.dll"),0);//对所有进程安装钩子!
}
msg文件代码是
#include <windows.h>
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // pointer to command line
int nCmdShow // show state of window
)
{
MessageBox(NULL,"外挂","努力学习",0);
}
但是hook以后对msg调用应该输出123但是没有输出!
我不知道那里错拉!还请高手指教
Top
5 楼louifox(兰陵笑笑生)回复于 2005-08-03 02:04:52 得分 0
GetMsgProc没有被系统调用吧。
The system calls this function whenever the GetMessage or PeekMessage function has retrieved a message from an application message queueTop
6 楼5204711353(小文)回复于 2005-08-03 21:35:32 得分 0
自己的问题自己顶上去!Top
7 楼lianggj(aaaa)回复于 2005-08-14 14:22:50 得分 0
upTop
8 楼5204711353(小文)回复于 2005-08-20 11:56:21 得分 0
upTop
9 楼zhangze(喆)回复于 2005-08-20 12:06:59 得分 0
在hookapi的时候,使用输入节办法的话,记得保存原有的函数地址,你应该查看一下你自己的函数是否经过调用了,最简单的办法就是调用原来的messageboxa,将其中的某些字段替换掉就可以了。在进行输入节替换的时候,最保险的办法就是调用VirtualProtectEx,WriteProcessMemory,如果这些失败,那就只有提升权限再作了。Top
10 楼SammyLan((基础决定你能走多远)--英语菜才是真的菜)回复于 2005-08-20 12:18:40 得分 0
小文
我帮不了你
Top
11 楼K()回复于 2005-08-20 12:37:51 得分 0
不知道什么是HOOKTop




