2.1 Windows驱动开发:内核链表与结构体

Windows内核中,为了实现高效的数据结构操作,通常会使用链表和结构体相结合的方式进行数据存储和操作。内核提供了一个专门用于链表操作的数据结构LIST_ENTRY,可以用来描述一个链表中的每一个节点。

使用链表来存储结构体时,需要在结构体中嵌入一个LIST_ENTRY类型的成员变量,用来连接相邻的节点。通过一些列链表操作函数,如InitializeListHead、InsertHeadList、InsertTailList、RemoveEntryList等,可以对链表中的结构体进行插入、删除、遍历等操作。

下面是一个简单的实现,用于枚举所有用户进程,并将进程信息存储到链表结构体中:

c 复制代码
#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;

// 根据进程ID返回进程EPROCESS结构体失败返回NULL
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));

            // DbgPrint("进程名: %s --> 进程PID = %d --> 父进程PPID = %d\r\n", 
            // PsGetProcessImageFileName(eproc), PsGetProcessId(eproc), PsGetProcessInheritedFromUniqueProcessId(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;
}

DbgView是一款用于监视内核和应用程序调试输出的工具,可以输出各种调试信息和日志信息,包括OutputDebugString函数输出的信息。当我们在内核中调用OutputDebugString函数输出信息时,可以通过DbgView查看输出结果,我们手动上述代码后将可以在DbgView中看到输出的进程信息,如下图所示;

如果需要在内核模式中返回一个结构体,可以通过定义一个结构体指针作为函数参数,将结构体指针作为函数返回值来实现。返回结构体,则可以这样来写代码。

c 复制代码
#include <ntifs.h>
#include <windef.h>

typedef struct
{
    int count;
    char username[256];
    char password[256];
}MyData;

// 模拟返回一个结构
BOOLEAN GetProcess(PVOID OutPut)
{
    RtlZeroMemory(OutPut, sizeof(MyData));
    MyData *data = OutPut;

    data->count = 100;
    RtlCopyMemory(data->username, "xxxxxxxxxxx", sizeof("xxxxxxxxxxx"));
    RtlCopyMemory(data->password, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", sizeof("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
    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");
    PVOID Ptr = (PVOID)ExAllocatePool(NonPagedPool, sizeof(MyData));


    GetProcess(Ptr);

    MyData *data = (MyData *)Ptr;

    DbgPrint("count = %d \n", data->count);
    DbgPrint("username = %s \n", data->username);
    DbgPrint("password = %s \n", data->password);

    Driver->DriverUnload = UnDriver;
    return STATUS_SUCCESS;
}

输出效果如下图所示;

相关推荐
跟着珅聪学java6 分钟前
在 Java 中处理 JSON 去除空 children数组,可以使用 Jackson 库。这里有几种实现方式
开发语言·windows·python
JasmineX-128 分钟前
数据结构(笔记)——单向循环链表
c语言·数据结构·笔记·链表
AIBox36534 分钟前
codex api 配置教程:安装、鉴权、Windows 环境变量
javascript·人工智能·windows·gpt
网络安全许木1 小时前
自学渗透测试第八天(网络安全法、伦理规范与工具链联动)
windows·web安全·网络安全·渗透测试·kali linux
idolao1 小时前
微软常用运行库 安装教程:一键修复VC++运行环境(AIO合集)
windows
路溪非溪1 小时前
Linux中Netlink简介和使用总结
linux·网络·arm开发·驱动开发
爱分享的阿Q1 小时前
RISC-V驱动开发合规解析
驱动开发·risc-v
Mr_Xuhhh10 小时前
Java泛型进阶:从基础到高级特性完全指南
开发语言·windows·python
建行一世10 小时前
【Windows笔记本大模型“傻瓜式”教程】使用LLaMA-Factory工具来完成对Windows笔记本大模型Qwen2.5-3B-Instruct微调
windows·ai·语言模型·llama
王者鳜錸14 小时前
Windows安装OpenClaw龙虾助手
windows·龙虾·自动化脚本执行管家