From 23f1f4686aeb5b0b658ad3f51beb11bcb0450673 Mon Sep 17 00:00:00 2001 From: maowenrui <3505719167@qq.com> Date: Thu, 8 Oct 2026 10:05:17 +0800 Subject: [PATCH] feat memory vad protect --- Anti-Cheat_Driver/memory_protect.cpp | 220 +++++++++++++++++++++++++++ Anti-Cheat_Driver/memory_protect.h | 172 +++++++++++++++++++++ 2 files changed, 392 insertions(+) create mode 100644 Anti-Cheat_Driver/memory_protect.cpp create mode 100644 Anti-Cheat_Driver/memory_protect.h diff --git a/Anti-Cheat_Driver/memory_protect.cpp b/Anti-Cheat_Driver/memory_protect.cpp new file mode 100644 index 0000000..8b5e8b9 --- /dev/null +++ b/Anti-Cheat_Driver/memory_protect.cpp @@ -0,0 +1,220 @@ +#include "memory_protect.h" +#include "utils.h" + +namespace memory_protect +{ + auto _eprocess_get_vadroot_offset() -> unsigned int + { + UNICODE_STRING unName{ 0 }; + + RtlInitUnicodeString(&unName, L"PsGetProcessExitStatus"); + + auto func = (unsigned char*)MmGetSystemRoutineAddress(&unName); + + auto offset = *(int*)(func + 2); + + return (unsigned int)(offset + 4); + } + + + VOID SetVadSecNoChange(_In_ MY_MMVAD_SHORT* Vad) + { + if (!Vad) return; + + auto Block = reinterpret_cast<_MY_MI_VAD_EVENT_BLOCK*>(Vad->EventList); + + while (Block) + { + Block->SecureInfo.u1.Flags.SecNoChange = 1; + + Block = Block->Next; + } + } + + MY_MMVAD_SHORT* FindVadByAddress( + _In_ RTL_BALANCED_NODE* Node, + _In_ ULONG64 Address + ) + { + if (!Node) return nullptr; + + auto Vad = CONTAINING_RECORD(Node, MY_MMVAD_SHORT, VadNode); + + ULONGLONG StartVpn = ((ULONGLONG)Vad->StartingVpnHigh << 32) | Vad->StartingVpn; + + ULONGLONG EndVpn = ((ULONGLONG)Vad->EndingVpnHigh << 32) | Vad->EndingVpn; + + ULONGLONG StartAddress = StartVpn << PAGE_SHIFT; + + ULONGLONG EndAddress = ((EndVpn + 1) << PAGE_SHIFT) - 1; + + if (Address < StartAddress) + return FindVadByAddress(Node->Left, Address); + + if (Address > EndAddress) + return FindVadByAddress(Node->Right, Address); + + return Vad; + } + + PMMVAD_W7 FindVadByAddressWin7( + _In_ _MMADDRESS_NODE_W7* Node, + _In_ ULONG64 Address + ) + { + if (!Node) + return nullptr; + + + auto Vad = reinterpret_cast(Node); + + ULONGLONG StartAddress = Vad->StartingVpn << PAGE_SHIFT; + ULONGLONG EndAddress = ((Vad->EndingVpn + 1) << PAGE_SHIFT) - 1; + + if (Address < StartAddress) + return FindVadByAddressWin7(reinterpret_cast<_MMADDRESS_NODE_W7*>(Vad->LeftChild), Address); + + if (Address > EndAddress) + return FindVadByAddressWin7(reinterpret_cast<_MMADDRESS_NODE_W7*>(Vad->RightChild), Address); + + return Vad; + } + + NTSTATUS ProtectUserRegionByPidWin7( + HANDLE Pid, + ULONG64 Address + ) + { + PEPROCESS Process = nullptr; + + NTSTATUS Status = PsLookupProcessByProcessId(Pid, &Process); + + if (!NT_SUCCESS(Status)) + return Status; + + KAPC_STATE ApcState{ 0 }; + + KeStackAttachProcess(Process, &ApcState); + + // + // Win7 x64: + // + // EPROCESS + 0x448 + // | + // +---- _MM_AVL_TABLE + // | + // +0x000 BalancedRoot + // | + // +0x010 RightChild + // + // BalancedRoot.RightChild ²ΕΚΗΚ΅ΌΚ VAD Tree Root + // + + ULONG VadRootOffset = _eprocess_get_vadroot_offset(); + + auto BalancedRoot = reinterpret_cast<_MMADDRESS_NODE_W7*>(reinterpret_cast(Process) + VadRootOffset); + + auto VadRoot = BalancedRoot->RightChild; + + //DbgPrintEx( + // 77, + // 0, + // "[VAD] Process=%p VadRootOffset=%X BalancedRoot=%p VadRoot=%p Address=%llX\n", + // Process, + // VadRootOffset, + // BalancedRoot, + // VadRoot, + // Address + //); + + auto Vad = FindVadByAddressWin7(VadRoot, Address); + + //DbgPrintEx(77, 0, "[VAD] Vad=%p\n", Vad); + + if (!Vad) + { + KeUnstackDetachProcess(&ApcState); + ObDereferenceObject(Process); + return STATUS_NOT_FOUND; + } + + ULONGLONG StartAddress = Vad->StartingVpn << PAGE_SHIFT; + ULONGLONG EndAddress = ((Vad->EndingVpn + 1) << PAGE_SHIFT) - 1; + + SIZE_T RegionSize = (SIZE_T)(EndAddress - StartAddress + 1); + + //DbgPrintEx( + // 77, + // 0, + // "[VAD] Start=%llX End=%llX Size=%llX\n", + // StartAddress, + // EndAddress, + // RegionSize + //); + + Vad->u.VadFlags.NoChange = 1; + Vad->u2.VadFlags2.SecNoChange = 1; + + + KeUnstackDetachProcess(&ApcState); + ObDereferenceObject(Process); + + return STATUS_SUCCESS; + } + + + NTSTATUS ProtectUserRegionByPid(HANDLE Pid, ULONG64 Address) + { + if (!Pid || !Address) return STATUS_INVALID_PARAMETER; + + auto version = utils::GetVersion(); + + if (version.dwBuildNumber == 7600 || version.dwBuildNumber == 7601) + return ProtectUserRegionByPidWin7(Pid, Address); + + PEPROCESS Process = nullptr; + + NTSTATUS Status = PsLookupProcessByProcessId(Pid, &Process); + + if (!NT_SUCCESS(Status)) + return Status; + + KAPC_STATE ApcState{ 0 }; + KeStackAttachProcess(Process, &ApcState); + + auto VadRoot = *reinterpret_cast(reinterpret_cast(Process) + _eprocess_get_vadroot_offset()); + + auto Vad = FindVadByAddress(VadRoot, Address); + + if (!Vad) + { + KeUnstackDetachProcess(&ApcState); + ObDereferenceObject(Process); + return STATUS_NOT_FOUND; + } + + ULONGLONG StartVpn = ((ULONGLONG)Vad->StartingVpnHigh << 32) | Vad->StartingVpn; + + ULONGLONG EndVpn = ((ULONGLONG)Vad->EndingVpnHigh << 32) | Vad->EndingVpn; + + ULONGLONG StartAddress = StartVpn << PAGE_SHIFT; + + ULONGLONG EndAddress = ((EndVpn + 1) << PAGE_SHIFT) - 1; + + SIZE_T RegionSize = (SIZE_T)(EndAddress - StartAddress + 1); + + PVOID SecureHandle = MmSecureVirtualMemory((PVOID)StartAddress, RegionSize, PAGE_READONLY); + + if (SecureHandle) SetVadSecNoChange(Vad); + + + KeUnstackDetachProcess(&ApcState); + ObDereferenceObject(Process); + + if (!SecureHandle) + return STATUS_ACCESS_DENIED; + + return STATUS_SUCCESS; + } + +} \ No newline at end of file diff --git a/Anti-Cheat_Driver/memory_protect.h b/Anti-Cheat_Driver/memory_protect.h new file mode 100644 index 0000000..da3650d --- /dev/null +++ b/Anti-Cheat_Driver/memory_protect.h @@ -0,0 +1,172 @@ +#pragma once +#ifndef MEMORY_PROTECT_H +#define MEMORY_PROTECT_H +#include "Base.h" + +#pragma warning(disable : 4201) + +struct _MMSECURE_FLAGS +{ + ULONG ReadOnly : 1; //0x0 + ULONG ReadWrite : 1; //0x0 + ULONG SecNoChange : 1; //0x0 + ULONG NoDelete : 1; //0x0 + ULONG RequiresPteReversal : 1; //0x0 + ULONG ExclusiveSecure : 1; //0x0 + ULONG UserModeOnly : 1; //0x0 + ULONG NoInherit : 1; //0x0 + ULONG CheckVad : 1; //0x0 + ULONG Spare : 3; //0x0 +}; + +struct _MMADDRESS_LIST +{ + union + { + struct _MMSECURE_FLAGS Flags; //0x0 + ULONG FlagsLong; //0x0 + VOID* StartVa; //0x0 + } u1; //0x0 + VOID* EndVa; //0x8 +}; +typedef struct _MY_MI_VAD_EVENT_BLOCK +{ + struct _MY_MI_VAD_EVENT_BLOCK* Next; // +0x00 + + union + { + _MMADDRESS_LIST SecureInfo; // +0x08 + }; + + UCHAR Reserved[0x38]; // +0x18 ~ +0x4F + + ULONG WaitReason; // +0x40 + +} MY_MI_VAD_EVENT_BLOCK, * PMY_MI_VAD_EVENT_BLOCK; + +typedef struct _MY_MMVAD_SHORT +{ + union + { + struct + { + struct _MY_MMVAD_SHORT* NextVad; + PVOID ExtraCreateInfo; + }; + + RTL_BALANCED_NODE VadNode; + }; + + ULONG StartingVpn; // +0x18 + ULONG EndingVpn; // +0x1C + UCHAR StartingVpnHigh; // +0x20 + UCHAR EndingVpnHigh; // +0x21 + UCHAR CommitChargeHigh; // +0x22 + UCHAR SpareNT64VadUChar; // +0x23 + LONG ReferenceCount; // +0x24 + EX_PUSH_LOCK PushLock; // +0x28 + + union + { + ULONG LongFlags; // +0x30 + ULONG VadFlags; + } u; + + ULONG LongFlags1; // +0x34 + PMY_MI_VAD_EVENT_BLOCK EventList; // +0x38 + +} MY_MMVAD_SHORT, * PMY_MMVAD_SHORT; + + + +//0x28 bytes (sizeof) +typedef struct _MMADDRESS_NODE_W7 +{ + union + { + LONGLONG Balance : 2; //0x0 + struct _MMADDRESS_NODE_W7* Parent; //0x0 + } u1; //0x0 + struct _MMADDRESS_NODE_W7* LeftChild; //0x8 + struct _MMADDRESS_NODE_W7* RightChild; //0x10 + ULONGLONG StartingVpn; //0x18 + ULONGLONG EndingVpn; //0x20 + +}MMADDRESS_NODE_W7, * PMMADDRESS_NODE_W7; + + +typedef struct _MMVAD_FLAGS +{ + ULONGLONG CommitCharge : 51; //0x0 + ULONGLONG NoChange : 1; //0x0 + ULONGLONG VadType : 3; //0x0 + ULONGLONG MemCommit : 1; //0x0 + ULONGLONG Protection : 5; //0x0 + ULONGLONG Spare : 2; //0x0 + ULONGLONG PrivateMemory : 1; //0x0 +} MMVAD_FLAGS, * PMMVAD_FLAGS; + +typedef struct _MMVAD_FLAGS2 +{ + ULONG FileOffset : 24; //0x0 + ULONG SecNoChange : 1; //0x0 + ULONG OneSecured : 1; //0x0 + ULONG MultipleSecured : 1; //0x0 + ULONG Spare : 1; //0x0 + ULONG LongVad : 1; //0x0 + ULONG ExtendableFile : 1; //0x0 + ULONG Inherit : 1; //0x0 + ULONG CopyOnWrite : 1; //0x0 +} MMVAD_FLAGS2, * PMMVAD_FLAGS2; + + +typedef struct _MMVAD_W7 +{ + union + { + struct _MMVAD_W7* Parent; + LONGLONG Balance : 2; + } u1; // 0x00 + + struct _MMVAD_W7* LeftChild; // 0x08 + struct _MMVAD_W7* RightChild; // 0x10 + + ULONGLONG StartingVpn; // 0x18 + ULONGLONG EndingVpn; // 0x20 + + union + { + ULONGLONG LongFlags; // 0x28 + MMVAD_FLAGS VadFlags; + } u; // 0x28 + + BYTE Reserved1[0x10]; // 0x30 + + union + { + ULONG LongFlags2; // 0x40 + MMVAD_FLAGS2 VadFlags2; + } u2; // 0x40 + +} MMVAD_W7, * PMMVAD_W7; + +namespace memory_protect +{ + VOID SetVadSecNoChange( + _In_ MY_MMVAD_SHORT* Vad + ); + + MY_MMVAD_SHORT* FindVadByAddress( + _In_ RTL_BALANCED_NODE* Node, + _In_ ULONG64 Address + ); + + NTSTATUS ProtectUserRegionByPid( + HANDLE Pid, + ULONG64 Address + ); + +} + + +#endif // !MEMORY_PROTECT_H