-
Notifications
You must be signed in to change notification settings - Fork 38
/
GuardAMD64.c
1072 lines (885 loc) · 32.2 KB
/
GuardAMD64.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
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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Copyright (c) 2015-2018 by blindtiger ( [email protected] )
*
* The contents of this file are subject to the Mozilla Public License Version
* 2.0 (the "License")); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. SEe the License
* for the specific language governing rights and limitations under the
* License.
*
* The Initial Developer of the Original e is blindtiger.
*
*/
#include <OsDefs.h>
#include "Ctx.h"
#include "Except.h"
#include "Guard.h"
#include "Jump.h"
#include "Reload.h"
#include "Scan.h"
#include "Stack.h"
#include "Testis.h"
#include "Thread.h"
#define __ROL64(x, n) (((x) << ((n % 64))) | ((x) >> (64 - (n % 64))))
#define __ROR64(x, n) (((x) >> ((n % 64))) | ((x) << (64 - (n % 64))))
#define PG_KEY_INTERVAL 0x100
#define PG_FIELD_OFFSET 0x100
#define PG_FIELD_ROL_BITS 9
#define PG_MAX_FOUND 10
static PEX_SPIN_LOCK LargePoolTableLock;
static PPOOL_BIG_PAGES PoolBigPageTable;
static SIZE_T PoolBigPageTableSize;
static BOOLEAN PgIsBtcEncode;
static ULONG PgEntryRvaOffset;
static ULONG PgAppendSectionSize;
static PVOID PgAppendSection;
static ULONG PgNtSectionSize;
static ULONG64 PgContextField[2];
static WORK_QUEUE_ITEM PgClearWorkerItem;
PVOID NtosExpWorkerContext;
POOL_TYPE
(NTAPI * NtosMmDeterminePoolType)(
__in PVOID VirtualAddress
);
VOID
(NTAPI * NtosKiStartSystemThread)(
VOID
);
VOID
(NTAPI * NtosPspSystemThreadStartup)(
__in PKSTART_ROUTINE StartRoutine,
__in PVOID StartContext
);
VOID
(NTAPI * NtosExpWorkerThread)(
__in PVOID StartContext
);
ULONG64
NTAPI
_btc64(
__in ULONG64 a,
__in ULONG64 b
);
PVOID
NTAPI
_PgEncodeClear(
__in PVOID Reserved,
__in PVOID PgContext
);
VOID
NTAPI
_RevertWorkerThreadToSelf(
VOID
);
ULONG64
NTAPI
GetKeyOffset(
__in ULONG64 XorKey,
__in ULONG Index
)
{
ULONG64 ReturnKey = 0;
ULONG LastIndex = 0;
ULONG64 LastKey = 0;
LastIndex = PG_KEY_INTERVAL;
LastKey = XorKey;
do {
LastKey = __ROR64(
LastKey,
(LastIndex & 0xff));
if (FALSE != PgIsBtcEncode) {
LastKey = _btc64(
LastKey,
LastKey);
}
LastIndex--;
if ((Index % PG_KEY_INTERVAL) == LastIndex) {
ReturnKey = LastKey;
break;
}
} while (0 != LastIndex);
return ReturnKey;
}
VOID
NTAPI
SetPgContextField(
VOID
)
{
NTSTATUS Status = STATUS_SUCCESS;
HANDLE FileHandle = NULL;
HANDLE SectionHandle = NULL;
OBJECT_ATTRIBUTES ObjectAttributes = { 0 };
UNICODE_STRING ImageFileName = { 0 };
IO_STATUS_BLOCK IoStatusBlock = { 0 };
PVOID ViewBase = NULL;
SIZE_T ViewSize = 0;
PVOID ImageBase = NULL;
PCHAR ControlPc = NULL;
PCHAR TargetPc = NULL;
ULONG Length = 0;
PIMAGE_SECTION_HEADER NtSection = NULL;
PRUNTIME_FUNCTION FunctionEntry = NULL;
CHAR SectionSig[] = "2e 48 31 11 48 31 51 08 48 31 51 10 48 31 51 18";
CHAR FieldSig[] = "fb 48 8d 05";
CHAR FieldSigEx[] = "?? 89 ?? 00 01 00 00 48 8D 05 ?? ?? ?? ?? ?? 89 ?? 08 01 00 00";
CHAR PgEntrySig[] = "48 81 ec c0 02 00 00 48 8d a8 d8 fd ff ff 48 83 e5 80";
CHAR KiEntrySig[] = "b9 01 00 00 00 44 0f 22 c1 48 8b 14 24 48 8b 4c 24 08 ff 54 24 10";
CHAR PspEntrySig[] = "eb ?? b9 1e 00 00 00 e8";
ImageBase = GetImageHandle("ntoskrnl.exe");
if (NULL != ImageBase) {
RtlInitUnicodeString(
&ImageFileName,
L"\\SystemRoot\\System32\\ntoskrnl.exe");
InitializeObjectAttributes(
&ObjectAttributes,
&ImageFileName,
(OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE),
NULL,
NULL);
Status = ZwOpenFile(
&FileHandle,
FILE_EXECUTE,
&ObjectAttributes,
&IoStatusBlock,
FILE_SHARE_READ | FILE_SHARE_DELETE,
0);
if (NT_SUCCESS(Status)) {
InitializeObjectAttributes(&ObjectAttributes,
NULL,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
NULL,
NULL);
Status = ZwCreateSection(
&SectionHandle,
SECTION_MAP_READ | SECTION_MAP_EXECUTE,
&ObjectAttributes,
NULL,
PAGE_EXECUTE,
SEC_IMAGE,
FileHandle);
if (NT_SUCCESS(Status)) {
Status = ZwMapViewOfSection(
SectionHandle,
NtCurrentProcess(),
&ViewBase,
0L,
0L,
NULL,
&ViewSize,
ViewShare,
0L,
PAGE_EXECUTE);
if (NT_SUCCESS(Status)) {
ControlPc = ScanBytes(
ViewBase,
(PCHAR)ViewBase + ViewSize,
SectionSig);
if (NULL != ControlPc) {
TargetPc = ControlPc;
while (0 != CmpByte(TargetPc[0], 0x41) &&
0 != CmpByte(TargetPc[1], 0xff) &&
0 != CmpByte(TargetPc[2], 0xe0)) {
Length = GetInstructionLength(TargetPc);
if (0 == PgAppendSectionSize) {
if (8 == Length) {
if (0 == CmpByte(TargetPc[0], 0x48) &&
0 == CmpByte(TargetPc[1], 0x31) &&
0 == CmpByte(TargetPc[2], 0x84) &&
0 == CmpByte(TargetPc[3], 0xca)) {
PgAppendSectionSize = *(PULONG)(TargetPc + 4);
if (0 != PgAppendSectionSize) {
PgAppendSection = ExAllocatePool(
NonPagedPool,
PgAppendSectionSize);
if (NULL != PgAppendSection) {
RtlCopyMemory(
PgAppendSection,
ControlPc,
PgAppendSectionSize);
}
}
#ifndef VMP
DbgPrint(
"Soul - Testis - < %p > pg context append section size\n",
PgAppendSectionSize);
#endif // !VMP
if (0 == CmpByte(TargetPc[11], 0x48) ||
0 == CmpByte(TargetPc[12], 0x0f) ||
0 == CmpByte(TargetPc[13], 0xbb) ||
0 == CmpByte(TargetPc[14], 0xc0)) {
PgIsBtcEncode = TRUE;
#ifndef VMP
DbgPrint("Soul - Testis - pg context btc encode enable\n");
#endif // !VMP
}
}
}
}
if (6 == Length) {
if (0 == CmpByte(TargetPc[0], 0x8b) &&
0 == CmpByte(TargetPc[1], 0x82)) {
PgEntryRvaOffset = *(PULONG)(TargetPc + 2);
#ifndef VMP
DbgPrint(
"Soul - Testis - < %p > pg context entry rva offset\n",
PgEntryRvaOffset);
#endif // !VMP
break;
}
}
TargetPc += Length;
}
}
ControlPc = ViewBase;
while (NULL != ControlPc) {
ControlPc = ScanBytes(
ControlPc,
(PCHAR)ViewBase + ViewSize,
FieldSig);
if (NULL != ControlPc) {
TargetPc = ScanBytes(
ControlPc,
ControlPc + PgEntryRvaOffset,
FieldSigEx);
if (NULL != TargetPc) {
PgContextField[0] = (ULONG64)
((TargetPc - (ULONG64)ViewBase + (ULONG64)ImageBase - 4) +
*(PLONG)(TargetPc - 4) +
sizeof(LONG));
PrintSymbol((PVOID)PgContextField[0]);
PgContextField[1] = (ULONG64)
((TargetPc - (ULONG64)ViewBase + (ULONG64)ImageBase + 10) +
*(PLONG)(TargetPc + 10) +
sizeof(LONG));
PrintSymbol((PVOID)PgContextField[1]);
break;
}
ControlPc++;
}
else {
break;
}
}
ControlPc = ScanBytes(
ViewBase,
(PCHAR)ViewBase + ViewSize,
PgEntrySig);
if (NULL != ControlPc) {
NtSection = SectionTableFromVirtualAddress(
ViewBase,
ControlPc);
if (NULL != NtSection) {
PgNtSectionSize = max(
NtSection->SizeOfRawData,
NtSection->Misc.VirtualSize);
#ifndef VMP
DbgPrint(
"Soul - Testis - < %p > pg context nt section size\n",
PgNtSectionSize);
#endif // !VMP
}
}
ControlPc = ScanBytes(
ViewBase,
(PCHAR)ViewBase + ViewSize,
KiEntrySig);
if (NULL != ControlPc) {
TargetPc = ControlPc;
NtosKiStartSystemThread = (PVOID)
(TargetPc - (ULONG64)ViewBase + (ULONG64)ImageBase);
#ifndef VMP
DbgPrint(
"Soul - Testis - < %p > KiStartSystemThread\n",
NtosKiStartSystemThread);
#endif // !VMP
}
ControlPc = ScanBytes(
ViewBase,
(PCHAR)ViewBase + ViewSize,
PspEntrySig);
if (NULL != ControlPc) {
TargetPc = (PVOID)
(ControlPc - (ULONG64)ViewBase + (ULONG64)ImageBase);
FunctionEntry = DetourRtlLookupFunctionEntry(
(ULONG64)TargetPc,
(PULONG64)&ImageBase,
NULL);
if (NULL != FunctionEntry) {
NtosPspSystemThreadStartup = (PVOID)
((PCHAR)ImageBase + FunctionEntry->BeginAddress);
#ifndef VMP
DbgPrint(
"Soul - Testis - < %p > PspSystemThreadStartup\n",
NtosPspSystemThreadStartup);
#endif // !VMP
}
}
ZwUnmapViewOfSection(
NtCurrentProcess(),
ViewBase);
}
ZwClose(SectionHandle);
}
ZwClose(FileHandle);
}
}
}
PVOID
NTAPI
FindExProtectPool(
VOID
)
{
PVOID Result = NULL;
PVOID ImageBase = NULL;
PIMAGE_NT_HEADERS NtHeaders = NULL;
PIMAGE_SECTION_HEADER NtSection = NULL;
PCHAR ControlPc = NULL;
PCHAR TargetPc = NULL;
CHAR Sig[] = "48 8b ?? e8";
CHAR SigEx[] = "ff 0f 00 00 0f 85 ?? ?? ?? ?? 48 8b ?? e8"; // check call MiDeterminePoolType
ImageBase = GetImageHandle("ntoskrnl.exe");
if (NULL != ImageBase) {
NtHeaders = RtlImageNtHeader(ImageBase);
if (NULL != NtHeaders) {
NtSection = IMAGE_FIRST_SECTION(NtHeaders);
ControlPc = (PCHAR)ImageBase + NtSection[0].VirtualAddress;
while (TRUE) {
ControlPc = ScanBytes(
ControlPc,
(PCHAR)ImageBase + NtSection[0].VirtualAddress + NtSection[0].SizeOfRawData,
Sig);
if (NULL != ControlPc) {
TargetPc = ScanBytes(
ControlPc,
ControlPc + 0x100,
SigEx);
if (NULL != TargetPc) {
Result = TargetPc;
TargetPc = (TargetPc + 0xe) +
*(PLONG)(TargetPc + 0xe) + sizeof(LONG);
RtlCopyMemory(
(PVOID)&NtosMmDeterminePoolType,
&TargetPc,
sizeof(PVOID));
#ifndef VMP
DbgPrint(
"Soul - Testis - < %p > MmDeterminePoolType\n",
NtosMmDeterminePoolType);
#endif // !VMP
break;
}
}
else {
break;
}
ControlPc++;
}
}
}
return Result;
}
VOID
NTAPI
FindPoolBigPageTable(
VOID
)
{
PVOID ImageBase = NULL;
PCHAR ControlPc = NULL;
PCHAR TargetPc = NULL;
ULONG Length = 0;
PPOOL_BIG_PAGES * PageTable = NULL;
PSIZE_T PageTableSize = NULL;
ControlPc = FindExProtectPool();
if (NULL != ControlPc) {
#ifndef VMP
DbgPrint(
"Soul - Testis - < %p > ExProtectPool\n",
ControlPc);
#endif // !VMP
TargetPc = ControlPc;
while (TRUE) {
Length = GetInstructionLength(TargetPc);
if (1 == Length) {
if (0 == CmpByte(TargetPc[0], 0xc3)) {
break;
}
}
if (7 == Length) {
if (0x40 == (TargetPc[0] & 0xf0)) {
if (0 == CmpByte(TargetPc[1], 0x8b)) {
if (NULL == PageTable) {
PageTable = (PPOOL_BIG_PAGES *)
((TargetPc + 3) +
*(PLONG)(TargetPc + 3) +
sizeof(LONG));
if (0 == (ULONG64)*PageTable ||
0 != ((ULONG64)(*PageTable) & 0xfff)) {
PageTable = NULL;
}
}
else if (NULL == PageTableSize) {
PageTableSize = (PSIZE_T)
((TargetPc + 3) +
*(PLONG)(TargetPc + 3) +
sizeof(LONG));
if (0 == *PageTableSize ||
0 != ((ULONG64)(*PageTableSize) & 0xfff)) {
PageTableSize = NULL;
}
}
}
else if (0 == CmpByte(TargetPc[1], 0x8d)) {
if (NULL == LargePoolTableLock) {
LargePoolTableLock = (PEX_SPIN_LOCK)
((TargetPc + 3) +
*(PLONG)(TargetPc + 3) +
sizeof(LONG));
}
}
}
if (0 == CmpByte(TargetPc[0], 0x0f) &&
0 == CmpByte(TargetPc[1], 0x0d) &&
0 == CmpByte(TargetPc[2], 0x0d)) {
if (NULL == LargePoolTableLock) {
LargePoolTableLock = (PEX_SPIN_LOCK)
((TargetPc + 3) +
*(PLONG)(TargetPc + 3) +
sizeof(LONG));
}
}
}
if (NULL != PageTable &&
NULL != PageTableSize &&
NULL != LargePoolTableLock) {
if ((ULONG64)*PageTable > (ULONG64)*PageTableSize) {
PoolBigPageTable = (PPOOL_BIG_PAGES)*PageTable;
PoolBigPageTableSize = (SIZE_T)*PageTableSize;
}
else {
// swap
PoolBigPageTable = (PPOOL_BIG_PAGES)*PageTableSize;
PoolBigPageTableSize = (SIZE_T)*PageTable;
}
#ifndef VMP
DbgPrint(
"Soul - Testis - < %p > PoolBigPageTable\n",
PoolBigPageTable);
DbgPrint(
"Soul - Testis - < %p > PoolBigPageTableSize\n",
PoolBigPageTableSize);
DbgPrint(
"Soul - Testis - < %p > LargePoolTableLock\n",
LargePoolTableLock);
#endif // !VMP
break;
}
TargetPc +=
GetInstructionLength(TargetPc);
}
}
}
PVOID
NTAPI
FindPgEntrySig(
__in PVOID VirtualAddress
)
{
PVOID ControlPc = NULL;
ULONG Index = 0;
KIRQL Irql = 0;
CHAR SdbpCheckDll[] =
"48 8b 74 24 30 48 8b 7c 24 28 4c 8b 54 24 38 33 c0 49 89 02 49 83 ea 08 4c 3b d4 73 f4 48 89 7c 24 28 8b d8 8b f8 8b e8 4c 8b d0 4c 8b d8 4c 8b e0 4c 8b e8 4c 8b f0 4c 8b f8 ff e6";
if (NULL != LargePoolTableLock) {
Irql = ExAcquireSpinLockShared(LargePoolTableLock);
for (Index = 0;
Index < PoolBigPageTableSize;
Index++) {
if (POOL_BIG_TABLE_ENTRY_FREE != FlagOn(
(ULONG64)PoolBigPageTable[Index].Va,
POOL_BIG_TABLE_ENTRY_FREE)) {
if (NonPagedPool == NtosMmDeterminePoolType(PoolBigPageTable[Index].Va)) {
if (PoolBigPageTable[Index].NumberOfPages > PgNtSectionSize) {
if ((ULONG64)VirtualAddress >= (ULONG64)PoolBigPageTable[Index].Va &&
(ULONG64)VirtualAddress < (ULONG64)PoolBigPageTable[Index].Va +
PoolBigPageTable[Index].NumberOfPages) {
ControlPc = ScanBytes(
PoolBigPageTable[Index].Va,
(PCHAR)PoolBigPageTable[Index].Va + PoolBigPageTable[Index].NumberOfPages,
SdbpCheckDll);
break;
}
}
}
}
}
ExReleaseSpinLockShared(LargePoolTableLock, Irql);
}
return ControlPc;
}
VOID
NTAPI
PgEncodeClear(
__in PVOID Reserved,
__in PVOID PgContext
)
{
DbgPrint(
"Soul - Testis - < %p > pg context clear\n",
PgContext);
}
VOID
NTAPI
PgSetEncodeEntry(
__in PVOID PgContext,
__in ULONG64 RorKey
)
{
ULONG64 LastRorKey = 0;
ULONG EntryRva = 0;
ULONG64 FieldBuffer[10] = { 0 };
ULONG FieldIndex = 0;
ULONG Index = 0;
PCHAR ControlPc = NULL;
FieldIndex = (PgEntryRvaOffset -
PgAppendSectionSize) / sizeof(ULONG64);
RtlCopyMemory(
&FieldBuffer,
(PCHAR)PgContext + (PgEntryRvaOffset & ~7),
sizeof(FieldBuffer));
for (Index = 0;
Index < RTL_NUMBER_OF(FieldBuffer);
Index++) {
LastRorKey = GetKeyOffset(RorKey, FieldIndex + Index);
FieldBuffer[Index] = FieldBuffer[Index] ^ LastRorKey;
}
EntryRva = *(PULONG)((PCHAR)&FieldBuffer + (PgEntryRvaOffset & 7));
FieldIndex = (EntryRva - PgAppendSectionSize) / sizeof(ULONG64);
RtlCopyMemory(
&FieldBuffer,
(PCHAR)PgContext + (EntryRva & ~7),
sizeof(FieldBuffer));
for (Index = 0;
Index < RTL_NUMBER_OF(FieldBuffer);
Index++) {
LastRorKey = GetKeyOffset(RorKey, FieldIndex + Index);
FieldBuffer[Index] = FieldBuffer[Index] ^ LastRorKey;
}
ControlPc = (PCHAR)&FieldBuffer + (EntryRva & 7);
BuildJumpCode(
_PgEncodeClear,
&ControlPc);
for (Index = 0;
Index < RTL_NUMBER_OF(FieldBuffer);
Index++) {
LastRorKey = GetKeyOffset(RorKey, FieldIndex + Index);
FieldBuffer[Index] = FieldBuffer[Index] ^ LastRorKey;
}
RtlCopyMemory(
(PCHAR)PgContext + (EntryRva & ~7),
&FieldBuffer,
sizeof(FieldBuffer));
DbgPrint("Soul - Testis - pg context disarmed\n");
}
VOID
NTAPI
PgClearContext(
__in ULONG_PTR Argument
)
{
BOOLEAN Enable = FALSE;
PCHAR TargetPc = NULL;
SIZE_T Index = 0;
ULONG64 RorKey = 0;
PULONG64 Field = NULL;
PVOID PgContext = NULL;
Enable = KeDisableInterrupts();
if (0 == KeGetCurrentProcessorNumber()) {
ExAcquireSpinLockShared(LargePoolTableLock);
for (Index = 0;
Index < PoolBigPageTableSize;
Index++) {
if (POOL_BIG_TABLE_ENTRY_FREE != FlagOn(
(ULONG64)PoolBigPageTable[Index].Va,
POOL_BIG_TABLE_ENTRY_FREE)) {
if (NonPagedPool == NtosMmDeterminePoolType(PoolBigPageTable[Index].Va)) {
if (PoolBigPageTable[Index].NumberOfPages > PgNtSectionSize) {
TargetPc = PoolBigPageTable[Index].Va;
while ((ULONG64)TargetPc <
(ULONG64)PoolBigPageTable[Index].Va +
PoolBigPageTable[Index].NumberOfPages - PgAppendSectionSize) {
Field = TargetPc;
if ((ULONG64)Field == (ULONG64)&PgContextField) {
break;
}
RorKey = Field[1] ^ PgContextField[1];
if (0 == RorKey) {
if (Field[0] == PgContextField[0]) {
PgContext = TargetPc - PG_FIELD_OFFSET;
#ifndef VMP
DbgPrint(
"Soul - Testis - found decode pg context at < %p >\n",
PgContext);
#endif // !VMP
break;
}
}
else {
RorKey = __ROR64(RorKey, PG_FIELD_ROL_BITS);
if (FALSE != PgIsBtcEncode) {
RorKey = _btc64(RorKey, RorKey);
}
if ((ULONG64)(Field[0] ^ RorKey) == (ULONG64)PgContextField[0]) {
PgContext = TargetPc - PG_FIELD_OFFSET;
RorKey = __ROR64(Field[0] ^ PgContextField[0], 8);
if (FALSE != PgIsBtcEncode) {
RorKey = _btc64(RorKey, RorKey);
}
RorKey = __ROR64(RorKey, 7);
if (FALSE != PgIsBtcEncode) {
RorKey = _btc64(RorKey, RorKey);
}
RorKey = __ROR64(RorKey, 6);
if (FALSE != PgIsBtcEncode) {
RorKey = _btc64(RorKey, RorKey);
}
RorKey = __ROR64(RorKey, 5);
if (FALSE != PgIsBtcEncode) {
RorKey = _btc64(RorKey, RorKey);
}
RorKey = __ROR64(RorKey, 4);
if (FALSE != PgIsBtcEncode) {
RorKey = _btc64(RorKey, RorKey);
}
RorKey = __ROR64(RorKey, 3);
if (FALSE != PgIsBtcEncode) {
RorKey = _btc64(RorKey, RorKey);
}
RorKey = __ROR64(RorKey, 2);
if (FALSE != PgIsBtcEncode) {
RorKey = _btc64(RorKey, RorKey);
}
RorKey = __ROR64(RorKey, 1);
if (FALSE != PgIsBtcEncode) {
RorKey = _btc64(RorKey, RorKey);
}
DbgPrint(
"Soul - Testis - found encode pg context at < %p > RorKey < %p >\n",
PgContext,
RorKey);
PgSetEncodeEntry(PgContext, RorKey);
}
}
TargetPc++;
}
}
}
}
}
ExReleaseSpinLockSharedFromDpcLevel(LargePoolTableLock);
}
KeEnableInterrupts(Enable);
}
VOID
NTAPI
PgDecodeClear(
VOID
)
{
DbgPrint("Soul - Testis - pg worker clear\n");
}
VOID
NTAPI
PgSetDecodeEntry(
__in PVOID Context
)
{
ULONG64 LowLimit = 0;
ULONG64 HighLimit = 0;
PULONG64 InitialStack = 0;
PULONG64 TargetPc = NULL;
ULONG Count = 0;
PCALLERS Callers = NULL;
PVOID ControlPc = NULL;
Callers = ExAllocatePool(
NonPagedPool,
MAX_STACK_DEPTH * sizeof(CALLERS));
if (NULL != Callers) {
Count = WalkFrameChain(
Callers,
MAX_STACK_DEPTH);
if (0 != Count) {
IoGetStackLimits(&LowLimit, &HighLimit);
InitialStack = IoGetInitialStack();
if (NULL != Callers[Count - 1].Establisher) {
ControlPc = FindPgEntrySig(Callers[Count - 1].Establisher);
if (NULL != ControlPc) {
for (TargetPc = (PULONG64)Callers[Count - 1].EstablisherFrame;
(ULONG64)TargetPc < (ULONG64)InitialStack;
TargetPc++) {
if ((ULONG64)*TargetPc == (ULONG64)Callers[Count - 1].Establisher) {
*TargetPc = (ULONG64)_RevertWorkerThreadToSelf;
DbgPrint(
"Soul - Testis - revert worker thread to self\n");
break;
}
}
}
}
}
ExFreePool(Callers);
}
}
VOID
NTAPI
PgClearWorker(
__in PKEVENT Notify
)
{
NTSTATUS Status = STATUS_SUCCESS;
PSYSTEM_PROCESS_INFORMATION ProcessInfo = NULL;
PSYSTEM_EXTENDED_THREAD_INFORMATION ThreadInfo = NULL;
PVOID Buffer = NULL;
ULONG BufferSize = PAGE_SIZE;
ULONG ReturnLength = 0;
ULONG Index = 0;
PULONG64 InitialStack = 0;
PKPRIQUEUE WorkPriQueue = NULL;
InitialStack = IoGetInitialStack();
NtosExpWorkerContext = UlongToPtr(CriticalWorkQueue);
while ((ULONG64)InitialStack != (ULONG64)&Notify) {
WorkPriQueue = *(PVOID *)InitialStack;
if (FALSE != MmIsAddressValid(WorkPriQueue)) {
if (FALSE != MmIsAddressValid((PCHAR)(WorkPriQueue + 1) - 1)) {
if (0x15 == WorkPriQueue->Header.Type &&
0xac == WorkPriQueue->Header.Hand) {
NtosExpWorkerContext = WorkPriQueue;
break;
}
}
}
InitialStack--;
}
retry:
Buffer = ExAllocatePool(
NonPagedPool,
BufferSize);
if (NULL != Buffer) {
RtlZeroMemory(
Buffer,
BufferSize);
Status = ZwQuerySystemInformation(
SystemExtendedProcessInformation,
Buffer,
BufferSize,
&ReturnLength);
if (Status >= 0) {
ProcessInfo = (PSYSTEM_PROCESS_INFORMATION)Buffer;
while (TRUE) {
if (PsGetCurrentProcessId() == ProcessInfo->UniqueProcessId) {
ThreadInfo = (PSYSTEM_EXTENDED_THREAD_INFORMATION)
(ProcessInfo + 1);
for (Index = 0;
Index < ProcessInfo->NumberOfThreads;
Index++) {
if ((ULONG64)PsGetCurrentThreadId() ==
(ULONG64)ThreadInfo[Index].ThreadInfo.ClientId.UniqueThread) {
NtosExpWorkerThread = ThreadInfo[Index].Win32StartAddress;
break;
}
}
for (Index = 0;
Index < ProcessInfo->NumberOfThreads;
Index++) {
if ((ULONG64)PsGetCurrentThreadId() !=
(ULONG64)ThreadInfo[Index].ThreadInfo.ClientId.UniqueThread &&
(ULONG64)NtosExpWorkerThread ==
(ULONG64)ThreadInfo[Index].Win32StartAddress) {
RemoteCall(
ThreadInfo[Index].ThreadInfo.ClientId.UniqueThread,
IMAGE_NT_OPTIONAL_HDR_MAGIC,
(PUSER_THREAD_START_ROUTINE)PgSetDecodeEntry,
NULL);
}
}
break;
}
if (0 == ProcessInfo->NextEntryOffset) {
break;
}
else {
ProcessInfo = (PSYSTEM_PROCESS_INFORMATION)
((PCHAR)ProcessInfo + ProcessInfo->NextEntryOffset);
}
}
}
ExFreePool(Buffer);