[Windows编程] 监视DLL装载/卸载

简介:
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,如需转载请自行联系原作者



相关文章
|
17天前
|
Windows
如何快速卸载windows电脑的一些软件?
如何快速卸载windows电脑的一些软件?
如何快速卸载windows电脑的一些软件?
|
27天前
|
编译器 开发工具 C语言
解锁QtCreator跨界神技!Windows下轻松驾驭OpenCV动态库,让你的跨平台开发如虎添翼,秒变视觉编程大师!
【8月更文挑战第4天】QtCreator是一款强大的跨平台IDE,便于创建多平台应用。本教程教你如何在Windows环境下集成OpenCV库至Qt项目。首先,下载匹配MinGW的OpenCV预编译版并解压。接着,在QtCreator中新建或打开项目,并在.pro文件中添加OpenCV的头文件和库文件路径。确保编译器设置正确。随后编写测试代码,例如加载和显示图片,并进行编译运行。完成这些步骤后,你就能在QtCreator中利用OpenCV进行图像处理开发了。
66 6
|
17天前
|
XML C# 数据格式
绝密档案曝光!Windows平台如何深挖一个dll背后的神秘依赖,揭露隐藏的秘密!
【8月更文挑战第14天】在Windows系统中,动态链接库(DLL)对程序运行至关重要。了解DLL的依赖关系有助于软件的调试与优化。本文以具体案例演示如何查看DLL依赖。首先确保环境已安装Windows及具备基本开发知识。
31 0
|
3月前
|
Java C++
jni编程(windows+JDK11+clion)
jni编程(windows+JDK11+clion)
39 1
|
3月前
|
关系型数据库 MySQL 程序员
Windows版本 - MySQL卸载
Windows版本 - MySQL卸载
33 0
|
3月前
|
关系型数据库 MySQL 程序员
Windows软件卸载基本思路
Windows软件卸载基本思路
34 0
|
4月前
|
API C++ Windows
windows编程入门_链接错误的配置
windows编程入门_链接错误的配置
44 0
|
4月前
|
Windows
火山中文编程 -- 第一个windows程序
火山中文编程 -- 第一个windows程序
27 0
|
4月前
|
编译器 API Windows
windows编程基础
windows编程基础
33 0
|
4月前
|
Windows
win32编程 -- windows绘图操作
win32编程 -- windows绘图操作
56 0
下一篇
云函数