-
Notifications
You must be signed in to change notification settings - Fork 3
/
port.c
318 lines (246 loc) · 7.5 KB
/
port.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Copyright (c) 2004 Security Architects Corporation. All rights reserved.
*
* Module Name:
*
* port.c
*
* Abstract:
*
* This module implements various port object hooking routines.
*
* Author:
*
* Eugene Tsyrklevich 25-Mar-2004
*
* Revision History:
*
* None.
*/
#include "port.h"
#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, InitPortHooks)
#endif
fpZwCreatePort OriginalNtCreatePort = NULL;
fpZwCreateWaitablePort OriginalNtCreateWaitablePort = NULL;
fpZwConnectPort OriginalNtConnectPort = NULL;
fpZwSecureConnectPort OriginalNtSecureConnectPort = NULL;
/*
* HookedNtCreatePort()
*
* Description:
* This function mediates the NtCreatePort() system service and checks the
* provided port name against the global and current process security policies.
*
* NOTE: ZwCreatePort creates a port object. [NAR]
*
* Parameters:
* Those of NtCreatePort().
*
* Returns:
* STATUS_ACCESS_DENIED if the call does not pass the security policy check.
* Otherwise, NTSTATUS returned by NtCreatePort().
*/
NTSTATUS
NTAPI
HookedNtCreatePort
(
OUT PHANDLE PortHandle,
IN POBJECT_ATTRIBUTES ObjectAttributes,
IN ULONG MaxDataSize,
IN ULONG MaxMessageSize,
IN ULONG Reserved
)
{
PCHAR FunctionName = "HookedNtCreatePort";
HOOK_ROUTINE_START_OPTYPE(PORT, OP_PORT_CREATE);
ASSERT(OriginalNtCreatePort);
rc = OriginalNtCreatePort(PortHandle, ObjectAttributes, MaxDataSize, MaxMessageSize, Reserved);
HOOK_ROUTINE_FINISH_OPTYPE(PORT, OP_PORT_CREATE);
}
/*
* HookedNtCreateWaitablePort()
*
* Description:
* This function mediates the NtCreateWaitablePort() system service and checks the
* provided port name against the global and current process security policies.
*
* NOTE: ZwCreateWaitablePort creates a waitable port object. [NAR]
*
* Parameters:
* Those of NtCreateWaitablePort().
*
* Returns:
* STATUS_ACCESS_DENIED if the call does not pass the security policy check.
* Otherwise, NTSTATUS returned by NtCreateWaitablePort().
*/
NTSTATUS
NTAPI
HookedNtCreateWaitablePort
(
OUT PHANDLE PortHandle,
IN POBJECT_ATTRIBUTES ObjectAttributes,
IN ULONG MaxDataSize,
IN ULONG MaxMessageSize,
IN ULONG Reserved
)
{
PCHAR FunctionName = "HookedNtCreateWaitablePort";
HOOK_ROUTINE_START_OPTYPE(PORT, OP_PORT_CREATE);
ASSERT(OriginalNtCreateWaitablePort);
rc = OriginalNtCreateWaitablePort(PortHandle, ObjectAttributes, MaxDataSize, MaxMessageSize, Reserved);
HOOK_ROUTINE_FINISH_OPTYPE(PORT, OP_PORT_CREATE);
}
/*
* HookedNtConnectPort()
*
* Description:
* This function mediates the NtConnectPort() system service and checks the
* provided port name against the global and current process security policies.
*
* NOTE: ZwConnectPort creates a port connected to a named port. [NAR]
*
* Parameters:
* Those of NtConnectPort().
*
* Returns:
* STATUS_ACCESS_DENIED if the call does not pass the security policy check.
* Otherwise, NTSTATUS returned by NtConnectPort().
*/
NTSTATUS
NTAPI
HookedNtConnectPort
(
OUT PHANDLE PortHandle,
IN PUNICODE_STRING PortName,
IN PSECURITY_QUALITY_OF_SERVICE SecurityQos,
IN OUT PPORT_SECTION_WRITE WriteSection OPTIONAL,
IN OUT PPORT_SECTION_READ ReadSection OPTIONAL,
OUT PULONG MaxMessageSize OPTIONAL,
IN OUT PVOID ConnectData OPTIONAL,
IN OUT PULONG ConnectDataLength OPTIONAL
)
{
PCHAR FunctionName = "HookedNtConnectPort";
UNICODE_STRING usInputPortName;
CHAR PORTNAME[MAX_PATH];
ANSI_STRING asPortName;
HOOK_ROUTINE_ENTER();
if (!VerifyUnicodeString(PortName, &usInputPortName))
{
LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("HookedNtConnectPort: VerifyUnicodeString failed\n"));
HOOK_ROUTINE_EXIT( STATUS_ACCESS_DENIED );
}
if (_snprintf(PORTNAME, MAX_PATH, "%S", usInputPortName.Buffer) < 0)
{
LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("%s: Port name '%S' is too long\n", FunctionName, usInputPortName.Buffer));
HOOK_ROUTINE_EXIT( STATUS_ACCESS_DENIED );
}
if (LearningMode == FALSE)
{
POLICY_CHECK_OPTYPE_NAME(PORT, OP_PORT_CONNECT);
}
ASSERT(OriginalNtConnectPort);
rc = OriginalNtConnectPort(PortHandle, PortName, SecurityQos, WriteSection, ReadSection, MaxMessageSize,
ConnectData, ConnectDataLength);
HOOK_ROUTINE_FINISH_OBJECTNAME_OPTYPE(PORT, PORTNAME, OP_PORT_CONNECT);
}
/*
* HookedNtSecureConnectPort()
*
* Description:
* This function mediates the NtSecureConnectPort() system service and checks the
* provided port name against the global and current process security policies.
*
* NOTE: ZwSecureConnectPort creates a port connected to a named port. [NAR]
*
* Parameters:
* Those of NtSecureConnectPort().
*
* Returns:
* STATUS_ACCESS_DENIED if the call does not pass the security policy check.
* Otherwise, NTSTATUS returned by NtSecureConnectPort().
*/
NTSTATUS
NTAPI
HookedNtSecureConnectPort
(
OUT PHANDLE PortHandle,
IN PUNICODE_STRING PortName,
IN PSECURITY_QUALITY_OF_SERVICE SecurityQos,
IN OUT PPORT_SECTION_WRITE WriteSection OPTIONAL,
IN PSID ServerSid OPTIONAL,
IN OUT PPORT_SECTION_READ ReadSection OPTIONAL,
OUT PULONG MaxMessageSize OPTIONAL,
IN OUT PVOID ConnectData OPTIONAL,
IN OUT PULONG ConnectDataLength OPTIONAL
)
{
PCHAR FunctionName = "HookedNtSecureConnectPort";
UNICODE_STRING usInputPortName;
CHAR PORTNAME[MAX_PATH];
ANSI_STRING asPortName;
HOOK_ROUTINE_ENTER();
if (!VerifyUnicodeString(PortName, &usInputPortName))
{
LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("HookedNtSecureConnectPort: VerifyUnicodeString failed\n"));
HOOK_ROUTINE_EXIT( STATUS_ACCESS_DENIED );
}
asPortName.Length = 0;
asPortName.MaximumLength = MAX_PATH - 1;
asPortName.Buffer = PORTNAME;
if (! NT_SUCCESS(RtlUnicodeStringToAnsiString(&asPortName, &usInputPortName, FALSE)))
{
LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("HookedNtSecureConnectPort: RtlUnicodeStringToAnsiString failed\n"));
HOOK_ROUTINE_EXIT( STATUS_ACCESS_DENIED );
}
PORTNAME[asPortName.Length] = 0;
if (LearningMode == FALSE)
{
POLICY_CHECK_OPTYPE_NAME(PORT, OP_PORT_CONNECT);
}
ASSERT(OriginalNtSecureConnectPort);
rc = OriginalNtSecureConnectPort(PortHandle, PortName, SecurityQos, WriteSection, ServerSid, ReadSection,
MaxMessageSize, ConnectData, ConnectDataLength);
HOOK_ROUTINE_FINISH_OBJECTNAME_OPTYPE(PORT, PORTNAME, OP_PORT_CONNECT);
}
/*
* InitPortHooks()
*
* Description:
* Initializes all the mediated port 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
InitPortHooks()
{
if ( (OriginalNtCreatePort = (fpZwCreatePort) ZwCalls[ZW_CREATE_PORT_INDEX].OriginalFunction) == NULL)
{
LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("InitPortHooks: OriginalNtCreatePort is NULL\n"));
return FALSE;
}
if ( (OriginalNtCreateWaitablePort = (fpZwCreateWaitablePort) ZwCalls[ZW_CREATE_WAITPORT_INDEX].OriginalFunction) == NULL)
{
LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("InitPortHooks: OriginalNtCreateWaitablePort is NULL\n"));
return FALSE;
}
if ( (OriginalNtConnectPort = (fpZwConnectPort) ZwCalls[ZW_CONNECT_PORT_INDEX].OriginalFunction) == NULL)
{
LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("InitPortHooks: OriginalNtConnectPort is NULL\n"));
return FALSE;
}
if ( (OriginalNtSecureConnectPort = (fpZwSecureConnectPort) ZwCalls[ZW_SECURECONNECT_PORT_INDEX].OriginalFunction) == NULL)
{
LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("InitPortHooks: OriginalNtSecureConnectPort is NULL\n"));
return FALSE;
}
return TRUE;
}