驅動開發:核心遍歷程序VAD結構體

2022-10-13 15:04:25

在上一篇文章《驅動開發:核心中實現Dump程序轉儲》中我們實現了ARK工具的轉存功能,本篇文章繼續以記憶體為出發點介紹VAD結構,該結構的全程是Virtual Address Descriptor虛擬地址描述符,VAD是一個AVL平衡二元樹,樹的每一個節點代表一段虛擬地址空間。程式中的程式碼段,資料段,堆段都會各種佔用一個或多個VAD節點,由一個MMVAD結構完整描述。

VAD結構的遍歷效果如下:

那麼這個結構在哪?每一個程序都有自己單獨的VAD結構樹,這個結構通常在EPROCESS結構裡面裡面,在核心偵錯模式下使用dt _EPROCESS可得到如下資訊。

lyshark.com 1: kd> dt _EPROCESS
ntdll!_EPROCESS
   +0x500 Vm               : _MMSUPPORT_FULL
   +0x640 MmProcessLinks   : _LIST_ENTRY
   +0x650 ModifiedPageCount : Uint4B
   +0x654 ExitStatus       : Int4B
   +0x658 VadRoot          : _RTL_AVL_TREE
   +0x660 VadHint          : Ptr64 Void
   +0x668 VadCount         : Uint8B
   +0x670 VadPhysicalPages : Uint8B
   +0x678 VadPhysicalPagesLimit : Uint8B

可以看到在本系統中VAD的偏移是+0x658緊跟其後的還有vadCount的計數等。

VAD結構是如何被新增的?通常情況下系統呼叫VirtualAllocate等申請一段堆記憶體時,則會在VAD樹上增加一個結點_MMVAD結構體,需要說明的是棧並不受VAD的管理。由系統直接分配空間,並把地址記錄在了TEB中。

lyshark.com 0: kd> dt _MMVAD
nt!_MMVAD
   +0x000 Core             : _MMVAD_SHORT
   +0x040 u2               : <anonymous-tag>
   +0x048 Subsection       : Ptr64 _SUBSECTION
   +0x050 FirstPrototypePte : Ptr64 _MMPTE
   +0x058 LastContiguousPte : Ptr64 _MMPTE
   +0x060 ViewLinks        : _LIST_ENTRY
   +0x070 VadsProcess      : Ptr64 _EPROCESS
   +0x078 u4               : <anonymous-tag>
   +0x080 FileObject       : Ptr64 _FILE_OBJECT

結構體MMVAD則是每一個VAD記憶體塊的屬性,這個記憶體結構定義在WinDBG中可看到。

如上在EPROCESS結構中可以找到VAD結構的相對偏移+0x658以及程序VAD計數偏移+0x668,我們首先通過!process 0 0指令得到當前所有程序的EPROCESS結構,並選中程序。

lyshark.com 0: kd> !process 0 0
PROCESS ffffe28fbb0860c0
    SessionId: 1  Cid: 11a8    Peb: 0035c000  ParentCid: 11c8
    DirBase: 309f3002  ObjectTable: ffffac87ba3da580  HandleCount: 145.
    Image: x64.exe

此處的ffffe28fbb0860c0正是我們所需要的EPROCESS結構。

當需要得到該程序的VAD結構時,只需要使用!vad ffffe28fbb0860c0 + 0x658來顯示該程序的VAD樹。

至於獲取VAD有多少條,則可以直接使用!vad ffffe28fbb0860c0 + 0x668來獲取到。

既然手動可以遍歷出來,那麼自動化也並不難,首先定義標頭檔案vad.h同樣這是微軟定義,如果想要的到最新的,自己下載WinDBG偵錯核心輸入命令。

#pragma once
#include <ntifs.h>

typedef struct _MM_GRAPHICS_VAD_FLAGS        // 15 elements, 0x4 bytes (sizeof) 
{
	/*0x000*/     ULONG32      Lock : 1;                   // 0 BitPosition                   
	/*0x000*/     ULONG32      LockContended : 1;          // 1 BitPosition                   
	/*0x000*/     ULONG32      DeleteInProgress : 1;       // 2 BitPosition                   
	/*0x000*/     ULONG32      NoChange : 1;               // 3 BitPosition                   
	/*0x000*/     ULONG32      VadType : 3;                // 4 BitPosition                   
	/*0x000*/     ULONG32      Protection : 5;             // 7 BitPosition                   
	/*0x000*/     ULONG32      PreferredNode : 6;          // 12 BitPosition                  
	/*0x000*/     ULONG32      PageSize : 2;               // 18 BitPosition                  
	/*0x000*/     ULONG32      PrivateMemoryAlwaysSet : 1; // 20 BitPosition                  
	/*0x000*/     ULONG32      WriteWatch : 1;             // 21 BitPosition                  
	/*0x000*/     ULONG32      FixedLargePageSize : 1;     // 22 BitPosition                  
	/*0x000*/     ULONG32      ZeroFillPagesOptional : 1;  // 23 BitPosition                  
	/*0x000*/     ULONG32      GraphicsAlwaysSet : 1;      // 24 BitPosition                  
	/*0x000*/     ULONG32      GraphicsUseCoherentBus : 1; // 25 BitPosition                  
	/*0x000*/     ULONG32      GraphicsPageProtection : 3; // 26 BitPosition                  
}MM_GRAPHICS_VAD_FLAGS, *PMM_GRAPHICS_VAD_FLAGS;
typedef struct _MM_PRIVATE_VAD_FLAGS         // 15 elements, 0x4 bytes (sizeof) 
{
	/*0x000*/     ULONG32      Lock : 1;                   // 0 BitPosition                   
	/*0x000*/     ULONG32      LockContended : 1;          // 1 BitPosition                   
	/*0x000*/     ULONG32      DeleteInProgress : 1;       // 2 BitPosition                   
	/*0x000*/     ULONG32      NoChange : 1;               // 3 BitPosition                   
	/*0x000*/     ULONG32      VadType : 3;                // 4 BitPosition                   
	/*0x000*/     ULONG32      Protection : 5;             // 7 BitPosition                   
	/*0x000*/     ULONG32      PreferredNode : 6;          // 12 BitPosition                  
	/*0x000*/     ULONG32      PageSize : 2;               // 18 BitPosition                  
	/*0x000*/     ULONG32      PrivateMemoryAlwaysSet : 1; // 20 BitPosition                  
	/*0x000*/     ULONG32      WriteWatch : 1;             // 21 BitPosition                  
	/*0x000*/     ULONG32      FixedLargePageSize : 1;     // 22 BitPosition                  
	/*0x000*/     ULONG32      ZeroFillPagesOptional : 1;  // 23 BitPosition                  
	/*0x000*/     ULONG32      Graphics : 1;               // 24 BitPosition                  
	/*0x000*/     ULONG32      Enclave : 1;                // 25 BitPosition                  
	/*0x000*/     ULONG32      ShadowStack : 1;            // 26 BitPosition                  
}MM_PRIVATE_VAD_FLAGS, *PMM_PRIVATE_VAD_FLAGS;


typedef struct _MMVAD_FLAGS            // 9 elements, 0x4 bytes (sizeof) 
{
	/*0x000*/     ULONG32      Lock : 1;             // 0 BitPosition                  
	/*0x000*/     ULONG32      LockContended : 1;    // 1 BitPosition                  
	/*0x000*/     ULONG32      DeleteInProgress : 1; // 2 BitPosition                  
	/*0x000*/     ULONG32      NoChange : 1;         // 3 BitPosition                  
	/*0x000*/     ULONG32      VadType : 3;          // 4 BitPosition                  
	/*0x000*/     ULONG32      Protection : 5;       // 7 BitPosition                  
	/*0x000*/     ULONG32      PreferredNode : 6;    // 12 BitPosition                 
	/*0x000*/     ULONG32      PageSize : 2;         // 18 BitPosition                 
	/*0x000*/     ULONG32      PrivateMemory : 1;    // 20 BitPosition                 
}MMVAD_FLAGS, *PMMVAD_FLAGS;

typedef struct _MM_SHARED_VAD_FLAGS            // 11 elements, 0x4 bytes (sizeof) 
{
	/*0x000*/     ULONG32      Lock : 1;                     // 0 BitPosition                   
	/*0x000*/     ULONG32      LockContended : 1;            // 1 BitPosition                   
	/*0x000*/     ULONG32      DeleteInProgress : 1;         // 2 BitPosition                   
	/*0x000*/     ULONG32      NoChange : 1;                 // 3 BitPosition                   
	/*0x000*/     ULONG32      VadType : 3;                  // 4 BitPosition                   
	/*0x000*/     ULONG32      Protection : 5;               // 7 BitPosition                   
	/*0x000*/     ULONG32      PreferredNode : 6;            // 12 BitPosition                  
	/*0x000*/     ULONG32      PageSize : 2;                 // 18 BitPosition                  
	/*0x000*/     ULONG32      PrivateMemoryAlwaysClear : 1; // 20 BitPosition                  
	/*0x000*/     ULONG32      PrivateFixup : 1;             // 21 BitPosition                  
	/*0x000*/     ULONG32      HotPatchAllowed : 1;          // 22 BitPosition                  
}MM_SHARED_VAD_FLAGS, *PMM_SHARED_VAD_FLAGS;

typedef struct _MMVAD_FLAGS2             // 7 elements, 0x4 bytes (sizeof) 
{
	/*0x000*/     ULONG32      FileOffset : 24;        // 0 BitPosition                  
	/*0x000*/     ULONG32      Large : 1;              // 24 BitPosition                 
	/*0x000*/     ULONG32      TrimBehind : 1;         // 25 BitPosition                 
	/*0x000*/     ULONG32      Inherit : 1;            // 26 BitPosition                 
	/*0x000*/     ULONG32      NoValidationNeeded : 1; // 27 BitPosition                 
	/*0x000*/     ULONG32      PrivateDemandZero : 1;  // 28 BitPosition                 
	/*0x000*/     ULONG32      Spare : 3;              // 29 BitPosition                 
}MMVAD_FLAGS2, *PMMVAD_FLAGS2;

typedef struct _MMVAD_SHORT
{
	RTL_BALANCED_NODE VadNode;
	UINT32 StartingVpn;               /*0x18*/
	UINT32 EndingVpn;                 /*0x01C*/
	UCHAR StartingVpnHigh;
	UCHAR EndingVpnHigh;
	UCHAR CommitChargeHigh;
	UCHAR SpareNT64VadUChar;
	INT32 ReferenceCount;
	EX_PUSH_LOCK PushLock;            /*0x028*/
	struct
	{
		union
		{
			ULONG_PTR flag;
			MM_PRIVATE_VAD_FLAGS PrivateVadFlags;                        /*0x030*/
			MMVAD_FLAGS  VadFlags;
			MM_GRAPHICS_VAD_FLAGS GraphicsVadFlags;
			MM_SHARED_VAD_FLAGS   SharedVadFlags;
		}Flags;

	}u1;

	PVOID EventList;                        /*0x038*/

}MMVAD_SHORT, *PMMVAD_SHORT;

typedef struct _MMADDRESS_NODE
{
	ULONG64 u1;
	struct _MMADDRESS_NODE* LeftChild;
	struct _MMADDRESS_NODE* RightChild;
	ULONG64 StartingVpn;
	ULONG64 EndingVpn;
}MMADDRESS_NODE, *PMMADDRESS_NODE;

typedef struct _MMEXTEND_INFO     // 2 elements, 0x10 bytes (sizeof) 
{
	/*0x000*/     UINT64       CommittedSize;
	/*0x008*/     ULONG32      ReferenceCount;
	/*0x00C*/     UINT8        _PADDING0_[0x4];
}MMEXTEND_INFO, *PMMEXTEND_INFO;
struct _SEGMENT
{
	struct _CONTROL_AREA* ControlArea;
	ULONG TotalNumberOfPtes;
	ULONG SegmentFlags;
	ULONG64 NumberOfCommittedPages;
	ULONG64 SizeOfSegment;
	union
	{
		struct _MMEXTEND_INFO* ExtendInfo;
		void* BasedAddress;
	}u;
	ULONG64 SegmentLock;
	ULONG64 u1;
	ULONG64 u2;
	PVOID* PrototypePte;
	ULONGLONG ThePtes[0x1];
};

typedef struct _EX_FAST_REF
{
	union
	{
		PVOID Object;
		ULONG_PTR RefCnt : 3;
		ULONG_PTR Value;
	};
} EX_FAST_REF, *PEX_FAST_REF;

typedef struct _CONTROL_AREA                      // 17 elements, 0x80 bytes (sizeof) 
{
	/*0x000*/     struct _SEGMENT* Segment;
	union                                         // 2 elements, 0x10 bytes (sizeof)  
	{
		/*0x008*/         struct _LIST_ENTRY ListHead;              // 2 elements, 0x10 bytes (sizeof)  
		/*0x008*/         VOID*        AweContext;
	};
	/*0x018*/     UINT64       NumberOfSectionReferences;
	/*0x020*/     UINT64       NumberOfPfnReferences;
	/*0x028*/     UINT64       NumberOfMappedViews;
	/*0x030*/     UINT64       NumberOfUserReferences;
	/*0x038*/     ULONG32 u;                     // 2 elements, 0x4 bytes (sizeof)   
	/*0x03C*/     ULONG32 u1;                    // 2 elements, 0x4 bytes (sizeof)   
	/*0x040*/     struct _EX_FAST_REF FilePointer;              // 3 elements, 0x8 bytes (sizeof)   
	// 4 elements, 0x8 bytes (sizeof)   
}CONTROL_AREA, *PCONTROL_AREA;

typedef struct _SUBSECTION_
{
	struct _CONTROL_AREA* ControlArea;

}SUBSECTION, *PSUBSECTION;

typedef struct _MMVAD
{
	MMVAD_SHORT Core;
	union                 /*0x040*/
	{
		UINT32 LongFlags2;
		//現在用不到省略
		MMVAD_FLAGS2 VadFlags2;

	}u2;
	PSUBSECTION Subsection;               /*0x048*/
	PVOID FirstPrototypePte;        /*0x050*/
	PVOID LastContiguousPte;        /*0x058*/
	LIST_ENTRY ViewLinks;           /*0x060*/
	PEPROCESS VadsProcess;          /*0x070*/
	PVOID u4;                       /*0x078*/
	PVOID FileObject;               /*0x080*/
}MMVAD, *PMMVAD;

typedef struct _RTL_AVL_TREE         // 1 elements, 0x8 bytes (sizeof) 
{
	/*0x000*/     struct _RTL_BALANCED_NODE* Root;
}RTL_AVL_TREE, *PRTL_AVL_TREE;

typedef struct _VAD_INFO_
{
	ULONG_PTR pVad;
	ULONG_PTR startVpn;
	ULONG_PTR endVpn;
	ULONG_PTR pFileObject;
	ULONG_PTR flags;
}VAD_INFO, *PVAD_INFO;

typedef struct _ALL_VADS_
{
	ULONG nCnt;
	VAD_INFO VadInfos[1];
}ALL_VADS, *PALL_VADS;

typedef struct _MMSECTION_FLAGS                        // 27 elements, 0x4 bytes (sizeof) 
{
	/*0x000*/     UINT32       BeingDeleted : 1;                     // 0 BitPosition                   
	/*0x000*/     UINT32       BeingCreated : 1;                     // 1 BitPosition                   
	/*0x000*/     UINT32       BeingPurged : 1;                      // 2 BitPosition                   
	/*0x000*/     UINT32       NoModifiedWriting : 1;                // 3 BitPosition                   
	/*0x000*/     UINT32       FailAllIo : 1;                        // 4 BitPosition                   
	/*0x000*/     UINT32       Image : 1;                            // 5 BitPosition                   
	/*0x000*/     UINT32       Based : 1;                            // 6 BitPosition                   
	/*0x000*/     UINT32       File : 1;                             // 7 BitPosition                   
	/*0x000*/     UINT32       AttemptingDelete : 1;                 // 8 BitPosition                   
	/*0x000*/     UINT32       PrefetchCreated : 1;                  // 9 BitPosition                   
	/*0x000*/     UINT32       PhysicalMemory : 1;                   // 10 BitPosition                  
	/*0x000*/     UINT32       ImageControlAreaOnRemovableMedia : 1; // 11 BitPosition                  
	/*0x000*/     UINT32       Reserve : 1;                          // 12 BitPosition                  
	/*0x000*/     UINT32       Commit : 1;                           // 13 BitPosition                  
	/*0x000*/     UINT32       NoChange : 1;                         // 14 BitPosition                  
	/*0x000*/     UINT32       WasPurged : 1;                        // 15 BitPosition                  
	/*0x000*/     UINT32       UserReference : 1;                    // 16 BitPosition                  
	/*0x000*/     UINT32       GlobalMemory : 1;                     // 17 BitPosition                  
	/*0x000*/     UINT32       DeleteOnClose : 1;                    // 18 BitPosition                  
	/*0x000*/     UINT32       FilePointerNull : 1;                  // 19 BitPosition                  
	/*0x000*/     ULONG32      PreferredNode : 6;                    // 20 BitPosition                  
	/*0x000*/     UINT32       GlobalOnlyPerSession : 1;             // 26 BitPosition                  
	/*0x000*/     UINT32       UserWritable : 1;                     // 27 BitPosition                  
	/*0x000*/     UINT32       SystemVaAllocated : 1;                // 28 BitPosition                  
	/*0x000*/     UINT32       PreferredFsCompressionBoundary : 1;   // 29 BitPosition                  
	/*0x000*/     UINT32       UsingFileExtents : 1;                 // 30 BitPosition                  
	/*0x000*/     UINT32       PageSize64K : 1;                      // 31 BitPosition                  
}MMSECTION_FLAGS, *PMMSECTION_FLAGS;

typedef struct _SECTION                          // 9 elements, 0x40 bytes (sizeof) 
{
	/*0x000*/     struct _RTL_BALANCED_NODE SectionNode;       // 6 elements, 0x18 bytes (sizeof) 
	/*0x018*/     UINT64       StartingVpn;
	/*0x020*/     UINT64       EndingVpn;
	/*0x028*/     union {
		PCONTROL_AREA   ControlArea;
		PVOID   FileObject;

	}u1;                   // 4 elements, 0x8 bytes (sizeof)  
	/*0x030*/     UINT64       SizeOfSection;
	/*0x038*/     union {
		ULONG32 LongFlags;
		MMSECTION_FLAGS Flags;
	}u;                    // 2 elements, 0x4 bytes (sizeof)  
	struct                                       // 3 elements, 0x4 bytes (sizeof)  
	{
		/*0x03C*/         ULONG32      InitialPageProtection : 12; // 0 BitPosition                   
		/*0x03C*/         ULONG32      SessionId : 19;             // 12 BitPosition                  
		/*0x03C*/         ULONG32      NoValidationNeeded : 1;     // 31 BitPosition                  
	};
}SECTION, *PSECTION;

引入vad.h標頭檔案,並寫入如下程式碼,此處的eprocess_offset_VadRoot以及eprocess_offset_VadCount 則是上方得出的相對於EPROCESS結構的偏移值,每個系統都不一樣,版本不同偏移值會不同。

#include "vad.h"
#include <ntifs.h>

// 定義VAD相對於EProcess頭部偏移值
#define eprocess_offset_VadRoot 0x658
#define eprocess_offset_VadCount 0x668

VOID EnumVad(PMMVAD Root, PALL_VADS pBuffer, ULONG nCnt)
{
	if (!Root || !pBuffer || !nCnt)
	{
		return;
	}

	__try
	{
		if (nCnt > pBuffer->nCnt)
		{
			// 得到起始頁與結束頁
			ULONG64 endptr = (ULONG64)Root->Core.EndingVpnHigh;
			endptr = endptr << 32;

			ULONG64 startptr = (ULONG64)Root->Core.StartingVpnHigh;
			startptr = startptr << 32;

			// 得到根節點
			pBuffer->VadInfos[pBuffer->nCnt].pVad = (ULONG_PTR)Root;

			// 起始頁: startingVpn * 0x1000
			pBuffer->VadInfos[pBuffer->nCnt].startVpn = (startptr | Root->Core.StartingVpn) << PAGE_SHIFT;

			// 結束頁: EndVpn * 0x1000 + 0xfff
			pBuffer->VadInfos[pBuffer->nCnt].endVpn = ((endptr | Root->Core.EndingVpn) << PAGE_SHIFT) + 0xfff;

			// VAD標誌 928 = Mapped    1049088 = Private   ....
			pBuffer->VadInfos[pBuffer->nCnt].flags = Root->Core.u1.Flags.flag;

			// 驗證節點可讀性
			if (MmIsAddressValid(Root->Subsection) && MmIsAddressValid(Root->Subsection->ControlArea))
			{
				if (MmIsAddressValid((PVOID)((Root->Subsection->ControlArea->FilePointer.Value >> 4) << 4)))
				{
					pBuffer->VadInfos[pBuffer->nCnt].pFileObject = ((Root->Subsection->ControlArea->FilePointer.Value >> 4) << 4);
				}
			}
			pBuffer->nCnt++;
		}

		if (MmIsAddressValid(Root->Core.VadNode.Left))
		{
			// 遞迴列舉左子樹
			EnumVad((PMMVAD)Root->Core.VadNode.Left, pBuffer, nCnt);
		}

		if (MmIsAddressValid(Root->Core.VadNode.Right))
		{
			// 遞迴列舉右子樹
			EnumVad((PMMVAD)Root->Core.VadNode.Right, pBuffer, nCnt);
		}
	}
	__except (1)
	{
	}
}

BOOLEAN EnumProcessVad(ULONG Pid, PALL_VADS pBuffer, ULONG nCnt)
{
	PEPROCESS Peprocess = 0;
	PRTL_AVL_TREE Table = NULL;
	PMMVAD Root = NULL;

	// 通過程序PID得到程序EProcess
	if (NT_SUCCESS(PsLookupProcessByProcessId((HANDLE)Pid, &Peprocess)))
	{
		// 與偏移相加得到VAD頭節點
		Table = (PRTL_AVL_TREE)((UCHAR*)Peprocess + eprocess_offset_VadRoot);
		if (!MmIsAddressValid(Table) || !eprocess_offset_VadRoot)
		{
			return FALSE;
		}

		__try
		{
			// 取出頭節點
			Root = (PMMVAD)Table->Root;

			if (nCnt > pBuffer->nCnt)
			{
				// 得到起始頁與結束頁
				ULONG64 endptr = (ULONG64)Root->Core.EndingVpnHigh;
				endptr = endptr << 32;

				ULONG64 startptr = (ULONG64)Root->Core.StartingVpnHigh;
				startptr = startptr << 32;

				pBuffer->VadInfos[pBuffer->nCnt].pVad = (ULONG_PTR)Root;

				// 起始頁: startingVpn * 0x1000
				pBuffer->VadInfos[pBuffer->nCnt].startVpn = (startptr | Root->Core.StartingVpn) << PAGE_SHIFT;

				// 結束頁: EndVpn * 0x1000 + 0xfff
				pBuffer->VadInfos[pBuffer->nCnt].endVpn = (endptr | Root->Core.EndingVpn) << PAGE_SHIFT;
				pBuffer->VadInfos[pBuffer->nCnt].flags = Root->Core.u1.Flags.flag;

				if (MmIsAddressValid(Root->Subsection) && MmIsAddressValid(Root->Subsection->ControlArea))
				{
					if (MmIsAddressValid((PVOID)((Root->Subsection->ControlArea->FilePointer.Value >> 4) << 4)))
					{
						pBuffer->VadInfos[pBuffer->nCnt].pFileObject = ((Root->Subsection->ControlArea->FilePointer.Value >> 4) << 4);
					}
				}
				pBuffer->nCnt++;
			}

			// 列舉左子樹
			if (Table->Root->Left)
			{
				EnumVad((MMVAD*)Table->Root->Left, pBuffer, nCnt);
			}

			// 列舉右子樹
			if (Table->Root->Right)
			{
				EnumVad((MMVAD*)Table->Root->Right, pBuffer, nCnt);
			}
		}
		__finally
		{
			ObDereferenceObject(Peprocess);
		}
	}
	else
	{
		return FALSE;
	}

	return TRUE;
}

VOID UnDriver(PDRIVER_OBJECT driver)
{
	DbgPrint(("Uninstall Driver Is OK \n"));
}

NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath)
{
	DbgPrint(("hello lyshark \n"));

	typedef struct
	{
		ULONG nPid;
		ULONG nSize;
		PALL_VADS pBuffer;
	}VADProcess;

	__try
	{
		VADProcess vad = { 0 };

		vad.nPid = 4520;

		// 預設有1000個執行緒
		vad.nSize = sizeof(VAD_INFO) * 0x5000 + sizeof(ULONG);

		// 分配臨時空間
		vad.pBuffer = (PALL_VADS)ExAllocatePool(PagedPool, vad.nSize);

		// 根據傳入長度得到列舉數量
		ULONG nCount = (vad.nSize - sizeof(ULONG)) / sizeof(VAD_INFO);

		// 列舉VAD
		EnumProcessVad(vad.nPid, vad.pBuffer, nCount);


		// 輸出VAD
		for (size_t i = 0; i < vad.pBuffer->nCnt; i++)
		{
			DbgPrint("StartVPN = %p | ", vad.pBuffer->VadInfos[i].startVpn);
			DbgPrint("EndVPN = %p | ", vad.pBuffer->VadInfos[i].endVpn);
			DbgPrint("PVAD = %p | ", vad.pBuffer->VadInfos[i].pVad);
			DbgPrint("Flags = %d | ", vad.pBuffer->VadInfos[i].flags);
			DbgPrint("pFileObject = %p \n", vad.pBuffer->VadInfos[i].pFileObject);
		}
	}
	__except (1)
	{
	}

	Driver->DriverUnload = UnDriver;
	return STATUS_SUCCESS;
}

程式執行後輸出效果如下: