Thema: Delphi Inject DLL from Memory

Einzelnen Beitrag anzeigen

Megamorph

Registriert seit: 28. Dez 2005
25 Beiträge
 
#3

Re: Inject DLL from Memory

  Alt 11. Sep 2008, 14:53
Hey,

danke für den Tipp.

Die Funktion InjectMe entsprach nicht ganz meiner Vorstellung. Ich möchte schon eine DLL injecten und keine EXE.
Jedoch habe ich einen Code in Anlehnung an die InjectLibrary geschrieben, welcher jedoch noch nicht ganz funktioniert.
Es wäre nett, wenn ihr mal einen Blick darüber werft, danke. (-;

Delphi-Quellcode:
procedure TForm1.Timer1Timer(Sender: TObject);
var
  BytesWritten, Process, Thread, ThreadId: cardinal;
  load_lib_param, body: pointer;
  DLL: pchar;
  szDll: string;
  dllptr : pointer;
  PID: cardinal;
begin
  if GetWindowThreadProcessID(FindWindow(nil,pchar('Unbenannt - Paint')),nil) = 0 then exit;
  GetWindowThreadProcessID(FindWindow(nil,pchar('Unbenannt - Paint')), PID);
  Timer1.Enabled:=false;

  tempstream.Seek(0, soFromBeginning);
  dllptr:=@tempstream;

  szdll:= ExtractFilePath(Application.ExeName) + 'test.dll';
  DLL := pchar (szdll);

  Process := OpenProcess(PROCESS_ALL_ACCESS, False, pid);

  load_lib_param := xVirtualAllocEx(Process, nil, 4096, MEM_COMMIT, PAGE_READWRITE);
  body := xVirtualAllocEx(Process, nil, tempstream.Size, MEM_COMMIT, PAGE_READWRITE);
  WriteProcessMemory(Process, load_lib_param, Pointer(DLL), 4096, BytesWritten);
  WriteProcessMemory(Process, body, dllptr, tempstream.size, BytesWritten);

  Thread := CreateRemoteThread(Process, nil, 0,
             GetProcAddress(GetModuleHandle('KERNEL32.DLL'), 'LoadLibraryA'),
             load_lib_param, 0, ThreadId);

  tempstream.Free;
  if Thread <> 0 then Memo1.Lines.Add('Injected!')
                 else Memo1.Lines.Add('Failed!');
  WaitForSingleObject(Thread, 6000);
  CloseHandle(Thread);
  CloseHandle(Process);
end;
Anmerkung:
1) tempstream enthält die DLL (mit SaveToFile getestet)
2) Die DLL befindet sich natürlich nicht in dem zusammengesetzten Pfad, aber man muss ja was übergeben ?!
3) kein Compiler-Error, kein Runtime-Error, die Ausgabe 'Injected!' kommt
4) Code in der DLL wird jedoch nicht ausgeführt

Hat jemand eine Idee?
Danke für Antworten (:
Megamorph
  Mit Zitat antworten Zitat