如何写一个HOOK使其能实时检测在win98系统中运行的应用程序。(200分)

  • 主题发起人 主题发起人 cooltimes
  • 开始时间 开始时间
试试这个:<br>unit testmain;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls,tlhelp32, AppEvnts;<br><br>&nbsp;<br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; ListBox1: TListBox;<br>&nbsp; &nbsp; ApplicationEvents1: TApplicationEvents;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure ApplicationEvents1Message(var Msg: tagMSG;<br>&nbsp; &nbsp; &nbsp; var Handled: Boolean);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; <br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; mymsg:dword;<br><br><br><br><br><br>type<br>EDLLLoadError=class(exception);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>implementation <br>{$R *.DFM} &nbsp; <br>function createhook:bool; external 'mydll.dll' ;<br>function freehook:bool; external 'mydll.dll' ;<br><br>var hookhandle:hhook=0;<br>&nbsp; &nbsp; oldwinproc:pointer;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>close;<br>end;<br><br>function newproc(windowhandle:hwnd; themessage, paramw,paraml:longint):longint; stdcall;<br>var <br>&nbsp; &nbsp; pid:integer;<br>&nbsp; &nbsp; ps:tprocessentry32;<br>&nbsp; &nbsp; hp:thandle;<br>&nbsp; &nbsp; filename:string;<br>&nbsp; &nbsp; isend:bool;<br>begin<br>&nbsp; &nbsp; result:=0;<br>&nbsp;if themessage=mymsg then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; form1.listbox1.Items.Clear;<br>&nbsp; &nbsp; getwindowthreadprocessid(paramw,@pid);<br>&nbsp; &nbsp; hp:= createtoolhelp32snapshot(TH32CS_SNAPPROCESS,pid);<br>&nbsp; &nbsp; ps.dwsize:=sizeof(ps); &nbsp; &nbsp;<br>&nbsp; &nbsp; isend:=process32first(hp,ps);<br>&nbsp; &nbsp; while isend do <br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; filename:=ps.szExeFile; <br>&nbsp; &nbsp; &nbsp; form1.listbox1.Items.Add('w'+filename);<br>&nbsp; &nbsp; &nbsp; isend:=process32next(hp,ps);<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; result:=0;<br>&nbsp; &nbsp;end<br>&nbsp;else<br>&nbsp; &nbsp;result:=callwindowproc(oldwinproc,form1.handle,themessage,paramw,paraml);<br>end;<br><br><br>procedure TForm1.FormCreate(Sender: TObject);<br><br>var lb:bool; &nbsp;<br>begin<br><br>&nbsp; mymsg:=registerwindowmessage('cbtcreatewndmessage');<br>&nbsp; oldwinproc:=pointer(setwindowlong(form1.handle,gwl_wndproc,longint(@newproc)));<br><br>lb:=createhook; &nbsp;<br>end;<br><br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>var lb:bool;<br>begin<br>lb:=freehook;<br><br>end;<br><br>procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;<br>&nbsp; var Handled: Boolean);<br>var <br>&nbsp; &nbsp; pid:integer;<br>&nbsp; &nbsp; ps:tprocessentry32;<br>&nbsp; &nbsp; hp:thandle;<br>&nbsp; &nbsp; filename:string;<br>&nbsp; &nbsp; isend:bool;<br>begin<br>&nbsp; if msg.message=mymsg then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; form1.listbox1.Items.Clear;<br>&nbsp; &nbsp; getwindowthreadprocessid(msg.wParam,@pid);<br>&nbsp; &nbsp; hp:= createtoolhelp32snapshot(TH32CS_SNAPPROCESS,pid);<br>&nbsp; &nbsp; ps.dwsize:=sizeof(ps); &nbsp; &nbsp;<br>&nbsp; &nbsp; isend:=process32first(hp,ps);<br>&nbsp; &nbsp; while isend do <br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; filename:=ps.szExeFile; <br>&nbsp; &nbsp; &nbsp; form1.listbox1.Items.Add('w'+filename);<br>&nbsp; &nbsp; &nbsp; isend:=process32next(hp,ps);<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;showwindow(msg.wparam,SW_normal);<br><br>&nbsp; &nbsp;end;<br>&nbsp;<br><br>end;<br><br>end.<br><br><br>以下是dll中的原代码<br>unit cbt;<br><br>interface<br><br>uses<br>messages,windows;<br><br>function createhook:bool;stdcall;<br>function freehook:bool;stdcall;<br>function cbtcreatewndhook( &nbsp;int: integer; // hook code<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WPARAM: longint; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// depends on hook code<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LPARAM: longint // depends on hook code<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;):longint &nbsp;stdcall;<br><br><br><br>&nbsp; &nbsp;<br>implementation<br><br>var hookhandle:hhook=0;<br><br>function cbtcreatewndhook( &nbsp;int: integer; // hook code<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WPARAM: longint; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// depends on hook code<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LPARAM: longint // depends on hook code<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;):longint ; <br>begin<br>result:=0;<br>if int=Hshell_WINDOWCREATED &nbsp;then<br>&nbsp;begin<br>&nbsp; lparam:=getwindowlong(wparam,GWL_STYLE);<br>&nbsp; postmessage(HWND_BROADCAST,registerwindowmessage('cbtcreatewndmessage'),wparam,lparam);<br>&nbsp;end<br>else<br>&nbsp;result:= callnexthookex(hookhandle,int,wparam,lparam);<br>end;<br><br>function createhook:bool; <br>begin<br>hookhandle:=setwindowshookex(wh_shell,cbtcreatewndhook,hinstance,0);<br>result:=hookhandle&lt;&gt;0;<br>end;<br><br>function freehook:bool;<br>begin<br>result:=unhookwindowshookex(hookhandle);<br>end;<br><br>end.<br><br>以下是dll接口程序的代码<br>library mydll;<br><br>{ Important note about DLL memory management: ShareMem must be the<br>&nbsp; first unit in your library's USES clause AND your project's (select<br>&nbsp; Project-View Source) USES clause if your DLL exports any procedures or<br>&nbsp; functions that pass strings as parameters or function results. This<br>&nbsp; applies to all strings passed to and from your DLL--even those that<br>&nbsp; are nested in records and classes. ShareMem is the interface unit to<br>&nbsp; the BORLNDMM.DLL shared memory manager, which must be deployed along<br>&nbsp; with your DLL. To avoid using BORLNDMM.DLL, pass string information<br>&nbsp; using PChar or ShortString parameters. }<br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; windows,<br>&nbsp; messages,<br>&nbsp; cbt in 'cbt.pas';<br><br>const<br>&nbsp; cbtgothandle=wm_user+101;<br><br>{$R *.RES}<br>exports<br>&nbsp;createhook,freehook,cbtcreatewndhook;<br><br>begin<br><br>end.
 
后退
顶部