关于API HOOK,帮我改改,谢谢(100分)

  • 主题发起人 主题发起人 zbird
  • 开始时间 开始时间
Z

zbird

Unregistered / Unconfirmed
GUEST, unregistred user!
我想将一个C写的API HOOK改为DELPHI的,<br>可是我改了半天还是有问题。<br>大家谁帮我看看,还要怎么改啊?<br>谢谢<br>原C程序地址<br>http://www.xfocus.net/article_view.php?id=266<br>下面是我改的<br>unit M_Unit;<br><br>interface<br><br>uses<br>&nbsp; Variants, SysUtils, Classes, windows, messages;<br><br>var<br>&nbsp; g_hHook:hhook;<br>&nbsp; g_hinstDll:THandle;<br>&nbsp; pfMessageBoxA:FARPROC;<br><br>&nbsp; OldMessageBoxACode,NewMessageBoxACode:array [0..4] of byte;<br>&nbsp; hModule:THandle;<br>&nbsp; dwIdOld,dwIdNew:Thandle;<br>&nbsp; bHook:boolean;<br><br>function MyMessageBoxA(hWnd:HWND; lpText:LPCTSTR; lpCaption:LPCTSTR; uType:UINT):integer;<br>procedure HookOn();<br>procedure HookOff();<br>function init():boolean;<br>function MousHook(nCode:integer; wParam:WPARAM; lParam:LPARAM):LRESULT;<br>function UninstallHook():boolean;<br><br>implementation<br><br><br>function DllMain( hModule:THANDLE;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ul_reason_for_call:DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lpReserved:Tpoint<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;):boolean;<br>begin<br>&nbsp; &nbsp; case ul_reason_for_call of<br>&nbsp; &nbsp; &nbsp; DLL_PROCESS_ATTACH:<br>&nbsp; &nbsp; &nbsp; &nbsp; if not init() then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBoxA(null,'Init','ERROR',MB_OK);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; DLL_THREAD_ATTACH:;<br>&nbsp; &nbsp; &nbsp; DLL_THREAD_DETACH:;<br>&nbsp; &nbsp; &nbsp; DLL_PROCESS_DETACH:;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; if bHook then<br>&nbsp; &nbsp; &nbsp; UninstallHook();<br>&nbsp; &nbsp; result:=TRUE;<br>end;<br><br>function InstallHook():boolean;//输出安装空的钩子函数<br>begin<br>&nbsp; &nbsp;g_hinstDll:=LoadLibrary('HookApi2.dll');<br>&nbsp; &nbsp;g_hHook:=SetWindowsHookEx(WH_GETMESSAGE,@MyMessageBoxA,g_hinstDll,0);<br>&nbsp; if g_hHook=0 then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBoxA(NULL,'SET ERROR','ERROR',MB_OK);<br>&nbsp; &nbsp; result:=false;<br>&nbsp; &nbsp; exit;<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;result:=true;<br>end;<br><br>function UninstallHook():boolean;//输出御在钩子函数<br>begin<br>&nbsp; result:=UnhookWindowsHookEx(g_hHook);<br>end;<br><br>//首先关闭拦截,然后才能调用被拦截的Api 函数<br>function MyMessageBoxA(hWnd:HWND; lpText:LPCTSTR; lpCaption:LPCTSTR; uType:UINT):integer;<br>var<br>&nbsp; nReturn:integer;<br>begin<br>&nbsp; nReturn:=0;<br>&nbsp; HookOff();<br>&nbsp; nReturn:=MessageBoxA(hWnd,'Hook',lpCaption,uType);<br>&nbsp; HookOn();<br>&nbsp; result:=nReturn;<br>end;<br><br>procedure HookOn();<br>var<br>&nbsp; hProc:THANDLE;<br>begin<br>&nbsp; dwIdOld:=dwIdNew;<br>&nbsp; hProc:=OpenProcess(PROCESS_ALL_ACCESS,false,dwIdOld);//得到所属进程的句柄<br>&nbsp; VirtualProtectEx(hProc,pfMessageBoxA,5,PAGE_READWRITE,@dwIdOld);//修改所属进程中MessageBoxA的前5个字节的属性为可写<br>&nbsp; WriteProcessMemory(hProc,pfMessageBoxA,@NewMessageBoxACode, 5,0);//将所属进程中MessageBoxA的前5个字节改为JMP 到MyMessageBoxA<br>&nbsp; VirtualProtectEx(hProc,pfMessageBoxA,5,dwIdOld,@dwIdOld);//修改所属进程中MessageBoxA的前5个字节的属性为原来的属性<br>&nbsp; bHook:=true;<br>end;<br><br>procedure HookOff();//将所属进程中JMP MyMessageBoxA的代码改为Jmp MessageBoxA<br>var<br>&nbsp; hProc:THANDLE;<br>begin<br>&nbsp; &nbsp; dwIdOld:=dwIdNew;<br>&nbsp; &nbsp; hProc:=OpenProcess(PROCESS_ALL_ACCESS,false,dwIdOld);<br>&nbsp; &nbsp; VirtualProtectEx(hProc,pfMessageBoxA,5,PAGE_READWRITE,@dwIdOld);<br>&nbsp; &nbsp; WriteProcessMemory(hProc,pfMessageBoxA,@OldMessageBoxACode,5,0);<br>&nbsp; &nbsp; VirtualProtectEx(hProc,pfMessageBoxA,5,dwIdOld,@dwIdOld);<br>&nbsp; &nbsp; bHook:=false;<br>end;<br><br>function init():boolean;//初始化得到MessageBoxA的地址,并生成Jmp XXX(MyMessageBoxA)的跳转指令<br>begin<br>&nbsp; hModule:=LoadLibrary('user32.dll');<br>&nbsp; pfMessageBoxA:=GetProcAddress(hModule,'MessageBoxA');<br>&nbsp; if pfMessageBoxA=nil then<br>&nbsp; begin<br>&nbsp; &nbsp; result:=false;<br>&nbsp; &nbsp; exit<br>&nbsp; end;<br>&nbsp; &nbsp; asm<br>&nbsp; &nbsp; &nbsp; lea edi,OldMessageBoxACode<br>&nbsp; &nbsp; &nbsp; mov esi,pfMessageBoxA<br>&nbsp; &nbsp; &nbsp; cld<br>&nbsp; &nbsp; &nbsp; movsd<br>&nbsp; &nbsp; &nbsp; movsb<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; NewMessageBoxACode[0]:=0xe9;//jmp MyMessageBoxA的相对地址的指令<br>&nbsp; &nbsp; asm<br>&nbsp; &nbsp; &nbsp; lea eax,MyMessageBoxA<br>&nbsp; &nbsp; &nbsp; mov ebx,pfMessageBoxA<br>&nbsp; &nbsp; &nbsp; sub eax,ebx<br>&nbsp; &nbsp; &nbsp; sub eax,5<br>&nbsp; &nbsp; &nbsp; mov dword ptr [NewMessageBoxACode+1],eax<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; dwIdNew:=GetCurrentProcessId(); //得到所属进程的ID<br>&nbsp; &nbsp; dwIdOld:=dwIdNew;<br>&nbsp; &nbsp; HookOn();//开始拦截<br>&nbsp; &nbsp; result:=true;<br>end;<br><br>function MousHook(nCode:integer; wParam:WPARAM; lParam:LPARAM):LRESULT;<br>begin<br><br>end;<br><br>end.
 
哦,对不起,<br>C程序的地址贴错了是<br>http://www.xfocus.net/article_view.php?id=336
 
看看现成的API Hook 例子吧:<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=1123349
 
接受答案了.
 
后退
顶部