#firewall #windows #ffi #com

sys windows_firewall

A crate for managing Windows Firewall rules and settings

15 releases (5 breaking)

Uses new Rust 2024

new 0.6.0 Feb 17, 2026
0.4.0 Dec 23, 2025
0.3.2 Nov 25, 2025
0.2.0 Jul 22, 2025

#82 in Windows APIs

Download history 6/week @ 2025-11-02 7/week @ 2025-11-09 25/week @ 2025-11-23 595/week @ 2025-11-30 871/week @ 2025-12-07 1196/week @ 2025-12-14 715/week @ 2025-12-21 409/week @ 2025-12-28 813/week @ 2026-01-04 888/week @ 2026-01-11 1359/week @ 2026-01-18 1965/week @ 2026-01-25 1641/week @ 2026-02-01 1392/week @ 2026-02-08 1322/week @ 2026-02-15

6,395 downloads per month
Used in firewall_audit

MIT/Apache

125KB
2K SLoC

Windows Firewall

Crates.io Build Status Dependency Status Documentation License MSRV codecov


A Rust crate for managing Windows Firewall rules and settings using the Windows API in Rust.

Features

  • Create, modify, and delete firewall rules
  • Check firewall status and active profiles
  • Manage incoming and outgoing rules
  • Full control over rule properties:
    • Ports and protocols
    • Applications and services
    • Network interfaces
    • IP addresses
    • ICMP settings
    • Edge traversal
    • Security profiles

Installation

Add this to your Cargo.toml:

[target.'cfg(windows)'.dependencies]
windows_firewall = "0.1.0"

Usage Examples

Creating and Managing Rules

use windows_firewall::{
    add_rule, remove_rule, rule_exists, update_rule, FirewallRule, FirewallRuleUpdate,
    Action, Direction, Protocol
};

// Create a new rule
let mut rule = FirewallRule::builder()
    .name("TestHTTPRule")
    .action(Action::Allow)
    .direction(Direction::In)
    .enabled(true)
    .description("Test HTTP rule")
    .protocol(Protocol::Tcp)
    .local_ports([80])
    .build();

// Add the rule
match add_rule(&rule) {
    Ok(_) => println!("Rule added successfully"),
    Err(e) => eprintln!("Failed to add rule: {}", e),
};

// Verify the rule exists
match rule_exists("TestHTTPRule") {
    Ok(exists) => println!("Rule exists: {}", exists),
    Err(e) => eprintln!("Failed to check rule: {}", e),
};

let updated_settings = FirewallRuleUpdate::builder()
    .enabled(false)
    .description("Updated test HTTP rule")
    .build();

// Update the rule
match update_rule("TestHTTPRule", &updated_settings) {
    Ok(_) => println!("Rule updated successfully"),
    Err(e) => eprintln!("Failed to update rule: {}", e),
};

// Remove the rule
match remove_rule("TestHTTPRule") {
    Ok(_) => println!("Rule removed successfully"),
    Err(e) => eprintln!("Failed to remove rule: {}", e),
};

Another example of using struct methods

use windows_firewall::{
    FirewallRule, FirewallRuleUpdate,
    Action, Direction, Protocol
};

// Create a new firewall rule
let mut rule = FirewallRule::builder()
    .name("TestDNSServerRule")
    .action(Action::Allow)
    .direction(Direction::In)
    .enabled(true)
    .description("Test DNS Server rule")
    .protocol(Protocol::Udp)
    .local_ports([53])
    .build();

// Add the rule
match rule.add() {
    Ok(_) => println!("DNS Server rule added successfully"),
    Err(e) => eprintln!("Failed to add DNS Server rule: {}", e),
};

// Verify the rule exists
match rule.exists() {
    Ok(exists) => println!("Rule exists: {}", exists),
    Err(e) => eprintln!("Failed to check rule: {}", e),
};

let updated_settings = FirewallRuleUpdate::builder()
    .enabled(false)
    .description("Updated DNS Server rule")
    .build();

// Update the rule
match rule.update(&updated_settings) {
    Ok(_) => println!("DNS Server rule updated successfully"),
    Err(e) => eprintln!("Failed to update DNS Server rule: {}", e),
};

// Remove the rule
match rule.remove() {
    Ok(_) => println!("DNS Server rule removed successfully"),
    Err(e) => eprintln!("Failed to remove DNS Server rule: {}", e),
};

Checking Firewall Status

use windows_firewall::{get_firewall_state, Profile};

match get_firewall_state(Profile::Public) {
    Ok(enabled) => println!("Firewall is {}", if enabled { "enabled" } else { "disabled" }),
    Err(e) => eprintln!("Failed to get firewall state: {}", e),
}

Listing Firewall Rules

use windows_firewall::list_rules;

match list_rules() {
    Ok(rules) => {
        for rule in rules {
            println!("Rule: {}", rule.name());
            println!("  Direction: {:?}", rule.direction());
            println!("  Action: {:?}", rule.action());
            println!("  Enabled: {}", rule.enabled());
        }
    },
    Err(e) => eprintln!("Failed to list rules: {}", e),
}

Requirements

  • Windows 7 or later
  • Administrative privileges for certain operations

Support

For issues and questions:

License

This project is licensed under either of

at your option.

Dependencies

~5–36MB
~521K SLoC