summaryrefslogtreecommitdiff
path: root/port.c
diff options
context:
space:
mode:
authortumagonx2017-08-08 10:54:53 +0700
committertumagonx2017-08-08 10:54:53 +0700
commit2acec63b2ed75bf4b71ad257db573c4b8f9639e7 (patch)
treea8bea139ddd26116d44ea182b0b8436f2162e6e3 /port.c
initial commit
Diffstat (limited to 'port.c')
-rw-r--r--port.c318
1 files changed, 318 insertions, 0 deletions
diff --git a/port.c b/port.c
new file mode 100644
index 0000000..bd99908
--- /dev/null
+++ b/port.c
@@ -0,0 +1,318 @@
1/*
2 * Copyright (c) 2004 Security Architects Corporation. All rights reserved.
3 *
4 * Module Name:
5 *
6 * port.c
7 *
8 * Abstract:
9 *
10 * This module implements various port object hooking routines.
11 *
12 * Author:
13 *
14 * Eugene Tsyrklevich 25-Mar-2004
15 *
16 * Revision History:
17 *
18 * None.
19 */
20
21
22#include "port.h"
23
24
25#ifdef ALLOC_PRAGMA
26#pragma alloc_text (INIT, InitPortHooks)
27#endif
28
29
30fpZwCreatePort OriginalNtCreatePort = NULL;
31fpZwCreateWaitablePort OriginalNtCreateWaitablePort = NULL;
32
33fpZwConnectPort OriginalNtConnectPort = NULL;
34fpZwSecureConnectPort OriginalNtSecureConnectPort = NULL;
35
36
37/*
38 * HookedNtCreatePort()
39 *
40 * Description:
41 * This function mediates the NtCreatePort() system service and checks the
42 * provided port name against the global and current process security policies.
43 *
44 * NOTE: ZwCreatePort creates a port object. [NAR]
45 *
46 * Parameters:
47 * Those of NtCreatePort().
48 *
49 * Returns:
50 * STATUS_ACCESS_DENIED if the call does not pass the security policy check.
51 * Otherwise, NTSTATUS returned by NtCreatePort().
52 */
53
54NTSTATUS
55NTAPI
56HookedNtCreatePort
57(
58 OUT PHANDLE PortHandle,
59 IN POBJECT_ATTRIBUTES ObjectAttributes,
60 IN ULONG MaxDataSize,
61 IN ULONG MaxMessageSize,
62 IN ULONG Reserved
63)
64{
65 PCHAR FunctionName = "HookedNtCreatePort";
66
67
68 HOOK_ROUTINE_START_OPTYPE(PORT, OP_PORT_CREATE);
69
70
71 ASSERT(OriginalNtCreatePort);
72
73 rc = OriginalNtCreatePort(PortHandle, ObjectAttributes, MaxDataSize, MaxMessageSize, Reserved);
74
75
76 HOOK_ROUTINE_FINISH_OPTYPE(PORT, OP_PORT_CREATE);
77}
78
79
80
81/*
82 * HookedNtCreateWaitablePort()
83 *
84 * Description:
85 * This function mediates the NtCreateWaitablePort() system service and checks the
86 * provided port name against the global and current process security policies.
87 *
88 * NOTE: ZwCreateWaitablePort creates a waitable port object. [NAR]
89 *
90 * Parameters:
91 * Those of NtCreateWaitablePort().
92 *
93 * Returns:
94 * STATUS_ACCESS_DENIED if the call does not pass the security policy check.
95 * Otherwise, NTSTATUS returned by NtCreateWaitablePort().
96 */
97
98NTSTATUS
99NTAPI
100HookedNtCreateWaitablePort
101(
102 OUT PHANDLE PortHandle,
103 IN POBJECT_ATTRIBUTES ObjectAttributes,
104 IN ULONG MaxDataSize,
105 IN ULONG MaxMessageSize,
106 IN ULONG Reserved
107)
108{
109 PCHAR FunctionName = "HookedNtCreateWaitablePort";
110
111
112 HOOK_ROUTINE_START_OPTYPE(PORT, OP_PORT_CREATE);
113
114
115 ASSERT(OriginalNtCreateWaitablePort);
116
117 rc = OriginalNtCreateWaitablePort(PortHandle, ObjectAttributes, MaxDataSize, MaxMessageSize, Reserved);
118
119
120 HOOK_ROUTINE_FINISH_OPTYPE(PORT, OP_PORT_CREATE);
121}
122
123
124
125/*
126 * HookedNtConnectPort()
127 *
128 * Description:
129 * This function mediates the NtConnectPort() system service and checks the
130 * provided port name against the global and current process security policies.
131 *
132 * NOTE: ZwConnectPort creates a port connected to a named port. [NAR]
133 *
134 * Parameters:
135 * Those of NtConnectPort().
136 *
137 * Returns:
138 * STATUS_ACCESS_DENIED if the call does not pass the security policy check.
139 * Otherwise, NTSTATUS returned by NtConnectPort().
140 */
141
142NTSTATUS
143NTAPI
144HookedNtConnectPort
145(
146 OUT PHANDLE PortHandle,
147 IN PUNICODE_STRING PortName,
148 IN PSECURITY_QUALITY_OF_SERVICE SecurityQos,
149 IN OUT PPORT_SECTION_WRITE WriteSection OPTIONAL,
150 IN OUT PPORT_SECTION_READ ReadSection OPTIONAL,
151 OUT PULONG MaxMessageSize OPTIONAL,
152 IN OUT PVOID ConnectData OPTIONAL,
153 IN OUT PULONG ConnectDataLength OPTIONAL
154)
155{
156 PCHAR FunctionName = "HookedNtConnectPort";
157 UNICODE_STRING usInputPortName;
158 CHAR PORTNAME[MAX_PATH];
159 ANSI_STRING asPortName;
160
161
162 HOOK_ROUTINE_ENTER();
163
164
165 if (!VerifyUnicodeString(PortName, &usInputPortName))
166 {
167 LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("HookedNtConnectPort: VerifyUnicodeString failed\n"));
168 HOOK_ROUTINE_EXIT( STATUS_ACCESS_DENIED );
169 }
170
171
172 if (_snprintf(PORTNAME, MAX_PATH, "%S", usInputPortName.Buffer) < 0)
173 {
174 LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("%s: Port name '%S' is too long\n", FunctionName, usInputPortName.Buffer));
175 HOOK_ROUTINE_EXIT( STATUS_ACCESS_DENIED );
176 }
177
178
179 if (LearningMode == FALSE)
180 {
181 POLICY_CHECK_OPTYPE_NAME(PORT, OP_PORT_CONNECT);
182 }
183
184
185 ASSERT(OriginalNtConnectPort);
186
187 rc = OriginalNtConnectPort(PortHandle, PortName, SecurityQos, WriteSection, ReadSection, MaxMessageSize,
188 ConnectData, ConnectDataLength);
189
190
191 HOOK_ROUTINE_FINISH_OBJECTNAME_OPTYPE(PORT, PORTNAME, OP_PORT_CONNECT);
192}
193
194
195
196/*
197 * HookedNtSecureConnectPort()
198 *
199 * Description:
200 * This function mediates the NtSecureConnectPort() system service and checks the
201 * provided port name against the global and current process security policies.
202 *
203 * NOTE: ZwSecureConnectPort creates a port connected to a named port. [NAR]
204 *
205 * Parameters:
206 * Those of NtSecureConnectPort().
207 *
208 * Returns:
209 * STATUS_ACCESS_DENIED if the call does not pass the security policy check.
210 * Otherwise, NTSTATUS returned by NtSecureConnectPort().
211 */
212
213NTSTATUS
214NTAPI
215HookedNtSecureConnectPort
216(
217 OUT PHANDLE PortHandle,
218 IN PUNICODE_STRING PortName,
219 IN PSECURITY_QUALITY_OF_SERVICE SecurityQos,
220 IN OUT PPORT_SECTION_WRITE WriteSection OPTIONAL,
221 IN PSID ServerSid OPTIONAL,
222 IN OUT PPORT_SECTION_READ ReadSection OPTIONAL,
223 OUT PULONG MaxMessageSize OPTIONAL,
224 IN OUT PVOID ConnectData OPTIONAL,
225 IN OUT PULONG ConnectDataLength OPTIONAL
226)
227{
228 PCHAR FunctionName = "HookedNtSecureConnectPort";
229 UNICODE_STRING usInputPortName;
230 CHAR PORTNAME[MAX_PATH];
231 ANSI_STRING asPortName;
232
233
234 HOOK_ROUTINE_ENTER();
235
236
237 if (!VerifyUnicodeString(PortName, &usInputPortName))
238 {
239 LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("HookedNtSecureConnectPort: VerifyUnicodeString failed\n"));
240 HOOK_ROUTINE_EXIT( STATUS_ACCESS_DENIED );
241 }
242
243
244 asPortName.Length = 0;
245 asPortName.MaximumLength = MAX_PATH - 1;
246 asPortName.Buffer = PORTNAME;
247
248 if (! NT_SUCCESS(RtlUnicodeStringToAnsiString(&asPortName, &usInputPortName, FALSE)))
249 {
250 LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("HookedNtSecureConnectPort: RtlUnicodeStringToAnsiString failed\n"));
251 HOOK_ROUTINE_EXIT( STATUS_ACCESS_DENIED );
252 }
253
254 PORTNAME[asPortName.Length] = 0;
255
256
257 if (LearningMode == FALSE)
258 {
259 POLICY_CHECK_OPTYPE_NAME(PORT, OP_PORT_CONNECT);
260 }
261
262
263 ASSERT(OriginalNtSecureConnectPort);
264
265 rc = OriginalNtSecureConnectPort(PortHandle, PortName, SecurityQos, WriteSection, ServerSid, ReadSection,
266 MaxMessageSize, ConnectData, ConnectDataLength);
267
268
269 HOOK_ROUTINE_FINISH_OBJECTNAME_OPTYPE(PORT, PORTNAME, OP_PORT_CONNECT);
270}
271
272
273
274/*
275 * InitPortHooks()
276 *
277 * Description:
278 * Initializes all the mediated port operation pointers. The "OriginalFunction" pointers
279 * are initialized by InstallSyscallsHooks() that must be called prior to this function.
280 *
281 * NOTE: Called once during driver initialization (DriverEntry()).
282 *
283 * Parameters:
284 * None.
285 *
286 * Returns:
287 * TRUE to indicate success, FALSE if failed.
288 */
289
290BOOLEAN
291InitPortHooks()
292{
293 if ( (OriginalNtCreatePort = (fpZwCreatePort) ZwCalls[ZW_CREATE_PORT_INDEX].OriginalFunction) == NULL)
294 {
295 LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("InitPortHooks: OriginalNtCreatePort is NULL\n"));
296 return FALSE;
297 }
298
299 if ( (OriginalNtCreateWaitablePort = (fpZwCreateWaitablePort) ZwCalls[ZW_CREATE_WAITPORT_INDEX].OriginalFunction) == NULL)
300 {
301 LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("InitPortHooks: OriginalNtCreateWaitablePort is NULL\n"));
302 return FALSE;
303 }
304
305 if ( (OriginalNtConnectPort = (fpZwConnectPort) ZwCalls[ZW_CONNECT_PORT_INDEX].OriginalFunction) == NULL)
306 {
307 LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("InitPortHooks: OriginalNtConnectPort is NULL\n"));
308 return FALSE;
309 }
310
311 if ( (OriginalNtSecureConnectPort = (fpZwSecureConnectPort) ZwCalls[ZW_SECURECONNECT_PORT_INDEX].OriginalFunction) == NULL)
312 {
313 LOG(LOG_SS_PORT, LOG_PRIORITY_DEBUG, ("InitPortHooks: OriginalNtSecureConnectPort is NULL\n"));
314 return FALSE;
315 }
316
317 return TRUE;
318}