Introduction
Have you ever experienced problems deploying your .NET distributed applications because of Windows Firewall configuration? Did you come up with an idea to write a service that needs to receive unsolicited traffic and can detect if a firewall is running and which exceptions are allowed? Perhaps even let your service find a suitable port to perform the communication?
My initial intention was just to detect if there is a firewall running on the target system. After exploring the Windows Firewall with Advanced Security, I realised that there are unlimited possibilities to query firewall information and even modify its configuration.
The problem was that there were no managed classes to use from C# or VB.NET. So I took the first step. My classes cover perhaps 5% of API functionality, but they show an approach to be used in order to implement further features. Maybe it is a good idea to start a project which will wrap the whole C# API? Your reactions to this article will show.
Approach
The library NetFwTypeLib
(C:\windows\system32\FirewallAPI.dll) includes classes and interfaces making it possible to programmatically read and manage Windows Firewall settings by allowing applications to create, enable, and disable firewall exceptions. There are actually two APIs, the "Windows Firewall" and "Windows Firewall with Advanced Security". The first one is supported under Windows XP SP2 or Vista and the second one is supported only under Windows Vista.
Detailed information can be found in MSDN under http://msdn2.microsoft.com/en-us/library/aa366453.aspx.
The difference is that the Vista API is more convenient and transparent. For example I did not find an equivalent of the Vista interface INetFwRule
which describes a restriction in Windows XP API. My classes are based on Windows Firewall with Advanced Security so this is a Vista only solution.
Using the code
First of all, ensure that you are running Windows Vista. You can reference the assembly or the project or just include my classes in your project. In this case (only when copying classes) you need to set a COM reference to the library NetFwTypeLib
(C:\windows\system32\FirewallAPI.dll).
Using the code consists basically of instantiation of the class Policy
and calling its Properties.
...
Policy policy = new Policy();
Console.WriteLine(string.Format("Firewal enabled? - {0}", policy.Enabled));
Console.WriteLine(string.Format("Inbound traffic allowd? - {0}",
policy.DefaultInboundAction ));
...
To demonstrate this class, I wrote a small Windows application showing all available properties in PropertyGrid
and all rules in DataGrid
(see the screenshot above).
At the end I will just note some techniques used which can generate some questions. Let's take a look at the class Rules
.
public class Rules : ReadOnlyCollection<Rule>
{
internal Rules(INetFwRules rules)
: base(RulesToList(rules))
{
}
private static IList<Rule> RulesToList(INetFwRules rules)
{
List<Rule> list = new List<Rule>(rules.Count);
foreach (INetFwRule currentFwRule in rules)
list.Add(new Rule(currentFwRule));
return list;
}
}
Some constructors in my classes are internal to prevent the user from creating instances. Also using internal methods, I hide the COM interfaces to keep the component interface 100% managed. To keep the Interface consistent and allow typed access to the rules list without enabling its modification, I derived the Rules
class from the generic ReadOnlyCollection<>
.