program Loader;
{$APPTYPE CONSOLE}
uses
Windows;
type
TRemoteInfo = record
LoadLibraryA : function (lpLibFileName: PAnsiChar): HMODULE; stdcall;
ReturnAddress : pointer;
end;
var
SI : TStartupInfo;
PI : TProcessInformation;
Name : String;
CT : TContext;
ShellPointer : Pointer;
BytesWritten : DWORD;
RemoteInfo : TRemoteInfo;
ParameterPointer : Pointer;
function InjectLibrary(lpProcessID: Cardinal; lpDllname: WideString):LongBool;
var
hProc: Cardinal;
oAlloc: Pointer;
cWPM: Cardinal;
hRemThread: Cardinal;
begin
result := false;
SetLastError(ERROR_SUCCESS);
hProc := OpenProcess(PROCESS_ALL_ACCESS, false, lpProcessID);
if hProc <> 0 then
begin
oAlloc := VirtualAllocEx(hProc, nil, (length(lpDllname) * 2) + 1,
MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if oAlloc <> nil then
begin
if WriteProcessMemory(hProc, oAlloc, pwidechar(lpDllName), (length(lpDllname) * 2) + 1,
cWPM) = true then
begin
CreateRemoteThread(hProc, nil, 0,
GetProcAddress(GetModuleHandle('kernel32.dll'), 'LoadLibraryW'),
oAlloc, 0, hRemThread);
if GetLastError = ERROR_SUCCESS then
begin
result := true;
end;
end;
end;
end;
CloseHandle(hProc);
end;
function TheThread ( p : pointer ) : Integer; stdcall;
var
Address : Pointer;
ws2_32 : array [0..6] of char;
begin
ws2_32[0] := 'w';
ws2_32[1] := 's';
ws2_32[2] := '2';
ws2_32[3] := '_';
ws2_32[4] := '3';
ws2_32[5] := '2';
ws2_32[6] := #0;
Address := TRemoteInfo(p^).ReturnAddress;
TRemoteInfo(p^).LoadLibraryA (ws2_32);
asm
jmp Address;
end;
end;
begin
writeln ('Type in your filename:');
readln (name);
FillChar(SI, SizeOf(TStartupInfo), #0);
FillChar(PI, SizeOf(TProcessInformation), #0);
SI.cb := SizeOf(TStartupInfo);
if CreateProcessW(pwidechar(WideString(Name)), NIL, nil, nil, FALSE, CREATE_SUSPENDED, nil, NIL, SI, PI) then begin
CT.ContextFlags := CONTEXT_FULL;
if GetThreadContext(PI.hThread, CT) then begin
RemoteInfo.ReturnAddress := Pointer(CT.EAX);
RemoteInfo.LoadLibraryA := GetProcAddress(LoadLibraryA('kernel32'), 'LoadLibraryA');
ShellPointer := VirtualAllocEx (PI.hProcess, NIL, 95, MEM_COMMIT or MEM_RESERVE, PAGE_EXECUTE_READWRITE);
WriteProcessMemory (PI.hProcess, ShellPointer, @TheThread, 95, BytesWritten);
ParameterPointer := VirtualAllocEx (PI.hProcess, NIL, SizeOf(TRemoteInfo), MEM_COMMIT or MEM_RESERVE, PAGE_EXECUTE_READWRITE);
WriteProcessMemory (PI.hProcess, ParameterPointer, @RemoteInfo, SizeOf(TRemoteInfo), BytesWritten);
CT.Ebx := DWORD(ParameterPointer);
CT.Eax := DWORD(ShellPointer);
SetThreadContext(PI.hThread, CT);
end;
InjectLibrary (PI.dwProcessId, 'hook.dll');
end;
end.