驱动开发之遍历驱动

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

在内核中,每一个已加载的模块(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;
}
相关推荐
TangDuoduo00055 天前
【Linux SPI驱动开发】
驱动开发
The️5 天前
Linux驱动开发之Read_Write函数
linux·运维·服务器·驱动开发·ubuntu·交互
FserSuN5 天前
AI编程 - 规范驱动开发(SDD)学习
驱动开发·学习·ai编程
TangDuoduo00056 天前
【Linux I2C设备驱动】
linux·驱动开发
The️6 天前
Linux驱动开发之Open_Close函数
linux·运维·驱动开发·mcu·ubuntu
LCG元6 天前
嵌入式GUI设计:STM32F429+LVGL,智能仪表盘界面开发指南
驱动开发·stm32·嵌入式硬件
小龙报7 天前
【51单片机】 给单片机加 “安全锁”!看门狗 WDT:原理 + 配置 + 复位验证全拆解,让程序稳定不跑飞
驱动开发·stm32·单片机·嵌入式硬件·物联网·51单片机·硬件工程
码农编程录7 天前
【notes12】kbuild,内核模块化,字符设备驱动,设备树,platform总线,设备驱动模型
驱动开发
乔碧萝成都分萝7 天前
二十六、IIO子系统 + SPI子系统 + ICM20608
linux·驱动开发·嵌入式
A星空1237 天前
二、交叉编译工具链(arm-linux-gnueabihf-gcc)安装与验证,搭建 TFTP+NFS 服务,调试开发板网络连通性;
linux·c++·驱动开发·单片机·嵌入式硬件