银行登录控件仿制--防钩子,防嗅探

银行登录控件仿制--防全局钩子,防嗅探
类似于支付宝登录控件、工商、招商银行登录控件,这个是一个DLL钩子,用于注入其他进程
另外有一个控件,用来模拟INPUT控件,近期发布!
变量定义
unit hook_const;
interface
  uses windows;
const
  Mouse_MFileName='_MyDllMouse';
  Key_MFileName='_MyDllKey';
  const WH_KEYBOARD_LL = 13;
  const LLKHF_ALTDOWN = $20;
Type
tagKBDLLHOOKSTRUCT = packed record
   vkCode: DWORD;//虚拟键值
   scanCode: DWORD;//扫描码值
   flags: DWORD;
   time: DWORD;//消息时间戳
   dwExtraInfo: DWORD;//和消息相关的扩展信息
end;
  KBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT;
  PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;

  TShareMouseMem= record
    data1:array [1..3] of DWORD;
    data2:TMOUSEHOOKSTRUCT;
    buffer:array[0..1024]of char;
  end;
  TShareKeyMem= record
    data1:array [1..3] of DWORD;
    keychr:char;
    buffer:array[0..1024]of char;
  end;
  PShareMouseMem=^TShareMouseMem;
  PShareKeyMem=^TShareKeyMem;
implementation
end.

unit hook_fun;
interface
  uses windows,messages,SysUtils,hook_const;
const
  _KeyPressMask=$80000000;
var
  FileMapKEYHandle,
  FileMapMouseHandle : THandle;
  pShKEYMem: PShareKeyMem;
  pShMouseMem : PShareMouseMem;
  HookLowKeyBoard,
  HookKeyBoard,
  HookMouse,
  HookDebug         : HHook;
  function StartKeyHook(sender : HWND; Parentsender : HWND;MessageID : WORD) : BOOL; stdcall;
  function StartMouseHook(sender : HWND; Parentsender : HWND;MessageID : WORD) : BOOL; stdcall;
  function StartDEBUGHook: BOOL; stdcall;
  function StopKeyHook: BOOL; stdcall;
  function StopMouseHook: BOOL; stdcall;
  function StopDEBUGHook: BOOL; stdcall;
  function OCRIsStarted(safetype: WORD) : BOOL; stdcall;
implementation
function LowKeyBoardProc(nCode:Integer; wp: WPARAM; lp:LPARAM): LRESULT; stdcall;
var
  p: PKBDLLHOOKSTRUCT;
  bforbidkey: boolean;
begin
  result:= 0;
  bforbidkey:= false;
  p := PKBDLLHOOKSTRUCT (lp);
  If nCode < 0 Then
    result:= CallNextHookEx(HookLowKeyBoard,nCode,wp,lp);
  if nCode= HC_ACTION then
  begin
   case wp of
     WM_KEYDOWN,WM_SYSKEYDOWN,WM_KEYUP,WM_SYSKEYUP:
     begin
       if ((p.vkCode = VK_TAB) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or   // tab + alt
          (p.vkCode = VK_TAB) or  //TAB、SHIFT+TAB
          ((p.vkCode = VK_ESCAPE) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or   // esc + alt
          ((p.vkCode = VK_ESCAPE) and ((GetKeyState(VK_CONTROL) and $8000) <> 0)) or  //esc + ctrl
          ((p.vkCode = VK_DELETE) and ((GetKeyState(VK_CONTROL) and $8000) <> 0) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or // ctrl+alt+del
          (p.vkCode = VK_LWIN) or (p.vkCode = VK_RWIN) or (p.vkCode =VK_APPS) or  // Win + contentMenu
          ((p.vkCode = VK_F4) and ((p.flags and LLKHF_ALTDOWN) <>0)) then   // alt + f
          bforbidkey:= True;
     end;
   end;
    if (p.vkCode = VK_TAB) and ((wp= WM_KEYDOWN) or (wp= WM_SYSKEYDOWN)) then
    begin
      pShKEYMem^.keychr:= chr(9);
      SendMessage(pShKEYMem^.data1[1],pShKEYMem^.data1[2],wp,lp);
   end;
    if ((p.vkCode = VK_TAB) and ((getkeystate(VK_SHIFT) and _KeyPressMask)=_KeyPressMask))
      and ((wp= WM_KEYDOWN) or (wp= WM_SYSKEYDOWN)) then
    begin
      pShKEYMem^.keychr:= chr(1);
      SendMessage(pShKEYMem^.data1[1],pShKEYMem^.data1[2],wp,lp);
   end;
    if bforbidkey= true then
     result:= 1
    else
      result:= CallNextHookEx(HookLowKeyBoard,nCode,wp,lp);
  end;
    //result:= CallNextHookEx(HookLowKeyBoard,nCode,wp,lp);
end;
function KeyBoardProc(nCode:Integer; wp: WPARAM; lp:LPARAM): LRESULT; stdcall;
var
  bShift,bCaps,{bCtrl,bAlt,}bNums,bchartype: boolean;
  iShift,iCaps,{iCtrl,iAlt,}iNums: Integer;
  ckey: char;
begin
  result:= 0;
  ckey:= chr(0);
  If nCode < 0 Then
    result:= CallNextHookEx(HookMouse,nCode,wp,lp);
  iShift:= getkeystate(VK_SHIFT);
  iCaps:= getkeystate(VK_CAPITAL);
  iNums:= getkeystate(VK_NUMLOCK);
  if ((iShift and _KeyPressMask)=_KeyPressMask) then
    bShift:= True
  else
    bShift:= False;
  {if ((iAlt and _KeyPressMask)=_KeyPressMask) then
    bCtrl:= True
  else
    bCtrl:= False;
  if ((iShift and _KeyPressMask)=_KeyPressMask) then
    bAlt:= True
  else
    bAlt:= False;}
  bCaps:=(iCaps and 1)=1;
  bNums:=(iNums and 1)=1;
  if (bShift and bCaps) or ((not bShift) and (not bCaps)) then
    bchartype:= True  //源码
  else
    bchartype:= False;
  if ((lp shr 31) and 1)<>1 then  //重复按下或者按下
  begin
    if (wp>= 96) and (wp<= 105) and bNums then
      ckey:= chr(wp-48)
    else if wp= 111 then
      ckey:= '/'
    else if wp= VK_RETURN then  //回车
      ckey:= chr(13)
    else if wp= 8 then  //backspace
      ckey:= chr(8)
    else if wp= VK_TAB then  //tab
      postmessage(pShKEYMem^.data1[3],wm_keydown,vk_tab,0)
    else if wp= 106 then
      ckey:= '*'
    else if wp= 109 then
      ckey:= '-'
    else if wp= 107 then
      ckey:= '+'
    else if wp= 110 then
      ckey:= '.'
    else begin
     if bchartype then
     begin
        if (wp>= 65) and (wp<= 90) then
          ckey:= chr(wp+32)
        else if (wp>= 48) and (wp<= 57) then
         ckey:= chr(wp)
       else begin
          case wp of
            186: ckey:= ';';
            187: ckey:= '=';
            188: ckey:= ',';
            189: ckey:= '-';
            190: ckey:= '.';
            191: ckey:= '/';
            192: ckey:= '`';
            219: ckey:= '[';
            220: ckey:= '\';
            221: ckey:= ']';
            222: ckey:= chr(39);
          end;
        end;
     end else begin
        if (wp>= 65) and (wp<= 90) then
         ckey:= chr(wp)
        else begin
          case wp of
            48: ckey:= ')';
            49: ckey:= '!';
            50: ckey:= ;
            51: ckey:= '#';
            52: ckey:= '$';
            53: ckey:= '%';
            54: ckey:= '^';
            55: ckey:= '&';
            56: ckey:= '*';
            57: ckey:= '(';
            186: ckey:= ':';
            187: ckey:= '+';
            188: ckey:= '<';
            189: ckey:= '_';
            190: ckey:= '>';
            191: ckey:= '?';
            192: ckey:= '~';
            219: ckey:= '{';
            220: ckey:= '|';
            221: ckey:= '}';
            222: ckey:= '"';
          end;
        end;
      end;
    end;
    if ckey<> chr(0) then
    begin
      pShKEYMem^.keychr:= ckey;
      SendMessage(pShKEYMem^.data1[1],pShKEYMem^.data1[2],wp,lp);
    end;
    //SendMessage(pShKEYMem^.data1[1],pShKEYMem^.data1[2],wp,lp);
    result:= 1;
    if ckey= chr(9) then
      result:= CallNextHookEx(HookMouse,nCode,wp,lp);
    
  end;
end;
function MouseProc(nCode:Integer; wp: WPARAM; lp:LPARAM): LRESULT; stdcall;
begin
  result:= 0;
  If nCode < 0 Then
    result:= CallNextHookEx(HookMouse,nCode,wp,lp);
  case wp of
    WM_LBUTTONDOWN, WM_LBUTTONDBLCLK,
    WM_RBUTTONDOWN, WM_RBUTTONUP, WM_RBUTTONDBLCLK,
    WM_MBUTTONDOWN, WM_MBUTTONUP, WM_MBUTTONDBLCLK:
    begin
      pShMouseMem^.data2:=pMOUSEHOOKSTRUCT(lp)^;
      getwindowtext(pShMouseMem^.data2.hwnd,pShMouseMem^.buffer,1024);
      SendMessage(pShMouseMem^.data1[1],pShMouseMem^.data1[2],wp,integer(@(pShMouseMem^.data2)));
      //             窗口                   消息                        坐标
    end;
  end;
  case wp of
    WM_LBUTTONDBLCLK, WM_RBUTTONDBLCLK,
    WM_MBUTTONDBLCLK, WM_RBUTTONUP,
    WM_MBUTTONUP,WM_MBUTTONDOWN:
    begin
      result:= 1;
    end;
    else
      result:= CallNextHookEx(HookMouse,nCode,wp,lp);
  end;
end;
function DebugProc(nCode:Integer; wp: WPARAM; lp:LPARAM): LRESULT; stdcall;
var
  strDebug: TDebugHookInfo;
begin
  strDebug:= PDebugHookInfo(lp)^;
  if (strDebug.wParam= WH_JOURNALRECORD)
    or (strDebug.wParam= WH_JOURNALPLAYBACK)
    or (strDebug.wParam= WH_DEBUG) then
    result:= 1
  Else
    result:= CallNextHookEx(HookDebug,nCode,wp,lp);
end;
function StartKeyHook(sender : HWND;Parentsender : HWND;MessageID : WORD) : BOOL; stdcall;
begin
  result:= false;
  pShKEYMem^.data1[1]:=sender;
  pShKEYMem^.data1[2]:=messageid;
  pShKEYMem^.data1[3]:=Parentsender;
  //HookKeyBoard := SetWindowsHookEx(WH_KEYBOARD, KeyBoardProc, HInstance, getcurrentthreadid);
  HookKeyBoard := SetWindowsHookEx(WH_KEYBOARD, KeyBoardProc, HInstance, 0);
  HookLowKeyBoard := SetWindowsHookEx(WH_KEYBOARD_LL, LowKeyBoardProc, HInstance, 0);
  Result := (HookKeyBoard <> 0) and (HookLowKeyBoard <> 0);
end;
function StopKeyHook: BOOL;
begin
  if HookKeyBoard <> 0 then
  begin
    UnhookWindowshookEx(HookKeyBoard);
    HookKeyBoard := 0;
  end;
  if HookLowKeyBoard <> 0 then
  begin
    UnhookWindowshookEx(HookLowKeyBoard);
    HookLowKeyBoard := 0;
  end;
  Result := (HookKeyBoard = 0) and (HookLowKeyBoard = 0);
end;
function StartMouseHook(sender : HWND;Parentsender : HWND;MessageID : WORD) : BOOL; stdcall;
begin
  result:= false;
  pShMouseMem^.data1[1]:=sender;
  pShMouseMem^.data1[2]:=messageid;
  pShMouseMem^.data1[3]:=Parentsender;
  HookMouse := SetWindowsHookEx(WH_MOUSE, MouseProc, HInstance, 0);
  Result := HookMouse <> 0;
end;
function StopMouseHook: BOOL;
begin
  if HookMouse <> 0 then
  begin
    UnhookWindowshookEx(HookMouse);
    HookMouse := 0;
  end;
  Result := HookMouse = 0;
end;
function StartDEBUGHook: BOOL;
begin
  result:= false;
  HookDebug:= SetWindowsHookEx(WH_DEBUG, DebugProc, HInstance , 0);
  result:= HookDebug <> 0;
end;
function StopDEBUGHook: BOOL;
begin
  if HookDebug <> 0 then
  begin
    UnhookWindowshookEx(HookDebug);
    HookDebug := 0;
  end;
  Result := HookDebug = 0;
end;
function OCRIsStarted(safetype: WORD) : BOOL; stdcall;
begin
  result:= false;
  case safetype of
    WH_KEYBOARD:
    begin
      result := HookKeyBoard <> 0;
    end;
    WH_MOUSE:
    begin
      result := HookMouse <> 0;
    end;
    WH_DEBUG:
    begin
      result := HookDebug <> 0;
    end;
  end;
end;
initialization
  FileMapKEYHandle := OpenFileMapping(FILE_MAP_WRITE,False,Key_MFileName);
  FileMapMouseHandle := OpenFileMapping(FILE_MAP_WRITE,False,Mouse_MFileName);
  if FileMapKEYHandle=0 then FileMapKEYHandle := CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShareKeyMem),Key_MFileName);
  if FileMapMouseHandle=0 then FileMapMouseHandle := CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShareMouseMem),Mouse_MFileName);
  if FileMapKEYHandle=0 then Exception.Create('不能建立KEYWORD_HOOK所用共享内存!');
  if FileMapMouseHandle=0 then Exception.Create('不能建立Mouse_HOOK所用共享内存!');
  pShKEYMem :=  MapViewOfFile(FileMapKEYHandle,FILE_MAP_WRITE or FILE_MAP_READ,0,0,0);
  pShMouseMem :=  MapViewOfFile(FileMapMouseHandle,FILE_MAP_WRITE or FILE_MAP_READ,0,0,0);
  if (pShKEYMem = nil) or (pShMouseMem = nil) then
  begin
    CloseHandle(FileMapKEYHandle);
    CloseHandle(FileMapMouseHandle);
    Exception.Create('不能映射共享内存!');
  end;
finalization
  UnMapViewOfFile(pShKEYMem);
  UnMapViewOfFile(pShMouseMem);
  CloseHandle(FileMapKEYHandle);
  CloseHandle(FileMapMouseHandle);
end.

posted on 2009-02-22 16:17  cntlis  阅读(1496)  评论(2编辑  收藏  举报

导航