Skip to main content

simple_block/
simple_block.rs

1//! Simple Block Demo - Add and Remove a WFP Filter
2//!
3//! This is a minimal example that:
4//! 1. Opens a WFP engine session
5//! 2. Adds a block filter for notepad.exe
6//! 3. Waits 10 seconds (test by launching notepad and trying to access network)
7//! 4. Removes the filter
8//!
9//! # Usage
10//!
11//! **REQUIRES ADMINISTRATOR PRIVILEGES**
12//!
13//! ```bash
14//! cargo run --example simple_block --release
15//! ```
16
17use std::thread;
18use std::time::Duration;
19use windows_wfp::{
20    initialize_wfp, Action, Direction, FilterBuilder, FilterRule, FilterWeight, WfpEngine,
21    WfpResult,
22};
23
24fn main() -> WfpResult<()> {
25    println!("windows-wfp - Simple Block Demo\n");
26
27    // Initialize WFP
28    println!("Opening WFP Engine...");
29    let engine = WfpEngine::new()?;
30    println!("Engine opened\n");
31
32    println!("Registering provider...");
33    initialize_wfp(&engine)?;
34    println!("Provider registered\n");
35
36    // Block notepad.exe outbound connections
37    println!("Adding block filter for notepad.exe...");
38    let notepad_rule = FilterRule::new("Block Notepad", Direction::Outbound, Action::Block)
39        .with_weight(FilterWeight::UserBlock)
40        .with_app_path(r"C:\Windows\System32\notepad.exe");
41
42    let filter_id = FilterBuilder::add_filter(&engine, &notepad_rule)?;
43    println!("Filter added (ID: {})\n", filter_id);
44
45    println!("Filter active for 10 seconds...");
46    println!("   (Try opening notepad.exe and accessing network)\n");
47
48    for i in (1..=10).rev() {
49        println!("   {} seconds remaining...", i);
50        thread::sleep(Duration::from_secs(1));
51    }
52
53    println!("\nRemoving filter...");
54    FilterBuilder::delete_filter(&engine, filter_id)?;
55    println!("Filter removed\n");
56
57    println!("Demo complete!");
58    Ok(())
59}