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
use std::{convert::TryFrom, fmt::Debug};
use super::{BitfieldUnit, WinDivertEvent, WinDivertFlags, WinDivertLayer};
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct WINDIVERT_DATA_NETWORK {
pub interface_id: u32,
pub subinterface_id: u32,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct WINDIVERT_DATA_FLOW {
pub endpoint_id: u64,
pub parent_endpoint_id: u64,
pub process_id: u32,
pub local_addr: [u32; 4usize],
pub remote_addr: [u32; 4usize],
pub local_port: u16,
pub remote_port: u16,
pub protocol: u8,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct WINDIVERT_DATA_SOCKET {
pub endpoint_id: u64,
pub parent_endpoint_id: u64,
pub process_id: u32,
pub local_addr: [u32; 4usize],
pub remote_addr: [u32; 4usize],
pub local_port: u16,
pub remote_port: u16,
pub protocol: u8,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct WINDIVERT_DATA_REFLECT {
pub timestamp: i64,
pub process_id: u32,
pub layer: WinDivertLayer,
pub flags: WinDivertFlags,
pub priority: i16,
}
impl Default for WINDIVERT_DATA_REFLECT {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
#[repr(C)]
#[derive(Copy, Clone)]
pub union WINDIVERT_ADDRESS_UNION_FIELD {
pub Network: WINDIVERT_DATA_NETWORK,
pub Flow: WINDIVERT_DATA_FLOW,
pub Socket: WINDIVERT_DATA_SOCKET,
pub Reflect: WINDIVERT_DATA_REFLECT,
reserved: [u8; 64usize],
_union_align: [u64; 8usize],
}
impl Default for WINDIVERT_ADDRESS_UNION_FIELD {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct WINDIVERT_ADDRESS {
pub timestamp: i64,
addr_bitfield: BitfieldUnit<[u8; 4usize], u8>,
reserved: u32,
pub union_field: WINDIVERT_ADDRESS_UNION_FIELD,
}
impl Default for WINDIVERT_ADDRESS {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
impl WINDIVERT_ADDRESS {
#[inline]
pub fn layer(&self) -> WinDivertLayer {
WinDivertLayer::try_from(self.addr_bitfield.get(0usize, 8u8) as u32)
.expect("Layer always is correct since it would have produced an error in Open()")
}
#[inline]
pub fn set_layer(&mut self, val: WinDivertLayer) {
self.addr_bitfield.set(0usize, 8u8, u32::from(val) as u64)
}
#[inline]
pub fn event(&self) -> WinDivertEvent {
WinDivertEvent::try_from(self.addr_bitfield.get(8usize, 8u8) as u8)
.expect("Event always is correct since teh value comes from the DLL functions.")
}
#[inline]
pub fn set_event(&mut self, val: WinDivertEvent) {
self.addr_bitfield.set(8usize, 8u8, u32::from(val) as u64)
}
#[inline]
pub fn sniffed(&self) -> bool {
self.addr_bitfield.get(16usize, 1u8) == 1
}
#[inline]
pub fn set_sniffed(&mut self, val: bool) {
self.addr_bitfield.set(16usize, 1u8, val as u64)
}
#[inline]
pub fn outbound(&self) -> bool {
self.addr_bitfield.get(17usize, 1u8) == 1
}
#[inline]
pub fn set_outbound(&mut self, val: bool) {
self.addr_bitfield.set(17usize, 1u8, val as u64)
}
#[inline]
pub fn loopback(&self) -> bool {
self.addr_bitfield.get(18usize, 1u8) == 1
}
#[inline]
pub fn set_loopback(&mut self, val: bool) {
self.addr_bitfield.set(18usize, 1u8, val as u64)
}
#[inline]
pub fn impostor(&self) -> bool {
self.addr_bitfield.get(19usize, 1u8) == 1
}
#[inline]
pub fn set_impostor(&mut self, val: bool) {
self.addr_bitfield.set(19usize, 1u8, val as u64)
}
#[inline]
pub fn ipv6(&self) -> bool {
self.addr_bitfield.get(20usize, 1u8) == 1
}
#[inline]
pub fn set_ipv6(&mut self, val: bool) {
self.addr_bitfield.set(20usize, 1u8, val as u64)
}
#[inline]
pub fn ipchecksum(&self) -> bool {
self.addr_bitfield.get(21usize, 1u8) == 1
}
#[inline]
pub fn set_ipchecksum(&mut self, val: bool) {
self.addr_bitfield.set(21usize, 1u8, val as u64)
}
#[inline]
pub fn tcpchecksum(&self) -> bool {
self.addr_bitfield.get(22usize, 1u8) == 1
}
#[inline]
pub fn set_tcpchecksum(&mut self, val: bool) {
self.addr_bitfield.set(22usize, 1u8, val as u64)
}
#[inline]
pub fn udpchecksum(&self) -> bool {
self.addr_bitfield.get(23usize, 1u8) == 1
}
#[inline]
pub fn set_udpchecksum(&mut self, val: bool) {
self.addr_bitfield.set(23usize, 1u8, val as u64)
}
}
impl Debug for WINDIVERT_ADDRESS {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let union_str = match self.event() {
WinDivertEvent::NetworkPacket => {
format!("{:?}", unsafe { self.union_field.Network })
}
WinDivertEvent::FlowStablished | WinDivertEvent::FlowDeleted => {
format!("{:?}", unsafe { self.union_field.Flow })
}
WinDivertEvent::SocketBind
| WinDivertEvent::SocketConnect
| WinDivertEvent::SocketListen
| WinDivertEvent::SocketAccept
| WinDivertEvent::SocketClose => {
format!("{:?}", unsafe { self.union_field.Socket })
}
WinDivertEvent::ReflectOpen | WinDivertEvent::ReflectClose => {
format!("{:?}", unsafe { self.union_field.Reflect })
}
};
write!(f, "WINDIVERT_ADDRESS {{ Timestamp: {:?}, Layer: {:?}, Event: {:?}, Sniffed: {:?}, Outbound: {:?}, Loopback: {:?}, Impostor: {:?}, IPv6: {:?}, IPChecksum: {:?}, TCPChecksum: {:?}, UDPChecksum: {:?}, {}}}",
self.timestamp, self.layer(), self.event(), self.sniffed(), self.outbound(), self.loopback(), self.impostor(), self.ipv6(), self.ipchecksum(), self.tcpchecksum(), self.udpchecksum(), union_str)
}
}