forked from ufrisk/MemProcFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm_win.c
1458 lines (1390 loc) · 59.1 KB
/
mm_win.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
// mm_win.c : implementation of functionality related to the windows paging subsystem.
//
// (c) Ulf Frisk, 2019-2021
// Author: Ulf Frisk, [email protected]
//
#include "vmm.h"
#include "mm.h"
#include "pdb.h"
#include "pe.h"
#include "statistics.h"
#include "util.h"
#define MM_LOOP_PROTECT_ADD(flags) ((flags & ~0x00ff0000) | ((((flags >> 16) & 0xff) + 1) << 16))
#define MM_LOOP_PROTECT_MAX(flags) (((flags >> 16) & 0xff) > 4)
#define PTE_SWIZZLE_BIT 0x10 // nt!_MMPTE_SOFTWARE.SwizzleBit
#define PTE_SWIZZLE_MASK (((PMMWIN_CONTEXT)ctxVmm->pMmContext)->MemCompress.dwInvalidPteMask)
#define MMWINX86_PTE_IS_HARDWARE(pte) (pte & 0x01)
#define MMWINX86_PTE_TRANSITION(pte) (((pte & 0x0c01) == 0x0800) ? ((pte & 0xfffff000) | 0x005) : 0)
#define MMWINX86_PTE_PROTOTYPE(pte) (((pte & 0x00000407) == 0x00000400) ? (0x80000000 | ((pte >> 1) & 0x7ffffc00) | ((pte << 1) & 0x3ff)) : 0)
#define MMWINX86_PTE_PAGE_FILE_NUMBER(pte) ((pte >> 1) & 0x0f)
#define MMWINX86_PTE_PAGE_FILE_OFFSET(pte) (pte >> 12)
#define MMWINX86PAE_PTE_IS_HARDWARE(pte) (pte & 0x01)
#define MMWINX86PAE_PTE_TRANSITION(pte) (((pte & 0x0c01) == 0x0800) ? ((pte & 0x0000003f'fffff000) | 0x005) : 0)
#define MMWINX86PAE_PTE_PROTOTYPE(pte) (((pte & 0x80000007'00000401) == 0x80000000'00000400) ? (pte >> 32) : 0)
#define MMWINX86PAE_PTE_PAGE_FILE_NUMBER(pte) ((pte >> (((ctxVmm->kernel.dwVersionBuild >= 17134) ? 12 : 1))) & 0x0f)
#define MMWINX86PAE_PTE_PAGE_FILE_OFFSET(pte) ((pte >> 32) ^ (!(pte & PTE_SWIZZLE_BIT) ? PTE_SWIZZLE_MASK : 0))
#define MMWINX86PAE_PTE_PAGE_KEY_COMPRESSED(pte) (DWORD)(((MMWINX86PAE_PTE_PAGE_FILE_NUMBER(pte) << 0x1c) | MMWINX86PAE_PTE_PAGE_FILE_OFFSET(pte)))
#define MMWINX64_PTE_IS_HARDWARE(pte) (pte & 0x01)
#define MMWINX64_PTE_TRANSITION(pte) (((pte & 0x0c01) == 0x0800) ? ((pte & 0xffffdfff'fffff000) | 0x005) : 0)
#define MMWINX64_PTE_PROTOTYPE(pte) (((pte & 0x80000000'00070401) == 0x80000000'00000400) ? ((pte >> 16) | 0xffff0000'00000000) : 0)
#define MMWINX64_PTE_PAGE_FILE_NUMBER(pte) ((pte >> (((ctxVmm->kernel.dwVersionBuild >= 17134) ? 12 : 1))) & 0x0f)
#define MMWINX64_PTE_PAGE_FILE_OFFSET(pte) ((pte >> 32) ^ (!(pte & PTE_SWIZZLE_BIT) ? PTE_SWIZZLE_MASK : 0))
#define MMWINX64_PTE_PAGE_KEY_COMPRESSED(pte) (DWORD)(((MMWINX64_PTE_PAGE_FILE_NUMBER(pte) << 0x1c) | MMWINX64_PTE_PAGE_FILE_OFFSET(pte)))
typedef struct tdMMWIN_MEMCOMPRESS_OFFSET {
BOOL _fValid;
BOOL _fProcessedTry;
WORD _Size;
struct {
WORD PagesTree;
WORD SmkmStore;
WORD ChunkMetaData;
WORD RegionSizeMask;
WORD RegionIndexMask;
WORD CompressionAlgorithm;
WORD CompressedRegionPtrArray;
WORD OwnerProcess;
} SMKM_STORE;
} MMWIN_MEMCOMPRESS_OFFSET, *PMMWIN_MEMCOMPRESS_OFFSET;
typedef struct tdMMWIN_MEMCOMPRESS_CONTEXT {
QWORD vaEPROCESS;
DWORD dwPid;
DWORD dwPageFileNumber;
DWORD dwInvalidPteMask; // top 32-bits of nt!MiState->Hardware.InvalidPteMask
BOOL fValid;
BOOL fInitialized;
QWORD vaSmGlobals;
QWORD vaKeyToStoreTree;
MMWIN_MEMCOMPRESS_OFFSET O;
} MMWIN_MEMCOMPRESS_CONTEXT, *PMMWIN_MEMCOMPRESS_CONTEXT;
typedef struct tdMMWIN_CONTEXT {
CRITICAL_SECTION Lock;
FILE *pPageFile[10];
MMWIN_MEMCOMPRESS_CONTEXT MemCompress;
} MMWIN_CONTEXT, *PMMWIN_CONTEXT;
//-----------------------------------------------------------------------------
// BTREE FUNCTIONALITY BELOW:
//-----------------------------------------------------------------------------
typedef struct td_BTREE_LEAF_ENTRY {
DWORD k;
DWORD v;
} _BTREE_LEAF_ENTRY;
typedef struct td_BTREE_NODE_ENTRY32 {
DWORD k;
DWORD vaLeaf;
} _BTREE_NODE_ENTRY32;
typedef struct td_BTREE_NODE_ENTRY64 {
DWORD k;
QWORD vaLeaf;
} _BTREE_NODE_ENTRY64;
typedef struct td_BTREE32 {
WORD cEntries;
BYTE cLevel;
BYTE fLeaf;
DWORD vaLeftChild;
union {
_BTREE_LEAF_ENTRY LeafEntries[];
_BTREE_NODE_ENTRY32 NodeEntries[];
};
} _BTREE32, *P_BTREE32;
typedef struct td_BTREE64 {
WORD cEntries;
BYTE cLevel;
BYTE fLeaf;
QWORD vaLeftChild;
union {
_BTREE_LEAF_ENTRY LeafEntries[];
_BTREE_NODE_ENTRY64 NodeEntries[];
};
} _BTREE64, *P_BTREE64;
_Success_(return)
BOOL MmWin_BTree32_Search(_In_ PVMM_PROCESS pProcess, _In_ QWORD vaTree, _In_ DWORD dwKey, _Out_ PDWORD pdwValue, _In_ QWORD fVmmRead);
_Success_(return)
BOOL MmWin_BTree64_Search(_In_ PVMM_PROCESS pProcess, _In_ QWORD vaTree, _In_ DWORD dwKey, _Out_ PDWORD pdwValue, _In_ QWORD fVmmRead);
_Success_(return)
BOOL MmWin_BTree32_SearchLeaf(_In_ PVMM_PROCESS pSystemProcess, _In_ P_BTREE32 pT, _In_ DWORD dwKey, _Out_ PDWORD pdwValue, _In_ QWORD fVmmRead)
{
BOOL fSearchPreFail = FALSE;
DWORD i, dwSearchStep, dwSearchIndex = 1, dwSearchCount = 0;
// 2: search tree for leaf
for(i = 1; (i < 12) && ((pT->cEntries - 1) >> i); i++);
dwSearchIndex = dwSearchStep = min(1 << (i - 1), pT->cEntries);
while(TRUE) {
dwSearchCount++;
dwSearchStep = dwSearchStep >> 1;
if(pT->LeafEntries[dwSearchIndex].k == dwKey) {
*pdwValue = pT->LeafEntries[dwSearchIndex].v;
return TRUE;
}
if(dwSearchStep == 0) {
if(fSearchPreFail) {
return FALSE;
}
fSearchPreFail = TRUE;
dwSearchStep = 1;
}
if(pT->LeafEntries[dwSearchIndex].k < dwKey) {
if(dwSearchIndex + dwSearchStep < pT->cEntries) {
dwSearchIndex += dwSearchStep;
}
} else {
if(dwSearchStep <= dwSearchIndex) {
dwSearchIndex -= dwSearchStep;
}
}
}
}
_Success_(return)
BOOL MmWin_BTree64_SearchLeaf(_In_ PVMM_PROCESS pSystemProcess, _In_ P_BTREE64 pT, _In_ DWORD dwKey, _Out_ PDWORD pdwValue, _In_ QWORD fVmmRead)
{
BOOL fSearchPreFail = FALSE;
DWORD i, dwSearchStep, dwSearchIndex = 1;
// 2: search tree for leaf
for(i = 1; (i < 12) && ((pT->cEntries - 1) >> i); i++);
dwSearchIndex = dwSearchStep = min(1 << (i - 1), pT->cEntries);
while(TRUE) {
dwSearchStep = dwSearchStep >> 1;
if(pT->LeafEntries[dwSearchIndex].k == dwKey) {
*pdwValue = pT->LeafEntries[dwSearchIndex].v;
return TRUE;
}
if(dwSearchStep == 0) {
if(fSearchPreFail) {
return FALSE;
}
fSearchPreFail = TRUE;
dwSearchStep = 1;
}
if(pT->LeafEntries[dwSearchIndex].k < dwKey) {
if(dwSearchIndex + dwSearchStep < pT->cEntries) {
dwSearchIndex += dwSearchStep;
}
} else {
if(dwSearchStep <= dwSearchIndex) {
dwSearchIndex -= dwSearchStep;
}
}
}
}
_Success_(return)
BOOL MmWin_BTree32_SearchNode(_In_ PVMM_PROCESS pSystemProcess, _In_ P_BTREE32 pT, _In_ DWORD dwKey, _Out_ PDWORD pdwValue, _In_ QWORD fVmmRead)
{
BOOL fSearchPreFail = FALSE;
DWORD i, dwSearchStep, dwSearchIndex = 1;
QWORD vaSubTree = 0;
// 2: search tree for entry
for(i = 1; (i < 12) && ((pT->cEntries - 1) >> i); i++);
dwSearchIndex = dwSearchStep = min(1 << (i - 1), pT->cEntries - 1);
while(TRUE) {
dwSearchStep = dwSearchStep >> 1;
if((dwSearchStep == 0) && !fSearchPreFail) {
fSearchPreFail = TRUE;
dwSearchStep = 1;
}
if((dwSearchStep == 0) || ((pT->NodeEntries[dwSearchIndex].k <= dwKey) && ((dwSearchIndex + 1 == pT->cEntries) || (pT->NodeEntries[dwSearchIndex + 1].k > dwKey)))) {
if((dwSearchIndex == 0) && (pT->NodeEntries[0].k > dwKey)) {
vaSubTree = pT->vaLeftChild;
} else {
vaSubTree = pT->NodeEntries[dwSearchIndex].vaLeaf;
}
return MmWin_BTree32_Search(pSystemProcess, vaSubTree, dwKey, pdwValue, MM_LOOP_PROTECT_ADD(fVmmRead));
} else if(pT->NodeEntries[dwSearchIndex].k < dwKey) {
if(dwSearchIndex + dwSearchStep < pT->cEntries) {
dwSearchIndex += dwSearchStep;
}
} else {
if(dwSearchStep <= dwSearchIndex) {
dwSearchIndex -= dwSearchStep;
}
}
}
}
_Success_(return)
BOOL MmWin_BTree64_SearchNode(_In_ PVMM_PROCESS pSystemProcess, _In_ P_BTREE64 pT, _In_ DWORD dwKey, _Out_ PDWORD pdwValue, _In_ QWORD fVmmRead)
{
BOOL fSearchPreFail = FALSE;
DWORD i, dwSearchStep, dwSearchIndex = 1, dwSearchCount = 0;
QWORD vaSubTree = 0;
// 2: search tree for entry
for(i = 1; (i < 12) && ((pT->cEntries - 1) >> i); i++);
dwSearchIndex = dwSearchStep = min(1 << (i - 1), pT->cEntries - 1);
while(TRUE) {
dwSearchCount++;
dwSearchStep = dwSearchStep >> 1;
if((dwSearchStep == 0) && !fSearchPreFail) {
fSearchPreFail = TRUE;
dwSearchStep = 1;
}
if((dwSearchStep == 0) || ((pT->NodeEntries[dwSearchIndex].k <= dwKey) && ((dwSearchIndex + 1 == pT->cEntries) || (pT->NodeEntries[dwSearchIndex + 1].k > dwKey)))) {
if((dwSearchIndex == 0) && (pT->NodeEntries[0].k > dwKey)) {
vaSubTree = pT->vaLeftChild;
} else {
vaSubTree = pT->NodeEntries[dwSearchIndex].vaLeaf;
}
return MmWin_BTree64_Search(pSystemProcess, vaSubTree, dwKey, pdwValue, MM_LOOP_PROTECT_ADD(fVmmRead));
} else if(pT->NodeEntries[dwSearchIndex].k < dwKey) {
if(dwSearchIndex + dwSearchStep < pT->cEntries) {
dwSearchIndex += dwSearchStep;
}
} else {
if(dwSearchStep <= dwSearchIndex) {
dwSearchIndex -= dwSearchStep;
}
}
}
}
_Success_(return)
BOOL MmWin_BTree32_Search(_In_ PVMM_PROCESS pProcess, _In_ QWORD vaTree, _In_ DWORD dwKey, _Out_ PDWORD pdwValue, _In_ QWORD fVmmRead)
{
BOOL f;
BYTE pbBuffer[0x1000];
P_BTREE32 pT = (P_BTREE32)pbBuffer;
// 1: read tree
f = !MM_LOOP_PROTECT_MAX(fVmmRead) &&
VMM_KADDR32_PAGE(vaTree) &&
VmmRead2(pProcess, vaTree, pbBuffer, 0x1000, fVmmRead) &&
pT->cEntries;
if(!f) { return FALSE; }
if(pT->fLeaf) {
// Leaf
if(pT->cEntries > 0x1ff) { return FALSE; }
return MmWin_BTree32_SearchLeaf(pProcess, pT, dwKey, pdwValue, fVmmRead);
} else {
// Node
if(pT->cEntries > 0x1ff) { return FALSE; }
return MmWin_BTree32_SearchNode(pProcess, pT, dwKey, pdwValue, fVmmRead);
}
}
_Success_(return)
BOOL MmWin_BTree64_Search(_In_ PVMM_PROCESS pProcess, _In_ QWORD vaTree, _In_ DWORD dwKey, _Out_ PDWORD pdwValue, _In_ QWORD fVmmRead)
{
BOOL f;
BYTE pbBuffer[0x1000];
P_BTREE64 pT = (P_BTREE64)pbBuffer;
// 1: read tree
f = !MM_LOOP_PROTECT_MAX(fVmmRead) &&
VMM_KADDR64_PAGE(vaTree) &&
VmmRead2(pProcess, vaTree, pbBuffer, 0x1000, fVmmRead) &&
pT->cEntries;
if(!f) { return FALSE; }
if(pT->fLeaf) {
// Leaf
if(pT->cEntries > 0x1ff) { return FALSE; }
return MmWin_BTree64_SearchLeaf(pProcess, pT, dwKey, pdwValue, fVmmRead);
} else {
// Node
if(pT->cEntries > 0xff) { return FALSE; }
return MmWin_BTree64_SearchNode(pProcess, pT, dwKey, pdwValue, fVmmRead);
}
}
_Success_(return)
BOOL MmWin_BTree_Search(_In_ PVMM_PROCESS pProcess, _In_ QWORD vaTree, _In_ DWORD dwKey, _Out_ PDWORD pdwValue, _In_ QWORD fVmmRead)
{
return ctxVmm->f32 ? MmWin_BTree32_Search(pProcess, vaTree, dwKey, pdwValue, fVmmRead) : MmWin_BTree64_Search(pProcess, vaTree, dwKey, pdwValue, fVmmRead);
}
//-----------------------------------------------------------------------------
// MEMCOMPRESSION INITIALIZATION FUNCTIONALITY BELOW:
//-----------------------------------------------------------------------------
/*
* Initialize offsets in _SMKM_STORE / _ST_STORE / _ST_DATA_MGR
*/
VOID MmWin_MemCompress_InitializeOffsets32()
{
PMMWIN_MEMCOMPRESS_OFFSET po = &((PMMWIN_CONTEXT)ctxVmm->pMmContext)->MemCompress.O;
po->SMKM_STORE.PagesTree = 0x38 + 0x0; // static = ok
po->SMKM_STORE.ChunkMetaData = 0x38 + 0x6C; // static = ok
po->SMKM_STORE.SmkmStore = 0x38 + 0x1C0; // static = ok
po->SMKM_STORE.RegionSizeMask = 0x38 + 0x1C4; // static = ok
po->SMKM_STORE.RegionIndexMask = 0x38 + 0x1C8; // static = ok
po->SMKM_STORE.CompressionAlgorithm = 0x38 + 0x224; // 1709+
po->SMKM_STORE.CompressedRegionPtrArray = 0x1184; // 1709+
po->SMKM_STORE.OwnerProcess = 0x125c; // 2004+
if(ctxVmm->kernel.dwVersionBuild <= 18363) { // 1709-1909
po->SMKM_STORE.OwnerProcess = 0x1254;
}
if(ctxVmm->kernel.dwVersionBuild == 15063) { // 1703
po->SMKM_STORE.CompressionAlgorithm = 0x38 + 0x220;
po->SMKM_STORE.CompressedRegionPtrArray = 0x1174;
po->SMKM_STORE.OwnerProcess = 0x1244;
}
if(ctxVmm->kernel.dwVersionBuild == 14393) { // 1607
po->SMKM_STORE.CompressionAlgorithm = 0x38 + 0x220;
po->SMKM_STORE.CompressedRegionPtrArray = 0x1124;
po->SMKM_STORE.OwnerProcess = 0x1204;
}
po->_Size = po->SMKM_STORE.OwnerProcess + 8;
po->_fProcessedTry = TRUE;
po->_fValid = TRUE;
}
VOID MmWin_MemCompress_InitializeOffsets64()
{
PMMWIN_MEMCOMPRESS_OFFSET po = &((PMMWIN_CONTEXT)ctxVmm->pMmContext)->MemCompress.O;
po->SMKM_STORE.PagesTree = 0x50 + 0x0; // static = ok
po->SMKM_STORE.ChunkMetaData = 0x50 + 0xC0; // static = ok
po->SMKM_STORE.SmkmStore = 0x50 + 0x320; // static = ok
po->SMKM_STORE.RegionSizeMask = 0x50 + 0x328; // static = ok
po->SMKM_STORE.RegionIndexMask = 0x50 + 0x32C; // static = ok
po->SMKM_STORE.CompressionAlgorithm = 0x50 + 0x3E0; // 1709+
po->SMKM_STORE.CompressedRegionPtrArray = 0x1848; // 1709+
po->SMKM_STORE.OwnerProcess = 0x19B8; // 2004+
if(ctxVmm->kernel.dwVersionBuild <= 18363) { // 1709-1909
po->SMKM_STORE.OwnerProcess = 0x19A8;
}
if(ctxVmm->kernel.dwVersionBuild == 15063) { // 1703
po->SMKM_STORE.CompressionAlgorithm = 0x50 + 0x3D0;
po->SMKM_STORE.CompressedRegionPtrArray = 0x1828;
po->SMKM_STORE.OwnerProcess = 0x1988;
}
if(ctxVmm->kernel.dwVersionBuild == 14393) { // 1607
po->SMKM_STORE.CompressionAlgorithm = 0x50 + 0x3D0;
po->SMKM_STORE.CompressedRegionPtrArray = 0x17A8;
po->SMKM_STORE.OwnerProcess = 0x1918;
}
po->_Size = po->SMKM_STORE.OwnerProcess + 8;
po->_fProcessedTry = TRUE;
po->_fValid = TRUE;
}
/*
* Retrieve the page file number of the virtual store. This will be '2' on a
* standard system, but if paging are configured in a non-standard way this
* number may differ.
* Walk nt!MiSystemPartition/nt!.data section for candidate pointers to
* nt!_MMPAGING_FILE which have pool header: 'Mm '. The page file number and
* the virtual store flag is contained at same bits in all known versions with
* MemCompression as per below (for 64-bit):
* dt nt!_MMPAGING_FILE
* +0x0cc PageFileNumber : Pos 0, 4 Bits
* +0x0cc VirtualStorePagefile : Pos 6, 1 Bit
* If this function fails it will automatically fallback to the default number
* of 2.
*/
VOID MmWin_MemCompress_InitializeVirtualStorePageFileNumber()
{
BOOL f;
BYTE pbMm[0x100] = { 0 };
QWORD j, va = 0;
DWORD i, cb, cbRead, oPoolHdr, oPfNum;
PBYTE pb = NULL;
PVMM_PROCESS pObSystemProcess = NULL;
IMAGE_SECTION_HEADER oSectionHeader;
POB_SET pObSet = NULL;
PMMWIN_CONTEXT ctx = (PMMWIN_CONTEXT)ctxVmm->pMmContext;
QWORD qw, vaMiState;
DWORD oMiStateHardware, oMiStateHardwareInvalidPteMask;
ctx->MemCompress.dwPageFileNumber = 2;
// 1: SetUp and locate nt!MiSystemPartition/nt!.data
if(!(pObSet = ObSet_New())) { goto finish; }
if(!(pObSystemProcess = VmmProcessGet(4))) { goto finish; }
if(PDB_GetSymbolAddress(PDB_HANDLE_KERNEL, "MiSystemPartition", &va) && va) {
cb = 0x3000;
} else {
if(!PE_SectionGetFromName(pObSystemProcess, ctxVmm->kernel.vaBase, ".data", &oSectionHeader)) {
vmmprintfv_fn("CANNOT READ ntoskrnl.exe .data SECTION from PE header.\n");
goto finish;
}
if(oSectionHeader.Misc.VirtualSize > 0x00100000) { goto finish; }
va = ctxVmm->kernel.vaBase + oSectionHeader.VirtualAddress;
cb = oSectionHeader.Misc.VirtualSize;
}
if(!(pb = LocalAlloc(0, cb))) { goto finish; }
if(!VmmRead(pObSystemProcess, va, pb, cb)) {
vmmprintfv_fn("CANNOT READ ntoskrnl.exe .data SECTION.\n");
goto finish;
}
if(ctxVmm->f32) {
// 32-bit
// 2: Search for candidate pointers
for(i = 0; i < cb - 0x90; i += 4) {
f = (*(PDWORD)(pb + i + 0x004) == 1) &&
*(PDWORD)(pb + i + 0x000) &&
(*(PDWORD)(pb + i + 0x000) < 16) &&
VMM_KADDR32_8(*(PDWORD)(pb + i + 0x008)) &&
VMM_KADDR32_8(*(PDWORD)(pb + i + 0x00c));
if(f) {
for(j = 0; j < *(PDWORD)(pb + i + 0x000); j++) {
va = *(PDWORD)(pb + i + 0x008 + j * 4);
if(VMM_KADDR32_8(va)) {
ObSet_Push(pObSet, va);
}
}
}
}
oPoolHdr = 12;
oPfNum = 0x74;
} else {
// 64-bit
// 2: Search for candidate pointers
for(i = 0; i < cb - 0x90; i += 8) {
f = (*(PDWORD)(pb + i + 0x004) == 1) &&
*(PDWORD)(pb + i + 0x000) &&
(*(PDWORD)(pb + i + 0x000) < 16) &&
VMM_KADDR64_16(*(PQWORD)(pb + i + 0x008)) &&
VMM_KADDR64_16(*(PQWORD)(pb + i + 0x010)) &&
((*(PQWORD)(pb + i + 0x008) >> 32) == (*(PQWORD)(pb + i + 0x010) >> 32));
if(f) {
for(j = 0; j < *(PDWORD)(pb + i + 0x000); j++) {
va = *(PQWORD)(pb + i + 0x008 + j * 8);
if(VMM_KADDR64_16(va)) {
ObSet_Push(pObSet, va);
}
}
}
}
oPoolHdr = 4;
oPfNum = 0xcc;
}
// 3: Set InvalidPteMask
if(ctxVmm->kernel.dwVersionBuild >= 15063) {
f = PDB_GetSymbolAddress(PDB_HANDLE_KERNEL, "MiState", &vaMiState) &&
PDB_GetTypeChildOffset(PDB_HANDLE_KERNEL, "_MI_SYSTEM_INFORMATION", L"Hardware", &oMiStateHardware) &&
PDB_GetTypeChildOffset(PDB_HANDLE_KERNEL, "_MI_HARDWARE_STATE", L"InvalidPteMask", &oMiStateHardwareInvalidPteMask) &&
VmmRead(pObSystemProcess, vaMiState + oMiStateHardware + oMiStateHardwareInvalidPteMask, (PBYTE)&qw, 8);
ctx->MemCompress.dwInvalidPteMask = f ? (qw >> 32) : 0x00002000; // if fail: [0x00002000 = most common on Intel]
}
// 4: Verify nt!dt _MMPAGING_FILE by looking at pool header and VirtualStorePagefile bit
VmmCachePrefetchPages(pObSystemProcess, pObSet, 0);
while((va = ObSet_Pop(pObSet))) {
VmmReadEx(pObSystemProcess, va - 0x10, pbMm, 0x100, &cbRead, VMM_FLAG_FORCECACHE_READ);
if((*(PDWORD)(pbMm + oPoolHdr) == ' mM') && (*(PBYTE)(pbMm + 0x10 + oPfNum) & 0x40)) {
ctx->MemCompress.dwPageFileNumber = (*(PBYTE)(pbMm + 0x10 + oPfNum) & 0x0f);
goto finish;
}
}
vmmprintfv_fn("WARN! did not find virtual store number - fallback to default.\n");
finish:
LocalFree(pb);
Ob_DECREF(pObSystemProcess);
Ob_DECREF(pObSet);
}
/*
* Locate SmGlobals in 1st page of ntoskrnl.exe!CACHEALI section by looking for
* pointers to SMKM_STORE_METADATA (pool hdr: 'SmSa').
* SMGLOBALS (_SMKM_STORE_MGR)+000 = Smkm Metadata (_SMKM sSmkm)
* _SMKM: PTR[32] to _SMKM_STORE_METADATA: (pool hdr smSa)
* _SMKM_STORE_METADATA: sizeof(_SMKM_STORE_METADATA) = 0x28
* +000 = PTR to SMKM_STORE
* +018 = PTR to EPROCESS
* SMGLOBALS (_SMKM_STORE_MGR)+1C0 = KeyToStoreTree (B_TREE sGlobalTree)
* -- pSystemProcess
*/
VOID MmWin_MemCompress_Initialize_NoPdb64()
{
BOOL f, f32 = ctxVmm->f32;
BYTE pbPage[0x1000] = { 0 };
DWORD i, dwSmsaPoolHdr = 0, cbRead;
QWORD vaSmGlobals, vaSmsa, vaKeyToStoreTree;
IMAGE_SECTION_HEADER oSectionHeader;
PVMM_PROCESS pObSystemProcess = NULL;
POB_SET pObSet = NULL;
PMMWIN_CONTEXT ctx = (PMMWIN_CONTEXT)ctxVmm->pMmContext;
EnterCriticalSection(&ctxVmm->LockMaster);
if(ctx->MemCompress.fInitialized || (ctxVmm->kernel.dwVersionBuild < 14393)) { goto finish; }
// 1: Locate SmGlobals candidates in ntoskrnl.exe!CACHEALI section
if(!(pObSystemProcess = VmmProcessGet(4))) { goto finish; }
if(!(pObSet = ObSet_New())) { goto finish; }
if(!PE_SectionGetFromName(pObSystemProcess, ctxVmm->kernel.vaBase, "CACHEALI", &oSectionHeader)) {
vmmprintfv_fn("CANNOT READ ntoskrnl.exe CACHEALI SECTION from PE header.\n");
goto finish;
}
if(!VmmRead(pObSystemProcess, ctxVmm->kernel.vaBase + oSectionHeader.VirtualAddress, pbPage, 0x1000)) {
vmmprintfv_fn("CANNOT READ ntoskrnl.exe CACHEALI SECTION.\n");
goto finish;
}
// 2: Verify SMGLOBALS / _SMKM_STORE_METADATA (pool hdr: 'smSa')
for(i = 0; i < 0x1000 - 0x1c0 - sizeof(QWORD); i += 8) {
vaSmGlobals = ctxVmm->kernel.vaBase + oSectionHeader.VirtualAddress + i;
vaSmsa = *(PQWORD)(pbPage + i);
vaKeyToStoreTree = *(PQWORD)(pbPage + i + 0x1c0);
f = VMM_KADDR64_PAGE(vaKeyToStoreTree) &&
VMM_KADDR64_16(vaSmsa);
if(f) {
ObSet_Push(pObSet, vaSmGlobals);
ObSet_Push(pObSet, vaKeyToStoreTree);
ObSet_Push(pObSet, vaSmsa);
}
}
// 2: Verify SMGLOBALS / _SMKM_STORE_METADATA (pool hdr: 'smSa')
VmmCachePrefetchPages(pObSystemProcess, pObSet, 0);
while(ObSet_Size(pObSet)) {
vaSmsa = ObSet_Pop(pObSet);
vaKeyToStoreTree = ObSet_Pop(pObSet);
vaSmGlobals = ObSet_Pop(pObSet);
VmmReadEx(pObSystemProcess, vaSmsa - 12, (PBYTE)&dwSmsaPoolHdr, sizeof(DWORD), &cbRead, VMM_FLAG_FORCECACHE_READ);
if(dwSmsaPoolHdr == 'aSms') {
MmWin_MemCompress_InitializeOffsets64();
ctx->MemCompress.fValid = TRUE;
ctx->MemCompress.vaSmGlobals = vaSmGlobals;
ctx->MemCompress.vaKeyToStoreTree = vaKeyToStoreTree;
vmmprintfv("Windows 10 Memory Compression Initialize #1 - SmGlobals located at: %16llx Pf: %i \n", ctx->MemCompress.vaSmGlobals, ctx->MemCompress.dwPageFileNumber);
break;
}
}
finish:
LeaveCriticalSection(&ctxVmm->LockMaster);
ctx->MemCompress.fInitialized = TRUE;
Ob_DECREF(pObSystemProcess);
Ob_DECREF(pObSet);
}
VOID MmWin_MemCompress_Initialize()
{
DWORD vaKeyToStoreTree32;
QWORD vaKeyToStoreTree64;
PVMM_PROCESS pObSystemProcess = NULL, pObProcess = NULL;
PMMWIN_CONTEXT ctx = (PMMWIN_CONTEXT)ctxVmm->pMmContext;
if(ctxVmm->kernel.dwVersionMajor < 10) { goto fail; }
MmWin_MemCompress_InitializeVirtualStorePageFileNumber();
// Retrieve MemCompression process PID and vaEPROCESS
while((pObProcess = VmmProcessGetNext(pObProcess, 0))) {
if((pObProcess->dwPPID == 4) && !memcmp("MemCompression", pObProcess->szName, 15)) {
ctx->MemCompress.dwPid = pObProcess->dwPID;
ctx->MemCompress.vaEPROCESS = pObProcess->win.EPROCESS.va;
}
}
// Retrieve SmGlobals address
if(!PDB_GetSymbolAddress(PDB_HANDLE_KERNEL, "SmGlobals", &ctx->MemCompress.vaSmGlobals)) {
if(ctxVmm->tpMemoryModel == VMM_MEMORYMODEL_X64) {
MmWin_MemCompress_Initialize_NoPdb64();
}
goto fail;
}
if(!(pObSystemProcess = VmmProcessGet(4))) { goto fail; }
if(ctxVmm->f32) {
MmWin_MemCompress_InitializeOffsets32();
if(!VmmRead(pObSystemProcess, ctx->MemCompress.vaSmGlobals + 0x0f4, (PBYTE)&vaKeyToStoreTree32, sizeof(DWORD))) { goto fail; }
if(!VMM_KADDR32_PAGE(vaKeyToStoreTree32)) { goto fail; }
ctx->MemCompress.vaKeyToStoreTree = vaKeyToStoreTree32;
} else {
MmWin_MemCompress_InitializeOffsets64();
if(!VmmRead(pObSystemProcess, ctx->MemCompress.vaSmGlobals + 0x1c0, (PBYTE)&vaKeyToStoreTree64, sizeof(QWORD))) { goto fail; }
if(!VMM_KADDR64_PAGE(vaKeyToStoreTree64)) { goto fail; }
ctx->MemCompress.vaKeyToStoreTree = vaKeyToStoreTree64;
}
ctx->MemCompress.fValid = TRUE;
fail:
Ob_DECREF(pObSystemProcess);
}
//-----------------------------------------------------------------------------
// COMPRESSED STORE FUNCTIONALITY BELOW:
//-----------------------------------------------------------------------------
#define COMPRESS_ALGORITHM_INVALID 0
#define COMPRESS_ALGORITHM_NULL 1
#define COMPRESS_ALGORITHM_MSZIP 2
#define COMPRESS_ALGORITHM_XPRESS 3
#define COMPRESS_ALGORITHM_XPRESS_HUFF 4
#define COMPRESS_ALGORITHM_LZMS 5
#define COMPRESS_ALGORITHM_MAX 6
#define COMPRESS_RAW (1 << 29)
typedef struct tdMMWINX64_COMPRESS_CONTEXT {
QWORD fVmmRead;
PVMM_PROCESS pProcess;
PVMM_PROCESS pSystemProcess;
PVMM_PROCESS pProcessMemCompress;
// per page items
struct {
QWORD va;
QWORD PTE;
DWORD dwPageKey;
DWORD iSmkm; // index into 32x32 array in SmGlobals/SMKM_STORE_METADATA
QWORD vaSmkmStore;
QWORD vaEPROCESS;
QWORD vaOwnerEPROCESS;
BYTE pbSmkm[0x2000];
DWORD dwRegionKey;
QWORD vaPageRecord;
QWORD vaRegion;
DWORD cbRegionOffset;
DWORD cbCompressedData;
BYTE pbCompressedData[0x1000];
} e;
} MMWINX64_COMPRESS_CONTEXT, *PMMWINX64_COMPRESS_CONTEXT;
typedef struct td_SMKM_STORE_METADATA32 {
DWORD vaSmkmStore;
DWORD Reserved1[2];
DWORD vaEPROCESS;
DWORD Reserved2;
} _SMKM_STORE_METADATA32;
typedef struct td_SMKM_STORE_METADATA64 {
QWORD vaSmkmStore;
QWORD Reserved1[2];
QWORD vaEPROCESS;
QWORD Reserved2;
} _SMKM_STORE_METADATA64;
typedef struct td_SMHP_CHUNK_METADATA32 {
DWORD avaChunkPtr[32];
QWORD Reserved1;
DWORD dwBitValue;
DWORD dwPageRecordsPerChunkMask;
DWORD dwPageRecordSize;
DWORD Reserved2;
DWORD dwChunkPageHeaderSize;
} _SMHP_CHUNK_METADATA32, *P_SMHP_CHUNK_METADATA32;
typedef struct td_SMHP_CHUNK_METADATA64 {
QWORD avaChunkPtr[32];
QWORD Reserved1;
DWORD dwBitValue;
DWORD dwPageRecordsPerChunkMask;
DWORD dwPageRecordSize;
DWORD Reserved2;
DWORD dwChunkPageHeaderSize;
} _SMHP_CHUNK_METADATA64, *P_SMHP_CHUNK_METADATA64;
typedef struct td_ST_PAGE_RECORD {
DWORD Key;
DWORD CompressedSize;
DWORD NextKey;
} _ST_PAGE_RECORD, *P_ST_PAGE_RECORD;
BOOL MmWin_MemCompress_LogError(_In_ PMMWINX64_COMPRESS_CONTEXT ctx, _In_ LPSTR sz)
{
vmmprintfvv(
"MmWin_CompressedPage: FAIL: %s\n" \
" va= %016llx ep= %016llx pgk=%08x ism=%04x vas=%016llx \n" \
" pte=%016llx oep=%016llx rgk=%08x pid=%04x vat=%016llx \n" \
" pgr=%016llx rgn=%016llx rgo=%08x cbc=%04x rga=%016llx\n",
sz,
ctx->e.va, ctx->e.vaEPROCESS, ctx->e.dwPageKey, ctx->e.iSmkm, ctx->e.vaSmkmStore,
ctx->e.PTE, ctx->e.vaOwnerEPROCESS, ctx->e.dwRegionKey, ctx->pProcess->dwPID, ((PMMWIN_CONTEXT)ctxVmm->pMmContext)->MemCompress.vaKeyToStoreTree,
ctx->e.vaPageRecord, ctx->e.vaRegion, ctx->e.cbRegionOffset, ctx->e.cbCompressedData, (ctx->e.vaRegion + ctx->e.cbRegionOffset)
);
return FALSE;
}
/*
* Retrieve the index of the 32x32 array in SmGlobals/SMKM_STORE_METADATA which
* points to the SmkmStore. The index is retrieved from the KeyToStoreTree BTree
* pointed by SmGlobals/SMKM_STORE_METADATA.
* -- ctx
* -- pwSmkmStoreIndex
* -- return
*/
_Success_(return)
BOOL MmWin_MemCompress1_SmkmStoreIndex(_In_ PMMWINX64_COMPRESS_CONTEXT ctx)
{
DWORD v;
if(!MmWin_BTree_Search(ctx->pSystemProcess, ((PMMWIN_CONTEXT)ctxVmm->pMmContext)->MemCompress.vaKeyToStoreTree, ctx->e.dwPageKey, &v, ctx->fVmmRead)) {
return MmWin_MemCompress_LogError(ctx, "#11 BTreeSearch");
}
if(v & 0x01000000) { return MmWin_MemCompress_LogError(ctx, "#12 InvalidValue"); }
ctx->e.iSmkm = 0x3ff & v;
return TRUE;
}
/*
* Retrieve the virtual address to the SmkmStore and the EPROCESS of the process
* by walking the 32x32 array in SmGlobals.
* -- ctx
* -- return
*/
_Success_(return)
BOOL MmWin_MemCompress2_SmkmStoreMetadata32(_In_ PMMWINX64_COMPRESS_CONTEXT ctx)
{
DWORD va;
_SMKM_STORE_METADATA32 MetaData;
// 1: 1st level fetch virtual address to 2nd level of 32x32 array
if(!VmmRead2(ctx->pSystemProcess, ((PMMWIN_CONTEXT)ctxVmm->pMmContext)->MemCompress.vaSmGlobals + (ctx->e.iSmkm >> 5) * sizeof(DWORD), (PBYTE)&va, sizeof(DWORD), ctx->fVmmRead)) { return MmWin_MemCompress_LogError(ctx, "#21 Read"); }
if(!VMM_KADDR32_8(va)) { return MmWin_MemCompress_LogError(ctx, "#22 NoKADDR"); }
// 2: 2nd fetch values (_SMKM_STORE_METADATA) from 2nd level of 32x32 array.
if(!VmmRead2(ctx->pSystemProcess, va + (ctx->e.iSmkm & 0x1f) * sizeof(_SMKM_STORE_METADATA32), (PBYTE)&MetaData, sizeof(_SMKM_STORE_METADATA32), ctx->fVmmRead)) { return MmWin_MemCompress_LogError(ctx, "#23 Read"); }
if(MetaData.vaEPROCESS && !VMM_KADDR32_8(MetaData.vaEPROCESS)) { return MmWin_MemCompress_LogError(ctx, "#24 NoKADDR"); }
if(!VMM_KADDR32_PAGE(MetaData.vaSmkmStore)) { return MmWin_MemCompress_LogError(ctx, "#25 NoKADDR"); }
ctx->e.vaSmkmStore = MetaData.vaSmkmStore;
ctx->e.vaEPROCESS = MetaData.vaEPROCESS;
return TRUE;
}
_Success_(return)
BOOL MmWin_MemCompress2_SmkmStoreMetadata64(_In_ PMMWINX64_COMPRESS_CONTEXT ctx)
{
QWORD va;
_SMKM_STORE_METADATA64 MetaData;
// 1: 1st level fetch virtual address to 2nd level of 32x32 array
if(!VmmRead2(ctx->pSystemProcess, ((PMMWIN_CONTEXT)ctxVmm->pMmContext)->MemCompress.vaSmGlobals + (ctx->e.iSmkm >> 5) * sizeof(QWORD), (PBYTE)&va, sizeof(QWORD), ctx->fVmmRead)) { return MmWin_MemCompress_LogError(ctx, "#21 Read"); }
if(!VMM_KADDR64_16(va)) { return MmWin_MemCompress_LogError(ctx, "#22 NoKADDR"); }
// 2: 2nd fetch values (_SMKM_STORE_METADATA) from 2nd level of 32x32 array.
if(!VmmRead2(ctx->pSystemProcess, va + (ctx->e.iSmkm & 0x1f) * sizeof(_SMKM_STORE_METADATA64), (PBYTE)&MetaData, sizeof(_SMKM_STORE_METADATA64), ctx->fVmmRead)) { return MmWin_MemCompress_LogError(ctx, "#23 Read"); }
if(MetaData.vaEPROCESS && !VMM_KADDR64_16(MetaData.vaEPROCESS)) { return MmWin_MemCompress_LogError(ctx, "#24 NoKADDR"); }
if(!VMM_KADDR64_PAGE(MetaData.vaSmkmStore)) { return MmWin_MemCompress_LogError(ctx, "#25 NoKADDR"); }
ctx->e.vaSmkmStore = MetaData.vaSmkmStore;
ctx->e.vaEPROCESS = MetaData.vaEPROCESS;
return TRUE;
}
/*
* Retrieve the SmkmStore and the PageRecord.
* -- ctx
* -- return
*/
_Success_(return)
BOOL MmWin_MemCompress3_SmkmStoreAndPageRecord32(_In_ PMMWINX64_COMPRESS_CONTEXT ctx)
{
DWORD vaPageRecordArray;
DWORD i, dwEncodedMetadata, iChunkPtr = 0, iChunkArray, dwPoolHdr = 0;
P_SMHP_CHUNK_METADATA32 pc;
PMMWIN_MEMCOMPRESS_OFFSET po = &((PMMWIN_CONTEXT)ctxVmm->pMmContext)->MemCompress.O;
// 1: Load SmkmStore
if(!VmmRead2(ctx->pSystemProcess, ctx->e.vaSmkmStore, ctx->e.pbSmkm, sizeof(ctx->e.pbSmkm), ctx->fVmmRead)) {
return MmWin_MemCompress_LogError(ctx, "#31 ReadSmkmStore");
}
// 2: Validate
if(!VMM_KADDR32_8(*(PDWORD)(ctx->e.pbSmkm + po->SMKM_STORE.PagesTree))) {
return MmWin_MemCompress_LogError(ctx, "#32 PagesTreePtrNoKADDR");
}
if(COMPRESS_ALGORITHM_XPRESS != *(PWORD)(ctx->e.pbSmkm + po->SMKM_STORE.CompressionAlgorithm)) {
return MmWin_MemCompress_LogError(ctx, "#33 InvalidCompressionAlgorithm");
}
// 3: Get region key
if(!MmWin_BTree_Search(ctx->pSystemProcess, *(PDWORD)(ctx->e.pbSmkm + po->SMKM_STORE.PagesTree), ctx->e.dwPageKey, &ctx->e.dwRegionKey, ctx->fVmmRead)) {
return MmWin_MemCompress_LogError(ctx, "#34 RegionKeyBTreeSearch");
}
// 4: Get page record and calculate:
// - chunk "encoded metadata"
// - index into chunk metadata array (= highest non-zero bit position of encoded_metadata)
// - index into chunk array (pointed to by chunk metadata array)
pc = (P_SMHP_CHUNK_METADATA32)(ctx->e.pbSmkm + po->SMKM_STORE.ChunkMetaData);
dwEncodedMetadata = ctx->e.dwRegionKey >> (pc->dwBitValue & 0xff);
for(i = 0; i < 32; i++) {
if(!(dwEncodedMetadata >> i)) { break; }
iChunkPtr = i;
}
iChunkArray = (1 << iChunkPtr) ^ dwEncodedMetadata;
// 5: Validate and fetch page record address
if(iChunkArray > 0x400) {
return MmWin_MemCompress_LogError(ctx, "#35 ChunkArrayTooLarge");
}
if(!VMM_KADDR32_8(pc->avaChunkPtr[iChunkPtr])) {
return MmWin_MemCompress_LogError(ctx, "#36 ChunkPtrNoKADDR");
}
if(pc->avaChunkPtr[iChunkPtr] & 0xfff) {
if(!VmmRead2(ctx->pSystemProcess, pc->avaChunkPtr[iChunkPtr] - 4, (PBYTE)&dwPoolHdr, 4, ctx->fVmmRead) || (dwPoolHdr != 'ABms')) {
return MmWin_MemCompress_LogError(ctx, "#37 ChunkBadPoolHdr");
}
}
if(!VmmRead2(ctx->pSystemProcess, pc->avaChunkPtr[iChunkPtr] + 0x0cULL * iChunkArray, (PBYTE)&vaPageRecordArray, sizeof(DWORD), ctx->fVmmRead) || !VMM_KADDR32_PAGE(vaPageRecordArray)) {
return MmWin_MemCompress_LogError(ctx, "#38 PageRecordArray");
}
ctx->e.vaPageRecord = (DWORD)((QWORD)vaPageRecordArray + pc->dwChunkPageHeaderSize + ((QWORD)pc->dwPageRecordSize * (ctx->e.dwRegionKey & pc->dwPageRecordsPerChunkMask)));
// 6: Get owner EPROCESS
ctx->e.vaOwnerEPROCESS = *(PDWORD)(ctx->e.pbSmkm + po->SMKM_STORE.OwnerProcess);
if(ctx->e.vaOwnerEPROCESS != ((PMMWIN_CONTEXT)ctxVmm->pMmContext)->MemCompress.vaEPROCESS) {
return MmWin_MemCompress_LogError(ctx, "#39 OwnerEPROCESS");
}
return TRUE;
}
/*
* Retrieve the SmkmStore and the PageRecord.
* -- ctx
* -- return
*/
_Success_(return)
BOOL MmWin_MemCompress3_SmkmStoreAndPageRecord64(_In_ PMMWINX64_COMPRESS_CONTEXT ctx)
{
QWORD vaPageRecordArray;
DWORD i, dwEncodedMetadata, iChunkPtr = 0, iChunkArray, dwPoolHdr = 0;
P_SMHP_CHUNK_METADATA64 pc;
PMMWIN_MEMCOMPRESS_OFFSET po = &((PMMWIN_CONTEXT)ctxVmm->pMmContext)->MemCompress.O;
// 1: Load SmkmStore
if(!VmmRead2(ctx->pSystemProcess, ctx->e.vaSmkmStore, ctx->e.pbSmkm, sizeof(ctx->e.pbSmkm), ctx->fVmmRead)) {
return MmWin_MemCompress_LogError(ctx, "#31 ReadSmkmStore");
}
// 2: Validate
if(!VMM_KADDR64_16(*(PQWORD)(ctx->e.pbSmkm + po->SMKM_STORE.PagesTree))) {
return MmWin_MemCompress_LogError(ctx, "#32 PagesTreePtrNoKADDR");
}
if(COMPRESS_ALGORITHM_XPRESS != *(PWORD)(ctx->e.pbSmkm + po->SMKM_STORE.CompressionAlgorithm)) {
return MmWin_MemCompress_LogError(ctx, "#33 InvalidCompressionAlgorithm");
}
// 3: Get region key
if(!MmWin_BTree_Search(ctx->pSystemProcess, *(PQWORD)(ctx->e.pbSmkm + po->SMKM_STORE.PagesTree), ctx->e.dwPageKey, &ctx->e.dwRegionKey, ctx->fVmmRead)) {
return MmWin_MemCompress_LogError(ctx, "#34 RegionKeyBTreeSearch");
}
// 4: Get page record and calculate:
// - chunk "encoded metadata"
// - index into chunk metadata array (= highest non-zero bit position of encoded_metadata)
// - index into chunk array (pointed to by chunk metadata array)
pc = (P_SMHP_CHUNK_METADATA64)(ctx->e.pbSmkm + po->SMKM_STORE.ChunkMetaData);
dwEncodedMetadata = ctx->e.dwRegionKey >> (pc->dwBitValue & 0xff);
for(i = 0; i < 32; i++) {
if(!(dwEncodedMetadata >> i)) { break; }
iChunkPtr = i;
}
iChunkArray = (1 << iChunkPtr) ^ dwEncodedMetadata;
// 5: Validate and fetch page record address
if(iChunkArray > 0x400) {
return MmWin_MemCompress_LogError(ctx, "#35 ChunkArrayTooLarge");
}
if(!VMM_KADDR64_16(pc->avaChunkPtr[iChunkPtr])) {
return MmWin_MemCompress_LogError(ctx, "#36 ChunkPtrNoKADDR");
}
if(pc->avaChunkPtr[iChunkPtr] & 0xfff) {
if(!VmmRead2(ctx->pSystemProcess, pc->avaChunkPtr[iChunkPtr] - 12, (PBYTE)&dwPoolHdr, 4, ctx->fVmmRead) || (dwPoolHdr != 'ABms')) {
return MmWin_MemCompress_LogError(ctx, "#37 ChunkBadPoolHdr");
}
}
if(!VmmRead2(ctx->pSystemProcess, pc->avaChunkPtr[iChunkPtr] + 0x10ULL * iChunkArray, (PBYTE)&vaPageRecordArray, sizeof(QWORD), ctx->fVmmRead) || !VMM_KADDR64_PAGE(vaPageRecordArray)) {
return MmWin_MemCompress_LogError(ctx, "#38 PageRecordArray");
}
ctx->e.vaPageRecord = (QWORD)(vaPageRecordArray + pc->dwChunkPageHeaderSize + ((QWORD)pc->dwPageRecordSize * (ctx->e.dwRegionKey & pc->dwPageRecordsPerChunkMask)));
// 6: Get owner EPROCESS
ctx->e.vaOwnerEPROCESS = *(PQWORD)(ctx->e.pbSmkm + po->SMKM_STORE.OwnerProcess);
if(ctx->e.vaOwnerEPROCESS != ((PMMWIN_CONTEXT)ctxVmm->pMmContext)->MemCompress.vaEPROCESS) {
return MmWin_MemCompress_LogError(ctx, "#39 OwnerEPROCESS");
}
return TRUE;
}
/*
* Retrieve the region address / data containing the compressed process.
* -- ctx
* -- return
*/
_Success_(return)
BOOL MmWin_MemCompress4_CompressedRegionData(_In_ PMMWINX64_COMPRESS_CONTEXT ctx)
{
QWORD vaRegionPtr = 0;
DWORD dwRegionIndexMask, dwRegionIndex;
_ST_PAGE_RECORD PageRecord;
PMMWIN_MEMCOMPRESS_OFFSET po = &((PMMWIN_CONTEXT)ctxVmm->pMmContext)->MemCompress.O;
// 1: Read page record
if(!VmmRead2(ctx->pSystemProcess, ctx->e.vaPageRecord, (PBYTE)&PageRecord, sizeof(PageRecord), ctx->fVmmRead)) {
return MmWin_MemCompress_LogError(ctx, "#41 ReadPageRecord");
}
if(PageRecord.Key == 0xffffffff) {
// TODO: implement support
return MmWin_MemCompress_LogError(ctx, "#42 InvalidPageRecord");
}
ctx->e.cbCompressedData = (PageRecord.CompressedSize == 0x1000) ? 0x1000 : PageRecord.CompressedSize & 0xfff;
if(ctxVmm->f32) {
// 2: Get pointer to region (32-bit)
dwRegionIndexMask = *(PDWORD)(ctx->e.pbSmkm + po->SMKM_STORE.RegionIndexMask) & 0xff;
dwRegionIndex = PageRecord.Key >> dwRegionIndexMask;
vaRegionPtr = *(PDWORD)(ctx->e.pbSmkm + po->SMKM_STORE.CompressedRegionPtrArray) + dwRegionIndex * sizeof(DWORD);
// 3: Get region and offset (32-bit)
if(!VmmRead2(ctx->pSystemProcess, vaRegionPtr, (PBYTE)&ctx->e.vaRegion, sizeof(DWORD), ctx->fVmmRead)) {
return MmWin_MemCompress_LogError(ctx, "#43 ReadRegionVA");
}
if(!ctx->e.vaRegion || (ctx->e.vaRegion & 0x8000ffff)) {
return MmWin_MemCompress_LogError(ctx, "#44 InvalidRegionVA");
}
} else {
// 2: Get pointer to region (64-bit)
dwRegionIndexMask = *(PDWORD)(ctx->e.pbSmkm + po->SMKM_STORE.RegionIndexMask) & 0xff;
dwRegionIndex = PageRecord.Key >> dwRegionIndexMask;
vaRegionPtr = *(PQWORD)(ctx->e.pbSmkm + po->SMKM_STORE.CompressedRegionPtrArray) + dwRegionIndex * sizeof(QWORD);
// 3: Get region and offset (64-bit)
if(!VmmRead2(ctx->pSystemProcess, vaRegionPtr, (PBYTE)&ctx->e.vaRegion, sizeof(QWORD), ctx->fVmmRead)) {
return MmWin_MemCompress_LogError(ctx, "#45 ReadRegionVA");
}
if(!ctx->e.vaRegion || (ctx->e.vaRegion & 0xffff8000'0000ffff)) {
return MmWin_MemCompress_LogError(ctx, "#46 InvalidRegionVA");
}
}
ctx->e.cbRegionOffset = (PageRecord.Key & *(PDWORD)(ctx->e.pbSmkm + po->SMKM_STORE.RegionSizeMask)) << 4;
return TRUE;
}
/*
* Decompress a compressed page
* -- ctx
* -- pbDecompressedPage
* -- return
*/
_Success_(return)
BOOL MmWin_MemCompress5_DecompressPage(_In_ PMMWINX64_COMPRESS_CONTEXT ctx, _Out_writes_(4096) PBYTE pbDecompressedPage)
{
DWORD cbDecompressed;
// 1: Read compressed data
if(!VmmRead2(ctx->pProcessMemCompress, ctx->e.vaRegion + ctx->e.cbRegionOffset, ctx->e.pbCompressedData, ctx->e.cbCompressedData, ctx->fVmmRead)) {
MmWin_MemCompress_LogError(ctx, "#51 Read");
return FALSE;
}
// 2: Decompress data
if(ctx->e.cbCompressedData == 0x1000) {
memcpy(pbDecompressedPage, ctx->e.pbCompressedData, 0x1000);
} else {
if((VMM_STATUS_SUCCESS != ctxVmm->fn.RtlDecompressBuffer(COMPRESS_ALGORITHM_XPRESS, pbDecompressedPage, 0x1000, ctx->e.pbCompressedData, ctx->e.cbCompressedData, &cbDecompressed)) || (cbDecompressed != 0x1000)) {
return MmWin_MemCompress_LogError(ctx, "#52 Decompress");
}
}
return TRUE;
}
/*
* Decompress a page.
* -- pProcess
* -- va
* -- pte
* -- pbPage
* -- fVmmRead = flags to VmmRead function calls.
* -- return
*/
_Success_(return)
BOOL MmWin_MemCompress(_In_ PVMM_PROCESS pProcess, _In_opt_ QWORD va, _In_ QWORD pte, _Out_writes_(4096) PBYTE pbPage, _In_ QWORD fVmmRead)
{
BOOL fResult = FALSE;
PMMWINX64_COMPRESS_CONTEXT ctx = NULL;
PVMM_PROCESS pObSystemProcess = NULL, pObMemCompressProcess = NULL;
QWORD tm = Statistics_CallStart();
if(!(ctx = LocalAlloc(LMEM_ZEROINIT, sizeof(MMWINX64_COMPRESS_CONTEXT)))) { goto fail; }
ctx->fVmmRead = fVmmRead;
ctx->e.va = va;
ctx->e.PTE = pte;
if(ctxVmm->f32) {
// 32-bit system
ctx->e.dwPageKey = MMWINX86PAE_PTE_PAGE_KEY_COMPRESSED(pte);
fResult =
(ctx->pProcess = pProcess) &&
(ctx->pSystemProcess = pObSystemProcess = VmmProcessGet(4)) &&
(ctx->pProcessMemCompress = pObMemCompressProcess = VmmProcessGet(((PMMWIN_CONTEXT)ctxVmm->pMmContext)->MemCompress.dwPid)) &&
MmWin_MemCompress1_SmkmStoreIndex(ctx) &&
MmWin_MemCompress2_SmkmStoreMetadata32(ctx) &&
MmWin_MemCompress3_SmkmStoreAndPageRecord32(ctx) &&
MmWin_MemCompress4_CompressedRegionData(ctx) &&
MmWin_MemCompress5_DecompressPage(ctx, pbPage);
} else {
// 64-bit system
ctx->e.dwPageKey = MMWINX64_PTE_PAGE_KEY_COMPRESSED(pte);
fResult =
(ctx->pProcess = pProcess) &&
(ctx->pSystemProcess = pObSystemProcess = VmmProcessGet(4)) &&
(ctx->pProcessMemCompress = pObMemCompressProcess = VmmProcessGet(((PMMWIN_CONTEXT)ctxVmm->pMmContext)->MemCompress.dwPid)) &&
MmWin_MemCompress1_SmkmStoreIndex(ctx) &&
MmWin_MemCompress2_SmkmStoreMetadata64(ctx) &&
MmWin_MemCompress3_SmkmStoreAndPageRecord64(ctx) &&
MmWin_MemCompress4_CompressedRegionData(ctx) &&
MmWin_MemCompress5_DecompressPage(ctx, pbPage);
}
fail:
LocalFree(ctx);
Ob_DECREF(pObSystemProcess);
Ob_DECREF(pObMemCompressProcess);
Statistics_CallEnd(STATISTICS_ID_VMM_PagedCompressedMemory, tm);
return fResult;
}