[C++] 纯文本查看 复制代码
#include <ntifs.h>
HANDLE GetPhysicalHandle()
{
static HANDLE hMemory = NULL;
if (hMemory) return hMemory;
UNICODE_STRING PhysicalMemoryString = { 0 };
WCHAR PhysicalMemoryName[] = L"\\Device\\PhysicalMemory";
RtlInitUnicodeString(&PhysicalMemoryString, PhysicalMemoryName);
OBJECT_ATTRIBUTES obj;
InitializeObjectAttributes(&obj, &PhysicalMemoryString, OBJ_CASE_INSENSITIVE, NULL, NULL);
NTSTATUS status = ZwOpenSection(&hMemory, SECTION_MAP_READ | SECTION_MAP_WRITE, &obj);
if (!NT_SUCCESS(status))
{
return NULL;
}
return hMemory;
}
ULONG64 Va2Pa(ULONG64 VirtAddr, int processId)
{
ULONG64 virtaddr = VirtAddr;
PEPROCESS Process = NULL;
PsLookupProcessByProcessId((HANDLE)processId, &Process);
ULONG64 CR3 = *(PULONG64)((PUCHAR)Process + 0x28);
CR3 &= ~0xFFF;
ULONG64 pml4Index = ((virtaddr >> 39) & 0x1FF) * 8;
ULONG64 pdpteIndex = ((virtaddr >> 30) & 0x1FF) * 8;
ULONG64 pdeIndex = ((virtaddr >> 21) & 0x1FF) * 8;
ULONG64 pteIndex = ((virtaddr >> 12) & 0x1FF) * 8;
PHYSICAL_ADDRESS phy = { 0 };
phy.QuadPart = CR3 + pml4Index;
HANDLE pHandle = GetPhysicalHandle();
if (!pHandle) return STATUS_UNSUCCESSFUL;
NTSTATUS status;
PULONG64 pml4 = NULL;
SIZE_T map_size = 8;
ULONG64 retaddress = 0;
status = ZwMapViewOfSection(pHandle, NtCurrentProcess(), (PVOID*)&pml4, 0, 8, &phy, &map_size, ViewUnmap, MEM_TOP_DOWN, PAGE_READWRITE);
if (!NT_SUCCESS(status) || !pml4)
{
return 0;
}
PULONG64 pdpte = NULL;
phy.QuadPart = (*pml4 & 0x000FFFFFFFFFF000) + pdpteIndex;
status = ZwMapViewOfSection(pHandle, NtCurrentProcess(), (PVOID*)&pdpte, 0, 8, &phy, &map_size, ViewUnmap, MEM_TOP_DOWN, PAGE_READWRITE);
if (!NT_SUCCESS(status) || !pdpte)
{
ZwUnmapViewOfSection(NtCurrentProcess(), pml4);
return 0;
}
PULONG64 pde = NULL;
phy.QuadPart = (*pdpte & 0x000FFFFFFFFFF000) + pdeIndex;
status = ZwMapViewOfSection(pHandle, NtCurrentProcess(), (PVOID*)&pde, 0, 8, &phy, &map_size, ViewUnmap, MEM_TOP_DOWN, PAGE_READWRITE);
if (!NT_SUCCESS(status) || !pde)
{
ZwUnmapViewOfSection(NtCurrentProcess(), pdpte);
ZwUnmapViewOfSection(NtCurrentProcess(), pml4);
return 0;
}
PULONG64 pte = NULL;
phy.QuadPart = (*pde & 0x000FFFFFFFFFF000) + pteIndex;
status = ZwMapViewOfSection(pHandle, NtCurrentProcess(), (PVOID*)&pte, 0, 8, &phy, &map_size, ViewUnmap, MEM_TOP_DOWN, PAGE_READWRITE);
if (!NT_SUCCESS(status) || !pte)
{
ZwUnmapViewOfSection(NtCurrentProcess(), pde);
ZwUnmapViewOfSection(NtCurrentProcess(), pdpte);
ZwUnmapViewOfSection(NtCurrentProcess(), pml4);
return 0;
}
if (*pte & 0x1)
{
retaddress = (*pte & 0xFFFFFFFFFFFFF000) + (virtaddr & 0xFFF);
}
else
{
retaddress = (*pte & 0xFFFFFFFFFFFFF000) + (virtaddr & 0xFFF);
}
ZwUnmapViewOfSection(NtCurrentProcess(), pte);
ZwUnmapViewOfSection(NtCurrentProcess(), pde);
ZwUnmapViewOfSection(NtCurrentProcess(), pdpte);
ZwUnmapViewOfSection(NtCurrentProcess(), pml4);
return retaddress;
}
VOID DriverUnload(PDRIVER_OBJECT pDriver)
{
}
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriver, PUNICODE_STRING pReg)
{
ULONG64 ret = Va2Pa(0x00401F6A, 1996);
DbgPrintEx(77, 0, "[=]:函数返回 : %llx\r\n", ret);
pDriver->DriverUnload = DriverUnload;
return STATUS_SUCCESS;
}