AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

How unhook LdrLoadDll function?

Ein Thema von flashcoder · begonnen am 16. Dez 2017 · letzter Beitrag vom 17. Dez 2017
Antwort Antwort
flashcoder

Registriert seit: 10. Nov 2013
83 Beiträge
 
#1

How unhook LdrLoadDll function?

  Alt 16. Dez 2017, 23:01
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.
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.116 Beiträge
 
Delphi 12 Athens
 
#2

AW: How unhook LdrLoadDll function?

  Alt 16. Dez 2017, 23:21
HOOK overwrites a few bytes, so what do you have to do to undo?

Save old content and write back.
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
flashcoder

Registriert seit: 10. Nov 2013
83 Beiträge
 
#3

AW: How unhook LdrLoadDll function?

  Alt 16. Dez 2017, 23:35
HOOK overwrites a few bytes, so what do you have to do to undo?

Save old content and write back.
How?

could be:

Code:
Unhook(@NewLdrLoadDll, GetProcAddress(GetModuleHandle('ntdll.dll'), 'LdrLoadDll'));
?
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.116 Beiträge
 
Delphi 12 Athens
 
#4

AW: How unhook LdrLoadDll function?

  Alt 16. Dez 2017, 23:59
Zitat:
pbyte(target)^ := $E9;
Assignments can also be read out.
variable := pbyte(target)^;
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
flashcoder

Registriert seit: 10. Nov 2013
83 Beiträge
 
#5

AW: How unhook LdrLoadDll function?

  Alt 17. Dez 2017, 00:14
Zitat:
pbyte(target)^ := $E9;
Assignments can also be read out.
variable := pbyte(target)^;

Like this:

Code:
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;
Usage:

Code:
Unhook(@NewLdrLoadDll, GetProcAddress(GetModuleHandle('ntdll.dll'), 'LdrLoadDll'));
right?

Geändert von flashcoder (17. Dez 2017 um 02:17 Uhr)
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 01:38 Uhr.
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