Delphi-PRAXiS
Seite 3 von 3     123   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Globaler API-Hook funktioniert nicht (https://www.delphipraxis.net/113581-globaler-api-hook-funktioniert-nicht.html)

Neutral General 26. Mai 2008 20:19

Re: Globaler API-Hook funktioniert nicht
 
Hi,

Im Moment funktionierts nicht so ganz. Also die Delphi IDE ballert mich dauernd mit ShowMessages zu aber sonst siehts mager aus.

Hier mal die ganze DLL (wobei ShowMessage zum Debuggen gedacht ist ;) )

Delphi-Quellcode:
library FindNextFileHook;

uses
  Windows, SysUtils, Dialogs;

type
  TFindNext = function (hFindFile: THandle; var lpFindFileData: TWIN32FindData): BOOL; stdcall;

var
  Hook: hHook;

function Dummy(code: Integer; wparam: wParam; lparam: lParam): LRESULT; stdcall;
begin
  Result := CallNextHookEx(Hook,Code,wparam,lparam);
end;

var FindNextOld: TFindNext = nil;
    tmp: THandle = INVALID_HANDLE_VALUE;

function MyFindNextFile(hFindFile: THandle; var lpFindFileData: TWIN32FindData): BOOL; stdcall;
var s: String;
begin
  s := lpFIndFileData.cFileName;
  if ExtractFileExt(s) = '.txt' then
    ShowMessage('Erwischt!');
end;

function NextHook(var F: TSearchRec): Integer;
asm
  nop
  nop
  nop
  nop
  nop
  add FindNextOld,5
  jmp FindNextOld
end;

procedure FindNextHooked;
asm
  push [esp+8]
  push [esp+8]
  call nexthook
  sub FindNextOld,5
  jmp MyFindNextFile
  ret
end;

procedure HookFindNext;
var hProc: THandle;
    br,old: Cardinal;
    jmp: Pointer;
    Sicherung: Pointer;
    lib: hModule;
begin
  if @FindNextOld = nil then
  begin
    Lib := LoadLibrary('kernel32.dll');
    FindNextOld := GetProcAddress(lib,'FindNextFileA');
    FreeLibrary(lib);
  end;
  GetMem(jmp,5);
  hProc := OpenProcess(PROCESS_ALL_ACCESS,false,GetCurrentProcessID);
  try
    // Auslesen / Sichern
    VirtualProtectEx(hProc,@FindNextOld,5,PAGE_EXECUTE_WRITECOPY,old);
    GetMem(Sicherung,5);
    ReadProcessMemory(hProc,@FindNextOld,Sicherung,5,br);
    WriteProcessMemory(hProc,@NextHook,Sicherung,5,br);
    // Schreiben
    PByte(jmp)^ := $E9;
    inc(PByte(jmp));
    PCardinal(jmp)^ := Cardinal(@FindNextHooked) - Cardinal(@FindNextOld) - 5;
    dec(PByte(jmp));
    WriteProcessMemory(hProc,@FindNextOld,jmp,5,br);
  finally
    FreeMem(jmp);
    CloseHandle(hProc);
  end;
end;

function InstallHook: Boolean; stdcall;
begin
  Result := False;
  if Hook = 0 then
  begin
    Hook := SetWindowsHookEx(WH_GETMESSAGE,@Dummy,
                             HInstance,0);
    Result := true;
  end;
end;

function UninstallHook: Boolean; stdcall;
begin
  Result := UnhookWindowsHookEx(Hook);
  Hook := 0;
end;

exports
  InstallHook,
  UnInstallHook;

begin
  HookFindNext;
end.
Gruß
Neutral General

Apollonius 26. Mai 2008 20:25

Re: Globaler API-Hook funktioniert nicht
 
Hast du mal probiert, die DLL in ein eigenes Programm zu laden und dann einfach mal durchzusteppen?

Neutral General 26. Mai 2008 20:33

Re: Globaler API-Hook funktioniert nicht
 
Hi,

Ich merke gerade, das es in Delphiprogrammen funktioniert. Also in meinem Testprogramm zumindest...

Im Explorer funktioniert es allerdings nicht... Kann es sein, dass der Explorer FindNextFileW benutzt?

Apollonius 26. Mai 2008 20:34

Re: Globaler API-Hook funktioniert nicht
 
Ich bin mir diesbezüglich sogar ziemlich sicher.

mr_emre_d 26. Mär 2009 18:03

Re: Globaler API-Hook funktioniert nicht
 
Neutral General:

Hast du nun versucht, die FindNextFileW zu hooken - bzw hat es funktioniert ?

MfG

Neutral General 26. Mär 2009 18:16

Re: Globaler API-Hook funktioniert nicht
 
Hi,

Ehm das ist schon lange her.. Ich weiß gar nicht :mrgreen:
Entweder hab ichs nicht probiert oder es hat nicht funktioniert. Würde ich mal tippen. :stupid:

mr_emre_d 26. Mär 2009 18:19

Re: Globaler API-Hook funktioniert nicht
 
Mist :(

Ich versuche auch schon seit ca ner 1/2 h FindFirstFileW zu hooken, nur misslingt mir das Hooken mit der injizierten Dll in Explorer.exe

Wäre schon, wenn irgendein andere mir sagen könnte, wie / womit genau Windows die Dateien auflistet / findet.

EDIT:
Am besten erstell ich nen eigenen Thread dafür :)

MfG


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:31 Uhr.
Seite 3 von 3     123   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz