Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#1

CreateRemoteThread - DLL Injection

  Alt 14. Mai 2008, 13:09
CreateRemoteThread - DLL Injection
Anbei eine Demo zur API-Funktion MSDN-Library durchsuchenCreateRemoteThread bzw. wie man mittels einer DLL Code von einer anderen Anwendung ausführen lässt.

Das Beispiel nutzt dafür die notepad.exe und lässt von ihr eine Messagebox anzeigen, die den Pfad ausgibt, der den Code ausführt.

Der relevante Code:
Delphi-Quellcode:
const
  DLLFILENAME = 'MsgBox.dll';

var
  ProcessID : Integer;
  hProcess : THandle;
  DLLPath : string;
  pDLLPath : Pointer;
  BytesWritten : Cardinal;
  ThreadID : Cardinal;

begin
  About;

  DLLPath := ExtractFilePath(ParamStr(0)) + '\' + DLLFILENAME;

  ProcessID := GetProcessID('notepad.exe');
  if ProcessID <> 0 then
  begin
    hProcess := GetProcessHandleFromID(ProcessID);
    if hProcess <> 0 then
    begin
      // alloc memory in remote process
      pDLLPath := VirtualAllocEx(hProcess, nil, Length(DLLPath), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
      if Assigned(pDLLPath) then
      begin
        // write DLL Path into the allocated memory
        if WriteProcessMemory(hProcess, pDLLPath, PChar(DLLPath), Length(DLLPath), BytesWritten) then
        begin
          // create remote thread and load library
          if CreateRemoteThread(hProcess, nil, 0, GetProcAddress(GetModuleHandle('kernel32.dll'), 'LoadLibraryA'),
            pDLLPath, 0, ThreadID) <> 0 then
          begin
            CloseHandle(hProcess);
            Writeln('DLL injected...');
          end
          else
            Writeln(GetLastError);
        end
        else
          Writeln(GetLastError);
      end
      else
        Writeln(GetLastError);
    end
    else
      Writeln(GetLastError);
  end
  else
    Writeln(GetLastError);

  Readln;
end.
Der ganze Code kann in der Demo im Anhang nachvollzogen werden.
Angehängte Dateien
Dateityp: zip createremotethread_144.zip (25,3 KB, 212x aufgerufen)
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat