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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2014 Microsoft Corporation. All Rights Reserved.
//
// Module Name:
// Scenarios_PendEndpointClosure.cpp
//
// Abstract:
// This module contains functions which implements the PEND_ENDPOINT_CLOSURE scenarios.
//
// Naming Convention:
//
// <Scope><Object><Action><Modifier>
// <Scope><Object><Action>
//
// i.e.
//
// <Scope>
// {
// - Function is likely visible to other modules.
// Prv - Function is private to this module.
// }
// <Object>
// {
// ScenarioPendEndpointClosure - Function pertains to all of the Pend Endpoint Closure
// Scenarios.
// RPC - Function is and RPC entry point.
// }
// <Action>
// {
// Add - Function adds objects.
// Remove - Function removes objects.
// Invoke - Function implements the scenario based on parameters
// passed from the commandline interface
// (WFPSampler.exe).
// }
// <Modifier>
// {
// FwpmObjects - Function acts on WFP objects.
// ScenarioPendEndpointClosure - Function pertains to all of the Pend Endpoint Closure
// Scenarios.
// }
//
// Private Functions:
// PrvScenarioPendEndpointClosureAddFwpmObjects(),
// PrvScenarioPendEndpointClosureDeleteFwpmObjects(),
// ScenarioPendEndpointClosureAdd(),
// ScenarioPendEndpointClosureRemove(),
//
// Public Functions:
// RPCInvokeScenarioPendEndpointClosure(),
//
// Author:
// Dusty Harper (DHarper)
//
// Revision History:
//
// [ Month ][Day] [Year] - [Revision]-[ Comments ]
// December 13, 2013 - 1.1 - Creation
//
////////////////////////////////////////////////////////////////////////////////////////////////////
#include "Framework_WFPSamplerService.h" /// .
#if(NTDDI_VERSION >= NTDDI_WIN7)
/**
@private_function="PrvScenarioPendEndpointClosureDeleteFwpmObjects"
Purpose: Function that disables the SCENARIO_PEND_ENDPOINT_CLOSURE scenarios. <br>
<br>
Notes: Scenario removes the filters using specified filtering conditions at the specified
layer. The associated callout and provider contexts are removed as well. <br>
<br>
MSDN_Ref: <br>
*/
_Success_(return == NO_ERROR)
UINT32 PrvScenarioPendEndpointClosureDeleteFwpmObjects(_In_ const FWPM_FILTER* pFilter)
{
ASSERT(pFilter);
UINT32 status = NO_ERROR;
HANDLE engineHandle = 0;
HANDLE enumHandle = 0;
UINT32 entryCount = 0;
FWPM_FILTER** ppFilters = 0;
FWPM_FILTER_CONDITION* pFilterConditions = 0;
UINT32 numFilterConditions = 0;
FWPM_FILTER_ENUM_TEMPLATE filterEnumTemplate = {0};
FWPM_PROVIDER_CONTEXT_ENUM_TEMPLATE providerContextEnumTemplate = {0};
GUID calloutKey = {0};
HlprFwpmFilterConditionPrune(pFilter->filterCondition,
pFilter->numFilterConditions,
&pFilterConditions,
&numFilterConditions);
calloutKey = WFPSAMPLER_CALLOUT_PEND_ENDPOINT_CLOSURE;
calloutKey.Data4[7] = HlprFwpmLayerGetIDByKey(&(pFilter->layerKey)); /// Uniquely identifies the callout used
providerContextEnumTemplate.providerContextType = FWPM_GENERAL_CONTEXT;
filterEnumTemplate.providerKey = (GUID*)&WFPSAMPLER_PROVIDER;
filterEnumTemplate.layerKey = pFilter->layerKey;
filterEnumTemplate.enumType = FWP_FILTER_ENUM_FULLY_CONTAINED;
filterEnumTemplate.flags = FWP_FILTER_ENUM_FLAG_INCLUDE_BOOTTIME |
FWP_FILTER_ENUM_FLAG_INCLUDE_DISABLED;
filterEnumTemplate.numFilterConditions = pFilterConditions ? numFilterConditions : pFilter->numFilterConditions;
filterEnumTemplate.filterCondition = pFilterConditions ? pFilterConditions : pFilter->filterCondition;
filterEnumTemplate.providerContextTemplate = &providerContextEnumTemplate;
filterEnumTemplate.actionMask = FWP_ACTION_FLAG_CALLOUT;
filterEnumTemplate.calloutKey = &calloutKey;
status = HlprFwpmEngineOpen(&engineHandle);
HLPR_BAIL_ON_FAILURE(status);
status = HlprFwpmFilterCreateEnumHandle(engineHandle,
&filterEnumTemplate,
&enumHandle);
HLPR_BAIL_ON_FAILURE(status);
status = HlprFwpmFilterEnum(engineHandle,
enumHandle,
0xFFFFFFFF,
&ppFilters,
&entryCount);
HLPR_BAIL_ON_FAILURE(status);
if(ppFilters)
{
for(UINT32 filterIndex = 0;
filterIndex < entryCount;
filterIndex++)
{
HlprFwpmFilterDeleteByKey(engineHandle,
&(ppFilters[filterIndex]->filterKey));
HlprFwpmCalloutDeleteByKey(engineHandle,
&(ppFilters[filterIndex]->action.calloutKey));
if(ppFilters[filterIndex]->flags & FWPM_FILTER_FLAG_HAS_PROVIDER_CONTEXT)
HlprFwpmProviderContextDeleteByKey(engineHandle,
&(ppFilters[filterIndex]->providerContextKey));
}
FwpmFreeMemory((VOID**)&ppFilters);
}
HLPR_BAIL_LABEL:
if(engineHandle)
{
if(enumHandle)
HlprFwpmFilterDestroyEnumHandle(engineHandle,
&enumHandle);
HlprFwpmEngineClose(&engineHandle);
}
HLPR_DELETE_ARRAY(pFilterConditions);
return status;
}
/**
@private_function="PrvScenarioPendEndpointClosureAddFwpmObjects"
Purpose: Function that enables the SCENARIO_PEND_ENDPOINT_CLOSURE scenarios. <br>
<br>
Notes: Scenario adds a filter using specified filtering conditions to the specified layer.
This filter is associated with WFPSampler's default sublayer and provider. The
appropriate callout and provider context is added and associated with the filter. <br>
<br>
MSDN_Ref: <br>
*/
_Success_(return == NO_ERROR)
UINT32 PrvScenarioPendEndpointClosureAddFwpmObjects(_In_ const FWPM_FILTER* pFilter,
_In_ const PC_PEND_ENDPOINT_CLOSURE_DATA* pPCPendEndpointClosureData)
{
ASSERT(pFilter);
ASSERT(pPCPendEndpointClosureData);
UINT32 status = NO_ERROR;
HANDLE engineHandle = 0;
FWP_BYTE_BLOB byteBlob = {0};
FWPM_PROVIDER_CONTEXT providerContext = {0};
FWPM_CALLOUT callout = {0};
FWPM_FILTER filter = {0};
RtlCopyMemory(&filter,
pFilter,
sizeof(FWPM_FILTER));
status = HlprGUIDPopulate(&(providerContext.providerContextKey));
HLPR_BAIL_ON_FAILURE(status);
providerContext.displayData.name = L"WFPSampler's Pend Endpoint Closure ProviderContext";
providerContext.providerKey = (GUID*)&WFPSAMPLER_PROVIDER;
providerContext.type = FWPM_GENERAL_CONTEXT;
providerContext.dataBuffer = &byteBlob;
providerContext.dataBuffer->size = sizeof(PC_PEND_ENDPOINT_CLOSURE_DATA);
providerContext.dataBuffer->data = (UINT8*)pPCPendEndpointClosureData;
callout.calloutKey = WFPSAMPLER_CALLOUT_PEND_ENDPOINT_CLOSURE;
callout.calloutKey.Data4[7] = HlprFwpmLayerGetIDByKey(&(filter.layerKey)); /// Uniquely identifies the callout used
callout.displayData.name = L"WFPSampler's Pend Endpoint Closure Callout";
callout.displayData.description = L"Pends the endpoint's closing request and completes out of band";
callout.flags = FWPM_CALLOUT_FLAG_USES_PROVIDER_CONTEXT;
callout.providerKey = (GUID*)&WFPSAMPLER_PROVIDER;
callout.applicableLayer = filter.layerKey;
status = HlprGUIDPopulate(&(filter.filterKey));
HLPR_BAIL_ON_FAILURE(status);
filter.flags |= FWPM_FILTER_FLAG_HAS_PROVIDER_CONTEXT;
filter.providerKey = (GUID*)&WFPSAMPLER_PROVIDER;
filter.weight.type = FWP_UINT8;
filter.weight.uint8 = 0xF;
filter.action.type = FWP_ACTION_CALLOUT_UNKNOWN;
filter.action.calloutKey = callout.calloutKey;
filter.providerContextKey = providerContext.providerContextKey;
if(filter.flags & FWPM_FILTER_FLAG_BOOTTIME ||
filter.flags & FWPM_FILTER_FLAG_PERSISTENT)
{
providerContext.flags |= FWPM_PROVIDER_CONTEXT_FLAG_PERSISTENT;
callout.flags |= FWPM_CALLOUT_FLAG_PERSISTENT;
}
status = HlprFwpmEngineOpen(&engineHandle);
HLPR_BAIL_ON_FAILURE(status);
status = HlprFwpmTransactionBegin(engineHandle);
HLPR_BAIL_ON_FAILURE(status);
status = HlprFwpmProviderContextAdd(engineHandle,
&providerContext);
HLPR_BAIL_ON_FAILURE(status);
status = HlprFwpmCalloutAdd(engineHandle,
&callout);
HLPR_BAIL_ON_FAILURE(status);
status = HlprFwpmFilterAdd(engineHandle,
&filter);
HLPR_BAIL_ON_FAILURE(status);
status = HlprFwpmTransactionCommit(engineHandle);
HLPR_BAIL_ON_FAILURE(status);
HLPR_BAIL_LABEL:
if(engineHandle)
{
if(status != NO_ERROR)
HlprFwpmTransactionAbort(engineHandle);
HlprFwpmEngineClose(&engineHandle);
}
return status;
}
/**
@scenario_function="ScenarioPendEndpointClosureRemove"
Purpose: Function that removes corresponding objects for a previously added
SCENARIO_PEND_ENDPOINT_CLOSURE. <br>
<br>
Notes: <br>
<br>
MSDN_Ref: <br>
*/
_Success_(return == NO_ERROR)
UINT32 ScenarioPendEndpointClosureRemove(_In_ const FWPM_FILTER* pFilter)
{
ASSERT(pFilter);
return PrvScenarioPendEndpointClosureDeleteFwpmObjects(pFilter);
}
/**
@scenario_function="ScenarioPendEndpointClosureAdd"
Purpose: Scenario which will delay the endpoint from from closing. <br>
<br>
Notes: Adds a filter which references one of the
WFPSAMPLER_CALLOUT_PEND_ENDPOINT_CLOSURE callouts for the provided layer. <br>
<br>
MSDN_Ref: <br>
*/
_Success_(return == NO_ERROR)
UINT32 ScenarioPendEndpointClosureAdd(_In_ const FWPM_FILTER* pFilter,
_In_ const PC_PEND_ENDPOINT_CLOSURE_DATA* pPCPendEndpointClosureData)
{
ASSERT(pFilter);
ASSERT(pPCPendEndpointClosureData);
return PrvScenarioPendEndpointClosureAddFwpmObjects(pFilter,
pPCPendEndpointClosureData);
}
/**
@rpc_function="RPCInvokeScenarioPendEndpointClosure"
Purpose: RPC exposed function used to dipatch the scenario routines for
SCENARIO_PEND_ENDPOINT_CLOSURE. <br>
<br>
Notes: <br>
<br>
MSDN_Ref: <br>
*/
/* [fault_status][comm_status] */
error_status_t RPCInvokeScenarioPendEndpointClosure(/* [in] */ handle_t rpcBindingHandle,
/* [in] */ WFPSAMPLER_SCENARIO scenario,
/* [in] */ FWPM_CHANGE_TYPE changeType,
/* [ref][in] */ __RPC__in const FWPM_FILTER0* pFilter,
/* [unique][in] */ __RPC__in_opt const PC_PEND_ENDPOINT_CLOSURE_DATA* pPCPendEndpointClosureData)
{
UNREFERENCED_PARAMETER(rpcBindingHandle);
UNREFERENCED_PARAMETER(scenario);
ASSERT(pFilter);
ASSERT(scenario == SCENARIO_PEND_AUTHORIZATION);
ASSERT(changeType < FWPM_CHANGE_TYPE_MAX);
UINT32 status = NO_ERROR;
if(changeType == FWPM_CHANGE_ADD)
{
if(pPCPendEndpointClosureData)
status = ScenarioPendEndpointClosureAdd(pFilter,
pPCPendEndpointClosureData);
else
{
status = ERROR_INVALID_PARAMETER;
HlprLogError(L"RPCInvokeScenarioPendEndpointClosure() [status: %#x][pPCPendEndpointClosureData: %#p]",
status,
pPCPendEndpointClosureData);
}
}
else
status = ScenarioPendEndpointClosureRemove(pFilter);
return status;
}
#else
/**
@rpc_function="RPCInvokeScenarioBasicAction"
Purpose: RPC exposed function used to dipatch the scenario routines for
SCENARIO_APP_CONTAINER. <br>
<br>
Notes: This particular function is only a stub for RPC on downlevel SKUs (Windows 7 and
below). <br>
<br>
MSDN_Ref: <br>
*/
/* [fault_status][comm_status] */
error_status_t RPCInvokeScenarioPendEndpointClosure(/* [in] */ handle_t rpcBindingHandle,
/* [in] */ WFPSAMPLER_SCENARIO scenario,
/* [in] */ FWPM_CHANGE_TYPE changeType,
/* [ref][in] */ __RPC__in const FWPM_FILTER0* pFilter,
/* [unique][in] */ __RPC__in_opt const PC_PEND_ENDPOINT_CLOSURE_DATA* pPCPendEndpointClosureData)
{
UNREFERENCED_PARAMETER(rpcBindingHandle);
UNREFERENCED_PARAMETER(scenario);
UNREFERENCED_PARAMETER(changeType);
UNREFERENCED_PARAMETER(pFilter);
UNREFERENCED_PARAMETER(pPCPendEndpointClosureData);
return ERROR_NOT_SUPPORTED;
}
#endif /// (NTDDI_VERSION >= NTDDI_WIN7)
|