Thema: zum Hooking
Einzelnen Beitrag anzeigen
Alt 07.06.08, 14:48   #5 (permalink)
.garfield
 
Registriert seit: 13.01.08
.garfield Leistung: Facit NTK
Likes: 0
Standard

Also hier mal ne Funktion zum Hooken einer Funktion:
Code:
procedure PatchIAT(strMod : Pchar; Alt, Neu : Pointer);
var
pImportDir : pImage_Import_Descriptor;
size : CardinaL;
Base : Cardinal;
pThunk : PDWORD;
bw : Cardinal;
begin
Base := GetModuleHandle(0);
pImportDir := ImageDirectoryEntryToData(Pointer(Base),True,IMAGE_DIRECTORY_ENTRY_IMPORT,size);
while pImportDIr^.Name <> 0 Do begin
  If (lstrcmpiA(Pchar(pImportDir^.Name+ Base),strMod) = 0) then begin
    pThunk := PDWORD(Base + pImportDir^.FirstThunk);
    While pThunk^ <> 0 Do begin
      if DWORD(Alt) = pthunk^ Then begin
        //pthunk^ :=  Cardinal(Neu);
        VirtualProtectEx(GetCurrentProcess,pthunk,4,PAGE_EXECUTE_READWRITE,bw);
        WriteProcessMemory(GetCurrentProcess, pthunk, @Neu, sizeof(Neu), bw);
      end;
    Inc(pThunk);
    end;
  end;
  Inc(PImportDir);
end;
end;
Das benutzt man dann zum Beispiel so:
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
pOldMessageBoxA := GetProcAddress(GetModuleHAndle('user32.dll'),'MessageBoxA'); //alte Adresse speichern
PatchIat('user32.dll',GetProcAddress(GetModuleHAndle('user32.dll'),'MessageBoxA'),@NewMessageBoxA);
end;
Und die Neue Funktion:
Code:
function newMessageBoxA(hWnd: HWND; lpText, lpCaption: PAnsiChar; uType: UINT): Integer; stdcall;
var
myMesBoxA : function(hWnd: Cardinal; lpText, lpCaption: PAnsiChar; uType: UINT): Integer;stdcall;
begin
If lpText = 'DD2' Then begin
  myMesBoxA := pOldMessageBoxA;
  myMEsBoxA(hwnd,lptext,lpCaption,uType);
end else begin
  Form1.Button1.Caption := 'keine MSGBOX!';
end;
end;
.garfield ist offline   Mit Zitat antworten
 

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