A class library to manage the Windows Firewall as well as adding your program to the Windows Firewall Exception list.
This project supports dotNet4.6, NetStandard2 and NetCore5, therefore, is compatible with NetCore2+ and any version of dotNet equal or greater than version 4.6.
Even though it is possible to reference this library under Linux or Mac; it's obviously not going to work.
This readme file is for the version 2 of this library. Please check the V1
branch for older readme file.
This library is available as a NuGet package at nuget.org.
--OR--
You can always donate your time by contributing to the project or by introducing it to others.
The 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.
If you are only targeting WinVista+ consider using the FirewallWAS.Instance
static property to access the library's functionality. It allows for more flexibility and is easier to work with.
This static class contains properties about the currently active Windows Firewall management class instance or the registered third party firewall products. This class also provides methods to register a third-party firewall product.
FirewallManager.Instance
: Gets an instance of the Windows Firewall management class.FirewallManager.Version
: Gets the type of firewall thatFirewallManager.Instance
property returns.FirewallManager.IsServiceRunning
: Gets a boolean value indicating if the Windows Firewall Service is currently running.FirewallManager.RegisteredProducts
: Gets an array containing all registered third party firewall products.
FirewallManager.RegisterProduct()
: Registers a third-party firewall product returning a handle that will unregisters the product while getting disposed.
This namespace contains shared and general classes as well as the main starting point of this library, FirewallManager
class.
FirewallManager
: A static class to manage the current active firewallFirewallProtocol
: A class representing a Firewall ProtocolFirewallLegacy
: Contains properties and methods of Windows Firewall v1 - Implementing theIFirewall
interfaceFirewallLegacyProfile
: Contains properties of a Windows Firewall v1 profile - Implementing theIFirewallProfile
interfaceFirewallWAS
: Contains properties and methods of Windows Firewall with Advanced Security (Vista+) - Implementing theIFirewall
interfaceFirewallWASProfile
: Contains properties of a Windows Firewall with Advanced Security profile (Vista+) - Implementing theIFirewallProfile
interfaceFirewallWASRuleGroup
: Contains properties and methods for managing a Windows Firewall with Advanced Security rule group (Vista+)FirewallWASInternetControlMessage
: Representing an Internet Control Message (ICM) typeFirewallProduct
: Representing a third-party firewall productFirewallProductRegistrationHandle
: Representing a third-party firewall product registration handle that will automatically unregisters the product while getting disposed.
IFirewall
: Defines expected methods and properties of a firewall program or APIIFirewallProfile
: Defines expected properties of a firewall profileIFirewallRule
: Defines expected properties of a firewall ruleIAddress
: Defines expected methods of a network address
This namespace contains classes that can be used for direct manipulation of a firewall rule.
FirewallLegacyApplicationRule
: Contains properties of a Windows Firewall v1 application rule - Implementing theIFirewallRule
interfaceFirewallLegacyPortRule
: Contains properties of a Windows Firewall v1 port rule - Implementing theIFirewallRule
interfaceFirewallWASRule
: Contains properties of a Windows Firewall with Advanced Security rule - Implementing theIFirewallRule
interfaceFirewallWASRuleWin7
: Contains properties of a Windows Firewall with Advanced Security rule for Windows 7+ - Extending theFirewallWASRule
classFirewallWASRuleWin8
: Contains properties of a Windows Firewall with Advanced Security rule for Windows 8+ - Extending theFirewallWASRuleWin7
class
This namespace contains exception classes that might be thrown when using this library
FirewallLegacyNotSupportedException
: The exception that is thrown when an invoked method or action is not supported with the 'Windows Firewall API v1' - Extending theNotSupportedException
classFirewallWASNotSupportedException
: The exception that is thrown when an invoked method or action is not supported with the 'Windows Firewall with Advanced Security' - Extending theNotSupportedException
classFirewallWASInvalidProtocolException
: The exception that is thrown when a passed FirewallProtocol is invalid for a 'Windows Firewall with Advanced Security' action or method - Extending the `InvalidOperationException`` class
This namespace contains the classes needed for manipulating or understanding a network address or a network service.
SingleIP
: Represents a single network IP address - Implementing theIAddress
interfaceIPRange
: Represents a range of network IP addresses - Implementing theIAddress
interfaceNetworkAddress
: Represents a range of network IP addresses by subnet - Implementing theIAddress
interfaceSpecialAddress
: An abstract class represents a special network address or network service - Implementing theIAddress
interfaceDefaultGateway
: Represents the default network gateway - Extending the `SpecialAddress`` classLocalSubnet
: Represents thelocal network subnet - Extending the `SpecialAddress`` classDHCPService
: Represents the DHCP service - Extending the `SpecialAddress`` classDNSService
: Represents the DNS service - Extending the `SpecialAddress`` classWINSService
: Represents the WINS service - Extending the `SpecialAddress`` class
This namespace contains the interface
s and enum
s that is used to access the underlying COM objects. Some of these types are public and can be used to directly modify a COM object. Usually firewall rules. Rest of types are internal to this library.
Check the 'WindowsFirewallHelper.Sample' and 'WindowsFirewallHelper.NetCoreSample' projects as a brief example of what can be done using this class library.
- Creating and registering a new application exception rule for outbound traffic on the currently active profile:
var rule = FirewallManager.Instance.CreateApplicationRule(
@"MyApp Rule",
FirewallAction.Allow,
@"C:\MyApp.exe"
);
rule.Direction = FirewallDirection.Outbound;
FirewallManager.Instance.Rules.Add(rule);
- Creating and registering a new port rule for inbound traffic on the currently active profile:
var rule = FirewallManager.Instance.CreatePortRule(
@"Port 80 - Any Protocol",
FirewallAction.Allow,
80,
FirewallProtocol.TCP
);
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(
@"Port 80 - Any Protocol",
FirewallAction.Allow,
80,
FirewallProtocol.Any
);
if (rule is FirewallWASRule wasRule)
{
wasRule.Interfaces = NetworkInterface.GetAllNetworkInterfaces()
.Where(i => i.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
.ToArray();
wasRule.ICMPTypesAndCodes = new[]
{
new FirewallWASInternetControlMessage(InternetControlMessageKnownTypes.Echo),
new FirewallWASInternetControlMessage(InternetControlMessageKnownTypes.EchoReply)
};
if (rule is FirewallWASRuleWin7 wasRuleWin7)
{
wasRuleWin7.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 (FirewallWAS.IsSupported && FirewallWASRuleWin8.IsSupported)
{
var rule = new FirewallWASRuleWin8(
"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",
NetworkInterfaceTypes = NetworkInterfaceTypes.Wireless,
Protocol = FirewallProtocol.TCP
};
FirewallWAS.Instance.Rules.Add(rule);
}
The MIT License (MIT)
Copyright (c) 2016-2020 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.