![]() |
How unhook LdrLoadDll function?
I had created this code below where i try prevent a dll injection that will use LdrLoadDll function through of a kernel driver.
Now i want know how i can unhook this api (based in my Hook() function) when the dll of protection is unloaded or when protected process is finalized?
Code:
library mydll;
uses Windows, SysUtils, Classes; {$R *.res} type NTSTATUS = UINT; CONST STATUS_ACCESS_DENIED = NTSTATUS($C0000022); type PUNICODE_STRING = ^UNICODE_STRING; UNICODE_STRING = packed record Length: Word; MaximumLength: Word; Buffer: PWideChar; end; var Old_LdrLoadDll: function(szcwPath: PWideChar; dwFlags: DWORD; pUniModuleName: PUNICODE_STRING; pResultInstance: PPointer) : NTSTATUS; stdcall; function LdrLoadDll(szcwPath: PWideChar; dwFlags: DWORD; pUniModuleName: PUNICODE_STRING; pResultInstance: PPointer) : NTSTATUS; stdcall; begin Result := Old_LdrLoadDll(szcwPath, dwFlags, pUniModuleName, pResultInstance); end; function NewLdrLoadDll(szcwPath: PWideChar; dwFlags: DWORD; pUniModuleName: PUNICODE_STRING; pResultInstance: PPointer) : NTSTATUS; stdcall; begin if (CompareStr(pUniModuleName^.Buffer, 'hackdll.dll') = 0) or (CompareStr(szcwPath, 'Hack') = 0) then Result := STATUS_ACCESS_DENIED else Result := LdrLoadDll(szcwPath, dwFlags, pUniModuleName, pResultInstance); end; procedure Hook(target, newfunc: pointer); var jmpto: DWORD; OldProtect: Cardinal; begin jmpto := DWORD(newfunc) - DWORD(target) - 5; VirtualProtect(target, 5, PAGE_EXECUTE_READWRITE, @OldProtect); pbyte(target)^ := $E9; pdword(DWORD(target) + 1)^ := jmpto; end; procedure DllEntryPoint(Reason: Integer); stdcall; begin case Reason of DLL_PROCESS_ATTACH: begin DisableThreadLibraryCalls(HInstance); Hook(GetProcAddress(GetModuleHandle('ntdll.dll'), 'LdrLoadDll'), @NewLdrLoadDll); end; DLL_THREAD_ATTACH: ; // DLL_THREAD_DETACH: ; // DLL_PROCESS_DETACH: begin // Unhook(); end; end; end; begin DllProc := @DllEntryPoint; DllEntryPoint(DLL_PROCESS_ATTACH); end. |
AW: How unhook LdrLoadDll function?
HOOK overwrites a few bytes, so what do you have to do to undo?
Save old content and write back. :zwinker: |
AW: How unhook LdrLoadDll function?
Zitat:
could be:
Code:
?
Unhook(@NewLdrLoadDll, GetProcAddress(GetModuleHandle('ntdll.dll'), 'LdrLoadDll'));
|
AW: How unhook LdrLoadDll function?
Zitat:
Delphi-Quellcode:
:zwinker:
variable := pbyte(target)^;
|
AW: How unhook LdrLoadDll function?
Zitat:
Like this:
Code:
Usage:
procedure Unhook(hookedfunc, oldfunc: pointer);
var jmpto: DWORD; OldProtect: Cardinal; begin jmpto := DWORD(oldfunc) - DWORD(hookedfunc) - 5; VirtualProtect(hookedfunc, 5, PAGE_EXECUTE_READWRITE, @OldProtect); hookedfunc := pbyte(oldfunc)^; pdword(DWORD(hookedfunc) + 1)^ := jmpto; end;
Code:
right?
Unhook(@NewLdrLoadDll, GetProcAddress(GetModuleHandle('ntdll.dll'), 'LdrLoadDll'));
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 08:42 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz