-
Notifications
You must be signed in to change notification settings - Fork 3
/
event.c
259 lines (200 loc) · 5.38 KB
/
event.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright (c) 2004 Security Architects Corporation. All rights reserved.
*
* Module Name:
*
* event.c
*
* Abstract:
*
* This module implements various event hooking routines.
*
* Author:
*
* Eugene Tsyrklevich 09-Mar-2004
*
* Revision History:
*
* None.
*/
#include <NTDDK.h>
#include "event.h"
#include "policy.h"
#include "pathproc.h"
#include "hookproc.h"
#include "accessmask.h"
#include "learn.h"
#include "log.h"
#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, InitEventHooks)
#endif
fpZwCreateEventPair OriginalNtCreateEventPair = NULL;
fpZwOpenEventPair OriginalNtOpenEventPair = NULL;
fpZwCreateEvent OriginalNtCreateEvent = NULL;
fpZwOpenEvent OriginalNtOpenEvent = NULL;
/*
* HookedNtCreateEvent()
*
* Description:
* This function mediates the NtCreateEvent() system service and checks the
* provided event name against the global and current process security policies.
*
* NOTE: ZwCreateEvent creates or opens an event object. [NAR]
*
* Parameters:
* Those of NtCreateEvent().
*
* Returns:
* STATUS_ACCESS_DENIED if the call does not pass the security policy check.
* Otherwise, NTSTATUS returned by NtCreateEvent().
*/
NTSTATUS
NTAPI
HookedNtCreateEvent
(
OUT PHANDLE EventHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
IN EVENT_TYPE EventType,
IN BOOLEAN InitialState
)
{
PCHAR FunctionName = "HookedNtCreateEvent";
HOOK_ROUTINE_START(EVENT);
ASSERT(OriginalNtCreateEvent);
rc = OriginalNtCreateEvent(EventHandle, DesiredAccess, ObjectAttributes, EventType, InitialState);
HOOK_ROUTINE_FINISH(EVENT);
}
/*
* HookedNtOpenEvent()
*
* Description:
* This function mediates the NtOpenEvent() system service and checks the
* provided event name against the global and current process security policies.
*
* NOTE: ZwOpenEvent opens an event object. [NAR]
*
* Parameters:
* Those of NtOpenEvent().
*
* Returns:
* STATUS_ACCESS_DENIED if the call does not pass the security policy check.
* Otherwise, NTSTATUS returned by NtOpenEvent().
*/
NTSTATUS
NTAPI
HookedNtOpenEvent
(
OUT PHANDLE EventHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes
)
{
PCHAR FunctionName = "HookedNtOpenEvent";
HOOK_ROUTINE_START(EVENT);
ASSERT(OriginalNtOpenEvent);
rc = OriginalNtOpenEvent(EventHandle, DesiredAccess, ObjectAttributes);
HOOK_ROUTINE_FINISH(EVENT);
}
/*
* HookedNtCreateEventPair()
*
* Description:
* This function mediates the NtCreateEventPair() system service and checks the
* provided eventpair name against the global and current process security policies.
*
* NOTE: ZwCreateEventPair creates or opens an event pair object. [NAR]
*
* Parameters:
* Those of NtCreateEventPair().
*
* Returns:
* STATUS_ACCESS_DENIED if the call does not pass the security policy check.
* Otherwise, NTSTATUS returned by NtCreateEventPair().
*/
NTSTATUS
NTAPI
HookedNtCreateEventPair
(
OUT PHANDLE EventPairHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes
)
{
PCHAR FunctionName = "HookedNtCreateEventPair";
HOOK_ROUTINE_START(EVENT);
ASSERT(OriginalNtCreateEventPair);
rc = OriginalNtCreateEventPair(EventPairHandle, DesiredAccess, ObjectAttributes);
HOOK_ROUTINE_FINISH(EVENT);
}
/*
* HookedNtOpenEventPair()
*
* Description:
* This function mediates the NtOpenEventPair() system service and checks the
* provided event name against the global and current process security policies.
*
* NOTE: ZwOpenEventPair opens an event pair object. [NAR]
*
* Parameters:
* Those of NtOpenEventPair().
*
* Returns:
* STATUS_ACCESS_DENIED if the call does not pass the security policy check.
* Otherwise, NTSTATUS returned by NtOpenEventPair().
*/
NTSTATUS
NTAPI
HookedNtOpenEventPair
(
OUT PHANDLE EventPairHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes
)
{
PCHAR FunctionName = "HookedNtOpenEventPair";
HOOK_ROUTINE_START(EVENT);
ASSERT(OriginalNtOpenEventPair);
rc = OriginalNtOpenEventPair(EventPairHandle, DesiredAccess, ObjectAttributes);
HOOK_ROUTINE_FINISH(EVENT);
}
/*
* InitEventHooks()
*
* Description:
* Initializes all the mediated event operation pointers. The "OriginalFunction" pointers
* are initialized by InstallSyscallsHooks() that must be called prior to this function.
*
* NOTE: Called once during driver initialization (DriverEntry()).
*
* Parameters:
* None.
*
* Returns:
* TRUE to indicate success, FALSE if failed.
*/
BOOLEAN
InitEventHooks()
{
if ( (OriginalNtCreateEventPair = (fpZwCreateEventPair) ZwCalls[ZW_CREATE_EVENT_PAIR_INDEX].OriginalFunction) == NULL)
{
LOG(LOG_SS_EVENT, LOG_PRIORITY_DEBUG, ("InitEventHooks: OriginalNtCreateEventPair is NULL\n"));
return FALSE;
}
if ( (OriginalNtOpenEventPair = (fpZwOpenEventPair) ZwCalls[ZW_OPEN_EVENT_PAIR_INDEX].OriginalFunction) == NULL)
{
LOG(LOG_SS_EVENT, LOG_PRIORITY_DEBUG, ("InitEventHooks: OriginalNtOpenEventPair is NULL\n"));
return FALSE;
}
if ( (OriginalNtCreateEvent = (fpZwCreateEvent) ZwCalls[ZW_CREATE_EVENT_INDEX].OriginalFunction) == NULL)
{
LOG(LOG_SS_EVENT, LOG_PRIORITY_DEBUG, ("InitEventHooks: OriginalNtCreateEvent is NULL\n"));
return FALSE;
}
if ( (OriginalNtOpenEvent = (fpZwOpenEvent) ZwCalls[ZW_OPEN_EVENT_INDEX].OriginalFunction) == NULL)
{
LOG(LOG_SS_EVENT, LOG_PRIORITY_DEBUG, ("InitEventHooks: OriginalNtOpenEvent is NULL\n"));
return FALSE;
}
return TRUE;
}