-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.c
465 lines (363 loc) · 14.8 KB
/
main.c
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include <windows.h>
#include "headers/beacon.h"
#include "headers/syscalls.h"
#include "headers/win32_api.h"
#include <winnt.h>
// Forward declarations
void getExportedDLLNamesWin32(char* args, int arglength);
void depositFormatObjectContents(formatp* pFormatStruct, WINBOOL transactionDump);
BOOL extraFancyTransactionDump(const char* dataToDump, int dataSize);
void downloadFile(char* fileName, int downloadFileNameLength, char* returnData, int fileSize) {
//Intializes random number generator to create fileId
time_t t;
MSVCRT$srand((unsigned)MSVCRT$time(&t));
int fileId = MSVCRT$rand();
//8 bytes for fileId and fileSize
int messageLength = downloadFileNameLength + 8;
char* packedData = (char*)MSVCRT$malloc(messageLength);
//pack on fileId as 4-byte int first
packedData[0] = (fileId >> 24) & 0xFF;
packedData[1] = (fileId >> 16) & 0xFF;
packedData[2] = (fileId >> 8) & 0xFF;
packedData[3] = fileId & 0xFF;
//pack on fileSize as 4-byte int second
packedData[4] = (fileSize >> 24) & 0xFF;
packedData[5] = (fileSize >> 16) & 0xFF;
packedData[6] = (fileSize >> 8) & 0xFF;
packedData[7] = fileSize & 0xFF;
int packedIndex = 8;
//pack on the file name last
for (int i = 0; i < downloadFileNameLength; i++) {
packedData[packedIndex] = fileName[i];
packedIndex++;
}
BeaconOutput(CALLBACK_FILE, packedData, messageLength);
if (fileSize > (1024 * 900)){
//Lets see how many times this constant goes into our file size, then add one (because if it doesn't go in at all, we still have one chunk)
int numOfChunks = (fileSize / (1024 * 900)) + 1;
int index = 0;
int chunkSize = 1024 * 900;
while(index < fileSize) {
if (fileSize - index > chunkSize){//We have plenty of room, grab the chunk and move on
/*First 4 are the fileId
then account for length of file
then a byte for the good-measure null byte to be included
then lastly is the 4-byte int of the fileSize*/
int chunkLength = 4 + chunkSize;
char* packedChunk = (char*) MSVCRT$malloc(chunkLength);
//pack on fileId as 4-byte int first
packedChunk[0] = (fileId >> 24) & 0xFF;
packedChunk[1] = (fileId >> 16) & 0xFF;
packedChunk[2] = (fileId >> 8) & 0xFF;
packedChunk[3] = fileId & 0xFF;
int chunkIndex = 4;
//pack on the file name last
for (int i = index; i < index + chunkSize; i++) {
packedChunk[chunkIndex] = returnData[i];
chunkIndex++;
}
BeaconOutput(CALLBACK_FILE_WRITE, packedChunk, chunkLength);
} else {//This chunk is smaller than the chunkSize, so we have to be careful with our measurements
int lastChunkLength = fileSize - index + 4;
char* lastChunk = (char*) MSVCRT$malloc(lastChunkLength);
//pack on fileId as 4-byte int first
lastChunk[0] = (fileId >> 24) & 0xFF;
lastChunk[1] = (fileId >> 16) & 0xFF;
lastChunk[2] = (fileId >> 8) & 0xFF;
lastChunk[3] = fileId & 0xFF;
int lastChunkIndex = 4;
//pack on the file name last
for (int i = index; i < fileSize; i++) {
lastChunk[lastChunkIndex] = returnData[i];
lastChunkIndex++;
}
BeaconOutput(CALLBACK_FILE_WRITE, lastChunk, lastChunkLength);
}
index = index + chunkSize;
}
} else {
/*first 4 are the fileId
then account for length of file
then a byte for the good-measure null byte to be included
then lastly is the 4-byte int of the fileSize*/
int chunkLength = 4 + fileSize;
char* packedChunk = (char*) MSVCRT$malloc(chunkLength);
//pack on fileId as 4-byte int first
packedChunk[0] = (fileId >> 24) & 0xFF;
packedChunk[1] = (fileId >> 16) & 0xFF;
packedChunk[2] = (fileId >> 8) & 0xFF;
packedChunk[3] = fileId & 0xFF;
int chunkIndex = 4;
//pack on the file name last
for (int i = 0; i < fileSize; i++) {
packedChunk[chunkIndex] = returnData[i];
chunkIndex++;
}
BeaconOutput(CALLBACK_FILE_WRITE, packedChunk, chunkLength);
}
//We need to tell the teamserver that we are done writing to this fileId
char packedClose[4];
//pack on fileId as 4-byte int first
packedClose[0] = (fileId >> 24) & 0xFF;
packedClose[1] = (fileId >> 16) & 0xFF;
packedClose[2] = (fileId >> 8) & 0xFF;
packedClose[3] = fileId & 0xFF;
BeaconOutput(CALLBACK_FILE_CLOSE, packedClose, 4);
return;
}
BOOL extraFancyTransactionDump(const char* dataToDump, int dataSize)
{
HANDLE hFile = INVALID_HANDLE_VALUE;
HANDLE tFile = INVALID_HANDLE_VALUE;
HANDLE mapFile = INVALID_HANDLE_VALUE;
IO_STATUS_BLOCK IoStatusBlock;
NTSTATUS status = 0;
OBJECT_ATTRIBUTES objAttributes;
void* returnData = NULL;
SIZE_T ViewSize = 0;
_RtlSetCurrentTransaction RtlSetCurrentTransaction = (_RtlSetCurrentTransaction) GetProcAddress(GetModuleHandleA("ntdll.dll"), "RtlSetCurrentTransaction");
_RtlInitUnicodeString RtlInitUnicodeString = (_RtlInitUnicodeString) GetProcAddress(GetModuleHandleA("ntdll.dll"), "RtlInitUnicodeString");
InitializeObjectAttributes(&objAttributes, NULL, 0, NULL, NULL);
status = NtCreateTransaction(&tFile, TRANSACTION_ALL_ACCESS, &objAttributes, NULL, NULL, 0, 0, 0, NULL, NULL);
if (status != 0)
{
if (tFile != INVALID_HANDLE_VALUE)
{
NtClose(tFile);
}
BeaconPrintf(CALLBACK_ERROR, "NtCreateTransaction failed, returning and freeing: %lx.\n", status);
return FALSE;
}
status = RtlSetCurrentTransaction(tFile);
if (status != 1)
{
if (tFile != INVALID_HANDLE_VALUE)
{
NtClose(tFile);
}
BeaconPrintf(CALLBACK_ERROR, "RtlSetCurrentTransaction failed, returning and freeing: %lx.\n", status);
return FALSE;
}
PCWSTR filePath = L"\\??\\C:\\SomeBogusFile1221.txt";
UNICODE_STRING unicodeString;
RtlInitUnicodeString(&unicodeString, filePath);
InitializeObjectAttributes(&objAttributes, &unicodeString, OBJ_CASE_INSENSITIVE, NULL, NULL);
const int allocSize = 0;
LARGE_INTEGER largeInteger;
largeInteger.QuadPart = allocSize;
status = NtCreateFile(&hFile, FILE_GENERIC_READ | FILE_GENERIC_WRITE | SYNCHRONIZE, &objAttributes, &IoStatusBlock, &largeInteger, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_WRITE | FILE_SHARE_READ, FILE_OVERWRITE_IF, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
if (status != 0)
{
if (hFile != INVALID_HANDLE_VALUE)
{
NtClose(hFile);
}
if (tFile != INVALID_HANDLE_VALUE)
{
NtClose(tFile);
}
BeaconPrintf(CALLBACK_ERROR, "NtCreateFile failed, returning and freeing: %lx.\n", status);
return FALSE;
}
status = RtlSetCurrentTransaction(0);
if (status != 1)
{
if (hFile != INVALID_HANDLE_VALUE)
{
NtClose(hFile);
}
if (tFile != INVALID_HANDLE_VALUE)
{
NtClose(tFile);
}
BeaconPrintf(CALLBACK_ERROR, "RtlSetCurrentTransaction failed with status %lx\n", status);
return FALSE;
}
DWORD dwBytesWritten = 0;
BOOL successfulWrite = KERNEL32$WriteFile(hFile, dataToDump, (DWORD)dataSize, &dwBytesWritten, NULL);
if (successfulWrite == TRUE)
{
LARGE_INTEGER fs;
BOOL success = KERNEL32$GetFileSizeEx(hFile, &fs);
unsigned long long fileSize = fs.QuadPart;
status = NtCreateSection(&mapFile, SECTION_MAP_READ, 0, &largeInteger, PAGE_READONLY, SEC_COMMIT, hFile);
if (status != 0)
{
if (hFile != INVALID_HANDLE_VALUE)
{
NtClose(hFile);
}
if (tFile != INVALID_HANDLE_VALUE)
{
NtClose(tFile);
}
BeaconPrintf(CALLBACK_ERROR, "NtCreateSection failed with status %lx\n", status);
return FALSE;
}
status = NtMapViewOfSection(mapFile, (HANDLE)-1, &returnData, 0, 0, 0, &ViewSize, ViewUnmap, 0, PAGE_READONLY);
if (status != 0)
{
if (hFile != INVALID_HANDLE_VALUE)
{
NtClose(hFile);
}
if (tFile != INVALID_HANDLE_VALUE)
{
NtClose(tFile);
}
BeaconPrintf(CALLBACK_ERROR, "NtMapViewOfSection failed with status %lx\n", status);
return FALSE;
}
int downloadFileNameLength;
char* downloadFileName;
downloadFileNameLength = MSVCRT$_snprintf(NULL,0,"%i", 12345) + 9;
downloadFileName = (char*) MSVCRT$malloc(downloadFileNameLength);
MSVCRT$sprintf(downloadFileName, "mem:\\%d.txt", 12345);
BeaconPrintf(CALLBACK_OUTPUT, "Checks passed, download should occur shortly!\n");
downloadFile(downloadFileName, downloadFileNameLength, returnData, fileSize);
}
if (tFile)
{
NtClose(tFile);
}
if (hFile)
{
NtClose(hFile);
}
return TRUE;
}
void depositFormatObjectContents(formatp* pFormatStruct, WINBOOL transactionDump)
{
char* outputString = NULL;
int sizeOfObject = 0;
outputString = BeaconFormatToString(pFormatStruct, &sizeOfObject);
if (transactionDump == FALSE)
{
BeaconOutput(CALLBACK_OUTPUT, outputString, sizeOfObject);
}
else
{
BOOL dumpedResult = extraFancyTransactionDump(outputString, sizeOfObject);
if (dumpedResult != TRUE)
{
BeaconOutput(CALLBACK_OUTPUT, outputString, sizeOfObject);
}
}
BeaconFormatFree(&formatObject);
return;
}
void getExportedDLLNamesWin32(char* args, int arglength)
{
datap parser;
formatp formatObject;
char* fileToEnumerate = NULL;
char* fileWithoutBase = NULL;
int getNumberOfFunctionsOnly;
int getTransactionData;
BeaconDataParse(&parser, args, arglength);
getNumberOfFunctionsOnly = BeaconDataInt(&parser);
getTransactionData = BeaconDataInt(&parser);
fileToEnumerate = BeaconDataExtract(&parser, NULL);
fileWithoutBase = BeaconDataExtract(&parser, NULL);
BeaconFormatAlloc(&formatObject, 128 * 1024);
HANDLE hFile = KERNEL32$CreateFileA(fileToEnumerate, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
BeaconPrintf(CALLBACK_ERROR, "Error in ascertaining file handle.\n");
BeaconFormatFree(&formatObject);
return;
}
HANDLE hMappedFile = KERNEL32$CreateFileMappingA(hFile, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL);
if ((hMappedFile == INVALID_HANDLE_VALUE) | (hMappedFile == 0))
{
BeaconPrintf(CALLBACK_ERROR, "Error in creating file mapping.\n");
BeaconFormatFree(&formatObject);
NtClose(hFile);
return;
}
unsigned char* fileMapping = (unsigned char*)KERNEL32$MapViewOfFile(hMappedFile, FILE_MAP_READ, 0, 0, 0);
if (!fileMapping)
{
BeaconPrintf(CALLBACK_ERROR, "Error in mapping view of file.\n");
BeaconFormatFree(&formatObject);
NtClose(hMappedFile);
NtClose(hFile);
return;
}
PIMAGE_DOS_HEADER imageDosHeader = (PIMAGE_DOS_HEADER)fileMapping;
if (imageDosHeader)
{
PIMAGE_NT_HEADERS64 pnth = (PIMAGE_NT_HEADERS64)(fileMapping + imageDosHeader->e_lfanew);
if (pnth->Signature == IMAGE_NT_SIGNATURE)
{
WORD wArchHint = pnth->OptionalHeader.Magic;
if (wArchHint == 0x10b)
{
if (getNumberOfFunctionsOnly != 0)
{
BeaconFormatPrintf(&formatObject, "Filename - %s\nEXPORTS\n", fileToEnumerate);
}
PIMAGE_NT_HEADERS32 pntHeader = (PIMAGE_NT_HEADERS32)(fileMapping + imageDosHeader->e_lfanew);
PIMAGE_EXPORT_DIRECTORY piedExportDirectory = (PIMAGE_EXPORT_DIRECTORY)(fileMapping + pntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
DWORD dwOrdinalBase = piedExportDirectory->Base;
unsigned int uiIndex = 0;
for (uiIndex; uiIndex < piedExportDirectory->NumberOfNames; ++uiIndex, dwOrdinalBase++)
{
char* szFilestuff = (char*)(fileMapping + ((unsigned long*)(fileMapping + piedExportDirectory->AddressOfNames))[uiIndex]);
if (getNumberOfFunctionsOnly != 0)
{
BeaconFormatPrintf(&formatObject, "\t%s=%s.%s\t@%d\n", szFilestuff, fileWithoutBase, szFilestuff, dwOrdinalBase);
}
}
if (getNumberOfFunctionsOnly == 0)
{
BeaconFormatPrintf(&formatObject, "Total functions found: %d\n", uiIndex);
}
}
else if (wArchHint == 0x20b)
{
if (getNumberOfFunctionsOnly != 0)
{
BeaconFormatPrintf(&formatObject, "Filename - %s\nEXPORTS\n", fileToEnumerate);
}
PIMAGE_NT_HEADERS64 pntHeader = (PIMAGE_NT_HEADERS64)(fileMapping + imageDosHeader->e_lfanew);
PIMAGE_EXPORT_DIRECTORY piedExportDirectory = (PIMAGE_EXPORT_DIRECTORY)(fileMapping + pntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
DWORD dwOrdinalBase = piedExportDirectory->Base;
unsigned int uiIndex = 0;
for (uiIndex; uiIndex < piedExportDirectory->NumberOfNames; ++uiIndex, dwOrdinalBase++)
{
char* szFilestuff = (char*)(fileMapping + ((unsigned long*)(fileMapping + piedExportDirectory->AddressOfNames))[uiIndex]);
if (getNumberOfFunctionsOnly != 0)
{
BeaconFormatPrintf(&formatObject, "\t%s=%s.%s\t@%d\n", szFilestuff, fileWithoutBase, szFilestuff, dwOrdinalBase);
}
}
if (getNumberOfFunctionsOnly == 0)
{
BeaconFormatPrintf(&formatObject, "Total functions found: %d\n", uiIndex);
}
}
}
}
if (fileMapping)
{
KERNEL32$UnmapViewOfFile(fileMapping);
}
if (hMappedFile)
{
NtClose(hMappedFile);
}
if (hFile)
{
NtClose(hFile);
}
if (getTransactionData)
{
depositFormatObjectContents(&formatObject, TRUE);
}
else
{
depositFormatObjectContents(&formatObject, FALSE);
}
return;
}