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
319
320
321
322
323
324
325
|
/********************************************************/
/* ntapi: Native API core library */
/* Copyright (C) 2013--2017 Z. Gilboa */
/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
/********************************************************/
#include <psxtypes/psxtypes.h>
#include <ntapi/nt_guid.h>
#include <ntapi/nt_port.h>
#include <ntapi/nt_string.h>
#include "ntapi_impl.h"
typedef wchar16_t __port_service_prefix[6];
static const __port_service_prefix __port_service_null = {0};
static const __port_service_prefix __port_service_prefixes[NT_PORT_TYPE_CAP][NT_PORT_SUBTYPE_CAP] = {
{{'s','v','c','a','n','y'}},
{{'n','t','c','t','t','y'}},
{{'v','m','o','u','n','t'}},
{{'d','a','e','m','o','n'}},
{{'b','r','i','d','g','e'}},
{{'n','t','p','r','o','c'}},
{{'s','e','m','c','t','l'}},
{{'s','e','m','s','v','c'}},
{{'m','s','q','c','t','l'}},
{{'m','s','q','s','v','c'}},
{{'a','f','l','c','t','l'}},
{{'a','f','l','s','v','c'}}};
static const nt_guid __port_guids[NT_PORT_TYPE_CAP][NT_PORT_SUBTYPE_CAP] = {
{NT_PORT_GUID_DEFAULT},
{NT_PORT_GUID_SUBSYSTEM},
{NT_PORT_GUID_VMOUNT},
{NT_PORT_GUID_DAEMON},
{NT_PORT_GUID_BRIDGE},
{NT_PORT_GUID_NTPROC},
{NT_PORT_GUID_SEMCTL},
{NT_PORT_GUID_SEMSVC},
{NT_PORT_GUID_MSQCTL},
{NT_PORT_GUID_MSQSVC},
{NT_PORT_GUID_AFLCTL},
{NT_PORT_GUID_AFLSVC}};
int32_t __stdcall __ntapi_tt_port_guid_from_type(
__out nt_guid * guid,
__in nt_port_type type,
__in nt_port_subtype subtype)
{
const nt_guid * src_guid;
if ((type >= NT_PORT_TYPE_CAP) || (subtype >= NT_PORT_SUBTYPE_CAP))
return NT_STATUS_INVALID_PARAMETER;
src_guid = &(__port_guids[type][subtype]);
__ntapi->tt_aligned_block_memcpy(
(uintptr_t *)guid,
(uintptr_t *)src_guid,
sizeof(nt_guid));
return NT_STATUS_SUCCESS;
}
int32_t __stdcall __ntapi_tt_port_type_from_guid(
__out nt_port_type * type,
__out nt_port_subtype * subtype,
__in const nt_guid * guid)
{
int itype;
int isubtype;
for (itype=0; itype<NT_PORT_TYPE_CAP; itype++) {
for (isubtype=0; isubtype<NT_PORT_SUBTYPE_CAP; isubtype++) {
if (!(__ntapi_tt_guid_compare(
&__port_guids[itype][isubtype],
guid))) {
*type = (nt_port_type)itype;
*subtype = (nt_port_subtype)isubtype;
return NT_STATUS_SUCCESS;
}
}
}
return NT_STATUS_NOT_FOUND;
}
int32_t __stdcall __ntapi_tt_port_generate_keys(
__out nt_port_keys * keys)
{
int32_t status;
nt_large_integer ticks;
nt_large_integer pcfreq;
nt_luid luid;
if ((status = __ntapi->zw_query_performance_counter(&ticks,&pcfreq)))
return status;
if ((status = __ntapi->zw_allocate_locally_unique_id(&luid)))
return status;
keys->key[0] = pe_get_current_process_id();
keys->key[1] = pe_get_current_thread_id();
keys->key[2] = ticks.ihigh;
keys->key[3] = ticks.ulow;
keys->key[4] = luid.high;
keys->key[5] = luid.low;
return NT_STATUS_SUCCESS;
}
void __stdcall __ntapi_tt_port_format_keys(
__in nt_port_keys * keys,
__out nt_port_name_keys * name_keys)
{
__ntapi->tt_uint32_to_hex_utf16(keys->key[0],name_keys->key_1st);
__ntapi->tt_uint32_to_hex_utf16(keys->key[1],name_keys->key_2nd);
__ntapi->tt_uint32_to_hex_utf16(keys->key[2],name_keys->key_3rd);
__ntapi->tt_uint32_to_hex_utf16(keys->key[3],name_keys->key_4th);
__ntapi->tt_uint32_to_hex_utf16(keys->key[4],name_keys->key_5th);
__ntapi->tt_uint32_to_hex_utf16(keys->key[5],name_keys->key_6th);
return;
}
void __stdcall __ntapi_tt_port_name_from_attr(
__out nt_port_name * name,
__in nt_port_attr * attr)
{
wchar16_t bno[] = __NT_BASED_NAMED_OBJECTS;
/* base named objects */
__ntapi->tt_memcpy_utf16(
name->base_named_objects,
bno,sizeof(bno));
/* service prefix */
if (attr && (attr->type < NT_PORT_TYPE_CAP) && (attr->subtype < NT_PORT_SUBTYPE_CAP))
__ntapi->tt_memcpy_utf16(
name->svc_prefix,
__port_service_prefixes[attr->type][attr->subtype],
sizeof(name->svc_prefix));
else
__ntapi->tt_memcpy_utf16(
name->svc_prefix,
__port_service_null,
sizeof(name->svc_prefix));
/* port guid */
__ntapi->tt_guid_to_string_utf16(
&attr->guid,
(nt_guid_str_utf16 *)&name->port_guid);
/* port name keys */
__ntapi_tt_port_format_keys(
&attr->keys,
&name->port_name_keys);
/* backslash and underscores */
name->backslash = '\\';
name->port_guid.uscore_guid = '_';
name->port_guid.uscore_keys = '_';
name->port_name_keys.uscore_1st = '_';
name->port_name_keys.uscore_2nd = '_';
name->port_name_keys.uscore_3rd = '_';
name->port_name_keys.uscore_4th = '_';
name->port_name_keys.uscore_5th = '_';
/* null termination */
name->null_termination = 0;
return;
}
static int32_t __tt_port_attr_from_name(
__out nt_port_attr * attr,
__in const nt_port_name * name)
{
int i;
int type;
int32_t status;
const wchar16_t bno[] = __NT_BASED_NAMED_OBJECTS;
/* base named objects */
if (__ntapi->tt_strncmp_utf16(
name->base_named_objects,
bno,sizeof(bno)))
return NT_STATUS_INVALID_PARAMETER;
/* service prefix */
for (i=0, type=-1; i<NT_PORT_TYPE_CAP && type<0; i++)
if (!(__ntapi->tt_strncmp_utf16(
name->svc_prefix,
__port_service_prefixes[i][0],
sizeof(__port_service_prefix))))
type = i;
if (type < 0)
return NT_STATUS_INVALID_PARAMETER;
/* port guid */
if ((status = __ntapi->tt_string_to_guid_utf16(
(nt_guid_str_utf16 *)&name->port_guid,
&attr->guid)))
return status;
/* port keys */
if ((status = __ntapi->tt_hex_utf16_to_uint32(
name->port_name_keys.key_1st,
&attr->keys.key[0])))
return status;
if ((status = __ntapi->tt_hex_utf16_to_uint32(
name->port_name_keys.key_2nd,
&attr->keys.key[1])))
return status;
if ((status = __ntapi->tt_hex_utf16_to_uint32(
name->port_name_keys.key_3rd,
&attr->keys.key[2])))
return status;
if ((status = __ntapi->tt_hex_utf16_to_uint32(
name->port_name_keys.key_4th,
&attr->keys.key[3])))
return status;
if ((status = __ntapi->tt_hex_utf16_to_uint32(
name->port_name_keys.key_5th,
&attr->keys.key[4])))
return status;
if ((status = __ntapi->tt_hex_utf16_to_uint32(
name->port_name_keys.key_6th,
&attr->keys.key[5])))
return status;
/* backslash and underscores */
if (name->backslash != '\\')
return NT_STATUS_INVALID_PARAMETER;
if (name->port_name_keys.uscore_1st != '_')
return NT_STATUS_INVALID_PARAMETER;
if (name->port_name_keys.uscore_2nd != '_')
return NT_STATUS_INVALID_PARAMETER;
if (name->port_name_keys.uscore_3rd != '_')
return NT_STATUS_INVALID_PARAMETER;
if (name->port_name_keys.uscore_4th != '_')
return NT_STATUS_INVALID_PARAMETER;
if (name->port_name_keys.uscore_5th != '_')
return NT_STATUS_INVALID_PARAMETER;
/* type */
attr->type = type;
attr->subtype = 0;
/* unknown defaults */
attr->ver_major = 0;
attr->ver_minor = 0;
attr->options = 0;
attr->flags = 0;
/* all done */
return NT_STATUS_SUCCESS;
}
int32_t __stdcall __ntapi_tt_port_attr_from_name(
__out nt_port_attr * attr,
__in const nt_port_name * name)
{
/* null termination */
if (name->null_termination)
return NT_STATUS_INVALID_PARAMETER;
return __tt_port_attr_from_name(
attr,name);
}
int32_t __stdcall __ntapi_tt_port_attr_from_string(
__out nt_port_attr * attr,
__in const nt_unicode_string * str)
{
if (str->strlen != ((size_t)&(((nt_port_name *)0)->null_termination)))
return NT_STATUS_INVALID_PARAMETER;
return __tt_port_attr_from_name(
attr,(const nt_port_name *)str->buffer);
}
int32_t __stdcall __ntapi_tt_port_attr_from_symlink(
__out nt_port_attr * attr,
__in void * symlink)
{
int32_t status;
size_t size;
nt_unicode_string * target;
uintptr_t buffer[512/sizeof(uintptr_t)];
target = (nt_unicode_string *)buffer;
target->strlen = 0;
target->maxlen = sizeof(buffer) - sizeof(nt_unicode_string);
target->buffer = (wchar16_t *)&target[1];
if ((status = __ntapi->zw_query_symbolic_link_object(
symlink,target,&size)))
return status;
if (target->strlen != ((size_t)&(((nt_port_name *)0)->null_termination)))
return NT_STATUS_INVALID_PARAMETER;
return __tt_port_attr_from_name(
attr,(const nt_port_name *)target->buffer);
}
|