在Windows 内核中,为了实现高效的数据结构操作,通常会使用链表和结构体相结合的方式进行数据存储和操作。内核提供了一个专门用于链表操作的数据结构LIST_ENTRY ,可以用来描述一个链表中的每一个节点。使用链表来存储结构体时,需要在结构体中嵌入一个LIST_ENTRY 类型的成员变量,用来连接相邻的节点。通过一些列链表操作函数,如InitializeListHead、InsertHeadList、InsertTailList、RemoveEntryList 等,可以对链表中的结构体进行插入、删除、遍历等操作。 下面是一个简单的实现,用于枚举所有用户进程,并将进程信息存储到链表结构体中: #include <ntifs.h> #include <windef.h>
extern PVOID PsGetProcessPeb(_In_ PEPROCESS Process); NTKERNELAPI NTSTATUS PsLookupProcessByProcessId(HANDLE ProcessId, PEPROCESS *Process); extern NTKERNELAPI PVOID PsGetProcessWow64Process(_In_ PEPROCESS Process); extern NTKERNELAPI UCHAR* PsGetProcessImageFileName(IN PEPROCESS Process); extern NTKERNELAPI HANDLE PsGetProcessInheritedFromUniqueProcessId(IN PEPROCESS Process);
typedef struct { DWORD Pid; UCHAR ProcessName[2048]; DWORD Handle; LIST_ENTRY ListEntry; }ProcessList;
PEPROCESS LookupProcess(HANDLE Pid) { PEPROCESS eprocess = NULL; NTSTATUS Status = STATUS_UNSUCCESSFUL; Status = PsLookupProcessByProcessId(Pid, &eprocess); if (NT_SUCCESS(Status)) { return eprocess; } return NULL; }
BOOLEAN GetAllProcess() { PEPROCESS eproc = NULL; LIST_ENTRY linkListHead;
InitializeListHead(&linkListHead); ProcessList *pData = NULL;
for (int temp = 0; temp < 100000; temp += 4) { eproc = LookupProcess((HANDLE)temp); if (eproc != NULL) { STRING nowProcessnameString = { 0 }; RtlInitString(&nowProcessnameString, PsGetProcessImageFileName(eproc));
pData = (ProcessList *)ExAllocatePool(PagedPool, sizeof(ProcessList)); RtlZeroMemory(pData, sizeof(ProcessList));
pData->Pid = (DWORD)PsGetProcessId(eproc); RtlCopyMemory(pData->ProcessName, PsGetProcessImageFileName(eproc), strlen(PsGetProcessImageFileName(eproc)) * 2); pData->Handle = (DWORD)PsGetProcessInheritedFromUniqueProcessId(eproc);
InsertTailList(&linkListHead, &pData->ListEntry); ObDereferenceObject(eproc); } }
while (!IsListEmpty(&linkListHead)) { LIST_ENTRY *pEntry = RemoveHeadList(&linkListHead); pData = CONTAINING_RECORD(pEntry, ProcessList, ListEntry);
DbgPrint("%d \n", pData->Pid); DbgPrint("%s \n", pData->ProcessName); DbgPrint("%d \n", pData->Handle); ExFreePool(pData); } 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");
GetAllProcess();
Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
|