关于定位随机化的pte base

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
LONG64 MmGetVirtalPteBase()
{
LONG64 pte_base = 0;
PHYSICAL_ADDRESS cr3_pa = { __readcr3() & 0xfffffffffffffff0 };
PTE* page_directory_va = static_cast<PTE*>(MmGetVirtualForPhysical(cr3_pa));

if (page_directory_va)
{
for (auto index = 0; index < 512; index++)
{
if (page_directory_va[index].page_frame == (cr3_pa.QuadPart >> 12))
{
pte_base = (index + 0x1FFFE00i64) << 39;

break;
}
}
}
return pte_base;
}

本质上是利用了页表自映射机制,通过找到指向当前cr3的index,有如下规律 index : 512 == ptebase_va : 0xffffffffffff,代入计算即可得pte base。pxe base 等同理。