forked from cheat-engine/cheat-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memscan.c
1319 lines (1067 loc) · 30.2 KB
/
memscan.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
#pragma warning( disable: 4100 4103 4146 4213)
#include "ntifs.h"
#include <windef.h>
#ifdef CETC
#include "tdiwrapper.h"
#include "kfiles.h"
#endif
#include "memscan.h"
#include "DBKFunc.h"
#include "vmxhelper.h"
#include "vmxoffload.h" //PTE structs
#include "noexceptions.h"
/*#include "deepkernel.h"
*/
UINT_PTR KnownPageTableBase = 0;
void VirtualAddressToIndexes(QWORD address, int *pml4index, int *pagedirptrindex, int *pagedirindex, int *pagetableindex)
/*
* Returns the indexes for the given address (ia32e)
*/
{
if (PTESize == 8)
{
*pml4index = (address >> 39) & 0x1ff;
*pagedirptrindex = (address >> 30) & 0x1ff;
*pagedirindex = (address >> 21) & 0x1ff;
*pagetableindex = (address >> 12) & 0x1ff;
}
else
{
*pml4index = 0;
*pagedirptrindex = 0;
*pagedirindex = (address >> 22) & 0x3ff;
*pagetableindex = (address >> 12) & 0x3ff;
}
}
QWORD IndexesToVirtualAddress(int pml4index, int pagedirptrindex, int pagedirindex, int pagetableindex, int offset)
{
QWORD r;
#ifndef AMD64
if (PTESize == 8)
{
#endif
r = ((QWORD)pml4index & 0x1ff) << 39;
if (pml4index >= 256)
r = r | 0xFFFF000000000000ULL;
r |= ((QWORD)pagedirptrindex & 0x1ff) << 30;
r |= ((QWORD)pagedirindex & 0x1ff) << 21;
r |= ((QWORD)pagetableindex & 0x1ff) << 12;
r |= offset & 0xfff;
#ifndef AMD64
}
else
{
r |= (pagedirindex & 0x3ff) << 22;
r |= (pagetableindex & 0x3ff) << 12;
r |= offset & 0xfff;
}
#endif
return r;
}
#ifndef AMD64
void VirtualAddressToPageEntries32(DWORD address, PPDE *pagedirentry, PPTE *pagetableentry)
{
DWORD PTE = address;
UINT_PTR PTB = (DWORD)getPageTableBase();
PTE = PTE >> 12;
PTE = PTE * 4;
PTE = PTE + PTB;
*pagetableentry = (PPTE)PTE;
UINT_PTR PDE = PTE;
PDE = PDE & 0x0000ffffffffffffULL;
PDE = PDE >> 12;
PDE = PDE * 4;
PDE = PDE + PTB;
*pagedirentry = (PPDE)PDE;
}
#endif
void VirtualAddressToPageEntries64(QWORD address, PPDPTE_PAE *pml4entry, PPDPTE_PAE *pagedirpointerentry, PPDE_PAE *pagedirentry, PPTE_PAE *pagetableentry)
{
QWORD PTE = address;
QWORD PTB=getPageTableBase();
PTE = PTE & 0x0000ffffffffffffULL;
PTE = PTE >> 12;
PTE = PTE * 8;
PTE = PTE + PTB;
*pagetableentry = (PPTE_PAE)PTE;
//*pagetableentry = (PPTE_PAE)((((QWORD)address & 0x0000ffffffffffffull) >> 12)*8) + 0xfffff80000000000ULL;
QWORD PDE = PTE;
PDE = PDE & 0x0000ffffffffffffULL;
PDE = PDE >> 12;
PDE = PDE * 8;
PDE = PDE + PTB;
*pagedirentry = (PPDE_PAE)PDE;
//*pagedirentry = (PPDE_PAE)((((QWORD)*pagetableentry & 0x0000ffffffffffffull )>> 12)*8) + 0xfffff80000000000ULL;
QWORD PDPTR = PDE;
PDPTR = PDPTR & 0x0000ffffffffffffULL;
PDPTR = PDPTR >> 12;
PDPTR = PDPTR * 8;
PDPTR = PDPTR + PTB;
*pagedirpointerentry = (PPDPTE_PAE)PDPTR;
//*pagedirpointerentry = (PPDPTE_PAE)((((QWORD)*pagedirentry & 0x0000ffffffffffffull )>> 12)*8) + 0xfffff80000000000ULL;
#ifdef AMD64
QWORD PML4 = PDPTR;
PML4 = PML4 & 0x0000ffffffffffffULL;
PML4 = PML4 >> 12;
PML4 = PML4 * 8;
PML4 = PML4 + PTB;
*pml4entry = (PPDPTE_PAE)PML4;
#else
*pml4entry = NULL;
#endif
}
BOOLEAN IsAddressSafe(UINT_PTR StartAddress)
{
#ifdef AMD64
//cannonical check. Bits 48 to 63 must match bit 47
UINT_PTR toppart=(StartAddress >> 47);
if (toppart & 1)
{
//toppart must be 0x1ffff
if (toppart != 0x1ffff)
return FALSE;
}
else
{
//toppart must be 0
if (toppart != 0)
return FALSE;
}
#endif
//return TRUE;
if (loadedbydbvm)
{
BYTE x=0;
UINT_PTR lasterror;
disableInterrupts();
vmx_disable_dataPageFaults();
x=*(volatile BYTE *)StartAddress;
vmx_enable_dataPageFaults();
lasterror=vmx_getLastSkippedPageFault();
enableInterrupts();
DbgPrint("IsAddressSafe dbvm-mode: lastError=%p\n", lasterror);
if (lasterror) return FALSE;
}
{
#ifdef AMD64
UINT_PTR kernelbase=0x7fffffffffffffffULL;
if (StartAddress<kernelbase)
return TRUE;
else
{
PHYSICAL_ADDRESS physical;
physical.QuadPart=0;
physical=MmGetPhysicalAddress((PVOID)StartAddress);
return (physical.QuadPart!=0);
}
#else
/* MDL x;
MmProbeAndLockPages(&x,KernelMode,IoModifyAccess);
MmUnlockPages(&x);
*/
ULONG kernelbase=0x7ffe0000;
if ((!HiddenDriver) && (StartAddress<kernelbase))
return TRUE;
{
UINT_PTR PTE,PDE;
struct PTEStruct *x;
/*
PHYSICAL_ADDRESS physical;
physical=MmGetPhysicalAddress((PVOID)StartAddress);
return (physical.QuadPart!=0);*/
PTE=(UINT_PTR)StartAddress;
PTE=PTE/0x1000*PTESize+0xc0000000;
//now check if the address in PTE is valid by checking the page table directory at 0xc0300000 (same location as CR3 btw)
PDE=PTE/0x1000*PTESize+0xc0000000; //same formula
x=(PVOID)PDE;
if ((x->P==0) && (x->A2==0))
{
//Not present or paged, and since paging in this area isn't such a smart thing to do just skip it
//perhaps this is only for the 4 mb pages, but those should never be paged out, so it should be 1
//bah, I've got no idea what this is used for
return FALSE;
}
if (x->PS==1)
{
//This is a 4 MB page (no pte list)
//so, (startaddress/0x400000*0x400000) till ((startaddress/0x400000*0x400000)+(0x400000-1) ) ) is specified by this page
}
else //if it's not a 4 MB page then check the PTE
{
//still here so the page table directory agreed that it is a usable page table entry
x=(PVOID)PTE;
if ((x->P==0) && (x->A2==0))
return FALSE; //see for explenation the part of the PDE
}
return TRUE;
}
#endif
}
}
UINT_PTR getPEThread(UINT_PTR threadid)
{
//UINT_PTR *threadid;
PETHREAD selectedthread;
UINT_PTR result=0;
if (PsLookupThreadByThreadId((PVOID)(UINT_PTR)threadid,&selectedthread)==STATUS_SUCCESS)
{
result=(UINT_PTR)selectedthread;
ObDereferenceObject(selectedthread);
}
return result;
}
BOOLEAN WriteProcessMemory(DWORD PID,PEPROCESS PEProcess,PVOID Address,DWORD Size, PVOID Buffer)
{
PEPROCESS selectedprocess=PEProcess;
KAPC_STATE apc_state;
NTSTATUS ntStatus=STATUS_UNSUCCESSFUL;
if (selectedprocess==NULL)
{
//DbgPrint("WriteProcessMemory:Getting PEPROCESS\n");
if (!NT_SUCCESS(PsLookupProcessByProcessId((PVOID)(UINT_PTR)PID,&selectedprocess)))
return FALSE; //couldn't get the PID
//DbgPrint("Retrieved peprocess");
}
//selectedprocess now holds a valid peprocess value
__try
{
RtlZeroMemory(&apc_state,sizeof(apc_state));
KeAttachProcess((PEPROCESS)selectedprocess);
__try
{
char* target;
char* source;
unsigned int i;
//DbgPrint("Checking safety of memory\n");
if ((IsAddressSafe((UINT_PTR)Address)) && (IsAddressSafe((UINT_PTR)Address+Size-1)))
{
//still here, then I gues it's safe to read. (But I can't be 100% sure though, it's still the users problem if he accesses memory that doesn't exist)
BOOL disabledWP = FALSE;
target=Address;
source=Buffer;
if ((loadedbydbvm) || (KernelWritesIgnoreWP)) //add a extra security around it as the PF will not be handled
{
disableInterrupts();
if (loadedbydbvm)
vmx_disable_dataPageFaults();
if (KernelWritesIgnoreWP)
{
DbgPrint("Disabling CR0.WP");
setCR0(getCR0() & (~(1 << 16))); //disable the WP bit
disabledWP = TRUE;
}
}
if ((loadedbydbvm) || ((UINT_PTR)target < 0x8000000000000000ULL))
{
RtlCopyMemory(target, source, Size);
ntStatus = STATUS_SUCCESS;
}
else
{
i = NoExceptions_CopyMemory(target, source, Size);
if (i != (int)Size)
ntStatus = STATUS_UNSUCCESSFUL;
else
ntStatus = STATUS_SUCCESS;
}
if ((loadedbydbvm) || (disabledWP))
{
UINT_PTR lastError=0;
if (disabledWP)
{
setCR0(getCR0() | (1 << 16));
DbgPrint("Enabled CR0.WP");
}
if (loadedbydbvm)
{
lastError = vmx_getLastSkippedPageFault();
vmx_enable_dataPageFaults();
}
enableInterrupts();
DbgPrint("lastError=%p\n", lastError);
if (lastError)
ntStatus=STATUS_UNSUCCESSFUL;
}
}
}
__finally
{
KeDetachProcess();
}
}
__except(1)
{
//DbgPrint("Error while writing\n");
ntStatus = STATUS_UNSUCCESSFUL;
}
if (PEProcess==NULL) //no valid peprocess was given so I made a reference, so lets also dereference
ObDereferenceObject(selectedprocess);
return NT_SUCCESS(ntStatus);
}
BOOLEAN ReadProcessMemory(DWORD PID,PEPROCESS PEProcess,PVOID Address,DWORD Size, PVOID Buffer)
{
PEPROCESS selectedprocess=PEProcess;
//KAPC_STATE apc_state;
NTSTATUS ntStatus=STATUS_UNSUCCESSFUL;
if (PEProcess==NULL)
{
if (!NT_SUCCESS(PsLookupProcessByProcessId((PVOID)(UINT_PTR)PID,&selectedprocess)))
return FALSE; //couldn't get the PID
}
//selectedprocess now holds a valid peprocess value
__try
{
KeAttachProcess((PEPROCESS)selectedprocess);
__try
{
char* target;
char* source;
int i;
if ((IsAddressSafe((UINT_PTR)Address)) && (IsAddressSafe((UINT_PTR)Address+Size-1)))
{
target=Buffer;
source=Address;
if (loadedbydbvm) //add a extra security around it
{
disableInterrupts();
vmx_disable_dataPageFaults();
}
if ((loadedbydbvm) || ((UINT_PTR)source < 0x8000000000000000ULL))
{
RtlCopyMemory(target, source, Size);
ntStatus = STATUS_SUCCESS;
}
else
{
i=NoExceptions_CopyMemory(target, source, Size);
if (i != (int)Size)
ntStatus = STATUS_UNSUCCESSFUL;
else
ntStatus = STATUS_SUCCESS;
}
if (loadedbydbvm)
{
UINT_PTR lastError;
lastError=vmx_getLastSkippedPageFault();
vmx_enable_dataPageFaults();
enableInterrupts();
DbgPrint("lastError=%p\n", lastError);
if (lastError)
ntStatus=STATUS_UNSUCCESSFUL;
}
}
}
__finally
{
KeDetachProcess();
}
}
__except(1)
{
//DbgPrint("Error while reading: ReadProcessMemory(%x,%p, %p, %d, %p\n", PID, PEProcess, Address, Size, Buffer);
ntStatus = STATUS_UNSUCCESSFUL;
}
if (PEProcess==NULL) //no valid peprocess was given so I made a reference, so lets also dereference
ObDereferenceObject(selectedprocess);
return NT_SUCCESS(ntStatus);
}
UINT64 maxPhysAddress = 0;
UINT64 getMaxPhysAddress(void)
{
if (maxPhysAddress==0)
{
int physicalbits;
DWORD r[4];
__cpuid(r, 0x80000008);
//get max physical address
physicalbits = r[0] & 0xff;
maxPhysAddress = 0xFFFFFFFFFFFFFFFFULL;
maxPhysAddress = maxPhysAddress >> physicalbits; //if physicalbits==36 then maxPhysAddress=0x000000000fffffff
maxPhysAddress = ~(maxPhysAddress << physicalbits); //<< 36 = 0xfffffff000000000 . after inverse : 0x0000000fffffffff
}
return maxPhysAddress;
}
NTSTATUS ReadPhysicalMemory(char *startaddress, UINT_PTR bytestoread, void *output)
{
HANDLE physmem;
UNICODE_STRING physmemString;
OBJECT_ATTRIBUTES attributes;
WCHAR physmemName[] = L"\\device\\physicalmemory";
UCHAR* memoryview;
NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
PMDL outputMDL;
DbgPrint("ReadPhysicalMemory(%p, %d, %p)", startaddress, bytestoread, output);
if (((UINT64)startaddress > getMaxPhysAddress()) || ((UINT64)startaddress + bytestoread > getMaxPhysAddress()))
{
DbgPrint("Invalid physical address\n");
return ntStatus;
}
outputMDL = IoAllocateMdl(output, (ULONG)bytestoread, FALSE, FALSE, NULL);
__try
{
MmProbeAndLockPages(outputMDL, KernelMode, IoWriteAccess);
}
__except (1)
{
IoFreeMdl(outputMDL);
return STATUS_UNSUCCESSFUL;
}
__try
{
RtlInitUnicodeString( &physmemString, physmemName );
InitializeObjectAttributes( &attributes, &physmemString, OBJ_CASE_INSENSITIVE, NULL, NULL );
ntStatus=ZwOpenSection( &physmem, SECTION_ALL_ACCESS, &attributes );
if (ntStatus==STATUS_SUCCESS)
{
//hey look, it didn't kill it
SIZE_T length;
PHYSICAL_ADDRESS viewBase;
UINT_PTR offset;
UINT_PTR toread;
viewBase.QuadPart = (ULONGLONG)(startaddress);
length=0x2000;//pinp->bytestoread; //in case of a overlapping region
toread=bytestoread;
memoryview=NULL;
DbgPrint("ReadPhysicalMemory:viewBase.QuadPart=%x", viewBase.QuadPart);
ntStatus=ZwMapViewOfSection(
physmem, //sectionhandle
NtCurrentProcess(), //processhandle (should be -1)
&memoryview, //BaseAddress
0L, //ZeroBits
length, //CommitSize
&viewBase, //SectionOffset
&length, //ViewSize
ViewShare,
0,
PAGE_READWRITE);
if ((ntStatus == STATUS_SUCCESS) && (memoryview!=NULL))
{
if (toread > length)
toread = length;
if (toread)
{
__try
{
offset = (UINT_PTR)(startaddress)-(UINT_PTR)viewBase.QuadPart;
if (offset + toread > length)
{
DbgPrint("Too small map");
}
else
{
RtlCopyMemory(output, &memoryview[offset], toread);
}
ZwUnmapViewOfSection(NtCurrentProcess(), memoryview);
}
__except (1)
{
DbgPrint("Failure mapping physical memory");
}
}
}
else
{
DbgPrint("ReadPhysicalMemory error:ntStatus=%x", ntStatus);
}
ZwClose(physmem);
};
}
__except(1)
{
DbgPrint("Error while reading physical memory\n");
}
MmUnlockPages(outputMDL);
IoFreeMdl(outputMDL);
return ntStatus;
}
UINT_PTR SignExtend(UINT_PTR a)
{
#ifdef AMD64
if ((a >> 47)==1)
return a | 0xFFFF000000000000ULL; //add sign extended bits
else
return a;
#else
return a;
#endif
}
UINT_PTR getPageTableBase()
{
#if (NTDDI_VERSION >= NTDDI_VISTA)
if (KnownPageTableBase==0)
{
RTL_OSVERSIONINFOW v;
v.dwOSVersionInfoSize = sizeof(v);
if (RtlGetVersion(&v))
{
DbgPrint("RtlGetVersion failed");
return 0;
}
if ((v.dwMajorVersion >= 10) && (v.dwBuildNumber >= 14393))
{
PHYSICAL_ADDRESS a;
PVOID r;
a.QuadPart = getCR3() & 0xFFFFFFFFFFFFF000ULL;
r = MmGetVirtualForPhysical(a); //if this stops working, look for CR3 in the pml4 table
KnownPageTableBase = ((UINT_PTR)r) & 0xFFFFFF8000000000ULL;
MAX_PTE_POS = (UINT_PTR)((QWORD)KnownPageTableBase + 0x7FFFFFFFF8ULL);
MAX_PDE_POS = (UINT_PTR)((QWORD)KnownPageTableBase + 0x7B7FFFFFF8ULL);
}
else
KnownPageTableBase=PAGETABLEBASE;
DbgPrint("PageTableBase at %p\n", KnownPageTableBase);
}
return KnownPageTableBase;
#else
return PAGETABLEBASE;
#endif
}
typedef void PRESENTPAGECALLBACK(UINT_PTR StartAddress, UINT_PTR EndAddress, struct PTEStruct *pageEntry);
BOOL walkPagingLayout(PEPROCESS PEProcess, UINT_PTR MaxAddress, PRESENTPAGECALLBACK OnPresentPage)
{
#ifdef AMD64
UINT_PTR pagebase = getPageTableBase();
#else
UINT_PTR pagebase = PAGETABLEBASE;
#endif
if (pagebase == 0)
return FALSE;
if (OnPresentPage == NULL)
return FALSE;
__try
{
KeAttachProcess((PEPROCESS)PEProcess);
__try
{
UINT_PTR currentAddress = 0; //start from address 0
UINT_PTR lastAddress = 0;
struct PTEStruct *PPTE, *PPDE, *PPDPE, *PPML4E;
while ((currentAddress < MaxAddress) && (lastAddress<=currentAddress) )
{
//DbgPrint("currentAddress=%p\n", currentAddress);
lastAddress = currentAddress;
(UINT_PTR)PPTE = (UINT_PTR)(((currentAddress & 0xFFFFFFFFFFFFULL) >> 12) *PTESize + pagebase);
(UINT_PTR)PPDE = (UINT_PTR)((((UINT_PTR)PPTE) & 0xFFFFFFFFFFFFULL) >> 12) *PTESize + pagebase;
(UINT_PTR)PPDPE = (UINT_PTR)((((UINT_PTR)PPDE) & 0xFFFFFFFFFFFFULL) >> 12) *PTESize + pagebase;
(UINT_PTR)PPML4E = (UINT_PTR)((((UINT_PTR)PPDPE) & 0xFFFFFFFFFFFFULL) >> 12) *PTESize + pagebase;
if (PTESize == 8)
(UINT_PTR)PPDPE = ((((UINT_PTR)PPDE) & 0xFFFFFFFFFFFFULL) >> 12) *PTESize + pagebase;
else
(UINT_PTR)PPDPE = 0;
#ifdef AMD64
(UINT_PTR)PPML4E = ((((UINT_PTR)PPDPE) & 0xFFFFFFFFFFFFULL) >> 12) *PTESize + pagebase;
#else
(UINT_PTR)PPML4E = 0;
#endif
#ifdef AMD64
if ((PPML4E) && (PPML4E->P == 0))
{
currentAddress &= 0xffffff8000000000ULL;
currentAddress += 0x8000000000ULL;
continue;
}
#endif
if ((PPDPE) && (PPDPE->P == 0))
{
currentAddress &= 0xffffffffc0000000ULL;
currentAddress += 0x40000000;
continue;
}
if (PPDE->P == 0)
{
if (PAGE_SIZE_LARGE == 0x200000)
currentAddress &= 0xffffffffffe00000ULL;
else
currentAddress &= 0xffffffffffc00000ULL;
currentAddress += PAGE_SIZE_LARGE;
continue;
}
if (PPDE->PS)
{
OnPresentPage(currentAddress, currentAddress + PAGE_SIZE_LARGE-1, PPDE);
currentAddress += PAGE_SIZE_LARGE;
continue;
}
if (PPTE->P == 0)
{
currentAddress &= 0xfffffffffffff000ULL;
currentAddress += 0x1000;
continue;
}
OnPresentPage(currentAddress, currentAddress + 0xfff, PPTE);
currentAddress += 0x1000;
}
}
__finally
{
KeDetachProcess();
}
}
__except (1)
{
DbgPrint("Excepion while walking the paging layout\n");
return FALSE;
}
return TRUE;
}
PPENTRY AccessedList = NULL;
int AccessedListSize;
void CleanAccessedList()
{
PPENTRY e = AccessedList;
PPENTRY previous;
//DbgPrint("Cleaning list");
while (e)
{
previous = e;
e = e->Next;
ExFreePool(previous);
}
AccessedList = NULL;
AccessedListSize = 0;
}
void StoreAccessedRanges(UINT_PTR StartAddress, UINT_PTR EndAddress, struct PTEStruct *pageEntry)
{
if (pageEntry->A)
{
if ((AccessedList) && (AccessedList->Range.EndAddress == StartAddress - 1)) //group
AccessedList->Range.EndAddress = EndAddress;
else
{
//insert
PPENTRY e;
e = ExAllocatePoolWithTag(PagedPool, sizeof(PENTRY), 0);
e->Range.StartAddress = StartAddress;
e->Range.EndAddress = EndAddress;
e->Next = AccessedList;
AccessedList = e;
AccessedListSize++;
}
}
}
int enumAllAccessedPages(PEPROCESS PEProcess)
{
#ifdef AMD64
UINT_PTR MaxAddress = 0x80000000000ULL;
#else
UINT_PTR MaxAddress = 0x80000000;
#endif
CleanAccessedList();
if (walkPagingLayout(PEProcess, MaxAddress, StoreAccessedRanges))
{
//DbgPrint("AccessedListSize=%d\n", AccessedListSize);
return AccessedListSize*sizeof(PRANGE);
}
else
return 0;
}
int getAccessedPageList(PPRANGE List, int ListSizeInBytes)
{
PPENTRY e = AccessedList;
int maxcount = ListSizeInBytes / sizeof(PRANGE);
int i = 0;
// DbgPrint("getAccessedPageList\n");
while (e)
{
if (i >= maxcount)
{
//DbgPrint("%d>=%d", i, maxcount);
break;
}
//DbgPrint("i=%d (%p -> %p)\n", i, e->Range.StartAddress, e->Range.EndAddress);
List[i] = e->Range;
e = e->Next;
i++;
}
CleanAccessedList();
return i*sizeof(PRANGE);
}
void MarkPageAsNotAccessed(UINT_PTR StartAddress, UINT_PTR EndAddress, struct PTEStruct *pageEntry)
{
pageEntry->A = 0;
}
NTSTATUS markAllPagesAsNeverAccessed(PEPROCESS PEProcess)
{
#ifdef AMD64
UINT_PTR MaxAddress = 0x80000000000ULL;
#else
UINT_PTR MaxAddress = 0x80000000;
#endif
if (walkPagingLayout(PEProcess, MaxAddress, MarkPageAsNotAccessed))
return STATUS_SUCCESS;
else
return STATUS_UNSUCCESSFUL;
}
UINT_PTR FindFirstDifferentAddress(QWORD address, DWORD *protection)
//scans the pagetable system for the first fully present entry
//returns 0 when there is no other present address
{
int i,ri;
int pml4index, pagedirpointerindex, pagedirindex, pagetableindex;
VirtualAddressToIndexes(address, &pml4index, &pagedirpointerindex, &pagedirindex, &pagetableindex);
#ifndef AMD64
if (PTESize == 8)
#endif
{
PPDPTE_PAE pml4entry, pagedirpointerentry;
PPDE_PAE pagedirentry;
PPTE_PAE pagetableentry;
*protection = 0;
//scan till present or end of memory
while (1)
{
VirtualAddressToPageEntries64(address, &pml4entry, &pagedirpointerentry, &pagedirentry, &pagetableentry);
VirtualAddressToIndexes(address, &pml4index, &pagedirpointerindex, &pagedirindex, &pagetableindex);
if (*protection == 0)
{
//get the initial protection
if ((((pml4entry) && (pml4entry->P)) || (pml4entry == NULL)) && (pagedirpointerentry->P) && (pagedirentry->P) && ((pagedirentry->PS) || (pagetableentry->P)))
{
if (pagedirentry->PS)
{
if (pagedirentry->RW)
*protection = PAGE_EXECUTE_READWRITE;
else
*protection = PAGE_EXECUTE_READ;
}
else
{
if (pagetableentry->RW)
*protection = PAGE_EXECUTE_READWRITE;
else
*protection = PAGE_EXECUTE_READ;
}
}
else
*protection = PAGE_NOACCESS;
}
#ifdef AMD64
if (pml4entry->P == 0)
{
//unreadable at PML4 level
if (*protection != PAGE_NOACCESS)
return address;
pagedirpointerindex = 0;
pagedirindex = 0;
pagetableindex = 0;
for (ri=1, i = pml4index + 1; i < 512; i++, ri++)
{
if ((ri >= 512) || (ri < 0))
DbgBreakPointWithStatus(ri);
if (pml4entry[ri].P)
{
//found a valid PML4 entry
//scan for a valid pagedirpointerentry
pml4index = i;
address = IndexesToVirtualAddress(pml4index, pagedirpointerindex, pagedirindex, pagetableindex, 0);
VirtualAddressToPageEntries64(address, &pml4entry, &pagedirpointerentry, &pagedirentry, &pagetableentry);
break;
}
}
}
if (pml4entry->P == 0)
{
//nothing readable found
return 0;
}
#endif
if (pagedirpointerentry->P == 0)
{
if (*protection != PAGE_NOACCESS)
return address;
pagedirindex = 0;
pagetableindex = 0;
for (ri=1, i = pagedirpointerindex + 1; i < 512; i++, ri++)
{
if ((ri >= 512) || (ri < 0))
DbgBreakPointWithStatus(ri);
if (pagedirpointerentry[ri].P)
{
//found a valid pagedirpointerentry
pagedirpointerindex = i;
address = IndexesToVirtualAddress(pml4index, pagedirpointerindex, pagedirindex, pagetableindex, 0);
VirtualAddressToPageEntries64(address, &pml4entry, &pagedirpointerentry, &pagedirentry, &pagetableentry);
break;
}
}
if (pagedirpointerentry->P == 0)
{
#ifdef AMD64
pagedirpointerindex = 0;
pml4index++;