-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathnetmon.c
More file actions
4725 lines (3793 loc) · 142 KB
/
netmon.c
File metadata and controls
4725 lines (3793 loc) · 142 KB
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2010-2024 JiJie.Shi.
*
* This file is part of netmon.
* Licensed under the Gangoo License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <strsafe.h>
#include "common.h"
#include <tdikrnl.h>
#include "hash.h"
#include "netmon_global.h"
extern volatile KSYSTEM_TIME KeTickCount;
extern POBJECT_TYPE *IoFileObjectType;
DWORD TDI_FILTER_DRIVER_VERSION[] = { 1, 0, 0, 0006 };
#define UDP_DEVICE_NAME L"\\Device\\Udp"
#define TCP_DEVICE_NAME L"\\Device\\Tcp"
#define TDI_FILTER_DEVICE_DOS_NAME L"\\DosDevices\\BCTdiFilter"
#define TDI_FILTER_DEVICE_NAME L"\\Device\\BCTdiFilter"
#define DOS_DEVICE_NAME_PREFIX L"\\??\\"
#define TICK_COUNT_RECORD_INIT_VAL 0xBB40E64E
#define TICK_COUNT_RECORD_NOT_INIT_VAL 0x44BF19B1
#define DEL_EVENT_WRAP 1
#define GET_EVENT_WRAP 2
#define MASTER_IRP_HASH_TABLE_SIZE 500
#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, DriverEntry)
#endif
PKTHREAD ThreadUpdateConfig = NULL;
PKTHREAD ThreadProcessIrp = NULL;
PDEVICE_OBJECT g_FilterDeviceForDeviceTcp = NULL;
PDEVICE_OBJECT g_FilterDeviceForDeviceUdp = NULL;
PDEVICE_OBJECT g_DevTdiFilter = NULL;
BOOL g_bThreadsRunning = FALSE;
BOOL g_bBeginStartThreads = FALSE;
DWORD g_dwTickCountFactor = TICK_COUNT_RECORD_INIT_VAL;
DWORD g_dwTickCountFactorNot = TICK_COUNT_RECORD_NOT_INIT_VAL;
\
hash_table g_MasterIrpHash;
BOOL g_bFiltering = TRUE;
BOOL g_bThreadUpdateConfigStop = FALSE;
BOOL g_bThreadIrpProcessStop = FALSE;
LARGE_INTEGER Interval;
LARGE_INTEGER g_SendingDelayTime = { 0 };
LARGE_INTEGER g_TimerElapse = { 0 };
LARGE_INTEGER g_ThreadWaitConfigProcTime = { 0 };
LARGE_INTEGER g_WaitNewIistItemTime = { 0 };
LARGE_INTEGER g_AllSendedDataSize;
LARGE_INTEGER g_AllRecvedDataSize;
KSPIN_LOCK g_SpLockProcessNetWorkTrafficInfo;
KSPIN_LOCK g_SpLockTdiEventHandlerInfo;
KEVENT g_EventProcessInformationAdded;
KEVENT g_EventIrpListAdded;
KEVENT g_EventCompletion;
ERESOURCE g_SyncResource;
NPAGED_LOOKASIDE_LIST g_CompletionWrapList;
LIST_ENTRY g_TdiEventHandlerInfoList;
LIST_ENTRY g_ProcessIoInfoList;
LIST_ENTRY g_ProcessInformationList;
BOOL IsDriverDevice( PDEVICE_OBJECT pDeviceObject );
NTSTATUS TdiFilterChainedRecvHandler(
IN PVOID TdiEventContext,
IN CONNECTION_CONTEXT ConnectionContext,
IN ULONG ReceiveFlags,
IN ULONG ReceiveLength,
IN ULONG StartingOffset,
IN PMDL Tsdu,
IN PVOID TsduDescriptor
);
NTSTATUS TdiFilterRecvDatagramEventHandler(
IN PVOID TdiEventContext,
IN LONG SourceAddressLength,
IN PVOID SourceAddress,
IN LONG OptionsLength,
IN PVOID Options,
IN ULONG ReceiveDatagramFlags,
IN ULONG BytesIndicated,
IN ULONG BytesAvailable,
OUT ULONG *BytesTaken,
IN PVOID Tsdu,
OUT PIRP *IoRequestPacket
);
DWORD dwBPFlag = 0;
DWORD dwPrintFlags = IRP_CANCEL_INFO |DRIVER_ENTRY_INFO | DRIVER_UNLOAD_INFO;
BOOL g_StopWaitCompletion = FALSE;
INT32 g_CompletionIrpCount = 0;
PKTHREAD g_ThreadWaitCompletion = NULL;
PIRP DequeueIrp( PLIST_ENTRY pListHead, PKSPIN_LOCK SpLock );
VOID TdiFilterCancel(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
);
VOID ThreadWaitCompletion( PVOID pParam );
NTSTATUS RestoreEventHandler( PTDI_EVENT_HANDLER_WRAP pEventHandlerWrap );
DWORD ReleaseAllEventHandlerWrap();
VOID ReleaseAllProcessNetWorkTrafficInfo();
NTSTATUS TdiFilterCompletion( PDEVICE_OBJECT pDeviceObject, PIRP pIrp, LPVOID pContext );
NTSTATUS TdiFilterSyncSendProcess( PROCESS_NETWORK_TRAFFIC *pProcessNetWorkTrafficInfo,
PDEVICE_OBJECT pDeviceObject,
PIRP pIrp );
VOID ThreadSendingSpeedControl( PVOID pParam );
VOID TimerDpcProcess( PKDPC pDpc, PVOID pEProcess, PVOID SystemArgument1 , PVOID SystemArgument2 );
VOID DeleteProcessIoInfo( DWORD dwParentId, DWORD dwProcessId, BOOL bCreate );
NTSTATUS GetAllProcessesIoInformation( LPVOID *pOutput, DWORD dwInputBuffLength, DWORD *pAllInfoLength );
NTSTATUS GetAllProcessesInformation( PPROCESS_INFORMATION_RECORD pAllProcessInfomation, DWORD dwBufferLength, DWORD *pAllInfoLength );
NTSTATUS ReleaseAllProcessesInformation();
VOID DeleteEventWrap( PTDI_EVENT_HANDLER_LIST pTdiEventHandlerList );
VOID UpdateEventHandlerWrap( PPROCESS_NETWORK_TRAFFIC pProcessNetWorkTrafficInfo,
PEPROCESS pEProcess,
PDEVICE_OBJECT pDeviceObject,
PFILE_OBJECT pFileObject,
DWORD dwEventType,
PVOID pEventHandler,
PVOID pEventContext,
PTDI_EVENT_HANDLER_LIST *ppEventHandlerWrap,
DWORD dwFlags );
VOID ThreadUpdateProcessIoState( PVOID pParam );
NTSTATUS TdiFilterRecvEventHandler( IN PVOID TdiEventContext,
IN CONNECTION_CONTEXT ConnectionContext,
IN ULONG ReceiveFlags,
IN ULONG BytesIndicated,
IN ULONG BytesAvailable,
OUT ULONG *BytesTaken,
IN PVOID Tsdu,
OUT PIRP *IoRequestPacket
);
VOID TdiFilterUnload( PDRIVER_OBJECT pDriverObject );
NTSTATUS TdiFilterCleanUp(PDEVICE_OBJECT DeviceObject, PIRP pIrp );
NTSTATUS TdiFilterDefaultIrpProcess( PDEVICE_OBJECT pDeviceObject, PIRP pIrp);
NTSTATUS StartWorkThreadManageProcessInfo(
PROCESS_INFORMATION_RECORD *pProcessInformationFind,
DWORD dwInputBufferLength
);
NTSTATUS TdiFilterInternalIoControl( PDEVICE_OBJECT pDeviceObject, PIRP pIrp );
NTSTATUS TdiFilterIoControl( PDEVICE_OBJECT pDeviceObject, PIRP pIrp );
NTSTATUS DriverEntry( PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath );
NTSTATUS AttachToTdiDevice( PDRIVER_OBJECT DriverObject, PUNICODE_STRING TargetDeviceName, PDEVICE_OBJECT *DeviceObject );
PPROCESS_NETWORK_TRAFFIC GetProcessNetWorkTrafficInfoFromEProcess( PEPROCESS pEProcess );
PPROCESS_NETWORK_TRAFFIC ReferenceProcessNetWorkTrafficInfo( PEPROCESS pEProcess );
DWORD ReleaseProcessNetWorkTrafficInfo( PROCESS_NETWORK_TRAFFIC *pProcessIoInformaiton );
NTSTATUS EnterUserProcessReadImagePath( PEPROCESS pEProcess, PUNICODE_STRING pImageFilePath );
NTSTATUS GetFileHandle( PHANDLE pFileHandle, PUNICODE_STRING FileName );
NTSTATUS QueryFileAndDirInfo( HANDLE hFile,
LPCWSTR pwszFilePath,
LPCWSTR pwszFileName,
PFILE_BOTH_DIRECTORY_INFORMATION FileInformation,
ULONG FileInformationLength );
LPWSTR FindWideCharInWideString(LPCWSTR pszwString, DWORD dwLength, WCHAR wFindChar);
NTSTATUS ShortPathNameToEntirePathName( PUNICODE_STRING ShortPathName, PUNICODE_STRING FullPathName );
NTSTATUS GetProcessImagePath( DWORD dwProcessId, PUNICODE_STRING ProcessImageFilePath, DWORD dwBufferLen );
NTSTATUS RegisterProcessCreateNotify();
NTSTATUS DeregisterProcessCreateNotify();
ULONG DebugPrintEx( DWORD dwFlags, CHAR *Format, ... )
{
ULONG uRet;
va_list va;
if( !( dwPrintFlags & dwFlags ) )
{
return 0;
}
va_start( va, Format );
uRet = vDbgPrintEx( 0xFFFFFFFF, 0, Format, va );
va_end( va );
return uRet;
}
NTSTATUS CreateWorkThread( PKSTART_ROUTINE StartRoutine, PVOID StartContext, PKTHREAD *kThread )
{
NTSTATUS ntStatus;
HANDLE hThread;
ASSERT( NULL != kThread );
*kThread = NULL;
hThread = NULL;
ntStatus = PsCreateSystemThread( &hThread,
THREAD_ALL_ACCESS,
NULL,
NULL,
NULL,
StartRoutine,
NULL
);
if( !NT_SUCCESS( ntStatus ) )
{
goto RETURN_;
}
ntStatus = ObReferenceObjectByHandle(
hThread,
THREAD_ALL_ACCESS,
NULL,
KernelMode,
( PVOID* )kThread,
NULL
);
if( !NT_SUCCESS( ntStatus ) )
{
goto RETURN_;
}
RETURN_:
if( NULL != hThread )
{
ZwClose( hThread );
}
return ntStatus;
}
NTSTATUS DriverEntry( PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath )
{
NTSTATUS ntStatus;
INT32 i;
UNICODE_STRING TcpDeviceName;
UNICODE_STRING UdpDeviceName;
UNICODE_STRING TdiFilterDeviceName;
UNICODE_STRING TdiFilterDeviceDosName;
DebugPrintEx( DRIVER_ENTRY_INFO, "netmon enter DriverEntry\n" );
RtlInitUnicodeString( ( PUNICODE_STRING )&TdiFilterDeviceName,
TDI_FILTER_DEVICE_NAME );
RtlInitUnicodeString( ( PUNICODE_STRING )&TdiFilterDeviceDosName,
TDI_FILTER_DEVICE_DOS_NAME );
DebugPrintEx( DRIVER_ENTRY_INFO, "netmon DriverEntry begin IoCreateDevice TdiFilterDeviceName\n" );
ntStatus = IoCreateDevice(
DriverObject,
0,
&TdiFilterDeviceName,
FILE_DEVICE_UNKNOWN,
0,
FALSE,
&g_DevTdiFilter
);
DebugPrintEx( DRIVER_ENTRY_INFO, "netmon DriverEntry end IoCreateDevice TdiFilterDeviceName\n" );
if ( !NT_SUCCESS( ntStatus ) )
{
return ntStatus;
}
DebugPrintEx( DRIVER_ENTRY_INFO, "netmon DriverEntry begin IoCreateSymbolicLink TdiFilterDeviceDosName\n" );
ntStatus = IoCreateSymbolicLink( &TdiFilterDeviceDosName,
&TdiFilterDeviceName );
if( !NT_SUCCESS( ntStatus ) )
{
goto Error;
}
KeInitializeSpinLock( &g_SpLockProcessNetWorkTrafficInfo );
KeInitializeSpinLock( &g_SpLockTdiEventHandlerInfo );
InitializeListHead( &g_ProcessIoInfoList );
InitializeListHead( &g_TdiEventHandlerInfoList );
InitializeListHead( &g_ProcessInformationList );
ExInitializeResourceLite(&g_SyncResource);
KeInitializeEvent( &g_EventProcessInformationAdded, SynchronizationEvent, 0 );
KeInitializeEvent( &g_EventIrpListAdded, SynchronizationEvent, 0 );
KeInitializeEvent( &g_EventCompletion, SynchronizationEvent, 0 );
g_TimerElapse.QuadPart = TDI_FILTER_TIMER_ELAPSE_TIME;
g_SendingDelayTime.QuadPart = -10000;
g_WaitNewIistItemTime.QuadPart = TDI_FILTER_TIMER_ELAPSE_TIME;
g_ThreadWaitConfigProcTime.LowPart = WAIT_CONFIGURED_PROC_TIME;
g_ThreadWaitConfigProcTime.HighPart = 0xFFFFFFFF;
ExInitializeNPagedLookasideList(
&g_CompletionWrapList,
NULL,
NULL,
0,
sizeof( TDI_COMPLETION_WRAP ),
TDI_FILTER_LOOKASIDE_POOL_TAG,
0
);
for( i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i ++ )
{
DriverObject->MajorFunction[ i ] = TdiFilterDefaultIrpProcess;
}
DriverObject->MajorFunction[ IRP_MJ_DEVICE_CONTROL ] = TdiFilterIoControl;
DriverObject->MajorFunction[ IRP_MJ_INTERNAL_DEVICE_CONTROL ] = TdiFilterInternalIoControl;
DriverObject->MajorFunction[ IRP_MJ_CLEANUP ] = TdiFilterCleanUp;
DriverObject->DriverUnload = TdiFilterUnload;
RtlInitUnicodeString( &TcpDeviceName, TCP_DEVICE_NAME );
RtlInitUnicodeString( &UdpDeviceName, UDP_DEVICE_NAME );
if( !NT_SUCCESS( AttachToTdiDevice( DriverObject, &TcpDeviceName, &g_FilterDeviceForDeviceTcp ) ) ||
!NT_SUCCESS( AttachToTdiDevice( DriverObject, &UdpDeviceName, &g_FilterDeviceForDeviceUdp ) ) )
{
ExDeleteNPagedLookasideList( &g_CompletionWrapList );
goto Error;
}
ntStatus = RegisterProcessCreateNotify();
if( !NT_SUCCESS( ntStatus ) )
{
ExDeleteNPagedLookasideList( &g_CompletionWrapList );
goto Error;
}
ntStatus = CreateWorkThread( ThreadWaitCompletion, NULL, &g_ThreadWaitCompletion );
if( !NT_SUCCESS( ntStatus ) )
{
ExDeleteNPagedLookasideList( &g_CompletionWrapList );
goto Error;
}
DebugPrintEx( DRIVER_ENTRY_INFO, "netmon DriverEntry end CreateWorkThread ThreadWaitCompletion\n" );
ntStatus = init_hash_table( &g_MasterIrpHash, MASTER_IRP_HASH_TABLE_SIZE );
if( 0 > ntStatus )
{
goto Error;
}
return ntStatus;
Error:
if ( NULL != g_FilterDeviceForDeviceTcp )
{
PDEVICE_OBJECT DeviceObject;
DeviceObject = *( PDEVICE_OBJECT * )g_FilterDeviceForDeviceTcp->DeviceExtension;
IoDetachDevice( DeviceObject );
IoDeleteDevice( g_FilterDeviceForDeviceTcp );
g_FilterDeviceForDeviceTcp = NULL;
}
if ( NULL != g_FilterDeviceForDeviceUdp )
{
PDEVICE_OBJECT DeviceObject;
DeviceObject = *( PDEVICE_OBJECT * )g_FilterDeviceForDeviceUdp->DeviceExtension;
IoDetachDevice( DeviceObject );
IoDeleteDevice( g_FilterDeviceForDeviceUdp );
g_FilterDeviceForDeviceUdp = NULL;
}
if( NULL != g_DevTdiFilter )
{
IoDeleteDevice( g_DevTdiFilter );
g_DevTdiFilter = NULL;
}
IoDeleteSymbolicLink( &TdiFilterDeviceDosName );
return ntStatus;
}
DWORD ReleaseAllEventHandlerWrap()
{
NTSTATUS ntStatus;
DWORD dwErrorCount;
KIRQL OldIrql;
PLIST_ENTRY pListEntry;
PLIST_ENTRY pListEntryPrev;
PTDI_EVENT_HANDLER_LIST pTdiEventHandlerList;
PTDI_EVENT_HANDLER_WRAP pTdiEventHandlerWrap;
KeAcquireSpinLock( &g_SpLockTdiEventHandlerInfo, &OldIrql );
pListEntry = g_TdiEventHandlerInfoList.Flink;
dwErrorCount = 0;
for( ; ; )
{
if( pListEntry == &g_TdiEventHandlerInfoList )
{
break;
}
pListEntryPrev = pListEntry->Flink;
pTdiEventHandlerList = ( PTDI_EVENT_HANDLER_LIST )pListEntry;
RemoveEntryList( pListEntry );
ntStatus = RestoreEventHandler( pTdiEventHandlerList->pTdiEventHandlerWrap );
if( !NT_SUCCESS( ntStatus ) )
{
dwErrorCount ++;
}
ExFreePoolWithTag( pTdiEventHandlerList->pTdiEventHandlerWrap, 0 );
ExFreePoolWithTag( pTdiEventHandlerList, 0 );
pListEntry = pListEntryPrev;
}
KeReleaseSpinLock( &g_SpLockTdiEventHandlerInfo, OldIrql );
return dwErrorCount;
}
VOID TdiFilterUnload( PDRIVER_OBJECT pDriverObject )
{
UNICODE_STRING TdiFilterDeviceDosName;
PDEVICE_OBJECT DeviceObject;
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon enter TdiFilterUnload\n" );
RtlInitUnicodeString( ( PUNICODE_STRING )&TdiFilterDeviceDosName,
TDI_FILTER_DEVICE_DOS_NAME );
g_bFiltering = FALSE;
DeregisterProcessCreateNotify();
ASSERT( NULL != g_FilterDeviceForDeviceTcp &&
NULL != g_FilterDeviceForDeviceUdp &&
NULL != g_DevTdiFilter );
if( NULL != g_FilterDeviceForDeviceTcp )
{
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon release g_FilterDeviceForDeviceTcp begin\n" );
DeviceObject = *( PDEVICE_OBJECT * )g_FilterDeviceForDeviceTcp->DeviceExtension;
IoDetachDevice( DeviceObject );
IoDeleteDevice( g_FilterDeviceForDeviceTcp );
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon release g_FilterDeviceForDeviceTcp end\n" );
}
if( NULL != g_FilterDeviceForDeviceUdp )
{
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon release g_FilterDeviceForDeviceUdp begin\n" );
DeviceObject = *( PDEVICE_OBJECT * )g_FilterDeviceForDeviceUdp->DeviceExtension;
IoDetachDevice( DeviceObject );
IoDeleteDevice( g_FilterDeviceForDeviceUdp );
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon release g_FilterDeviceForDeviceUdp end\n" );
}
if( NULL != g_DevTdiFilter )
{
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon release g_DevTdiFilter begin\n" );
IoDeleteDevice( g_DevTdiFilter );
IoDeleteSymbolicLink( &TdiFilterDeviceDosName );
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon release g_DevTdiFilter end\n" );
}
ASSERT( NULL != g_ThreadWaitCompletion );
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon ReleaseAllProcessesInformation begin\n" );
ReleaseAllProcessesInformation();
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon ReleaseAllProcessesInformation end\n" );
g_bThreadUpdateConfigStop = TRUE;
g_bThreadIrpProcessStop = TRUE;
if( NULL != ThreadUpdateConfig )
{
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon release ThreadUpdateConfig begin\n" );
KeWaitForSingleObject( ThreadUpdateConfig, Executive, KernelMode, FALSE, NULL );
ObDereferenceObject( ThreadUpdateConfig );
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon release ThreadUpdateConfig end\n" );
}
if( NULL != ThreadProcessIrp )
{
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon release ThreadProcessIrp begin\n" );
KeWaitForSingleObject( ThreadProcessIrp, Executive, KernelMode, FALSE, NULL );
ObDereferenceObject( ThreadProcessIrp );
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon release ThreadProcessIrp end\n" );
}
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon ReleaseAllEventHandlerWrap begin\n" );
ReleaseAllEventHandlerWrap();
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon ReleaseAllEventHandlerWrap end\n" );
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon ReleaseAllProcessNetWorkTrafficInfo begin\n" );
ReleaseAllProcessNetWorkTrafficInfo();
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon ReleaseAllProcessNetWorkTrafficInfo end\n" );
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon wait g_ThreadWaitCompletion begin\n" );
KeSetEvent( &g_EventCompletion, 0, FALSE );
KeWaitForSingleObject( g_ThreadWaitCompletion, Executive, KernelMode, FALSE, NULL );
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon wait g_ThreadWaitCompletion end\n" );
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon ExDeleteNPagedLookasideList begin\n" );
ExDeleteNPagedLookasideList( &g_CompletionWrapList );
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon ExDeleteNPagedLookasideList end\n" );
ExDeleteResourceLite( &g_SyncResource );
DebugPrintEx( DRIVER_UNLOAD_INFO, "netmon ExDeleteResourceLite end\n" );
return;
}
NTSTATUS TdiFilterCleanUp(PDEVICE_OBJECT DeviceObject, PIRP pIrp )
{
NTSTATUS ntStatus;
KIRQL OldIrql;
LIST_ENTRY *pListEntry;
PTDI_EVENT_HANDLER_LIST pTdiEventHandlerListFind;
PTDI_EVENT_HANDLER_WRAP pTdiEventHandlerWrapFind;
PFILE_OBJECT pFileObject;
TDI_FILTER_DEVICE_EXTENSION *pDeviceExtension;
PIO_STACK_LOCATION pIrpSp;
DebugPrintEx( CLEANUP_INFO, "netmon Enter TdiFilterCleanUp\n" );
pDeviceExtension = ( TDI_FILTER_DEVICE_EXTENSION* )DeviceObject->DeviceExtension;
pIrpSp = IoGetCurrentIrpStackLocation( pIrp );
pFileObject = pIrpSp->FileObject;
if( FALSE == IsDriverDevice( DeviceObject ) )
{
ntStatus = STATUS_INVALID_PARAMETER;
goto COMPLETE_IRP;
}
if( DeviceObject == g_DevTdiFilter )
{
ntStatus = STATUS_SUCCESS;
goto COMPLETE_IRP;
}
DebugPrintEx( CLEANUP_INFO, "netmon TdiFilterCleanUp IoCallDriver\n" );
IoSkipCurrentIrpStackLocation( pIrp );
ntStatus = IoCallDriver( pDeviceExtension->pTdiDeviceObject, pIrp );
if( !NT_SUCCESS( ntStatus ) )
{
DebugPrintEx( CLEANUP_INFO,"netmon TdiFilterCleanUp IoCallDriver return ERROR\n" );
return ntStatus;
}
KeAcquireSpinLock( &g_SpLockTdiEventHandlerInfo, &OldIrql );
FIND_LIST_AGAIN:
pListEntry = g_TdiEventHandlerInfoList.Flink;
for( ; ; )
{
if( pListEntry == &g_TdiEventHandlerInfoList )
{
break;
}
pTdiEventHandlerListFind = ( PTDI_EVENT_HANDLER_LIST )pListEntry;
pTdiEventHandlerWrapFind = pTdiEventHandlerListFind->pTdiEventHandlerWrap;
DebugPrintEx( CLEANUP_INFO,"Client address find: 0x%0.8x, input 0x%0.8x \n",
pTdiEventHandlerWrapFind->pAssocAddr,
pFileObject );
if( pTdiEventHandlerWrapFind->pAssocAddr == pFileObject )
{
DebugPrintEx( CLEANUP_INFO,"Client address finded: 0x%0.8x \n", pTdiEventHandlerWrapFind->pAssocAddr );
RemoveEntryList( pListEntry );
ExFreePoolWithTag( pTdiEventHandlerWrapFind, 0 );
ExFreePoolWithTag( pTdiEventHandlerListFind, 0 );
goto FIND_LIST_AGAIN;
}
pListEntry = pListEntry->Flink;
}
KeReleaseSpinLock( &g_SpLockTdiEventHandlerInfo, OldIrql );
return ntStatus;
COMPLETE_IRP:
pIrp->IoStatus.Status = ntStatus;
pIrp->IoStatus.Information = 0;
IoCompleteRequest( pIrp, 0 );
return ntStatus;
}
NTSTATUS TdiFilterDefaultIrpProcess( PDEVICE_OBJECT pDeviceObject, PIRP pIrp )
{
IO_STACK_LOCATION *pIrpSp;
NTSTATUS ntStatus;
TDI_FILTER_DEVICE_EXTENSION *pDeviceExtension;
DebugPrintEx( PROCESS_COMMON_INFO, "netmon enter TdiFilterDefaultIrpProcess \n" );
pDeviceExtension = ( TDI_FILTER_DEVICE_EXTENSION* )pDeviceObject->DeviceExtension;
pIrpSp = IoGetCurrentIrpStackLocation( pIrp );
if ( !IsDriverDevice( pDeviceObject, pIrp ) )
{
ntStatus = STATUS_INVALID_PARAMETER;
goto COMPLETE_IRP;
}
if ( pDeviceObject == g_DevTdiFilter )
{
ntStatus = STATUS_SUCCESS;
goto COMPLETE_IRP;
}
IoSkipCurrentIrpStackLocation( pIrp );
return IoCallDriver( pDeviceExtension->pTdiDeviceObject, pIrp );
COMPLETE_IRP:
pIrp->IoStatus.Status = ntStatus;
pIrp->IoStatus.Information = 0;
IofCompleteRequest( pIrp, 0 );
return ntStatus;
}
NTSTATUS RestoreEventHandler( PTDI_EVENT_HANDLER_WRAP pEventHandlerWrap )
{
NTSTATUS ntStatus;
PIRP pIrp = NULL;
PDEVICE_OBJECT pDeviceObject;
ASSERT( NULL != pEventHandlerWrap );
ASSERT( FALSE != MmIsAddressValid( pEventHandlerWrap ) );
pDeviceObject = pEventHandlerWrap->pDeviceObject;
ASSERT( FALSE != MmIsAddressValid( pDeviceObject ) );
if( NULL == pDeviceObject ||
FALSE == MmIsAddressValid( pEventHandlerWrap->pAssocAddr ) )
return STATUS_UNSUCCESSFUL;
if( NULL == pEventHandlerWrap->pOrgEventHandler )
{
return STATUS_SUCCESS;
}
pIrp = TdiBuildInternalDeviceControlIrp( TDI_SET_EVENT_HANDLER, pDeviceObject,
pEventHandlerWrap->pAssocAddr, NULL, NULL );
if ( NULL == pIrp )
{
ntStatus = STATUS_UNSUCCESSFUL;
goto RETURN_;
}
TdiBuildSetEventHandler( pIrp,
pDeviceObject,
pEventHandlerWrap->pAssocAddr,
NULL,
NULL,
pEventHandlerWrap->dwEventType,
pEventHandlerWrap->pOrgEventHandler,
pEventHandlerWrap->pOrgEventContext
);
DebugPrintEx( RESTORE_EVENT_HANDLER_INFO, "net RestoreEventHandler restore event wrap 0x%0.8x, handler 0x%0.8x file object 0x%0.8x success \n",
pEventHandlerWrap,
pEventHandlerWrap->pOrgEventHandler,
pEventHandlerWrap->pAssocAddr );
ntStatus = IoCallDriver( pDeviceObject, pIrp );
pIrp = NULL;
if( NT_SUCCESS( ntStatus ) ) {
DebugPrintEx( RESTORE_EVENT_HANDLER_INFO, "net RestoreEventHandler restore event wrap 0x%0.8x, handler 0x%0.8x success \n",
pEventHandlerWrap,
pEventHandlerWrap->pOrgEventHandler
);
goto RETURN_;
}
// don't wait to complete
RETURN_:
if( NULL != pIrp )
{
IoFreeIrp( pIrp );
}
return ntStatus;
}
VOID DeleteEventWrap( PTDI_EVENT_HANDLER_LIST pTdiEventHandlerList )
{
KIRQL OldIrql;
KeAcquireSpinLock( &g_SpLockTdiEventHandlerInfo, &OldIrql );
DebugPrintEx( RECV_EVENT_HANDLER_INFO, "Delete event wrap list: 0x%0.8x \n",
pTdiEventHandlerList );
RemoveEntryList( ( PLIST_ENTRY )pTdiEventHandlerList );
ExFreePoolWithTag( pTdiEventHandlerList->pTdiEventHandlerWrap, 0 );
ExFreePoolWithTag( pTdiEventHandlerList, 0 );
KeReleaseSpinLock( &g_SpLockTdiEventHandlerInfo, OldIrql );
return;
}
VOID UpdateEventHandlerWrap( PPROCESS_NETWORK_TRAFFIC pProcessNetWorkTrafficInfo,
PEPROCESS pEProcess,
PDEVICE_OBJECT pDeviceObject,
PFILE_OBJECT pFileObject,
DWORD dwEventType,
PVOID pEventHandler,
PVOID pEventContext,
PTDI_EVENT_HANDLER_LIST *ppEventHandlerList,
DWORD dwFlags )
{
KIRQL OldIrql;
PLIST_ENTRY pListEntry;
PTDI_EVENT_HANDLER_LIST pTdiEventHandlerListFind;
PTDI_EVENT_HANDLER_WRAP pTdiEventHandlerWrapFind;
PTDI_EVENT_HANDLER_LIST pTdiEventHandlerListNew = NULL;
PTDI_EVENT_HANDLER_WRAP pTdiEventHandlerWrapNew = NULL;
ASSERT( NULL != ppEventHandlerList );
*ppEventHandlerList = NULL;
KeAcquireSpinLock( &g_SpLockTdiEventHandlerInfo, &OldIrql );
pListEntry = g_TdiEventHandlerInfoList.Flink;
for( ; ; )
{
if( pListEntry == &g_TdiEventHandlerInfoList )
{
break;
}
pTdiEventHandlerListFind = ( PTDI_EVENT_HANDLER_LIST )pListEntry;
pTdiEventHandlerWrapFind = pTdiEventHandlerListFind->pTdiEventHandlerWrap;
if( pTdiEventHandlerWrapFind->pAssocAddr == pFileObject &&
pTdiEventHandlerWrapFind->dwEventType == dwEventType )
{
ASSERT( TRUE == MmIsAddressValid( pTdiEventHandlerWrapFind->pAssocAddr ) );
if( pProcessNetWorkTrafficInfo != NULL &&
pTdiEventHandlerWrapFind->pProcessNetWorkTrafficInfo != pProcessNetWorkTrafficInfo )
{
DebugPrintEx( RECV_EVENT_HANDLER_INFO, "***Find event handler wrap, but process is not same***, finded 0x%0.8x, input 0x%0.8x \n",
pTdiEventHandlerWrapFind->pProcessNetWorkTrafficInfo,
pProcessNetWorkTrafficInfo );
}
if( DEL_EVENT_WRAP == dwFlags )
{
DebugPrintEx( RECV_EVENT_HANDLER_INFO, "Delete event wrap: 0x%0.8x, Process io information 0x%0.8x, Address 0x%0.8x, event type %d, event handler 0x%0.8x \n",
pTdiEventHandlerWrapFind,
pProcessNetWorkTrafficInfo,
pFileObject,
pTdiEventHandlerWrapFind->dwEventType,
pTdiEventHandlerWrapFind->pOrgEventHandler
);
RemoveEntryList( pListEntry );
ExFreePoolWithTag( pTdiEventHandlerWrapFind, 0 );
ExFreePoolWithTag( pTdiEventHandlerListFind, 0 );
}
else
{
DebugPrintEx( RECV_EVENT_HANDLER_INFO, "Update event wrap: 0x%0.8x, Process io information 0x%0.8x, Address 0x%0.8x old event type %d, old event handler 0x%0.8x, new event type %d, new event handler 0x%0.8x \n",
pTdiEventHandlerWrapFind,
pProcessNetWorkTrafficInfo,
pFileObject,
pTdiEventHandlerWrapFind->dwEventType,
pTdiEventHandlerWrapFind->pOrgEventHandler,
dwEventType,
pEventHandler );
pTdiEventHandlerWrapFind->pOrgEventHandler = pEventHandler;
pTdiEventHandlerWrapFind->pOrgEventContext = pEventContext;
*ppEventHandlerList = pTdiEventHandlerListFind;
}
KeReleaseSpinLock( &g_SpLockTdiEventHandlerInfo, OldIrql );
return;
}
pListEntry = pListEntry->Flink;
}
if( GET_EVENT_WRAP == dwFlags )
{
pTdiEventHandlerWrapNew = ( PTDI_EVENT_HANDLER_WRAP )AllocZeroPoolWithTag( NonPagedPool, sizeof( TDI_EVENT_HANDLER_WRAP ) );
if( NULL == pTdiEventHandlerWrapNew )
{
goto RELEASE_POOL_EXIT;
}
pTdiEventHandlerListNew = ( PTDI_EVENT_HANDLER_LIST )AllocZeroPoolWithTag( NonPagedPool, sizeof( TDI_EVENT_HANDLER_LIST ) );
if( NULL == pTdiEventHandlerListNew )
{
goto RELEASE_POOL_EXIT;
}
pTdiEventHandlerWrapNew->dwEventContextMark = TDI_EVENT_CONTEXT_MARK;
pTdiEventHandlerWrapNew->dwEventType = dwEventType;
pTdiEventHandlerWrapNew->pOrgEventHandler = pEventHandler;
pTdiEventHandlerWrapNew->pOrgEventContext = pEventContext;
pTdiEventHandlerWrapNew->pEProcess = pEProcess;
pTdiEventHandlerWrapNew->pProcessNetWorkTrafficInfo = pProcessNetWorkTrafficInfo;
pTdiEventHandlerWrapNew->pAssocAddr = pFileObject;
pTdiEventHandlerWrapNew->pDeviceObject = pDeviceObject;
pTdiEventHandlerListNew->pTdiEventHandlerWrap = pTdiEventHandlerWrapNew;
InsertTailList( &g_TdiEventHandlerInfoList, ( PLIST_ENTRY )pTdiEventHandlerListNew );
DebugPrintEx( RECV_EVENT_HANDLER_INFO, "Add new event wrap: 0x%0.8x, Process io information 0x%0.8x, Address 0x%0.8x event type %d, new event handler 0x%0.8x \n",
pTdiEventHandlerWrapNew,
pProcessNetWorkTrafficInfo,
pFileObject,
pTdiEventHandlerWrapNew->dwEventType,
pTdiEventHandlerWrapNew->pOrgEventHandler
);
*ppEventHandlerList = pTdiEventHandlerListNew;
KeReleaseSpinLock( &g_SpLockTdiEventHandlerInfo, OldIrql );
return;
RELEASE_POOL_EXIT:
if( NULL != pTdiEventHandlerWrapNew )
{
ExFreePoolWithTag( pTdiEventHandlerWrapNew, NonPagedPool );
}
if( NULL != pTdiEventHandlerListNew )
{
ExFreePoolWithTag( pTdiEventHandlerListNew, NonPagedPool );
}
KeReleaseSpinLock( &g_SpLockTdiEventHandlerInfo, OldIrql );
return;
}
KeReleaseSpinLock( &g_SpLockTdiEventHandlerInfo, OldIrql );
return;
}
PIRP DequeueIrp( PLIST_ENTRY pListHead, PKSPIN_LOCK SpLock )
{
KIRQL oldIrql;
PIRP nextIrp = NULL;
KeAcquireSpinLock( SpLock, &oldIrql );
while( !nextIrp && !IsListEmpty( pListHead ) )
{
PDRIVER_CANCEL oldCancelRoutine;
PLIST_ENTRY listEntry = RemoveHeadList( pListHead );
// Get the next IRP off the queue.
nextIrp = CONTAINING_RECORD( listEntry, IRP, Tail.Overlay.ListEntry );
if( NULL == nextIrp->AssociatedIrp.MasterIrp )
{
// Clear the IRP's cancel routine
oldCancelRoutine = IoSetCancelRoutine(nextIrp, NULL);
// IoCancelIrp() could have just been called on this IRP.
// What we're interested in is not whether IoCancelIrp() was called (nextIrp->Cancel flag set),
// but whether IoCancelIrp() called (or is about to call) our cancel routine.
// To check that, check the result of the test-and-set macro IoSetCancelRoutine.
if (oldCancelRoutine){
// Cancel routine not called for this IRP. Return this IRP.
ASSERT(oldCancelRoutine == TdiFilterCancel );
}
else {
// This IRP was just canceled and the cancel routine was (or will be) called.
// The cancel routine will complete this IRP as soon as we drop the spin lock,
// so don't do anything with the IRP.
// Also, the cancel routine will try to dequeue the IRP,
// so make the IRP's listEntry point to itself.
ASSERT(nextIrp->Cancel);
InitializeListHead(&nextIrp->Tail.Overlay.ListEntry);
nextIrp = NULL;
}
}
}
KeReleaseSpinLock(SpLock, oldIrql);
return nextIrp;
}
VOID TdiFilterCancel(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
)
{
NTSTATUS ntStatus;
PLIST_ENTRY pListEntry = NULL;
PIRP pAssocIrp = NULL;
KIRQL OldIrql;
KIRQL CancelIrql = Irp->CancelIrql;
PPROCESS_NETWORK_TRAFFIC pProcessNetWorkTraffic = NULL;
hash_key key;
key.quad_part = make_hash_key( 0, ( DWORD )Irp );
DebugPrintEx( IRP_CANCEL_INFO, "netmon Enter TdiFilterCancel\n" );
IoReleaseCancelSpinLock( CancelIrql );
FIND_ASSOC_IRPS:
KeAcquireSpinLock( &pProcessNetWorkTraffic->IrpListLock, &OldIrql );
DebugPrintEx( IRP_CANCEL_INFO, "netmon TdiFilterCancel get_hash_value\n" );
ntStatus = get_hash_value( &g_MasterIrpHash, key, &pProcessNetWorkTraffic );
DebugPrintEx( IRP_CANCEL_INFO, "netmon TdiFilterCancel get_hash_value return 0x%0.8x, pProcessNetWorkTraffic 0x%0.8x\n",
ntStatus,
pProcessNetWorkTraffic
);
if( 0 > ntStatus )
{
ASSERT( FALSE );
goto RETURN_;
}
ASSERT( TRUE == MmIsAddressValid( pProcessNetWorkTraffic ) );
pListEntry = pProcessNetWorkTraffic->IrpList.Flink;
for ( ; ; )
{
if( pListEntry == &pProcessNetWorkTraffic->IrpList )
{
break;
}
pAssocIrp = CONTAINING_RECORD( pListEntry, IRP, Tail.Overlay.ListEntry );
DebugPrintEx( IRP_CANCEL_INFO, "netmon TdiFilterCancel find irp 0x%0.8x master irp 0x%0.8x cancel irp 0x%0.8x\n",
pAssocIrp->AssociatedIrp.MasterIrp,
Irp );
if ( Irp == pAssocIrp->AssociatedIrp.MasterIrp )
{
DebugPrintEx( IRP_CANCEL_INFO, "netmon TdiFilterCancel finded \n" );
RemoveEntryList( &pAssocIrp->Tail.Overlay.ListEntry );
KeReleaseSpinLock( &pProcessNetWorkTraffic->IrpListLock, OldIrql );
pAssocIrp->IoStatus.Status = STATUS_CANCELLED;
pAssocIrp->IoStatus.Information = 0;
IoCompleteRequest( pAssocIrp, IO_NO_INCREMENT );
goto FIND_ASSOC_IRPS;