驱动开发之遍历驱动

一、目标:遍历系统中所有已加载驱动

在内核中,每一个已加载的模块(exe / dll / sys)都会有一个
_LDR_DATA_TABLE_ENTRY 结构体描述它。

cpp 复制代码
对于驱动来说:会把"当前驱动对应的那个 _LDR_DATA_TABLE_ENTRY 的地址"
存放在 DriverObject->DriverSection 中。

DriverObject->DriverSection = 当前驱动自己的 _LDR_DATA_TABLE_ENTRY

_LDR_DATA_TABLE_ENTRY 并不是孤立存在的, 它的第一个成员 InLoadOrderLinks 表明:这个结构被设计成"可挂入链表的节点"。

换句话说:每一个模块的 _LDR_DATA_TABLE_ENTRY,都和其它模块的 _LDR_DATA_TABLE_ENTRY 通过 LIST_ENTRY 相互连接

二、遍历的本质

遍历代码做的事情只有三步:

  1. 拿到当前驱动的 _LDR_DATA_TABLE_ENTRY

  2. 通过它的InLoadOrderLinks.Flink 找到"下一个节点"

  3. 再从这个节点还原出下一个模块的 _LDR_DATA_TABLE_ENTRY

遍历驱动

完整代码:

cpp 复制代码
#include <ntddk.h>

typedef struct _LDR_DATA_TABLE_ENTRY {
    LIST_ENTRY InLoadOrderLinks;
    LIST_ENTRY InMemoryOrderLinks;
    LIST_ENTRY InInitializationOrderLinks;

    PVOID DllBase;
    PVOID EntryPoint;
    ULONG SizeOfImage;

    UNICODE_STRING FullDllName;
    UNICODE_STRING BaseDllName;

    ULONG Flags;
    USHORT LoadCount;
    USHORT TlsIndex;
    LIST_ENTRY HashLinks;

    PVOID SectionPointer;
    ULONG CheckSum;
    ULONG TimeDateStamp;

    PVOID LoadedImports;
    PVOID EntryPointActivationContext;
    PVOID PatchInformation;
    LIST_ENTRY ForwarderLinks;
    LIST_ENTRY ServiceTagLinks;
    LIST_ENTRY StaticLinks;
    PVOID ContextInformation;
    ULONG OriginalBase;
    LARGE_INTEGER LoadTime;
} LDR_DATA_TABLE_ENTRY, * PLDR_DATA_TABLE_ENTRY;

// 卸载函数
VOID DriverUnload(PDRIVER_OBJECT DriverObject)
{
    DbgPrint("(mydriver) 驱动程序停止运行了。\n");
}

NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
{
    

    DbgPrint("(mydriver) DRIVER_OBJECT 地址:%p\n", DriverObject);
    DbgPrint("(mydriver) 驱动名称:%wZ\n", &DriverObject->DriverName);
    DbgPrint("(mydriver) 模块基址:%p\n", DriverObject->DriverStart);
    DbgPrint("(mydriver) 模块大小:0x%X\n", DriverObject->DriverSize);

    DbgPrint("(mydriver) -------开始遍历模块-------\n");

    // DriverSection 通常指向当前驱动自身的 LDR_DATA_TABLE_ENTRY
    PLDR_DATA_TABLE_ENTRY first = (PLDR_DATA_TABLE_ENTRY)DriverObject->DriverSection;
    if (first == NULL)
    {
        DbgPrint("(mydriver) DriverSection 为空,无法遍历。\n");
        DriverObject->DriverUnload = DriverUnload;
        return STATUS_SUCCESS;
    }

    // 用 LIST_ENTRY 来做链表遍历
    PLIST_ENTRY head = &first->InLoadOrderLinks;
    PLIST_ENTRY cur = head->Flink;

    int i = 0;
    while (cur != head)
    {
        // 从 LIST_ENTRY* 还原回 LDR_DATA_TABLE_ENTRY*
        PLDR_DATA_TABLE_ENTRY ldr =
            CONTAINING_RECORD(cur, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);

        if (ldr->FullDllName.Length != 0)
        {
            DbgPrint("(mydriver) [%d] %wZ\n", i++, &ldr->FullDllName);
            // DbgPrint("(mydriver)     Base: %wZ\n", &ldr->BaseDllName);
            // DbgPrint("(mydriver)     BaseAddr: %p\n", ldr->DllBase);
            // DbgPrint("(mydriver)     Size: 0x%X\n", ldr->SizeOfImage);
        }

        cur = cur->Flink;
    }

    DbgPrint("(mydriver) -------遍历结束-------\n");

    DriverObject->DriverUnload = DriverUnload;
    return STATUS_SUCCESS;
}
相关推荐
世微 如初17 天前
AP5125大功率LED恒流驱动实战:地摊灯项目从原理图到调试笔记
驱动开发·芯片·led电源驱动·降压恒流ic
ScilogyHunter17 天前
Zephyr串口驱动开发及构建完全指南
驱动开发·uart·zephyr
_Emma_17 天前
【DRM&Graphic】Linux图形与显示框架
linux·驱动开发·图形渲染·显示器
董厂长17 天前
Loop Engineering:停止手动提示,开始设计自动提示的系统
大数据·人工智能·驱动开发·llm
Saniffer_SH18 天前
【高清视频】Gen6 服务器还没到,Gen6 SSD 怎么测?Emily 现场演示三种测试环境
人工智能·驱动开发·测试工具·缓存·fpga开发·计算机外设·压力测试
暮云星影18 天前
全志linux开发屏幕适配(二)`HDMI`驱动适配说明
linux·arm开发·驱动开发
charlie11451419118 天前
嵌入式Linux驱动开发——从轮询到中断
linux·开发语言·驱动开发·嵌入式
暮云星影18 天前
瑞芯微rk3566开发FIT Secure Boot
linux·arm开发·驱动开发·安全
暮云星影18 天前
全志linux开发 USB接口设置
linux·arm开发·驱动开发
sukalot18 天前
windows显示驱动开发-CCD DDI的其它技术
windows·驱动开发