Click here to Skip to main content
Licence 
First Posted 30 May 2007
Views 41,278
Downloads 596
Bookmarked 33 times

The managed classes to read Windows Firewall configuration on Vista using Advanced Security Interfaces

By | 30 May 2007 | Article
Provides a read-only access to Windows Firewall status, settings and rules on Windows Vista.

Screenshot - Firewall1.jpg

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).

Screenshot - Firewall0.jpg

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<>.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

George Mamaladze

Software Developer

Germany Germany

Member

Follow on Twitter Follow on Twitter
Tweeter: @gmamaladze
Google+: gmamaladze
Blog: gmamaladze.wordpress.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to read firewall configuration for windows xp [modified] PinmemberAmol_B2:14 31 Mar '11  
GeneralDetect all Firewalls PinmemberKarunish2:21 14 Jun '08  
GeneralRe: Detect all Firewalls Pinmemberreddragon3:18 15 Jun '08  
GeneralRe: Detect all Firewalls PinmemberKarunish0:08 16 Jun '08  
GeneralRe: Detect all Firewalls Pinmemberreddragon4:46 16 Jun '08  
GeneralRe: Detect all Firewalls Pinmemberkarunisharora858:55 17 Jun '08  
GeneralRe: Detect all Firewalls [modified] Pinmemberreddragon9:23 17 Jun '08  
hmm...
try to find keys in the windows registry
look for links to local files
and check if they exists.
 
Edit:
another idea is to use the Windows Security Center to find an installed Firewall!
 
thanks!
 
reddragon
 
modified on Saturday, June 21, 2008 5:36 PM

Questionis it possible [modified] Pinmemberreddragon5:50 8 Jun '08  
GeneralDefinitely useful Pinmemberpaoloden9:13 5 Jun '07  
GeneralRe: Definitely useful PinmemberArthur Kahwa19:52 13 Jul '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120604.1 | Last Updated 30 May 2007
Article Copyright 2007 by George Mamaladze
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid