-
Notifications
You must be signed in to change notification settings - Fork 3
/
dllinject.pas
304 lines (263 loc) · 9.76 KB
/
dllinject.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
unit dllinject;
{$mode delphi}
interface
uses
Classes, SysUtils, exereader, Windows, JwaWinType, InstDecode;
type
{ This buffer will be injected into the spawned process. This will be used to
load a dll (target) and execute a function inside it (procname)
See "PayloadProc" proc for more
}
PInjectionBuffer = ^TInjectionBuffer;
TInjectionBuffer = record
LoadLibrary: function(lpLibFileName:LPCSTR):HINST; stdcall; // 00
GetProcAddr: function(hModule:HINST; lpProcName:LPCSTR):FARPROC; stdcall; // 4
CustomData: Pointer; // 8
target: array[0..511] of Char; // dll path + name // 12
procName: array[0..63] of Char; // dll proc name // 524
end;
procedure injectLoader(target, params: string; dll, funcname: string; CustomData: Pointer; CustomDataSize: Integer; SimpleInject: Boolean = False);
function InjectDLL(PID: Cardinal; sDll, CallProc: string;CustomData: Pointer; CustomDataSize: Integer): boolean;
implementation
type
TCallFunc = procedure(Data: Pointer); stdcall;
{ the payload function - directly copied from here into the target process. }
procedure PayloadProc(data: PInjectionBuffer); stdcall;
begin
TCallFunc(data^.GetProcAddr(data^.LoadLibrary(@data^.target[0]), @data^.procName[0]))(data^.CustomData);
end;
function GetEntryPoint(aFilename: string): Integer;
var
f: TExeFile;
begin
result:=0;
f:=TExeFile.Create;
try
if f.LoadFile(aFilename) then
begin
case f.FileType of
fkExe32:
begin
result:=f.NTHeader.OptionalHeader.ImageBase + f.NTHeader.OptionalHeader.AddressOfEntryPoint;
end;
fkExe64:
begin
result:=f.NTHeader64.OptionalHeader.ImageBase + f.NTHeader64.OptionalHeader.AddressOfEntryPoint;
end;
end;
end;
finally
f.Free;
end;
end;
function SizeOfProc(Proc: pointer): longword;
var
Length: longword;
Inst: TInstruction;
begin
FillChar(Inst, SizeOf(Inst), 0);
{$ifdef CPUX86}
Inst.Archi:=CPUX32;
{$ELSE}
Inst.Archi:=CPUX64;
{$ENDIF}
Inst.Addr:=Proc;
Result := 0;
repeat
Length := DecodeInst(@Inst);
Inst.Addr := Inst.NextInst;
Inc(Result, Length);
if Inst.OpType = otRET then
Break;
until Length = 0;
end;
function InjectDLL(PID: Cardinal; sDll, CallProc: string; CustomData: Pointer;
CustomDataSize: Integer): boolean;
var
hThread: THandle;
pMod, pCode: Pointer;
dWritten: NativeUInt;
ThreadID: Cardinal;
buffer: TInjectionBuffer;
i: Integer;
begin
Result := False;
Fillchar(buffer, SizeOF(TInjectionBuffer), #0);
dWritten:=0;
if PID <> INVALID_HANDLE_VALUE then
begin
// fill payload structure
buffer.LoadLibrary:=GetProcAddress(GetModuleHandle(PChar('KERNEL32.dll')), 'LoadLibraryA');
buffer.GetProcAddr:=GetProcAddress(GetModuleHandle(PChar('KERNEL32.dll')), 'GetProcAddress');
for i:=1 to Length(sDll) do
buffer.target[i-1]:=sDll[i];
for i:=1 to Length(CallProc) do
buffer.procName[i-1]:=CallProc[i];
result:=True;
// allocate memory for custom data & copy if required
if Assigned(CustomData) then
begin
buffer.CustomData:=VirtualAllocEx(PID, nil, CustomDataSize, MEM_COMMIT or MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if WriteProcessMemory(PID, buffer.CustomData, CustomData, CustomDataSize, dWritten) then
Result := result and TRUE;
end else
buffer.CustomData:=nil;
// allocate memory & copy payload proc
pCode := VirtualAllocEx(PID, nil, SizeOfProc(@PayloadProc), MEM_COMMIT or MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if WriteProcessMemory(PID, pCode, @PayloadProc, SizeOfProc(@PayloadProc), dWritten) then
Result := result and TRUE;
// allocate memory & copy payload buffer
pMod := VirtualAllocEx(PID, nil, SizeOf(TInjectionBuffer), MEM_COMMIT or MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if WriteProcessMemory(PID, pMod, @Buffer, SizeOf(TInjectionBuffer), dWritten) then
Result := result and TRUE;
if result then
begin
ThreadID:=0;
hThread := CreateRemoteThread(PID, nil, 0, pCode, pMod, 0, ThreadID);
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
end;
end;
end;
procedure injectLoader(target, params: string; dll, funcname: string;
CustomData: Pointer; CustomDataSize: Integer; SimpleInject: Boolean);
var
StartupInfo: TSTARTUPINFO;
ProcessInformation: PROCESS_INFORMATION;
hProcess: THANDLE;
entry: PtrUInt;
oldProtect: DWORD;
bytesRead: PTRUINT;
original,
patch: array[0..1] of Byte;
i: Integer;
AContext: CONTEXT;
f: TExeFile;
IsRelocatable: Boolean;
s: string;
begin
f:=TExeFile.Create;
IsRelocatable:=False;
bytesRead:=0;
try
if f.LoadFile(target) then
begin
IsRelocatable:=False;
for i:=0 to f.SectionCount-1 do
if (f.Sections[i].Name='.reloc')and(f.Sections[i].NumberOfRelocations>0) then
IsRelocatable:=True;
case f.FileType of
fkExe32:
begin
entry:=f.NTHeader.OptionalHeader.ImageBase + f.NTHeader.OptionalHeader.AddressOfEntryPoint;
{$ifndef CPUX86}
raise Exception.Create('This is a 32 bit executable - please use the 32 bit flavour of this program!');
{$endif}
end;
fkExe64:
begin
entry:=f.NTHeader64.OptionalHeader.ImageBase + f.NTHeader64.OptionalHeader.AddressOfEntryPoint;
{$ifdef CPUX86}
raise Exception.Create('This is a 64 bit executable - please use the 64 bit flavour of this program!');
{$endif}
end;
fkExeArm:
begin
raise Exception.Create('LOL Windows ARM Binary!');
end;
else
raise Exception.Create('Executable required');
end;
end else
raise Exception.Create(f.Status);
finally
f.Free;
end;
Fillchar(StartupInfo, SizeOf(StartupInfo), #0);
FIllchar(ProcessInformation, SizeOf(ProcessInformation), #0);
StartupInfo.cb:=SizeOf(StartupInfo);
// change to same directory as target
chdir(ExtractFilePath(target));
// create new process with target
s:= target + ' ' + params;
if (CreateProcessA(nil, PChar(s), nil, nil, False, CREATE_SUSPENDED, nil, nil, StartupInfo, ProcessInformation)) then
begin
hProcess:=ProcessInformation.hProcess;
try
{$IFDEF CPUX86}
if SimpleInject or IsRelocatable then
{$ENDIF}
begin
{ just inject while running without all that fancy patching...
the more I test this, the more reasonable this approach seems - it just
works, no problems with ASLR/relocatable images.. but tested with win10 only.
I think the whole reason behind patching the entry point was to make
sure the process was properly initialized so a dll can be injected without
interfering with the rest of the process. }
if not InjectDLL(hProcess, dll, funcname, CustomData, CustomDataSize) then
begin
//if not InjectLibrary2(hProcess, dll, funcname, CustomData, CustomDataSize) then
raise Exception.Create('Could not inject dll');
end;
ResumeThread(ProcessInformation.hThread);
Exit;
end;
{$IFDEF CPUX86}
if not VirtualProtectEx(hProcess, LPVOID(entry), 2, PAGE_EXECUTE_READWRITE, @oldProtect) then
begin
raise Exception.Create('Cannot unprotect entrypoint (relocatable? ALSR?)');
end;
// save original entry point instructions
if not ReadProcessMemory(hProcess, LPVOID(entry), @original[0], 2, bytesRead) then
begin
raise Exception.Create('Read from process memory failed: '+SysErrorMessage(GetLastError));
end;
// patch entry point with jmp -2 => force process into an infinite loop once it's
// reached it's entry point
patch[0]:=$EB;
patch[1]:=$FE;
if not WriteProcessMemory(hProcess, Pointer(entry), @patch[0], 2, @bytesRead) then
begin
raise Exception.Create('Write to process memory failed: '+SysErrorMessage(GetLastError));
end;
// resume process and wait a reasonable time until it has instruction pointer is at entry point
ResumeThread(ProcessInformation.hThread);
for i:=0 to 49 do
begin
Sleep(100);
AContext.ContextFlags:=CONTEXT_CONTROL;
GetThreadContext(ProcessInformation.hThread, AContext);
if AContext.Eip = Entry then
Break;
end;
if AContext.Eip <> Entry then
begin
// wait timeout, this happens with ASLR
end;
// finally, inject the payload
if not InjectDLL(hProcess, dll, funcname, CustomData, CustomDataSize) then
begin
//if not InjectLibrary2(hProcess, dll, funcname, CustomData, CustomDataSize) then
raise Exception.Create('Could not inject dll');
end;
// suspend the thread again
SuspendThread(ProcessInformation.hThread);
// restore original entry point instructions
if not WriteProcessMemory(hProcess, Pointer(entry), @original[0], 2, @bytesRead) then
begin
// if this fails, it usually means that the process has terminated in the meantime
// (this e.g. happens when the injected dll throws an error and quits)
// raise Exception.Create('Write to process memory (2) failed: '+SysErrorMessage(GetLastError));
end;
// resume process
ResumeThread(ProcessInformation.hThread);
{$ENDIF}
except
// terminate process and throw again
TerminateProcess(hProcess, UINT(-1));
raise;
end;
end else
raise Exception.Create('Could not create process: '+SysErrorMessage(GetLastError));
end;
end.