-
Notifications
You must be signed in to change notification settings - Fork 12
/
WinDivert.cs
1053 lines (907 loc) · 33.1 KB
/
WinDivert.cs
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 (c) 2017 Jesse Nicholson.
* Copyright (c) 2016 basil@reqrypt.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Notice that this file based heavily on windivert.h, which can be found here:
* https://github.com/basil00/Divert/blob/master/include/windivert.h
*
* This is mentioned here to give proper credit to the author, basil, as this file
* is probably over 90% composed of a near direct copy and paste of his copyrighted
* work, with a much smaller degree of work invested by myself, Jesse Nicholson,
* to make the PInvoke function correctly, and also to throw in some convenience
* functions to make working with this library from .NET more natural.
*/
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading;
namespace WinDivert
{
internal static class WinApiHelpers
{
[DllImport("kernel32.dll", EntryPoint = "CreateEvent", SetLastError = true)]
public static extern IntPtr CreateEvent(IntPtr lpEventAttributes, [MarshalAs(UnmanagedType.Bool)] bool bManualReset, [MarshalAs(UnmanagedType.Bool)] bool bInitialState, IntPtr lpName);
[DllImport("kernel32.dll", EntryPoint = "CloseHandle", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", EntryPoint = "WaitForSingleObject", SetLastError = true)]
public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
[DllImport("kernel32.dll", EntryPoint = "GetOverlappedResult", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetOverlappedResult(IntPtr hFile, ref NativeOverlapped lpOverlapped, ref uint lpNumberOfBytesTransferred, [MarshalAs(UnmanagedType.Bool)] bool bWait);
}
internal class WinDivertConstants
{
/// WINDIVERT_DIRECTION_OUTBOUND -> 0
public const int WINDIVERT_DIRECTION_OUTBOUND = 0;
/// WINDIVERT_DIRECTION_INBOUND -> 1
public const int WINDIVERT_DIRECTION_INBOUND = 1;
/// WINDIVERT_FLAG_SNIFF -> 1
public const int WINDIVERT_FLAG_SNIFF = 1;
/// WINDIVERT_FLAG_DROP -> 2
public const int WINDIVERT_FLAG_DROP = 2;
/// WINDIVERT_PARAM_MAX -> WINDIVERT_PARAM_QUEUE_TIME
public const WINDIVERT_PARAM WINDIVERT_PARAM_MAX = WINDIVERT_PARAM.WINDIVERT_PARAM_QUEUE_TIME;
}
internal class WinDivertHelpers
{
public static ushort SwapOrder(ushort val)
{
return (ushort)(((val & 0xFF00) >> 8) | ((val & 0x00FF) << 8));
}
public static uint SwapOrder(uint val)
{
val = (val >> 16) | (val << 16);
return ((val & 0xFF00) >> 8) | ((val & 0x00FF) << 8);
}
public static ulong SwapOrder(ulong val)
{
val = (val >> 32) | (val << 32);
val = ((val & 0xFFFF0000FFFF0000) >> 16) | ((val & 0x0000FFFF0000FFFF) << 16);
return ((val & 0xFF00FF00FF00FF00) >> 8) | ((val & 0x00FF00FF00FF00FF) << 8);
}
/// <summary>
/// Gets the fragment offset for the given ipv4 header.
/// </summary>
/// <param name="hdr">
/// The ipv4 header.
/// </param>
/// <returns>
/// The extracted fragment offset from the given ipv4 header.
/// </returns>
public static ushort WINDIVERT_IPHDR_GET_FRAGOFF(WINDIVERT_IPHDR hdr)
{
return (ushort)((hdr.FragOff0) & 0xFF1F);
}
/// <summary>
/// Gets whether or not given ipv4 header has the more fragments flag set.
/// </summary>
/// <param name="hdr">
/// The ipv4 header.
/// </param>
/// <returns>
/// True if the given ipv4 has the more fragments flag set, false otherwise.
/// </returns>
public static bool WINDIVERT_IPHDR_GET_MF(WINDIVERT_IPHDR hdr)
{
return (ushort)((hdr.FragOff0) & 0x0020) != 0;
}
/// <summary>
/// Gets whether or not given ipv4 header has the don't fragment flag set.
/// </summary>
/// <param name="hdr">
/// The ipv4 header.
/// </param>
/// <returns>
/// True if the given ipv4 has the don't fragment flag set, false otherwise.
/// </returns>
public static bool WINDIVERT_IPHDR_GET_DF(WINDIVERT_IPHDR hdr)
{
return (ushort)((hdr.FragOff0) & 0x0040) != 0;
}
/// <summary>
/// Gets whether or not given ipv4 header has the reserved flag set.
/// </summary>
/// <param name="hdr">
/// The ipv4 header.
/// </param>
/// <returns>
/// True if the given ipv4 has the reserved flag set, false otherwise.
/// </returns>
public static bool WINDIVERT_IPHDR_GET_RESERVED(WINDIVERT_IPHDR hdr)
{
return (ushort)((hdr.FragOff0) & 0x0080) != 0;
}
/// <summary>
/// Sets the fragment offset for the given ipv4 header.
/// </summary>
/// <param name="header">
/// The ipv4 header.
/// </param>
/// <param name="val">
/// The fragment offset.
/// </param>
public static void WINDIVERT_IPHDR_SET_FRAGOFF(WINDIVERT_IPHDR header, ushort val)
{
header.FragOff0 = (ushort)(((header.FragOff0) & 0x00E0) | ((val) & 0xFF1F));
}
/// <summary>
/// Sets the more fragments flag to the given value.
/// </summary>
/// <param name="header">
/// The ipv4 header.
/// </param>
/// <param name="val">
/// The more fragments flag value.
/// </param>
public static void WINDIVERT_IPHDR_SET_MF(WINDIVERT_IPHDR header, ushort val)
{
header.FragOff0 = (ushort)(((header.FragOff0) & 0xFFDF) | (((val) & 0x0001) << 5));
}
/// <summary>
/// Sets the don't fragment flag to the given value.
/// </summary>
/// <param name="header">
/// The ipv4 header.
/// </param>
/// <param name="val">
/// The don't fragment flag value.
/// </param>
public static void WINDIVERT_IPHDR_SET_DF(WINDIVERT_IPHDR header, ushort val)
{
header.FragOff0 = (ushort)(((header.FragOff0) & 0xFFBF) | (((val) & 0x0001) << 6));
}
/// <summary>
/// Sets the reserved flag to the given value.
/// </summary>
/// <param name="header">
/// The ipv4 header.
/// </param>
/// <param name="val">
/// The reserved flag value.
/// </param>
public static void WINDIVERT_IPHDR_SET_RESERVED(WINDIVERT_IPHDR header, ushort val)
{
header.FragOff0 = (ushort)(((header.FragOff0) & 0xFF7F) | (((val) & 0x0001) << 7));
}
/// <summary>
/// Gets the traffic class value.
/// </summary>
/// <param name="hdr">
/// The ipv6 header.
/// </param>
/// <returns>
/// The traffic class value.
/// </returns>
public static uint WINDIVERT_IPV6HDR_GET_TRAFFICCLASS(WINDIVERT_IPV6HDR hdr)
{
return (byte)((hdr.TrafficClass0 << 4) | (byte)hdr.TrafficClass1);
}
/// <summary>
/// Gets the flow label value.
/// </summary>
/// <param name="hdr">
/// The ipv6 header.
/// </param>
/// <returns>
/// The flow label value.
/// </returns>
public static uint WINDIVERT_IPV6HDR_GET_FLOWLABEL(WINDIVERT_IPV6HDR hdr)
{
return (uint)((hdr.FlowLabel0 << 16) | hdr.FlowLabel1);
}
/// <summary>
/// Sets the traffic class value.
/// </summary>
/// <param name="hdr">
/// The ipv6 header.
/// </param>
/// <param name="val">
/// The value.
/// </param>
public static void WINDIVERT_IPV6HDR_SET_TRAFFICCLASS(WINDIVERT_IPV6HDR hdr, byte val)
{
hdr.TrafficClass0 = (byte)((val) >> 4);
hdr.TrafficClass1 = val;
}
/// <summary>
/// Sets the flow label value.
/// </summary>
/// <param name="hdr">
/// The ipv6 header.
/// </param>
/// <param name="val">
/// The value.
/// </param>
public static void WINDIVERT_IPV6HDR_SET_FLOWLABEL(WINDIVERT_IPV6HDR hdr, uint val)
{
//hdr.FlowLabel0 = (uint)(val >> 16);
//hdr.FlowLabel1 = (ushort)val;
}
/// WINDIVERT_HELPER_NO_IP_CHECKSUM -> 1
public const int WINDIVERT_HELPER_NO_IP_CHECKSUM = 1;
/// WINDIVERT_HELPER_NO_ICMP_CHECKSUM -> 2
public const int WINDIVERT_HELPER_NO_ICMP_CHECKSUM = 2;
/// WINDIVERT_HELPER_NO_ICMPV6_CHECKSUM -> 4
public const int WINDIVERT_HELPER_NO_ICMPV6_CHECKSUM = 4;
/// WINDIVERT_HELPER_NO_TCP_CHECKSUM -> 8
public const int WINDIVERT_HELPER_NO_TCP_CHECKSUM = 8;
/// WINDIVERT_HELPER_NO_UDP_CHECKSUM -> 16
public const int WINDIVERT_HELPER_NO_UDP_CHECKSUM = 16;
/// WINDIVERT_HELPER_NO_REPLACE -> 2048
public const int WINDIVERT_HELPER_NO_REPLACE = 2048;
}
[StructLayout(LayoutKind.Sequential)]
internal struct WINDIVERT_ADDRESS
{
/// UINT32->unsigned int
public uint IfIdx;
/// UINT32->unsigned int
public uint SubIfIdx;
/// UINT8->unsigned char
public byte Direction;
public void Reset()
{
IfIdx = 0;
SubIfIdx = 0;
Direction = 0;
}
}
internal enum WINDIVERT_LAYER
{
/// WINDIVERT_LAYER_NETWORK -> 0
WINDIVERT_LAYER_NETWORK = 0,
/// WINDIVERT_LAYER_NETWORK_FORWARD -> 1
WINDIVERT_LAYER_NETWORK_FORWARD = 1,
}
internal enum WINDIVERT_PARAM
{
/// WINDIVERT_PARAM_QUEUE_LEN -> 0
WINDIVERT_PARAM_QUEUE_LEN = 0,
/// WINDIVERT_PARAM_QUEUE_TIME -> 1
WINDIVERT_PARAM_QUEUE_TIME = 1,
}
internal struct WINDIVERT_IPHDR
{
public uint HdrLength
{
get
{
return ((uint)((this.bitvector1 & 15u)));
}
set
{
this.bitvector1 = ((byte)((value | this.bitvector1)));
}
}
public uint Version
{
get
{
return ((uint)(((this.bitvector1 & 240u)
/ 16)));
}
set
{
this.bitvector1 = ((byte)(((value * 16)
| this.bitvector1)));
}
}
/// HdrLength : 4
///Version : 4
private byte bitvector1;
/// UINT8->unsigned char
public byte TOS;
/// UINT16->unsigned short
public ushort Length;
/// UINT16->unsigned short
public ushort Id;
/// UINT16->unsigned short
public ushort FragOff0;
/// UINT8->unsigned char
public byte TTL;
/// UINT8->unsigned char
public byte Protocol;
/// UINT16->unsigned short
public ushort Checksum;
/// UINT32->unsigned int
public IPAddress SrcAddr
{
get
{
return new IPAddress(unchecked((long)this.SourceAddress));
}
set
{
Debug.Assert(value.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork, "Not a valid IPV4 address.");
if (value.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
{
throw new ArgumentException("Not a valid IPV4 address.", nameof(SrcAddr));
}
this.SourceAddress = (uint)BitConverter.ToInt32(value.GetAddressBytes(), 0);
}
}
private uint SourceAddress;
/// UINT32->unsigned int
public IPAddress DstAddr
{
get
{
return new IPAddress(unchecked((long)this.DestinationAddress));
}
set
{
Debug.Assert(value.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork, "Not a valid IPV4 address.");
if (value.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
{
throw new ArgumentException("Not a valid IPV4 address.", nameof(DstAddr));
}
this.DestinationAddress = (uint)BitConverter.ToInt32(value.GetAddressBytes(), 0);
}
}
private uint DestinationAddress;
}
internal unsafe struct WINDIVERT_IPV6HDR
{
public uint TrafficClass0
{
get
{
return ((uint)((this.bitvector1 & 15u)));
}
set
{
this.bitvector1 = ((ushort)((value | this.bitvector1)));
}
}
public uint Version
{
get
{
return ((uint)(((this.bitvector1 & 240u)
/ 16)));
}
set
{
this.bitvector1 = ((ushort)(((value * 16)
| this.bitvector1)));
}
}
public uint FlowLabel0
{
get
{
return ((uint)(((this.bitvector1 & 3840u)
/ 256)));
}
set
{
this.bitvector1 = ((ushort)(((value * 256)
| this.bitvector1)));
}
}
public uint TrafficClass1
{
get
{
return ((uint)(((this.bitvector1 & 61440u)
/ 4096)));
}
set
{
this.bitvector1 = ((ushort)(((value * 4096)
| this.bitvector1)));
}
}
/// TrafficClass0 : 4
///Version : 4
///FlowLabel0 : 4
///TrafficClass1 : 4
private ushort bitvector1;
/// UINT16->unsigned short
public ushort FlowLabel1;
/// UINT16->unsigned short
public ushort Length;
/// UINT8->unsigned char
public byte NextHdr;
/// UINT8->unsigned char
public byte HopLimit;
/// UINT32[4]
public IPAddress SrcAddr
{
get
{
fixed (uint* addr = this.SourceAddress)
{
var b1 = BitConverter.GetBytes(addr[0]);
var b2 = BitConverter.GetBytes(addr[1]);
var b3 = BitConverter.GetBytes(addr[2]);
var b4 = BitConverter.GetBytes(addr[3]);
var bytes = new byte[] {
b1[0], b1[1], b1[2], b1[3],
b2[0], b2[1], b2[2], b2[3],
b3[0], b3[1], b3[2], b3[3],
b4[0], b4[1], b4[2], b4[3]
};
return new IPAddress(bytes);
}
}
set
{
fixed (uint* addr = this.SourceAddress)
{
var valueBytes = value.GetAddressBytes();
Debug.Assert(valueBytes.Length == 16, "Not a valid IPV6 address.");
if (valueBytes.Length != 16)
{
throw new ArgumentException("Not a valid IPV6 address.", nameof(SrcAddr));
}
addr[0] = BitConverter.ToUInt32(valueBytes, 0);
addr[1] = BitConverter.ToUInt32(valueBytes, 4);
addr[2] = BitConverter.ToUInt32(valueBytes, 8);
addr[3] = BitConverter.ToUInt32(valueBytes, 12);
}
}
}
private fixed uint SourceAddress[4];
/// UINT32[4]
public IPAddress DstAddr
{
get
{
fixed (uint* addr = this.DestinationAddress)
{
var b1 = BitConverter.GetBytes(addr[0]);
var b2 = BitConverter.GetBytes(addr[1]);
var b3 = BitConverter.GetBytes(addr[2]);
var b4 = BitConverter.GetBytes(addr[3]);
var bytes = new byte[] {
b1[0], b1[1], b1[2], b1[3],
b2[0], b2[1], b2[2], b2[3],
b3[0], b3[1], b3[2], b3[3],
b4[0], b4[1], b4[2], b4[3]
};
return new IPAddress(bytes);
}
}
set
{
fixed (uint* addr = this.DestinationAddress)
{
var valueBytes = value.GetAddressBytes();
Debug.Assert(valueBytes.Length == 16, "Not a valid IPV6 address.");
if (valueBytes.Length != 16)
{
throw new ArgumentException("Not a valid IPV6 address.", nameof(DstAddr));
}
addr[0] = BitConverter.ToUInt32(valueBytes, 0);
addr[1] = BitConverter.ToUInt32(valueBytes, 4);
addr[2] = BitConverter.ToUInt32(valueBytes, 8);
addr[3] = BitConverter.ToUInt32(valueBytes, 12);
}
}
}
private fixed uint DestinationAddress[4];
}
[StructLayout(LayoutKind.Sequential)]
internal struct WINDIVERT_ICMPHDR
{
/// UINT8->unsigned char
public byte Type;
/// UINT8->unsigned char
public byte Code;
/// UINT16->unsigned short
public ushort Checksum;
/// UINT32->unsigned int
public uint Body;
}
[StructLayout(LayoutKind.Sequential)]
internal struct WINDIVERT_ICMPV6HDR
{
/// UINT8->unsigned char
public byte Type;
/// UINT8->unsigned char
public byte Code;
/// UINT16->unsigned short
public ushort Checksum;
/// UINT32->unsigned int
public uint Body;
}
[StructLayout(LayoutKind.Sequential)]
internal struct WINDIVERT_TCPHDR
{
/// UINT16->unsigned short
public ushort SrcPort
{
get
{
return WinDivertHelpers.SwapOrder(this.SourcePort);
}
set
{
this.SourcePort = WinDivertHelpers.SwapOrder(value);
}
}
public ushort SrcPortNw
{
get
{
return this.SourcePort;
}
set
{
this.SourcePort = value;
}
}
/// UINT16->unsigned short
public ushort DstPort
{
get
{
return WinDivertHelpers.SwapOrder(this.DestinationPort);
}
set
{
this.DestinationPort = WinDivertHelpers.SwapOrder(value);
}
}
public ushort DstPortNw
{
get
{
return this.DestinationPort;
}
set
{
this.DestinationPort = value;
}
}
/// UINT16->unsigned short
private ushort SourcePort;
/// UINT16->unsigned short
private ushort DestinationPort;
/// UINT32->unsigned int
public uint SeqNum;
/// UINT32->unsigned int
public uint AckNum;
/// Reserved1 : 4
///HdrLength : 4
///Fin : 1
///Syn : 1
///Rst : 1
///Psh : 1
///Ack : 1
///Urg : 1
///Reserved2 : 2
private ushort bitvector1;
/// UINT16->unsigned short
public ushort Window;
/// UINT16->unsigned short
public ushort Checksum;
/// UINT16->unsigned short
public ushort UrgPtr;
public uint Reserved1
{
get
{
return ((uint)((this.bitvector1 & 15u)));
}
set
{
this.bitvector1 = ((ushort)((value | this.bitvector1)));
}
}
public uint HdrLength
{
get
{
return ((uint)(((this.bitvector1 & 240u)
/ 16)));
}
set
{
this.bitvector1 = ((ushort)(((value * 16)
| this.bitvector1)));
}
}
public uint Fin
{
get
{
return ((uint)(((this.bitvector1 & 256u)
/ 256)));
}
set
{
this.bitvector1 = ((ushort)(((value * 256)
| this.bitvector1)));
}
}
public uint Syn
{
get
{
return ((uint)(((this.bitvector1 & 512u)
/ 512)));
}
set
{
this.bitvector1 = ((ushort)(((value * 512)
| this.bitvector1)));
}
}
public uint Rst
{
get
{
return ((uint)(((this.bitvector1 & 1024u)
/ 1024)));
}
set
{
this.bitvector1 = ((ushort)(((value * 1024)
| this.bitvector1)));
}
}
public uint Psh
{
get
{
return ((uint)(((this.bitvector1 & 2048u)
/ 2048)));
}
set
{
this.bitvector1 = ((ushort)(((value * 2048)
| this.bitvector1)));
}
}
public uint Ack
{
get
{
return ((uint)(((this.bitvector1 & 4096u)
/ 4096)));
}
set
{
this.bitvector1 = ((ushort)(((value * 4096)
| this.bitvector1)));
}
}
public uint Urg
{
get
{
return ((uint)(((this.bitvector1 & 8192u)
/ 8192)));
}
set
{
this.bitvector1 = ((ushort)(((value * 8192)
| this.bitvector1)));
}
}
public uint Reserved2
{
get
{
return ((uint)(((this.bitvector1 & 49152u)
/ 16384)));
}
set
{
this.bitvector1 = ((ushort)(((value * 16384)
| this.bitvector1)));
}
}
}
[StructLayout(LayoutKind.Sequential)]
internal struct WINDIVERT_UDPHDR
{
public ushort SrcPort
{
get
{
return WinDivertHelpers.SwapOrder(this.SourcePort);
}
set
{
this.SourcePort = WinDivertHelpers.SwapOrder(value);
}
}
public ushort SrcPortNw
{
get
{
return this.SourcePort;
}
set
{
this.SourcePort = value;
}
}
/// UINT16->unsigned short
public ushort DstPort
{
get
{
return WinDivertHelpers.SwapOrder(this.DestinationPort);
}
set
{
this.DestinationPort = WinDivertHelpers.SwapOrder(value);
}
}
public ushort DstPortNw
{
get
{
return this.DestinationPort;
}
set
{
this.DestinationPort = value;
}
}
/// UINT16->unsigned short
private ushort SourcePort;
/// UINT16->unsigned short
public ushort DestinationPort;
/// UINT16->unsigned short
public ushort Length;
/// UINT16->unsigned short
public ushort Checksum;
}
internal class WinDivertMethods
{
static WinDivertMethods()
{
// Modify PATH var to include our WinDivert DLL's so that the LoadLibrary function
// will find whatever WinDivert dll required for the current architecture.
var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };
var dllSearchPaths = new[]
{
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "x86"),
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "x64"),
};
string newPath = string.Join(Path.PathSeparator.ToString(), path.Concat(dllSearchPaths));
Environment.SetEnvironmentVariable("PATH", newPath);
}
/// Return Type: HANDLE->void*
///filter: char*
///layer: WINDIVERT_LAYER->Anonymous_d7dac89f_91f7_4aca_b997_239f157c8039
///priority: INT16->short
///flags: UINT64->unsigned __int64
[DllImport("WinDivert.dll", EntryPoint = "WinDivertOpen", SetLastError = true)]
public static extern IntPtr WinDivertOpen([In] [MarshalAs(UnmanagedType.LPStr)] string filter, WINDIVERT_LAYER layer, short priority, ulong flags);
/// Return Type: BOOL->int
///handle: HANDLE->void*
///pPacket: PVOID->void*
///packetLen: UINT->unsigned int
///pAddr: PWINDIVERT_ADDRESS->Anonymous_7d42ad21_898e_4fdf_a30d_dc1e2c77a38f*
///readLen: UINT*
[DllImport("WinDivert.dll", EntryPoint = "WinDivertRecv", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WinDivertRecv([In] IntPtr handle, byte[] pPacket, uint packetLen, ref WINDIVERT_ADDRESS pAddr, ref uint readLen);
/// Return Type: BOOL->int
///handle: HANDLE->void*
///pPacket: PVOID->void*
///packetLen: UINT->unsigned int
///flags: UINT64->unsigned __int64
///pAddr: PWINDIVERT_ADDRESS->Anonymous_7d42ad21_898e_4fdf_a30d_dc1e2c77a38f*
///readLen: UINT*
///lpOverlapped: LPOVERLAPPED->_OVERLAPPED*
[DllImport("WinDivert.dll", EntryPoint = "WinDivertRecvEx", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WinDivertRecvEx([In] IntPtr handle, byte[] pPacket, uint packetLen, ulong flags, ref WINDIVERT_ADDRESS pAddr, ref uint readLen, ref NativeOverlapped lpOverlapped);
/// Return Type: BOOL->int
///handle: HANDLE->void*
///pPacket: PVOID->void*
///packetLen: UINT->unsigned int
///pAddr: PWINDIVERT_ADDRESS->Anonymous_7d42ad21_898e_4fdf_a30d_dc1e2c77a38f*
///writeLen: UINT*
[DllImport("WinDivert.dll", EntryPoint = "WinDivertSend", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WinDivertSend([In] IntPtr handle, [In] byte[] pPacket, uint packetLen, [In] ref WINDIVERT_ADDRESS pAddr, ref uint writeLen);
/// Return Type: BOOL->int
///handle: HANDLE->void*
///pPacket: PVOID->void*
///packetLen: UINT->unsigned int
///flags: UINT64->unsigned __int64
///pAddr: PWINDIVERT_ADDRESS->Anonymous_7d42ad21_898e_4fdf_a30d_dc1e2c77a38f*
///writeLen: UINT*
///lpOverlapped: LPOVERLAPPED->_OVERLAPPED*
[DllImport("WinDivert.dll", EntryPoint = "WinDivertSendEx", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WinDivertSendEx([In] IntPtr handle, [In] byte[] pPacket, uint packetLen, ulong flags, [In] ref WINDIVERT_ADDRESS pAddr, ref uint writeLen, ref NativeOverlapped lpOverlapped);
[DllImport("WinDivert.dll", EntryPoint = "WinDivertSendEx", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WinDivertSendEx([In] IntPtr handle, [In] byte[] pPacket, uint packetLen, ulong flags, [In] ref WINDIVERT_ADDRESS pAddr, IntPtr writeLen, IntPtr lpOverlapped);
/// Return Type: BOOL->int
///handle: HANDLE->void*
[DllImport("WinDivert.dll", EntryPoint = "WinDivertClose", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WinDivertClose([In] IntPtr handle);
/// Return Type: BOOL->int
///handle: HANDLE->void*
///param: WINDIVERT_PARAM->Anonymous_e5050871_9359_4204_b35d_ed31a2bded35
///value: UINT64->unsigned __int64
[DllImport("WinDivert.dll", EntryPoint = "WinDivertSetParam", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WinDivertSetParam([In] IntPtr handle, WINDIVERT_PARAM param, ulong value);
/// Return Type: BOOL->int
///handle: HANDLE->void*
///param: WINDIVERT_PARAM->Anonymous_e5050871_9359_4204_b35d_ed31a2bded35
///pValue: UINT64*
[DllImport("WinDivert.dll", EntryPoint = "WinDivertGetParam", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WinDivertGetParam([In] IntPtr handle, WINDIVERT_PARAM param, [Out] out ulong pValue);
/// Return Type: BOOL->int
///pPacket: PVOID->void*
///packetLen: UINT->unsigned int