CreateRemoteThread - DLL Injection
Anbei eine Demo zur
API-Funktion
CreateRemoteThread 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.