博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Windows编程] 监视DLL装载/卸载
阅读量:6705 次
发布时间:2019-06-25

本文共 6194 字,大约阅读时间需要 20 分钟。

Windows 驱动开发库里面提供了函数 LdrRegisterDllNotification , LdrUnregisterDllNotification , 可以让你监视进程装载/卸载DLL 的事件。 当你想在某个DLL被加载的时候Hook它的函数; 或者当你想在某个DLL推出之前做一些保存清理工作; 或者当你想阻止某个DLL 被加载(比如外挂) .... 这个机制正可以派上用场 。
以下是代码示例如何使用 LdrRegisterDllNotification , LdrUnregisterDllNotification 监听DLL装载/卸载。
 view plaincopy to clipboardprint?
#include <Ntsecapi.h> // DDK   
typedef const UNICODE_STRING* PCUNICODE_STRING;   
  
typedef struct _LDR_DLL_LOADED_NOTIFICATION_DATA {   
    ULONG Flags;                    //Reserved.   
    PCUNICODE_STRING FullDllName;   //The full path name of the DLL module.   
    PCUNICODE_STRING BaseDllName;   //The base file name of the DLL module.   
    PVOID DllBase;                  //A pointer to the base address for the DLL in memory.   
    ULONG SizeOfImage;              //The size of the DLL image, in bytes.   
} LDR_DLL_LOADED_NOTIFICATION_DATA, *PLDR_DLL_LOADED_NOTIFICATION_DATA;   
  
typedef struct _LDR_DLL_UNLOADED_NOTIFICATION_DATA {   
    ULONG Flags;                    //Reserved.   
    PCUNICODE_STRING FullDllName;   //The full path name of the DLL module.   
    PCUNICODE_STRING BaseDllName;   //The base file name of the DLL module.   
    PVOID DllBase;                  //A pointer to the base address for the DLL in memory.   
    ULONG SizeOfImage;              //The size of the DLL image, in bytes.   
} LDR_DLL_UNLOADED_NOTIFICATION_DATA, *PLDR_DLL_UNLOADED_NOTIFICATION_DATA;   
  
typedef union _LDR_DLL_NOTIFICATION_DATA {   
    LDR_DLL_LOADED_NOTIFICATION_DATA Loaded;   
    LDR_DLL_UNLOADED_NOTIFICATION_DATA Unloaded;   
} LDR_DLL_NOTIFICATION_DATA, *PLDR_DLL_NOTIFICATION_DATA;   
  
typedef const PLDR_DLL_NOTIFICATION_DATA PCLDR_DLL_NOTIFICATION_DATA;   
  
typedef VOID (NTAPI *PLDR_DLL_NOTIFICATION_FUNCTION)(ULONG NotificationReason, PCLDR_DLL_NOTIFICATION_DATA NotificationData, PVOID Context);   
typedef NTSTATUS (NTAPI *pfnLdrRegisterDllNotification)(ULONG Flags, PLDR_DLL_NOTIFICATION_FUNCTION NotificationFunction, void* Context, void **Cookie);   
typedef NTSTATUS (NTAPI *pfnLdrUnregisterDllNotification)(void *Cookie);   
  
#define LDR_DLL_NOTIFICATION_REASON_LOADED 1    
#define LDR_DLL_NOTIFICATION_REASON_UNLOADED 2   
  
VOID NTAPI MyLdrDllNotification(   
  ULONG NotificationReason,   
  PCLDR_DLL_NOTIFICATION_DATA NotificationData,   
  PVOID Context   
)   
{   
    switch (NotificationReason)   
    {   
    case LDR_DLL_NOTIFICATION_REASON_LOADED:   
        printf ("Dll Loaded: %S\n", NotificationData->Loaded.FullDllName->Buffer);   
        break;   
    case LDR_DLL_NOTIFICATION_REASON_UNLOADED:   
        printf ("Dll Unloaded: %S\n", NotificationData->Unloaded.FullDllName->Buffer);   
        break;   
    }   
}   
  
int _tmain(int argc, _TCHAR* argv[])   
{   
  
    HMODULE hModule = GetModuleHandleW(L"NTDLL.DLL");   
            
         // 取得函数指针   
    pfnLdrRegisterDllNotification pLdrRegisterDllNotification = (pfnLdrRegisterDllNotification)GetProcAddress(hModule, "LdrRegisterDllNotification");   
    pfnLdrUnregisterDllNotification pLdrUnregisterDllNotification = (pfnLdrUnregisterDllNotification)GetProcAddress(hModule, "LdrUnregisterDllNotification");   
    void *pvCookie = NULL;   
  
         // 初始化   
    pLdrRegisterDllNotification(0, MyLdrDllNotification, NULL, &pvCookie);   
       
    // 测试DLL 装载   
    HMODULE hLoad = ::LoadLibraryW(L"mshtml.dll");   
    Sleep(1000);   
         // 测试 DLL 卸载   
    ::FreeLibrary(hLoad);   
  
         // 清除   
    if (pvCookie)   
    {   
        pLdrUnregisterDllNotification(pvCookie);   
        pvCookie = NULL;   
    }   
  
    return 0;   
}  
#include <Ntsecapi.h> // DDK
typedef const UNICODE_STRING* PCUNICODE_STRING;
typedef struct _LDR_DLL_LOADED_NOTIFICATION_DATA {
    ULONG Flags;                    //Reserved.
    PCUNICODE_STRING FullDllName;   //The full path name of the DLL module.
    PCUNICODE_STRING BaseDllName;   //The base file name of the DLL module.
    PVOID DllBase;                  //A pointer to the base address for the DLL in memory.
    ULONG SizeOfImage;              //The size of the DLL image, in bytes.
} LDR_DLL_LOADED_NOTIFICATION_DATA, *PLDR_DLL_LOADED_NOTIFICATION_DATA;
typedef struct _LDR_DLL_UNLOADED_NOTIFICATION_DATA {
    ULONG Flags;                    //Reserved.
    PCUNICODE_STRING FullDllName;   //The full path name of the DLL module.
    PCUNICODE_STRING BaseDllName;   //The base file name of the DLL module.
    PVOID DllBase;                  //A pointer to the base address for the DLL in memory.
    ULONG SizeOfImage;              //The size of the DLL image, in bytes.
} LDR_DLL_UNLOADED_NOTIFICATION_DATA, *PLDR_DLL_UNLOADED_NOTIFICATION_DATA;
typedef union _LDR_DLL_NOTIFICATION_DATA {
    LDR_DLL_LOADED_NOTIFICATION_DATA Loaded;
    LDR_DLL_UNLOADED_NOTIFICATION_DATA Unloaded;
} LDR_DLL_NOTIFICATION_DATA, *PLDR_DLL_NOTIFICATION_DATA;
typedef const PLDR_DLL_NOTIFICATION_DATA PCLDR_DLL_NOTIFICATION_DATA;
typedef VOID (NTAPI *PLDR_DLL_NOTIFICATION_FUNCTION)(ULONG NotificationReason, PCLDR_DLL_NOTIFICATION_DATA NotificationData, PVOID Context);
typedef NTSTATUS (NTAPI *pfnLdrRegisterDllNotification)(ULONG Flags, PLDR_DLL_NOTIFICATION_FUNCTION NotificationFunction, void* Context, void **Cookie);
typedef NTSTATUS (NTAPI *pfnLdrUnregisterDllNotification)(void *Cookie);
#define LDR_DLL_NOTIFICATION_REASON_LOADED 1 
#define LDR_DLL_NOTIFICATION_REASON_UNLOADED 2
VOID NTAPI MyLdrDllNotification(
  ULONG NotificationReason,
  PCLDR_DLL_NOTIFICATION_DATA NotificationData,
  PVOID Context
)
{
 switch (NotificationReason)
 {
 case LDR_DLL_NOTIFICATION_REASON_LOADED:
  printf ("Dll Loaded: %S\n", NotificationData->Loaded.FullDllName->Buffer);
  break;
 case LDR_DLL_NOTIFICATION_REASON_UNLOADED:
  printf ("Dll Unloaded: %S\n", NotificationData->Unloaded.FullDllName->Buffer);
  break;
 }
}
int _tmain(int argc, _TCHAR* argv[])
{
 HMODULE hModule = GetModuleHandleW(L"NTDLL.DLL");
         
         // 取得函数指针
 pfnLdrRegisterDllNotification pLdrRegisterDllNotification = (pfnLdrRegisterDllNotification)GetProcAddress(hModule, "LdrRegisterDllNotification");
 pfnLdrUnregisterDllNotification pLdrUnregisterDllNotification = (pfnLdrUnregisterDllNotification)GetProcAddress(hModule, "LdrUnregisterDllNotification");
 void *pvCookie = NULL;
         // 初始化
 pLdrRegisterDllNotification(0, MyLdrDllNotification, NULL, &pvCookie);
 
 // 测试DLL 装载
 HMODULE hLoad = ::LoadLibraryW(L"mshtml.dll");
 Sleep(1000);
         // 测试 DLL 卸载
 ::FreeLibrary(hLoad);
         // 清除
 if (pvCookie)
 {
  pLdrUnregisterDllNotification(pvCookie);
  pvCookie = NULL;
 }
 return 0;
}
 
运行程序, 输出如下。可以证实以上代码监听了 mshtml.dll 的装载和卸载。 而且系统自动装载的其他DLL也被监视到。
Dll Loaded: C:\Windows\system32\mshtml.dll
Dll Loaded: C:\Windows\system32\msls31.dll
Dll Loaded: C:\Windows\system32\VERSION.dll
Dll Unloaded: C:\Windows\system32\mshtml.dll
Dll Unloaded: C:\Windows\system32\VERSION.dll
Dll Unloaded: C:\Windows\system32\msls31.dll
 本文转自 陈本峰 51CTO博客,原文链接:http://blog.51cto.com/wingeek/274017,如需转载请自行联系原作者
你可能感兴趣的文章
How to check Ubuntu version
查看>>
php 解析xml 的四种方法(转)
查看>>
qt 试用 (3)配置编译源代码及调试
查看>>
(转)用CSS3移除点击交互元素的高亮背景
查看>>
遍历json获得数据的几种方法
查看>>
php Collection类的设计
查看>>
c++中的计时器代码
查看>>
语义Web和本体开发相关技术
查看>>
Mysql集群读写分离(Amoeba)
查看>>
Quest for sane signals in Qt - step 1 (hand coding a Q_OBJECT)
查看>>
SpringBoot的注解:@SpringBootApplication注解 vs @EnableAutoConfiguration+@ComponentScan+@Configuration...
查看>>
SQL Server性能调优之执行计划深度剖析 第一节 浅析SQL执行的过程
查看>>
利用自定义IHttpModule来实现URL地址重写
查看>>
在网页上嵌入 PowerPoint 演示文稿
查看>>
javascript日期格式化函数,跟C#中的使用方法类似
查看>>
Android杂谈--Activity、Window、View的关系
查看>>
使用delphi 开发多层应用(十)安全访问服务器
查看>>
JavaScript计算字符串中每个字符出现的次数
查看>>
mvc中的ViewData用到webfrom中去
查看>>
小白学数据分析------>描述性统计术语汇总
查看>>