windows-wfp 0.2.1

Safe Rust wrapper for the Windows Filtering Platform (WFP) kernel-level firewall API
Documentation
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
//! WFP Filter Enumeration
//!
//! Enumerate active WFP filters in the system. Useful for debugging,
//! auditing, and discovering filters from other firewall software.
//!
//! # Example
//!
//! ```no_run
//! use windows_wfp::{WfpEngine, FilterEnumerator, FilterInfo};
//!
//! let engine = WfpEngine::new()?;
//! let filters: Vec<FilterInfo> = FilterEnumerator::all(&engine)?;
//!
//! for filter in &filters {
//!     println!("{}: {} ({:?})", filter.id, filter.name, filter.action);
//! }
//! # Ok::<(), windows_wfp::WfpError>(())
//! ```

use crate::engine::WfpEngine;
use crate::errors::{WfpError, WfpResult};
use std::path::PathBuf;
use std::ptr;
use windows::core::GUID;
use windows::Win32::Foundation::{ERROR_SUCCESS, HANDLE};
use windows::Win32::NetworkManagement::WindowsFilteringPlatform::{
    FwpmFilterCreateEnumHandle0, FwpmFilterDestroyEnumHandle0, FwpmFilterEnum0, FwpmFreeMemory0,
    FWPM_FILTER0, FWP_ACTION_BLOCK, FWP_ACTION_CALLOUT_INSPECTION, FWP_ACTION_CALLOUT_TERMINATING,
    FWP_ACTION_CALLOUT_UNKNOWN, FWP_ACTION_PERMIT, FWP_BYTE_BLOB_TYPE, FWP_EMPTY, FWP_UINT16,
    FWP_UINT32, FWP_UINT64, FWP_UINT8,
};

use crate::constants::CONDITION_ALE_APP_ID;

/// Action type of a WFP filter
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FilterAction {
    /// Block traffic
    Block,
    /// Permit traffic
    Permit,
    /// Callout (terminating)
    CalloutTerminating,
    /// Callout (inspection only)
    CalloutInspection,
    /// Callout (unknown)
    CalloutUnknown,
    /// Other action type
    Other(u32),
}

/// Information about an active WFP filter
#[derive(Debug, Clone)]
pub struct FilterInfo {
    /// WFP filter ID
    pub id: u64,
    /// Display name
    pub name: String,
    /// Description
    pub description: String,
    /// Filter action (Block, Permit, Callout)
    pub action: FilterAction,
    /// Provider GUID (if set)
    pub provider_key: Option<GUID>,
    /// Filter weight/priority
    pub weight: u64,
    /// Layer GUID where the filter is installed
    pub layer_key: GUID,
    /// Sublayer GUID
    pub sublayer_key: GUID,
    /// Application path extracted from conditions (if present)
    pub app_path: Option<PathBuf>,
    /// Number of conditions on this filter
    pub num_conditions: u32,
}

/// Enumerates WFP filters from the system
///
/// Provides methods to list all active filters; callers can then filter by provider if desired.
///
/// # Example
///
/// ```no_run
/// use windows_wfp::{WfpEngine, FilterEnumerator};
///
/// let engine = WfpEngine::new()?;
///
/// // List all filters
/// let all = FilterEnumerator::all(&engine)?;
/// println!("Total filters: {}", all.len());
///
/// // Filter by provider GUID
/// use windows::core::GUID;
/// let my_guid = GUID::from_u128(0x12345678_1234_5678_1234_567812345678);
/// let mine: Vec<_> = all.iter().filter(|f| f.provider_key == Some(my_guid)).collect();
/// # Ok::<(), windows_wfp::WfpError>(())
/// ```
pub struct FilterEnumerator;

impl FilterEnumerator {
    /// Enumerate all active WFP filters
    ///
    /// Returns a vector of [`FilterInfo`] for every filter currently registered in WFP.
    ///
    /// # Errors
    ///
    /// Returns an error if the enumeration handle cannot be created or enumeration fails.
    /// Requires administrator privileges.
    pub fn all(engine: &WfpEngine) -> WfpResult<Vec<FilterInfo>> {
        Self::enumerate_raw(engine, |filter_array, num_returned, acc: &mut Vec<FilterInfo>| {
            for i in 0..num_returned {
                unsafe {
                    let filter = &**filter_array.offset(i as isize);
                    acc.push(parse_filter(filter));
                }
            }
        })
    }

    /// Count all active WFP filters without collecting details
    ///
    /// Counts filters without parsing their display names or conditions.
    /// Requires administrator privileges.
    pub fn count(engine: &WfpEngine) -> WfpResult<usize> {
        Self::enumerate_raw(engine, |_filter_array, num_returned, acc: &mut usize| {
            *acc += num_returned as usize;
        })
    }

    /// Internal helper: create an enum handle, iterate batches, accumulate results, then destroy the handle.
    ///
    /// `visitor` is called for each batch with the raw filter pointer array, the number of
    /// filters returned in that batch, and a mutable reference to the accumulator.
    fn enumerate_raw<T, F>(engine: &WfpEngine, mut visitor: F) -> WfpResult<T>
    where
        T: Default,
        F: FnMut(*mut *mut FWPM_FILTER0, u32, &mut T),
    {
        let mut enum_handle = HANDLE::default();

        unsafe {
            let result = FwpmFilterCreateEnumHandle0(engine.handle(), None, &mut enum_handle);
            if result != ERROR_SUCCESS.0 {
                return Err(WfpError::Other(format!(
                    "Failed to create filter enum handle: error code {}",
                    result
                )));
            }
        }

        let mut accumulator = T::default();
        let batch_size = 100;

        loop {
            let mut filter_array: *mut *mut FWPM_FILTER0 = ptr::null_mut();
            let mut num_returned: u32 = 0;

            let result = unsafe {
                FwpmFilterEnum0(
                    engine.handle(),
                    enum_handle,
                    batch_size,
                    &mut filter_array,
                    &mut num_returned,
                )
            };

            if result != ERROR_SUCCESS.0 {
                unsafe {
                    let _ = FwpmFilterDestroyEnumHandle0(engine.handle(), enum_handle);
                }
                return Err(WfpError::Other(format!(
                    "Failed to enumerate filters: error code {}",
                    result
                )));
            }

            if num_returned == 0 {
                unsafe {
                    if !filter_array.is_null() {
                        FwpmFreeMemory0(&mut filter_array as *mut _ as *mut *mut _);
                    }
                }
                break;
            }

            visitor(filter_array, num_returned, &mut accumulator);

            unsafe {
                if !filter_array.is_null() {
                    FwpmFreeMemory0(&mut filter_array as *mut _ as *mut *mut _);
                }
            }
        }

        unsafe {
            let _ = FwpmFilterDestroyEnumHandle0(engine.handle(), enum_handle);
        }

        Ok(accumulator)
    }
}

/// Parse a raw FWPM_FILTER0 into FilterInfo
///
/// # Safety
///
/// The filter pointer must be valid.
unsafe fn parse_filter(filter: &FWPM_FILTER0) -> FilterInfo {
    let name = if !filter.displayData.name.is_null() {
        filter
            .displayData
            .name
            .to_string()
            .unwrap_or_else(|_| String::new())
    } else {
        String::new()
    };

    let description = if !filter.displayData.description.is_null() {
        filter
            .displayData
            .description
            .to_string()
            .unwrap_or_else(|_| String::new())
    } else {
        String::new()
    };

    let action = match filter.action.r#type {
        FWP_ACTION_BLOCK => FilterAction::Block,
        FWP_ACTION_PERMIT => FilterAction::Permit,
        FWP_ACTION_CALLOUT_TERMINATING => FilterAction::CalloutTerminating,
        FWP_ACTION_CALLOUT_INSPECTION => FilterAction::CalloutInspection,
        FWP_ACTION_CALLOUT_UNKNOWN => FilterAction::CalloutUnknown,
        other => FilterAction::Other(other.0),
    };

    let provider_key = if !filter.providerKey.is_null() {
        Some(*filter.providerKey)
    } else {
        None
    };

    let weight = match filter.weight.r#type {
        FWP_UINT8 => filter.weight.Anonymous.uint8 as u64,
        FWP_UINT16 => filter.weight.Anonymous.uint16 as u64,
        FWP_UINT32 => filter.weight.Anonymous.uint32 as u64,
        FWP_UINT64 => {
            let ptr = filter.weight.Anonymous.uint64;
            if ptr.is_null() {
                0
            } else {
                *ptr
            }
        }
        FWP_EMPTY => 0,
        _ => 0,
    };

    let app_path = extract_app_path(filter);

    FilterInfo {
        id: filter.filterId,
        name,
        description,
        action,
        provider_key,
        weight,
        layer_key: filter.layerKey,
        sublayer_key: filter.subLayerKey,
        app_path,
        num_conditions: filter.numFilterConditions,
    }
}

/// Extract application path from filter conditions
///
/// Looks for FWPM_CONDITION_ALE_APP_ID in the filter's conditions
/// and decodes the wide-string blob.
unsafe fn extract_app_path(filter: &FWPM_FILTER0) -> Option<PathBuf> {
    if filter.numFilterConditions == 0 || filter.filterCondition.is_null() {
        return None;
    }

    let conditions =
        std::slice::from_raw_parts(filter.filterCondition, filter.numFilterConditions as usize);

    let condition = conditions
        .iter()
        .find(|c| c.fieldKey == CONDITION_ALE_APP_ID)?;

    if condition.conditionValue.r#type != FWP_BYTE_BLOB_TYPE {
        return None;
    }

    let blob_ptr = condition.conditionValue.Anonymous.byteBlob;
    if blob_ptr.is_null() {
        return None;
    }

    let blob = &*blob_ptr;
    if blob.data.is_null() || blob.size == 0 || (blob.size % 2) != 0 {
        return None;
    }

    let wide_slice = std::slice::from_raw_parts(blob.data as *const u16, (blob.size / 2) as usize);
    let null_pos = wide_slice
        .iter()
        .position(|&c| c == 0)
        .unwrap_or(wide_slice.len());

    String::from_utf16(&wide_slice[..null_pos])
        .ok()
        .map(PathBuf::from)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_filter_action_eq() {
        assert_eq!(FilterAction::Block, FilterAction::Block);
        assert_eq!(FilterAction::Permit, FilterAction::Permit);
        assert_ne!(FilterAction::Block, FilterAction::Permit);
        assert_eq!(FilterAction::Other(42), FilterAction::Other(42));
        assert_ne!(FilterAction::Other(1), FilterAction::Other(2));
    }

    #[test]
    fn test_filter_action_copy() {
        let action = FilterAction::Block;
        let copy = action;
        assert_eq!(action, copy);
    }

    #[test]
    fn test_filter_info_clone() {
        let info = FilterInfo {
            id: 42,
            name: "Test filter".to_string(),
            description: "A test".to_string(),
            action: FilterAction::Block,
            provider_key: None,
            weight: 1000,
            layer_key: GUID::zeroed(),
            sublayer_key: GUID::zeroed(),
            app_path: Some(PathBuf::from(r"C:\test.exe")),
            num_conditions: 1,
        };

        let cloned = info.clone();
        assert_eq!(cloned.id, 42);
        assert_eq!(cloned.name, "Test filter");
        assert_eq!(cloned.action, FilterAction::Block);
        assert!(cloned.app_path.is_some());
    }

    #[test]
    fn test_filter_info_default_values() {
        let info = FilterInfo {
            id: 0,
            name: String::new(),
            description: String::new(),
            action: FilterAction::Permit,
            provider_key: None,
            weight: 0,
            layer_key: GUID::zeroed(),
            sublayer_key: GUID::zeroed(),
            app_path: None,
            num_conditions: 0,
        };

        assert_eq!(info.id, 0);
        assert!(info.name.is_empty());
        assert!(info.provider_key.is_none());
        assert!(info.app_path.is_none());
    }

    #[test]
    fn test_filter_info_with_provider() {
        let guid = GUID::from_u128(0x12345678_1234_5678_1234_567812345678);
        let info = FilterInfo {
            id: 100,
            name: "Provider filter".to_string(),
            description: String::new(),
            action: FilterAction::Block,
            provider_key: Some(guid),
            weight: 5000,
            layer_key: GUID::zeroed(),
            sublayer_key: GUID::zeroed(),
            app_path: None,
            num_conditions: 3,
        };

        assert_eq!(info.provider_key, Some(guid));
        assert_eq!(info.num_conditions, 3);
    }

    #[test]
    #[ignore] // Requires administrator privileges
    fn test_enumerate_all_filters() {
        let engine = WfpEngine::new().expect("Failed to open WFP engine");
        let filters = FilterEnumerator::all(&engine).expect("Failed to enumerate");
        // Any Windows system should have at least some WFP filters
        assert!(!filters.is_empty(), "Expected at least one WFP filter");
    }

    #[test]
    #[ignore] // Requires administrator privileges
    fn test_count_filters() {
        let engine = WfpEngine::new().expect("Failed to open WFP engine");
        let count = FilterEnumerator::count(&engine).expect("Failed to count");
        let all = FilterEnumerator::all(&engine).expect("Failed to enumerate");
        assert_eq!(count, all.len());
    }
}