Ring0创建事件Ring3设置事件

同步事件(synchronizationEvent)
当事件对象为激发时,如遇到KeWaitForXX等内核函数,事件对象则自动变回未激发态
通知事件(NotificationEvent)
当事件对象为激发时,如遇到KeWaitForXX等内核函数,事件对象则不会自动变回未激发态

Ring0(创建事件).h

1 #include <ntifs.h>
2 
3 
4 #define EVENT_NAME L"\BaseNamedObjects\Ring0KernelEvent"
5 
6 VOID DriverUnload(PDRIVER_OBJECT DriverObject);

Ring0(创建事件).c

 1 #include "Ring0(创建事件).h"
 2 
 3 
 4 PKEVENT __Event;
 5 PKEVENT __EventHandle;
 6 
 7 NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegisterPath)
 8 {
 9     NTSTATUS Status = STATUS_SUCCESS;
10     PDEVICE_OBJECT  DeviceObject = NULL;
11     UNICODE_STRING EventName;
12 
13     DbgPrint("DriverEntry()
");
14     DriverObject->DriverUnload = DriverUnload;
15     
16     RtlInitUnicodeString(&EventName, EVENT_NAME);
17 
18     __Event = IoCreateNotificationEvent(
19         &EventName,//指向一个以NULL结尾的UNICODE字符串,该字符串包含事件的名称
20         &__EventHandle//指向一个地址,该地址存储了事件对象的句柄,
21     );//创建或者打开一个命名的通知事件用于通知一个或多个线程一个事件已经发生.
22     /*
23     如果不存在,则创建并打开,并设置事件为有信号状态,如果已存在,则打开
24     当事件处于有信号状态时,它会一直保持,直到被明确的清除
25     通知事件,像同步事件一样,被用于同步,但是又不像同步事件,通知事件不是自动重置的
26     一旦通知事件处于有信号状态,会一直保持,直到被明确重设(通过调用KeClearEvent or KeResetEvent)
27     */
28 
29 
30     return Status;
31 }
32 
33 
34 VOID DriverUnload(PDRIVER_OBJECT DriverObject)
35 {
36     DbgPrint("DriverUnload()
");
37 
38     if (__EventHandle != NULL)
39     {
40         KeClearEvent(__Event);
41 
42         ZwClose(__EventHandle);
43 
44         __EventHandle = NULL;
45         __Event = NULL;
46     }
47 
48 }

RIng3(设置事件).cpp

 1 // RIng3(设置事件).cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <windows.h>
 6 #include <iostream>
 7 
 8 using namespace std;
 9 
10 int main()
11 {
12     HANDLE EventHandle = NULL;
13     while (1)
14     {
15         EventHandle = OpenEvent(
16             SYNCHRONIZE, //同步  The right to use the object for synchronization. This enables a thread to wait until the object is in the signaled state.
17             FALSE, 
18             L"Global\Ring0KernelEvent"
19         );
20 
21         if (EventHandle == NULL)
22         {
23             continue;
24         }
25         break;
26 
27     }
28     cout << "Ring3的等待" << endl;
29     while (1)
30     {
31         int Index = WaitForSingleObject(EventHandle, 3000);
32         
33         Index -= WAIT_OBJECT_0;
34 
35         if (Index == WAIT_TIMEOUT)
36         {
37 
38             //注意:
39             //这里当驱动卸载并关闭事件时事件对象是不能够得到及时的销毁 因为应用层占用了该对象 
40             //所以我们长时间等待不到授信 就关闭并重新打开
41             
42             if (EventHandle != NULL)
43             {
44                 CloseHandle(EventHandle);
45                 EventHandle = NULL;
46                 EventHandle = OpenEvent(
47                     SYNCHRONIZE, 
48                     FALSE, 
49                     L"Global\Ring0KernelEvent"
50                 );
51 
52                 if (EventHandle == NULL)
53                 {
54                     cout << "对象已经不存在" << endl;
55                     break;
56                 }
57             }
58             continue;
59         }
60 
61         if (Index == 0)
62         {
63             cout << "Ring0触发Ring3" << endl;
64         }
65 
66 
67         if (Index == WAIT_FAILED)
68         {
69             break;
70         }
71 
72         Sleep(1);
73 
74 
75 
76     }
77 
78     cout << "Input AnyKey To Exit" << endl;
79 
80     getchar();
81 
82     if (EventHandle != NULL)
83     {
84         CloseHandle(EventHandle);
85         EventHandle = NULL;
86 
87     }
88     return 0;
89 }
原文地址:https://www.cnblogs.com/1228073191Blog/p/7497945.html