A class library to manage the Windows Firewall as well as adding your program to the Windows Firewall Exception list.
This library is available as a NuGet package at nuget.org.
Starting point of this library is the FirewallManager
static class which can be used to get the instance of the class managing the firewall currently on the system.
This static class contains properties about the current active Windows Firewall management class instance or the registered active third party firewall. This class also provides methods to register a third party firewall management object.
FirewallManager.Instance
: Gets the first active third party firewall object or the instance of the Windows Firewall management class.FirewallManager.Version
: Gets the type of firewall object thatFirewallManager.Instance
property returns.FirewallManager.ThirdPartyFirewalls
: Gets an array containing all registered third party firewall management objects.
FirewallManager.RegisterFirewall()
: Registers an instance of a third party firewall management class implementing theIFirewall
interface.
This namespace contains shared and general classes as well as main starting point of this library, FirewallManager
class.
WindowsFirewallHelper.FirewallManager
: A static class to manage the current active firewallWindowsFirewallHelper.FirewallProtocol
: A class representing a Firewall ProtocolWindowsFirewallHelper.ActiveCollection
: Represents a dynamic data collection that provides notifications when items get added or removed.WindowsFirewallHelper.ActiveCollectionChangedEventArgs
: class containing event data about theActiveCollection.ItemsModified
event.
WindowsFirewallHelper.IFirewall
: Defines expected methods and properties of a firewall program or APIWindowsFirewallHelper.IProfile
: Defines expected properties of a firewall profileWindowsFirewallHelper.IRule
: Defines expected properties of a firewall ruleWindowsFirewallHelper.IAddress
: Defines expected methods of a network address
This namespace contains the classes to manage and manipulate the Windows Firewall using the Windows Firewall API v1. Supporting Windows XP and above.
FirewallAPIv1.Firewall
: Contains properties and methods of Windows Firewall v1 - ImplementingIFirewall
interfaceFirewallAPIv1.FirewallProfile
: Contains properties of a Windows Firewall v1 profile - ImplementingIProfile
interfaceFirewallAPIv1.Rules.ApplicationRule
: Contains properties of a Windows Firewall v1 application rule - ImplementingIRule
interfaceFirewallAPIv1.Rules.PortRule
: Contains properties of a Windows Firewall v1 port rule - ImplementingIRule
interfaceFirewallAPIv1.FirewallAPIv1NotSupportedException
: The exception that is thrown when an invoked method is not supported with the 'Windows Firewall API v1' - ExtendingNotSupportedException
This namespace contains the classes to manage and manipulate the Windows Firewall using the Windows Firewall API v2 (aka. Windows Firewall with Advanced Security). Supporting Windows Vista and above.
FirewallAPIv2.Firewall
: Contains properties and methods of Windows Firewall with Advanced Security - ImplementingIFirewall
interfaceFirewallAPIv2.FirewallProfile
: Contains properties of a Windows Firewall with Advanced Security profile - ImplementingIProfile
interfaceFirewallAPIv2.Rules.StandardRule
: Contains properties of a Windows Firewall with Advanced Security rule - ImplementingIRule
interfaceFirewallAPIv2.Rules.StandardRuleWin7
: Contains properties of a Windows Firewall with Advanced Security rule for Windows 7+ - ExtendingFirewallAPIv2.Rules.StandardRule
FirewallAPIv2.Rules.StandardRuleWin8
: Contains properties of a Windows Firewall with Advanced Security rule for Windows 8+ - ExtendingFirewallAPIv2.Rules.StandardRuleWin7
FirewallAPIv2.InternetControlMessage
: A class representing an Internet Control Message (ICM) typeFirewallAPIv2.FirewallAPIv2NotSupportedException
: The exception that is thrown when an invoked method is not supported with the 'Windows Firewall with Advanced Security' - ExtendingNotSupportedException
Check the 'WindowsFirewallHelper.Sample' project as a brief example of what can be done using this class library.
- Creating and registering a new application exception rule for out-bound traffic on the currently enable profile:
var rule = FirewallManager.Instance.CreateApplicationRule(
FirewallManager.Instance.GetProfile().Type, @"MyApp Rule",
FirewallAction.Allow, @"C:\MyApp.exe");
rule.Direction = FirewallDirection.Outbound;
FirewallManager.Instance.Rules.Add(rule);
- Creating and registering a new port rule for in-bound traffic on the currently enable profile:
var rule = FirewallManager.Instance.CreatePortRule(
FirewallManager.Instance.GetProfile().Type,
@"Port 80 - Any Protocol", FirewallAction.Allow, 80,
FirewallProtocol.Any);
FirewallManager.Instance.Rules.Add(rule);
- Getting the list of all registered rules:
var allRules = FirewallManager.Instance.Rules.ToArray();
- Removing a rule by name:
var myRule = FirewallManager.Instance.Rules.SingleOrDefault(r => r.Name == "My Rule");
if (myRule != null)
{
FirewallManager.Instance.Rules.Remove(myRule);
}
- Disabling notifications for all firewall profiles:
foreach (var profile in FirewallManager.Instance.Profiles)
{
profile.ShowNotifications = false;
}
- Creating a heavily customized application rule (Some parts of the following code are only applicable to Windows Vista, Windows 7 and above):
var rule = FirewallManager.Instance.CreatePortRule(
FirewallManager.Instance.GetProfile().Type,
@"Port 80 - Any Protocol", FirewallAction.Allow, 80,
FirewallProtocol.Any);
if (rule is FirewallAPIv2.Rules.StandardRule)
{
((FirewallAPIv2.Rules.StandardRule)rule).Interfaces =
NetworkInterface.GetAllNetworkInterfaces()
.Where(@interface => @interface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
.ToArray();
((FirewallAPIv2.Rules.StandardRule)rule).IcmpTypesAndCodes = new[]
{
new InternetControlMessage(InternetControlMessageKnownTypes.Echo),
new InternetControlMessage(InternetControlMessageKnownTypes.EchoReply)
};
if (rule is FirewallAPIv2.Rules.StandardRuleWin7)
{
((FirewallAPIv2.Rules.StandardRuleWin7)rule).EdgeTraversalOptions = EdgeTraversalAction.Deny;
}
}
rule.Direction = FirewallDirection.Outbound;
FirewallManager.Instance.Rules.Add(rule);
- Working directly with the desired firewall management class without using the
FirewallManager
to add a new port rule (Following example is limited to Windows 8 and above):
if (FirewallAPIv2.Firewall.Instance.IsSupported && StandardRuleWin8.IsSupported)
{
rule = new StandardRuleWin8("My Port Rule", 1080, FirewallAction.Allow,
FirewallDirection.Inbound,
FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public)
{
Description =
"'My Port Rule' Allows Inbound traffic to my local Proxy Server from Wireless Adapters",
InterfaceTypes = FirewallInterfaceTypes.Wireless,
Protocol = FirewallProtocol.TCP
};
FirewallAPIv2.Firewall.Instance.Rules.Add(rule);
}
The MIT License (MIT)
Copyright (c) 2016 Soroush
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.