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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved
Abstract:
Stream monitor sample executable
Environment:
User mode
--*/
#include "windows.h"
#include "winioctl.h"
#include "strsafe.h"
#ifndef _CTYPE_DISABLE_MACROS
#define _CTYPE_DISABLE_MACROS
#endif
#include "fwpmu.h"
#include "winsock2.h"
#include "ws2def.h"
#include <conio.h>
#include <stdio.h>
#include "ioctl.h"
#define INITGUID
#include <guiddef.h>
#include "mntrguid.h"
#define MONITOR_FLOW_ESTABLISHED_CALLOUT_DESCRIPTION L"Monitor Sample - Flow Established Callout"
#define MONITOR_FLOW_ESTABLISHED_CALLOUT_NAME L"Flow Established Callout"
#define MONITOR_STREAM_CALLOUT_DESCRIPTION L"Monitor Sample - Stream Callout"
#define MONITOR_STREAM_CALLOUT_NAME L"Stream Callout"
HANDLE quitEvent;
DWORD
MonitorAppOpenMonitorDevice(
_Out_ HANDLE* monitorDevice)
/*++
Routine Description:
Opens the Monitor Sample monitorDevice
Arguments:
[out] HANDLE* monitorDevice
Return Value:
NO_ERROR, ERROR_INVALID_PARAMETER or a CreateFile specific result.
--*/
{
if (!monitorDevice)
{
return ERROR_INVALID_PARAMETER;
}
*monitorDevice = CreateFileW(MONITOR_DOS_NAME,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (*monitorDevice == INVALID_HANDLE_VALUE)
{
return GetLastError();
}
return NO_ERROR;
}
BOOL MonitorAppCloseMonitorDevice(
_In_ HANDLE monitorDevice)
/*++
Routine Description:
Closes the Monitor Sample monitorDevice
Arguments:
Return Value:
None.
--*/
{
return CloseHandle(monitorDevice);
}
DWORD
MonitorAppAddCallouts()
/*++
Routine Description:
Adds the callouts during installation
Arguments:
[in] PCWSTR AppPath - The path to the application to monitor.
Return Value:
NO_ERROR or a specific FWP result.
--*/
{
FWPM_CALLOUT callout;
DWORD result;
FWPM_DISPLAY_DATA displayData;
HANDLE engineHandle = NULL;
FWPM_SESSION session;
RtlZeroMemory(&session, sizeof(FWPM_SESSION));
session.displayData.name = L"Monitor Sample Non-Dynamic Session";
session.displayData.description = L"For Adding callouts";
printf("Opening Filtering Engine\n");
result = FwpmEngineOpen(
NULL,
RPC_C_AUTHN_WINNT,
NULL,
&session,
&engineHandle
);
if (NO_ERROR != result)
{
goto cleanup;
}
printf("Starting Transaction for adding callouts\n");
result = FwpmTransactionBegin(engineHandle, 0);
if (NO_ERROR != result)
{
goto abort;
}
printf("Successfully started the Transaction\n");
RtlZeroMemory(&callout, sizeof(FWPM_CALLOUT));
displayData.description = MONITOR_FLOW_ESTABLISHED_CALLOUT_DESCRIPTION;
displayData.name = MONITOR_FLOW_ESTABLISHED_CALLOUT_NAME;
callout.calloutKey = MONITOR_SAMPLE_FLOW_ESTABLISHED_CALLOUT_V4;
callout.displayData = displayData;
callout.applicableLayer = FWPM_LAYER_ALE_FLOW_ESTABLISHED_V4;
callout.flags = FWPM_CALLOUT_FLAG_PERSISTENT; // Make this a persistent callout.
printf("Adding Persistent Flow Established callout through the Filtering Engine\n");
result = FwpmCalloutAdd(engineHandle, &callout, NULL, NULL);
if (NO_ERROR != result)
{
goto abort;
}
printf("Successfully Added Persistent Flow Established callout.\n");
RtlZeroMemory(&callout, sizeof(FWPM_CALLOUT));
displayData.description = MONITOR_STREAM_CALLOUT_DESCRIPTION;
displayData.name = MONITOR_STREAM_CALLOUT_DESCRIPTION;
callout.calloutKey = MONITOR_SAMPLE_STREAM_CALLOUT_V4;
callout.displayData = displayData;
callout.applicableLayer = FWPM_LAYER_STREAM_V4;
callout.flags = FWPM_CALLOUT_FLAG_PERSISTENT; // Make this a persistent callout.
printf("Adding Persistent Stream callout through the Filtering Engine\n");
result = FwpmCalloutAdd(engineHandle, &callout, NULL, NULL);
if (NO_ERROR != result)
{
goto abort;
}
printf("Successfully Added Persistent Stream callout.\n");
printf("Committing Transaction\n");
result = FwpmTransactionCommit(engineHandle);
if (NO_ERROR == result)
{
printf("Successfully Committed Transaction.\n");
}
goto cleanup;
abort:
printf("Aborting Transaction\n");
result = FwpmTransactionAbort(engineHandle);
if (NO_ERROR == result)
{
printf("Successfully Aborted Transaction.\n");
}
cleanup:
if (engineHandle)
{
FwpmEngineClose(engineHandle);
}
return result;
}
DWORD
MonitorAppRemoveCallouts()
/*++
Routine Description:
Sets the kernel callout ID's through the Monitor Sample device
Arguments:
[in] HANDLE monitorDevice - Monitor Sample device
[in] CALLOUTS* callouts - Callout structure with ID's set
[in] DWORD size - Size of the callout structure.
Return Value:
NO_ERROR or a specific DeviceIoControl result.
--*/
{
DWORD result;
HANDLE engineHandle = NULL;
FWPM_SESSION session;
RtlZeroMemory(&session, sizeof(FWPM_SESSION));
session.displayData.name = L"Monitor Sample Non-Dynamic Session";
session.displayData.description = L"For Adding callouts";
printf("Opening Filtering Engine\n");
result = FwpmEngineOpen(
NULL,
RPC_C_AUTHN_WINNT,
NULL,
&session,
&engineHandle
);
if (NO_ERROR != result)
{
goto cleanup;
}
printf("Starting Transaction for Removing callouts\n");
result = FwpmTransactionBegin(engineHandle, 0);
if (NO_ERROR != result)
{
goto abort;
}
printf("Successfully started the Transaction\n");
printf("Deleting Flow Established callout\n");
result = FwpmCalloutDeleteByKey(engineHandle,
&MONITOR_SAMPLE_FLOW_ESTABLISHED_CALLOUT_V4);
if (NO_ERROR != result)
{
goto abort;
}
printf("Successfully Deleted Flow Established callout\n");
printf("Deleting Stream callout\n");
result = FwpmCalloutDeleteByKey(engineHandle,
&MONITOR_SAMPLE_STREAM_CALLOUT_V4);
if (NO_ERROR != result)
{
goto abort;
}
printf("Successfully Deleted Stream callout\n");
printf("Committing Transaction\n");
result = FwpmTransactionCommit(engineHandle);
if (NO_ERROR == result)
{
printf("Successfully Committed Transaction.\n");
}
goto cleanup;
abort:
printf("Aborting Transaction\n");
result = FwpmTransactionAbort(engineHandle);
if (NO_ERROR == result)
{
printf("Successfully Aborted Transaction.\n");
}
cleanup:
if (engineHandle)
{
FwpmEngineClose(engineHandle);
}
return result;
}
DWORD
MonitorAppEnableMonitoring(
_In_ HANDLE monitorDevice,
_In_ MONITOR_SETTINGS* monitorSettings)
/*++
Routine Description:
Enables monitoring on new connections.
Arguments:
[in] HANDLE monitorDevice - Monitor Sample device
[in] MONITOR_SETTINGS* monitorSettings - Settings for the Monitor Sample driver.
Return Value:
NO_ERROR or a specific DeviceIoControl result.
--*/
{
DWORD bytesReturned;
if (!DeviceIoControl(monitorDevice,
MONITOR_IOCTL_ENABLE_MONITOR,
monitorSettings,
sizeof(MONITOR_SETTINGS),
NULL,
0,
&bytesReturned,
NULL))
{
return GetLastError();
}
return NO_ERROR;
}
DWORD
MonitorAppDisableMonitoring(
_In_ HANDLE monitorDevice)
/*++
Routine Description:
Disables monitoring of new flows (existing flows will continue to be
monitored until the driver is stopped or the flows end).
Arguments:
[in] HANDLE monitorDevice - Monitor Sample device handle.
Return Value:
NO_ERROR or DeviceIoControl specific code.
--*/
{
DWORD bytesReturned;
if (!DeviceIoControl(monitorDevice,
MONITOR_IOCTL_DISABLE_MONITOR,
NULL,
0,
NULL,
0,
&bytesReturned,
NULL))
{
return GetLastError();
}
return NO_ERROR;
}
DWORD
MonitorAppAddFilters(
_In_ HANDLE engineHandle,
_In_ FWP_BYTE_BLOB* applicationPath)
/*++
Routine Description:
Adds the required sublayer, filters and callouts to the Windows
Filtering Platform (WFP).
Arguments:
[in] HANDLE engineHandle - Handle to the base Filtering engine
[in] FWP_BYTE_BLOB* applicationPath - full path to the application including
the NULL terminator and size also
including the NULL the terminator
[in] CALLOUTS* callouts - The callouts that need to be added.
Return Value:
NO_ERROR or a specific result
--*/
{
DWORD result = NO_ERROR;
FWPM_SUBLAYER monitorSubLayer;
FWPM_FILTER filter;
FWPM_FILTER_CONDITION filterConditions[2]; // We only need two for this call.
RtlZeroMemory(&monitorSubLayer, sizeof(FWPM_SUBLAYER));
monitorSubLayer.subLayerKey = MONITOR_SAMPLE_SUBLAYER;
monitorSubLayer.displayData.name = L"Monitor Sample Sub layer";
monitorSubLayer.displayData.description = L"Monitor Sample Sub layer";
monitorSubLayer.flags = 0;
// We don't really mind what the order of invocation is.
monitorSubLayer.weight = 0;
printf("Starting Transaction\n");
result = FwpmTransactionBegin(engineHandle, 0);
if (NO_ERROR != result)
{
goto abort;
}
printf("Successfully Started Transaction\n");
printf("Adding Sublayer\n");
result = FwpmSubLayerAdd(engineHandle, &monitorSubLayer, NULL);
if (NO_ERROR != result)
{
goto abort;
}
printf("Sucessfully added Sublayer\n");
RtlZeroMemory(&filter, sizeof(FWPM_FILTER));
filter.layerKey = FWPM_LAYER_ALE_FLOW_ESTABLISHED_V4;
filter.displayData.name = L"Flow established filter.";
filter.displayData.description = L"Sets up flow for traffic that we are interested in.";
filter.action.type = FWP_ACTION_CALLOUT_INSPECTION; // We're only doing inspection.
filter.action.calloutKey = MONITOR_SAMPLE_FLOW_ESTABLISHED_CALLOUT_V4;
filter.filterCondition = filterConditions;
filter.subLayerKey = monitorSubLayer.subLayerKey;
filter.weight.type = FWP_EMPTY; // auto-weight.
filter.numFilterConditions = 2;
RtlZeroMemory(filterConditions, sizeof(filterConditions));
//
// Add the application path to the filter conditions.
//
filterConditions[0].fieldKey = FWPM_CONDITION_ALE_APP_ID;
filterConditions[0].matchType = FWP_MATCH_EQUAL;
filterConditions[0].conditionValue.type = FWP_BYTE_BLOB_TYPE;
filterConditions[0].conditionValue.byteBlob = applicationPath;
//
// For the purposes of this sample, we will monitor TCP traffic only.
//
filterConditions[1].fieldKey = FWPM_CONDITION_IP_PROTOCOL;
filterConditions[1].matchType = FWP_MATCH_EQUAL;
filterConditions[1].conditionValue.type = FWP_UINT8;
filterConditions[1].conditionValue.uint8 = IPPROTO_TCP;
printf("Adding Flow Established Filter\n");
result = FwpmFilterAdd(engineHandle,
&filter,
NULL,
NULL);
if (NO_ERROR != result)
{
goto abort;
}
printf("Successfully added Flow Established filter\n");
RtlZeroMemory(&filter, sizeof(FWPM_FILTER));
filter.layerKey = FWPM_LAYER_STREAM_V4;
filter.action.type = FWP_ACTION_CALLOUT_INSPECTION; // We're only doing inspection.
filter.action.calloutKey = MONITOR_SAMPLE_STREAM_CALLOUT_V4;
filter.subLayerKey = monitorSubLayer.subLayerKey;
filter.weight.type = FWP_EMPTY; // auto-weight.
filter.numFilterConditions = 0;
RtlZeroMemory(filterConditions, sizeof(filterConditions));
filter.filterCondition = filterConditions;
filter.displayData.name = L"Stream Layer Filter";
filter.displayData.description = L"Monitors TCP traffic.";
printf("Adding Stream Filter\n");
result = FwpmFilterAdd(engineHandle,
&filter,
NULL,
NULL);
if (NO_ERROR != result)
{
goto abort;
}
printf("Successfully added Stream filter\n");
printf("Committing Transaction\n");
result = FwpmTransactionCommit(engineHandle);
if (NO_ERROR == result)
{
printf("Successfully Committed Transaction\n");
}
goto cleanup;
abort:
printf("Aborting Transaction\n");
result = FwpmTransactionAbort(engineHandle);
if (NO_ERROR == result)
{
printf("Successfully Aborted Transaction\n");
}
cleanup:
return result;
}
DWORD
MonitorAppIDFromPath(
_In_ PCWSTR fileName,
_Out_ FWP_BYTE_BLOB** appId)
{
DWORD result = NO_ERROR;
result = FwpmGetAppIdFromFileName(fileName, appId);
return result;
}
DWORD
MonitorAppDoMonitoring(PCWSTR AppPath)
{
HANDLE monitorDevice = NULL;
HANDLE engineHandle = NULL;
DWORD result;
MONITOR_SETTINGS monitorSettings;
FWPM_SESSION session;
FWP_BYTE_BLOB* applicationId = NULL;
RtlZeroMemory(&monitorSettings, sizeof(MONITOR_SETTINGS));
RtlZeroMemory(&session, sizeof(FWPM_SESSION));
session.displayData.name = L"Monitor Sample Session";
session.displayData.description = L"Monitors traffic at the Stream layer.";
// Let the Base Filtering Engine cleanup after us.
session.flags = FWPM_SESSION_FLAG_DYNAMIC;
printf("Opening Filtering Engine\n");
result = FwpmEngineOpen(
NULL,
RPC_C_AUTHN_WINNT,
NULL,
&session,
&engineHandle
);
if (NO_ERROR != result)
{
goto cleanup;
}
printf("Successfully opened Filtering Engine\n");
printf("Looking up Application ID from BFE\n");
result = MonitorAppIDFromPath(AppPath, &applicationId);
if (NO_ERROR != result)
{
goto cleanup;
}
printf("Successfully retrieved Application ID\n");
printf("Opening Monitor Sample Device\n");
result = MonitorAppOpenMonitorDevice(&monitorDevice);
if (NO_ERROR != result)
{
goto cleanup;
}
printf("Successfully opened Monitor Device\n");
printf("Adding Filters through the Filtering Engine\n");
result = MonitorAppAddFilters(engineHandle,
applicationId);
if (NO_ERROR != result)
{
goto cleanup;
}
printf("Successfully added Filters through the Filtering Engine\n");
printf("Enabling monitoring through the Monitor Sample Device\n");
monitorSettings.monitorOperation = monitorTraffic;
result = MonitorAppEnableMonitoring(monitorDevice,
&monitorSettings);
if (NO_ERROR != result)
{
goto cleanup;
}
printf("Successfully enabled monitoring.\n");
printf("Events will be traced through WMI. Please press any key to exit and cleanup filters.\n");
#pragma prefast(push)
#pragma prefast(disable:6031, "by design the return value of _getch() is ignored here")
_getch();
#pragma prefast(pop)
cleanup:
if (NO_ERROR != result)
{
printf("Monitor.\tError 0x%x occurred during execution\n", result);
}
if (monitorDevice)
{
MonitorAppCloseMonitorDevice(monitorDevice);
}
//
// Free the application Id that we retrieved.
//
if (applicationId)
{
FwpmFreeMemory((void**)&applicationId);
}
if (engineHandle)
{
result = FwpmEngineClose(engineHandle);
engineHandle = NULL;
}
return result;
}
void
MonitorPrintUsage()
{
wprintf(L"Usage: monitor ( addcallouts | delcallouts | monitor <targetApp.exe> )\n");
}
DWORD
MonitorAppProcessArguments(_In_ int argc, _In_reads_(argc) PCWSTR argv[])
{
if (argc == 2)
{
if (_wcsicmp(argv[1], L"addcallouts") == 0)
{
return MonitorAppAddCallouts();
}
if (_wcsicmp(argv[1], L"delcallouts") == 0)
{
return MonitorAppRemoveCallouts();
}
}
if (argc == 3)
{
if (_wcsicmp(argv[1], L"monitor") == 0)
{
return MonitorAppDoMonitoring(argv[2]);
}
}
MonitorPrintUsage();
return ERROR_INVALID_PARAMETER;
}
int __cdecl wmain(_In_ int argc, _In_reads_(argc) PCWSTR argv[])
{
DWORD result;
result = MonitorAppProcessArguments(argc, argv);
return (int)result;
}
|