-
Notifications
You must be signed in to change notification settings - Fork 432
/
Copy pathdiag.cpp
866 lines (684 loc) · 28.3 KB
/
diag.cpp
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
/*******************************************************************************
*
* (C) COPYRIGHT AUTHORS, 2022 - 2023
*
* TITLE: DIAG.CPP
*
* VERSION: 1.33
*
* DATE: 16 Jul 2023
*
* Hamakaze system diagnostics component.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
* ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*
*******************************************************************************/
#include "global.h"
BOOLEAN g_ConsoleOutput = TRUE;
typedef struct _OBJENUMPARAM {
PWSTR ObjectDirectory;
PUNICODE_STRING ObjectType;
} OBJENUMPARAM, * POBJENUMPARAM;
typedef struct _FILTER_FULL_INFORMATION {
ULONG NextEntryOffset;
ULONG FrameID;
ULONG NumberOfInstances;
USHORT FilterNameLength;
WCHAR FilterNameBuffer[1];
} FILTER_FULL_INFORMATION, * PFILTER_FULL_INFORMATION;
typedef enum _FILTER_INFORMATION_CLASS {
FilterFullInformation,
FilterAggregateBasicInformation,
FilterAggregateStandardInformation
} FILTER_INFORMATION_CLASS, * PFILTER_INFORMATION_CLASS;
NTSTATUS NTAPI EnumObjectsCallback(
_In_ POBJECT_DIRECTORY_INFORMATION Entry,
_In_ PVOID CallbackParam
)
{
POBJENUMPARAM Param = (POBJENUMPARAM)CallbackParam;
if (RtlEqualUnicodeString(&Entry->TypeName, Param->ObjectType, TRUE)) {
printf_s("\t%ws -> %wZ\r\n", Param->ObjectDirectory, &Entry->Name);
}
return STATUS_UNSUCCESSFUL;
}
NTSTATUS EmptyWorkingSet()
{
NTSTATUS ntStatus;
QUOTA_LIMITS quotaLimits;
ntStatus = NtQueryInformationProcess(NtCurrentProcess(),
ProcessQuotaLimits,
"aLimits,
sizeof(quotaLimits),
NULL);
if (!NT_SUCCESS(ntStatus)) {
return ntStatus;
}
quotaLimits.MinimumWorkingSetSize = (SIZE_T)-1;
quotaLimits.MaximumWorkingSetSize = (SIZE_T)-1;
return NtSetInformationProcess(NtCurrentProcess(),
ProcessQuotaLimits,
"aLimits,
sizeof(quotaLimits));
}
VOID KDUPrintBooleanValueWithColor(
_In_ CONST char* Name,
_In_ BOOLEAN Value
)
{
#define PRINTGRN "\x1B[32m"
#define PRINTWHT "\x1B[37m"
if (g_ConsoleOutput) {
printf_s("\t\t%s %s\r\n%s", Name, Value ? PRINTGRN"TRUE" : PRINTWHT"FALSE", PRINTWHT);
}
else {
printf_s("\t\t%s %s\r\n", Name, Value ? "TRUE" : "FALSE");
}
}
VOID KDUQuerySpecMitigationState()
{
union {
SYSTEM_SPECULATION_CONTROL_INFORMATION v1;
SYSTEM_SPECULATION_CONTROL_INFORMATION_V2 v2;
} SpecControlInfo;
SYSTEM_KERNEL_VA_SHADOW_INFORMATION KvaShadowInfo;
DWORD bytesIO = 0;
RtlSecureZeroMemory(&KvaShadowInfo, sizeof(KvaShadowInfo));
NTSTATUS ntStatus = NtQuerySystemInformation(SystemKernelVaShadowInformation, &KvaShadowInfo, sizeof(KvaShadowInfo), &bytesIO);
if (NT_SUCCESS(ntStatus)) {
printf_s("\t>> SystemKernelVaShadowInformation\r\n");
KDUPrintBooleanValueWithColor("KvaShadowEnabled", KvaShadowInfo.KvaShadowFlags.KvaShadowEnabled);
KDUPrintBooleanValueWithColor("KvaShadowUserGlobal", KvaShadowInfo.KvaShadowFlags.KvaShadowUserGlobal);
KDUPrintBooleanValueWithColor("KvaShadowPcid", KvaShadowInfo.KvaShadowFlags.KvaShadowPcid);
KDUPrintBooleanValueWithColor("KvaShadowInvpcid", KvaShadowInfo.KvaShadowFlags.KvaShadowInvpcid);
KDUPrintBooleanValueWithColor("KvaShadowRequired", KvaShadowInfo.KvaShadowFlags.KvaShadowRequired);
KDUPrintBooleanValueWithColor("KvaShadowRequiredAvailable", KvaShadowInfo.KvaShadowFlags.KvaShadowRequiredAvailable);
printf_s("\tInvalidPteBit %lu\r\n", KvaShadowInfo.KvaShadowFlags.InvalidPteBit);
KDUPrintBooleanValueWithColor("L1DataCacheFlushSupported", KvaShadowInfo.KvaShadowFlags.L1DataCacheFlushSupported);
KDUPrintBooleanValueWithColor("L1TerminalFaultMitigationPresent", KvaShadowInfo.KvaShadowFlags.L1TerminalFaultMitigationPresent);
}
else {
supShowHardError("Cannot query Kernel VA Shadow information", ntStatus);
}
RtlSecureZeroMemory(&SpecControlInfo, sizeof(SpecControlInfo));
bytesIO = sizeof(SpecControlInfo);
ntStatus = NtQuerySystemInformation(SystemSpeculationControlInformation, &SpecControlInfo, bytesIO, &bytesIO);
if (ntStatus == STATUS_NOT_IMPLEMENTED ||
ntStatus == STATUS_INVALID_INFO_CLASS)
{
supShowHardError("Speculation control information class not present", ntStatus);
}
else if (ntStatus != STATUS_SUCCESS) {
supShowHardError("Cannot query speculation control information", ntStatus);
}
else {
if (bytesIO != sizeof(SYSTEM_SPECULATION_CONTROL_INFORMATION_V2) &&
bytesIO != sizeof(SYSTEM_SPECULATION_CONTROL_INFORMATION)) {
supPrintfEvent(kduEventError,
"Unknown speculation control information size %lu\r\n", bytesIO);
}
printf_s("\t>> SystemSpeculationControlInformation\r\n");
KDUPrintBooleanValueWithColor("BpbEnabled", SpecControlInfo.v1.SpeculationControlFlags.BpbEnabled);
KDUPrintBooleanValueWithColor("BpbDisabledSystemPolicy", SpecControlInfo.v1.SpeculationControlFlags.BpbDisabledSystemPolicy);
KDUPrintBooleanValueWithColor("BpbDisabledNoHardwareSupport", SpecControlInfo.v1.SpeculationControlFlags.BpbDisabledNoHardwareSupport);
KDUPrintBooleanValueWithColor("SpecCtrlEnumerated", SpecControlInfo.v1.SpeculationControlFlags.SpecCtrlEnumerated);
KDUPrintBooleanValueWithColor("SpecCmdEnumerated", SpecControlInfo.v1.SpeculationControlFlags.SpecCmdEnumerated);
KDUPrintBooleanValueWithColor("IbrsPresent", SpecControlInfo.v1.SpeculationControlFlags.IbrsPresent);
KDUPrintBooleanValueWithColor("StibpPresent", SpecControlInfo.v1.SpeculationControlFlags.StibpPresent);
KDUPrintBooleanValueWithColor("SmepPresent", SpecControlInfo.v1.SpeculationControlFlags.SmepPresent);
KDUPrintBooleanValueWithColor("SpeculativeStoreBypassDisableAvailable", SpecControlInfo.v1.SpeculationControlFlags.SpeculativeStoreBypassDisableAvailable);
KDUPrintBooleanValueWithColor("SpeculativeStoreBypassDisableSupported", SpecControlInfo.v1.SpeculationControlFlags.SpeculativeStoreBypassDisableSupported);
KDUPrintBooleanValueWithColor("SpeculativeStoreBypassDisabledSystemWide", SpecControlInfo.v1.SpeculationControlFlags.SpeculativeStoreBypassDisabledSystemWide);
KDUPrintBooleanValueWithColor("SpeculativeStoreBypassDisabledKernel", SpecControlInfo.v1.SpeculationControlFlags.SpeculativeStoreBypassDisabledKernel);
KDUPrintBooleanValueWithColor("SpeculativeStoreBypassDisableRequired", SpecControlInfo.v1.SpeculationControlFlags.SpeculativeStoreBypassDisableRequired);
KDUPrintBooleanValueWithColor("BpbDisabledKernelToUser", SpecControlInfo.v1.SpeculationControlFlags.BpbDisabledKernelToUser);
KDUPrintBooleanValueWithColor("SpecCtrlRetpolineEnabled", SpecControlInfo.v1.SpeculationControlFlags.SpecCtrlRetpolineEnabled);
KDUPrintBooleanValueWithColor("SpecCtrlImportOptimizationEnabled", SpecControlInfo.v1.SpeculationControlFlags.SpecCtrlImportOptimizationEnabled);
KDUPrintBooleanValueWithColor("EnhancedIbrs", SpecControlInfo.v1.SpeculationControlFlags.EnhancedIbrs);
KDUPrintBooleanValueWithColor("HvL1tfStatusAvailable", SpecControlInfo.v1.SpeculationControlFlags.HvL1tfStatusAvailable);
KDUPrintBooleanValueWithColor("HvL1tfProcessorNotAffected", SpecControlInfo.v1.SpeculationControlFlags.HvL1tfProcessorNotAffected);
KDUPrintBooleanValueWithColor("HvL1tfMigitationEnabled", SpecControlInfo.v1.SpeculationControlFlags.HvL1tfMigitationEnabled);
KDUPrintBooleanValueWithColor("HvL1tfMigitationNotEnabled_Hardware", SpecControlInfo.v1.SpeculationControlFlags.HvL1tfMigitationNotEnabled_Hardware);
KDUPrintBooleanValueWithColor("HvL1tfMigitationNotEnabled_LoadOption", SpecControlInfo.v1.SpeculationControlFlags.HvL1tfMigitationNotEnabled_LoadOption);
KDUPrintBooleanValueWithColor("HvL1tfMigitationNotEnabled_CoreScheduler", SpecControlInfo.v1.SpeculationControlFlags.HvL1tfMigitationNotEnabled_CoreScheduler);
KDUPrintBooleanValueWithColor("EnhancedIbrsReported", SpecControlInfo.v1.SpeculationControlFlags.EnhancedIbrsReported);
KDUPrintBooleanValueWithColor("MdsHardwareProtected", SpecControlInfo.v1.SpeculationControlFlags.MdsHardwareProtected);
KDUPrintBooleanValueWithColor("MbClearEnabled", SpecControlInfo.v1.SpeculationControlFlags.MbClearEnabled);
KDUPrintBooleanValueWithColor("MbClearReported", SpecControlInfo.v1.SpeculationControlFlags.MbClearReported);
printf_s("\t\tTsxCtrlStatus %lu\r\n", SpecControlInfo.v1.SpeculationControlFlags.TsxCtrlStatus);
KDUPrintBooleanValueWithColor("TsxCtrlReported", SpecControlInfo.v1.SpeculationControlFlags.TsxCtrlReported);
KDUPrintBooleanValueWithColor("TaaHardwareImmune", SpecControlInfo.v1.SpeculationControlFlags.TaaHardwareImmune);
if (bytesIO == sizeof(SYSTEM_SPECULATION_CONTROL_INFORMATION_V2)) {
printf_s("\t>> SystemSpeculationControlInformation v2\r\n");
KDUPrintBooleanValueWithColor("SbdrSsdpHardwareProtected", SpecControlInfo.v2.SpeculationControlFlags2.SbdrSsdpHardwareProtected);
KDUPrintBooleanValueWithColor("FbsdpHardwareProtected", SpecControlInfo.v2.SpeculationControlFlags2.FbsdpHardwareProtected);
KDUPrintBooleanValueWithColor("PsdpHardwareProtected", SpecControlInfo.v2.SpeculationControlFlags2.PsdpHardwareProtected);
KDUPrintBooleanValueWithColor("FbClearEnabled", SpecControlInfo.v2.SpeculationControlFlags2.FbClearEnabled);
KDUPrintBooleanValueWithColor("FbClearReported", SpecControlInfo.v2.SpeculationControlFlags2.FbClearReported);
}
}
}
#define WATCH_COUNT 256
VOID KDUQueryProcessWorkingSet(
_In_ ULONG_PTR SystemRangeStart,
_In_ PRTL_PROCESS_MODULES pvModules
)
{
NTSTATUS ntStatus;
ntStatus = NtSetInformationProcess(
NtCurrentProcess(),
ProcessWorkingSetWatch,
NULL,
0);
if (!NT_SUCCESS(ntStatus)) {
supShowHardError("Cannot enable ws watch", ntStatus);
return;
}
PROCESS_WS_WATCH_INFORMATION_EX watchInfo[WATCH_COUNT];
RtlSecureZeroMemory(&watchInfo, sizeof(watchInfo));
ntStatus = EmptyWorkingSet();
if (!NT_SUCCESS(ntStatus)) {
supShowHardError("Error at EmptyWorkingSet", ntStatus);
return;
}
ntStatus = NtQueryInformationProcess(
NtCurrentProcess(),
ProcessWorkingSetWatchEx,
(PVOID*)&watchInfo,
sizeof(watchInfo),
NULL);
if (!NT_SUCCESS(ntStatus)) {
supShowHardError("Error at working set changes query", ntStatus);
return;
}
PVOID cookie;
ntStatus = LdrLockLoaderLock(LDR_LOCK_LOADER_LOCK_FLAG_RAISE_ON_ERRORS, NULL, &cookie);
if (NT_SUCCESS(ntStatus)) {
PLDR_DATA_TABLE_ENTRY entry;
UNICODE_STRING moduleUnknown;
RtlInitUnicodeString(&moduleUnknown, L"Unknown");
for (ULONG i = 0; i < WATCH_COUNT; i++) {
PVOID faultingPc = watchInfo[i].BasicInfo.FaultingPc;
PVOID faultingVa = watchInfo[i].BasicInfo.FaultingVa;
if (faultingPc == NULL || faultingVa == NULL)
continue;
PWSTR pcName = moduleUnknown.Buffer, vaName = moduleUnknown.Buffer;
ULONG moduleIndex;
UNICODE_STRING pcModuleName, vaModuleName;
RtlInitEmptyUnicodeString(&pcModuleName, NULL, 0);
RtlInitEmptyUnicodeString(&vaModuleName, NULL, 0);
if ((ULONG_PTR)faultingPc >= SystemRangeStart) {
moduleIndex = 0;
if (supFindModuleEntryByAddress(pvModules,
faultingPc,
&moduleIndex))
{
if (NT_SUCCESS(ntsupConvertToUnicode(
(LPCSTR)pvModules->Modules[moduleIndex].FullPathName,
&pcModuleName)))
{
pcName = pcModuleName.Buffer;
}
}
}
else {
ntStatus = LdrFindEntryForAddress(faultingPc, (PLDR_DATA_TABLE_ENTRY*)&entry);
if (NT_SUCCESS(ntStatus))
pcName = entry->BaseDllName.Buffer;
}
if ((ULONG_PTR)faultingVa >= SystemRangeStart) {
moduleIndex = 0;
if (supFindModuleEntryByAddress(pvModules,
faultingVa,
&moduleIndex))
{
if (NT_SUCCESS(ntsupConvertToUnicode(
(LPCSTR)pvModules->Modules[moduleIndex].FullPathName,
&vaModuleName)))
{
pcName = vaModuleName.Buffer;
}
}
}
else {
ntStatus = LdrFindEntryForAddress(faultingVa, (PLDR_DATA_TABLE_ENTRY*)&entry);
if (NT_SUCCESS(ntStatus))
vaName = entry->BaseDllName.Buffer;
}
printf_s("\t>> ThreadId [%llu] Pc %p (%ws) : Va %p (%ws)\r\n",
watchInfo[i].FaultingThreadId,
faultingPc,
pcName,
faultingVa,
vaName);
if (pcModuleName.Buffer) RtlFreeUnicodeString(&pcModuleName);
if (vaModuleName.Buffer) RtlFreeUnicodeString(&vaModuleName);
}
LdrUnlockLoaderLock(LDR_UNLOCK_LOADER_LOCK_FLAG_RAISE_ON_ERRORS, cookie);
}
else {
supShowHardError("Failed acquire loader lock", ntStatus);
}
}
VOID TraceHandle(
_In_ ULONG_PTR SystemRangeStart,
_In_ HANDLE Handle,
_In_ PRTL_PROCESS_MODULES pvModules
)
{
NTSTATUS ntStatus;
PROCESS_HANDLE_TRACING_QUERY trace;
RtlSecureZeroMemory(&trace, sizeof(trace));
trace.Handle = Handle;
ntStatus = NtQueryInformationProcess(NtCurrentProcess(), ProcessHandleTracing, &trace, sizeof(trace), NULL);
if (NT_SUCCESS(ntStatus)) {
for (ULONG i = 0; i < trace.TotalTraces; i++) {
for (ULONG j = 0; j < PROCESS_HANDLE_TRACING_MAX_STACKS; j++) {
ULONG moduleIndex = 0;
PVOID stackAddress = trace.HandleTrace[i].Stacks[j];
if (stackAddress == NULL)
continue;
if (supFindModuleEntryByAddress(pvModules,
stackAddress,
&moduleIndex))
{
printf_s("\t>> 0x%p, %s, base 0x%p\r\n",
stackAddress,
pvModules->Modules[moduleIndex].FullPathName,
pvModules->Modules[moduleIndex].ImageBase);
}
else {
if ((ULONG_PTR)stackAddress >= SystemRangeStart) {
printf_s("\t>> 0x%p, !UNKNOWN! module\r\n",
stackAddress);
}
else {
PVOID cookie = NULL;
ntStatus = LdrLockLoaderLock(LDR_LOCK_LOADER_LOCK_FLAG_RAISE_ON_ERRORS, NULL, &cookie);
if (NT_SUCCESS(ntStatus)) {
PLDR_DATA_TABLE_ENTRY entry;
ntStatus = LdrFindEntryForAddress(stackAddress, (PLDR_DATA_TABLE_ENTRY*)&entry);
if (NT_SUCCESS(ntStatus)) {
printf_s("\t>> 0x%p, %wZ, base 0x%p\r\n",
stackAddress,
entry->BaseDllName,
entry->DllBase);
}
else {
printf_s("\t>> 0x%p, !UNKNOWN! module\r\n",
stackAddress);
}
LdrUnlockLoaderLock(LDR_UNLOCK_LOADER_LOCK_FLAG_RAISE_ON_ERRORS, cookie);
}
else {
supShowHardError("Failed to acquire loader lock", ntStatus);
}
}
}
}
}
}
else {
supShowHardError("Cannot query trace", ntStatus);
}
}
VOID TracePsHandle(
_In_ PCLIENT_ID ClientId,
_In_ ULONG_PTR SystemRangeStart,
_In_ PRTL_PROCESS_MODULES pvModules,
_In_ BOOL TraceThread
)
{
NTSTATUS ntStatus;
HANDLE objectHandle = NULL;
CLIENT_ID clientId;
OBJECT_ATTRIBUTES obja;
if (ClientId->UniqueProcess == NtCurrentTeb()->ClientId.UniqueProcess) {
printf_s("> Process (self) handle trace\r\n");
}
else {
printf_s("> Process (%lu) handle trace\r\n", HandleToULong(ClientId->UniqueProcess));
}
clientId = *ClientId;
InitializeObjectAttributes(&obja, NULL, 0, NULL, NULL);
ntStatus = NtOpenProcess(&objectHandle, PROCESS_ALL_ACCESS, &obja, &clientId);
if (NT_SUCCESS(ntStatus)) {
TraceHandle(
SystemRangeStart,
objectHandle,
pvModules);
NtClose(objectHandle);
}
else {
supShowHardError("Cannot open process", ntStatus);
}
if (!TraceThread)
return;
printf_s("> Thread handle trace\r\n");
clientId = NtCurrentTeb()->ClientId;
ntStatus = NtOpenThread(&objectHandle, THREAD_ALL_ACCESS, &obja, &clientId);
if (NT_SUCCESS(ntStatus)) {
TraceHandle(
SystemRangeStart,
objectHandle,
pvModules);
NtClose(objectHandle);
}
else {
supShowHardError("Cannot open thread", ntStatus);
}
}
VOID TraceSectionHandle(
_In_ ULONG_PTR SystemRangeStart,
_In_ PRTL_PROCESS_MODULES pvModules
)
{
NTSTATUS ntStatus;
HANDLE sectionHandle = NULL, fileHandle = NULL;
OBJECT_ATTRIBUTES obja;
UNICODE_STRING usName;
IO_STATUS_BLOCK iost;
RtlInitUnicodeString(&usName, L"\\systemroot\\system32\\drivers\\acpi.sys");
InitializeObjectAttributes(&obja, &usName, OBJ_CASE_INSENSITIVE, NULL, NULL);
ntStatus = NtOpenFile(&fileHandle,
SYNCHRONIZE | FILE_EXECUTE,
&obja,
&iost,
FILE_SHARE_READ | FILE_SHARE_DELETE,
FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT);
if (!NT_SUCCESS(ntStatus)) {
supShowHardError("Cannot open test file", ntStatus);
return;
}
RtlInitUnicodeString(&usName, L"\\RPC Control\\zzz");
ntStatus = NtCreateSection(§ionHandle,
SECTION_ALL_ACCESS,
&obja,
NULL,
PAGE_EXECUTE,
SEC_IMAGE,
fileHandle);
if (NT_SUCCESS(ntStatus)) {
printf_s("> Section handle trace\r\n");
TraceHandle(SystemRangeStart, sectionHandle, pvModules);
NtClose(sectionHandle);
}
else {
supShowHardError("Cannot create test section", ntStatus);
}
NtClose(fileHandle);
}
#define FLTMGR_LINK_HANDLE_FUNCID 3
#define FLTMGR_FIND_FIRST_FUNCID 9
#define FLTMGR_FIND_NEXT_FUNCID 0xA
#define IOCTL_FLTMGR_LINK_HANDLE \
CTL_CODE(FILE_DEVICE_DISK_FILE_SYSTEM, FLTMGR_LINK_HANDLE_FUNCID, METHOD_BUFFERED, FILE_READ_ACCESS)
#define IOCTL_FLTMGR_FIND_FIRST \
CTL_CODE(FILE_DEVICE_DISK_FILE_SYSTEM, FLTMGR_FIND_FIRST_FUNCID, METHOD_BUFFERED, FILE_READ_ACCESS)
#define IOCTL_FLTMGR_FIND_NEXT \
CTL_CODE(FILE_DEVICE_DISK_FILE_SYSTEM, FLTMGR_FIND_NEXT_FUNCID, METHOD_BUFFERED, FILE_READ_ACCESS)
NTSTATUS KDUpFilterFindFirst(
_In_ HANDLE FltMgrHandle,
_In_ FILTER_INFORMATION_CLASS InformationClass,
_In_ PVOID Buffer,
_In_ ULONG BufferSize
)
{
FILTER_INFORMATION_CLASS infoClass = InformationClass;
NTSTATUS ntStatus;
DWORD linkInfo[2];
linkInfo[0] = 3; //type of callback, 3 is for filters.
linkInfo[1] = 0;
ntStatus = supFilterDeviceIoControl(FltMgrHandle,
IOCTL_FLTMGR_LINK_HANDLE,
&linkInfo,
sizeof(linkInfo),
NULL,
0,
NULL);
if (NT_SUCCESS(ntStatus)) {
ntStatus = supFilterDeviceIoControl(FltMgrHandle,
IOCTL_FLTMGR_FIND_FIRST,
&infoClass,
sizeof(infoClass),
Buffer,
BufferSize,
NULL);
}
return ntStatus;
}
NTSTATUS KDUpFilterFindNext(
_In_ HANDLE FltMgrHandle,
_In_ FILTER_INFORMATION_CLASS InformationClass,
_In_ PVOID Buffer,
_In_ ULONG BufferSize
)
{
FILTER_INFORMATION_CLASS infoClass = InformationClass;
return supFilterDeviceIoControl(FltMgrHandle,
IOCTL_FLTMGR_FIND_NEXT,
&infoClass,
sizeof(infoClass),
Buffer,
BufferSize,
NULL);
}
VOID KDUListFilters()
{
DWORD bufferSize;
PFILTER_FULL_INFORMATION buffer = NULL;
HANDLE fltMgrHandle = NULL;
NTSTATUS ntStatus;
UNICODE_STRING usDeviceName;
IO_STATUS_BLOCK iost;
OBJECT_ATTRIBUTES obja;
bufferSize = sizeof(FILTER_FULL_INFORMATION) + MAX_PATH * 2;
buffer = (PFILTER_FULL_INFORMATION)supHeapAlloc((SIZE_T)bufferSize);
if (buffer) {
RtlInitUnicodeString(&usDeviceName, L"\\??\\FltMgr");
InitializeObjectAttributes(&obja, &usDeviceName, OBJ_CASE_INSENSITIVE, NULL, NULL);
ntStatus = NtCreateFile(&fltMgrHandle,
GENERIC_READ,
&obja,
&iost,
NULL,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ,
FILE_OPEN,
0,
NULL,
0);
if (!NT_SUCCESS(ntStatus)) {
supPrintfEvent(kduEventError, "Cannot open %wZ, NTSTATUS (0x%lX)\r\n", usDeviceName, ntStatus);
return;
}
if (NT_SUCCESS(KDUpFilterFindFirst(fltMgrHandle, FilterFullInformation, buffer, bufferSize))) {
do {
printf_s("\t>> %ws\r\n", buffer->FilterNameBuffer);
RtlSecureZeroMemory(buffer, bufferSize);
} while (KDUpFilterFindNext(fltMgrHandle,
FilterFullInformation,
buffer, bufferSize) != STATUS_NO_MORE_ENTRIES);
}
NtClose(fltMgrHandle);
supHeapFree(buffer);
}
}
VOID KDUBacktraceByHandle(
_In_ ULONG_PTR SystemRangeStart,
_In_ PRTL_PROCESS_MODULES pvModules
)
{
NTSTATUS ntStatus;
CLIENT_ID cid;
PVOID procBuffer;
PROCESS_HANDLE_TRACING_ENABLE traceEnable;
ULONG nextEntryDelta = 0;
UNICODE_STRING usLsass;
union {
PSYSTEM_PROCESS_INFORMATION Process;
PBYTE ListRef;
} List;
RtlSecureZeroMemory(&traceEnable, sizeof(traceEnable));
ntStatus = NtSetInformationProcess(NtCurrentProcess(),
ProcessHandleTracing,
&traceEnable,
sizeof(traceEnable));
if (!NT_SUCCESS(ntStatus)) {
supShowHardError("Cannot enable backtrace", ntStatus);
return;
}
//
// Trace self process/thread
//
cid.UniqueProcess = NtCurrentTeb()->ClientId.UniqueProcess;
cid.UniqueThread = NULL;
TracePsHandle(&cid, SystemRangeStart, pvModules, TRUE);
//
// Trace lsass process
//
procBuffer = supGetSystemInfo(SystemProcessInformation);
List.ListRef = (PBYTE)procBuffer;
if (List.ListRef) {
RtlInitUnicodeString(&usLsass, L"lsass.exe");
cid.UniqueProcess = cid.UniqueThread = NULL;
do {
List.ListRef += nextEntryDelta;
if (RtlEqualUnicodeString(&usLsass, &List.Process->ImageName, TRUE)) {
cid.UniqueProcess = List.Process->UniqueProcessId;
TracePsHandle(&cid, SystemRangeStart, pvModules, FALSE);
break;
}
nextEntryDelta = List.Process->NextEntryDelta;
} while (nextEntryDelta);
supHeapFree(procBuffer);
}
else {
supPrintfEvent(kduEventError, "Cannot allocate process list, process trace unavailable\r\n");
}
TraceSectionHandle(SystemRangeStart, pvModules);
}
VOID KDUListObjects(
VOID
)
{
OBJENUMPARAM enumParam;
UNICODE_STRING usObjectType;
enumParam.ObjectDirectory = (PWSTR)L"\\";
RtlInitUnicodeString(&usObjectType, L"Device");
enumParam.ObjectType = &usObjectType;
supEnumSystemObjects(enumParam.ObjectDirectory, NULL,
(PENUMOBJECTSCALLBACK)EnumObjectsCallback,
(PVOID)&enumParam);
enumParam.ObjectDirectory = (PWSTR)L"\\Device";
supEnumSystemObjects(enumParam.ObjectDirectory, NULL,
(PENUMOBJECTSCALLBACK)EnumObjectsCallback,
(PVOID)&enumParam);
enumParam.ObjectDirectory = (PWSTR)L"\\Driver";
RtlInitUnicodeString(&usObjectType, L"Driver");
enumParam.ObjectType = &usObjectType;
supEnumSystemObjects(enumParam.ObjectDirectory, NULL,
(PENUMOBJECTSCALLBACK)EnumObjectsCallback,
(PVOID)&enumParam);
}
VOID KDUListDrivers(
_In_ PRTL_PROCESS_MODULES pvModules
)
{
for (ULONG i = 0; i < pvModules->NumberOfModules; i++) {
printf_s("\t%lu %p %lu %s\r\n",
pvModules->Modules[i].LoadOrderIndex,
pvModules->Modules[i].ImageBase,
pvModules->Modules[i].ImageSize,
pvModules->Modules[i].FullPathName);
}
}
VOID KDUListMemoryLayout()
{
PCM_FULL_RESOURCE_DESCRIPTOR pDesc;
PCM_RESOURCE_LIST pList = supQueryPhysicalMemoryLayout();
PCM_PARTIAL_RESOURCE_DESCRIPTOR pPartialDesc;
if (pList == NULL)
return;
printf_s("ResourceList Count %lx\r\n", pList->Count);
for (ULONG i = 0; i < pList->Count; i++) {
pDesc = &pList->List[i];
printf_s("pDesc[%lu].PartialResourceList.Count %lu\r\n",
i,
pDesc->PartialResourceList.Count);
for (ULONG j = 0; j < pDesc->PartialResourceList.Count; j++) {
pPartialDesc = &pDesc->PartialResourceList.PartialDescriptors[j];
if (pPartialDesc->Type == CmResourceTypeMemory ||
pPartialDesc->Type == CmResourceTypeMemoryLarge)
{
ULONGLONG length = pPartialDesc->u.Memory.Length;
switch (pPartialDesc->Flags & CM_RESOURCE_MEMORY_LARGE)
{
case CM_RESOURCE_MEMORY_LARGE_40:
length <<= 8;
break;
case CM_RESOURCE_MEMORY_LARGE_48:
length <<= 16;
break;
case CM_RESOURCE_MEMORY_LARGE_64:
length <<= 32;
break;
}
printf_s("#%lu Flags 0x%04lX 0x%016llX::0x%016llX (length 0x%016llX, %llu Mb)\r\n",
j,
pPartialDesc->Flags,
pPartialDesc->u.Memory.Start.QuadPart,
pPartialDesc->u.Memory.Start.QuadPart + length,
length,
length / 1024 / 1024);
}
else {
printf_s("#%lu Type 0x%04lX, Flags 0x%04lX\r\n", j, pPartialDesc->Type, pPartialDesc->Flags);
}
}
}
supHeapFree(pList);
}
VOID KDUDiagStart()
{
PRTL_PROCESS_MODULES pvModules;
ULONG_PTR systemRangeStart;
DWORD dwDummy = 0;
FUNCTION_ENTER_MSG(__FUNCTION__);
if (!GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &dwDummy)) {
g_ConsoleOutput = FALSE;
}
//header
printf_s("[+] Running system diagnostics\r\n");
__try {
systemRangeStart = ntsupQuerySystemRangeStart();
printf_s("> System range start %llX\r\n", systemRangeStart);
printf_s("> Speculation mitigation state flags\r\n");
KDUQuerySpecMitigationState();
pvModules = (PRTL_PROCESS_MODULES)supGetLoadedModulesList(FALSE, NULL);
if (pvModules) {
printf_s("> List of loaded drivers\r\n\t[#] [ImageBase] [ImageSize] [FileName]\r\n");
KDUListDrivers(pvModules);
printf_s("> List of device and driver objects in the common locations\r\n");
KDUListObjects();
KDUBacktraceByHandle(systemRangeStart, pvModules);
printf_s("> Analyzing process working set\r\n");
KDUQueryProcessWorkingSet(systemRangeStart, pvModules);
supHeapFree(pvModules);
}
else {
supPrintfEvent(kduEventError, "Cannot allocate memory\r\n");
}
printf_s("> List of registered minifilters\r\n");
KDUListFilters();
printf_s("> Physical memory layout\r\n");
KDUListMemoryLayout();
}
__except (EXCEPTION_EXECUTE_HANDLER) {
supPrintfEvent(kduEventError, "Exception (0x%lX) during diagnostics\r\n", GetExceptionCode());
}
FUNCTION_LEAVE_MSG(__FUNCTION__);
}