5,448,416 members and growing! (18,275 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Open Windows Firewall During Installation

By Don Hamson

This test code will serve as an MSI custom action to open the Windows firewall after installation.
Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 23 Jul 2006
Updated: 23 Jul 2006
Views: 19,460
Bookmarked: 15 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.80 Rating: 3.60 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
2 votes, 33.3%
3
4 votes, 66.7%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

During the installation of my application, I need to add it to the Windows firewall as an allowed application and open 2 ports for another application.  So this code will function as a custom action during the install to open the firewall on install and close it on uninstall.  In trying to keep things as simple as possible; the following C# class library will be called from setup - openFirewall() and closeFirewall().

First, I generated FWSetupAction project as a C# class library.  After that use the properties page to switch the output type to a console application to step through it with the debugger.  When it's operational, switch back to class library for integration with MSI setup logic and incorporate it as a custom action.

After the initial project creation, rename Class1.cs to Firewall.cs in the Solution navigator.  If you're writing code anew, add the NetFwTypeLib reference first to allow intellisense to help you recognize the terms you'll be coding.  This reference will be required for correct compilation, so whether you put it in before coding or after doesn't matter but it will be needed.  To add the reference, right click on References and select browse.  Browse to %windir%\system32\hnetcfg.dll and select it - the NetFwTypeLib will be created. 

Edit the Firewall.cs class to have the following code

using System;

using System.Collections.Generic;

using System.Text;

using System.Reflection;

using NetFwTypeLib;

using Microsoft.Win32;

namespace FWSetupAction

{

public class Firewall

{

protected int[] discoPorts = { 0xD100, 0xD101 };

protected INetFwProfile fwProfile;

public void openFirewall()

{

///////////// Firewall Authorize Application ////////////

String imageFilename = getImageFilename();

setProfile();

INetFwAuthorizedApplications apps = fwProfile.AuthorizedApplications;

INetFwAuthorizedApplication app = ( INetFwAuthorizedApplication ) getInstance( "INetAuthApp" );

app.Name = "Application Name";

app.ProcessImageFileName = imageFilename;

apps.Add( app );

apps = null;

//////////////// Open Needed Ports /////////////////

INetFwOpenPorts openports = fwProfile.GloballyOpenPorts;

foreach( int port in discoPorts )

{

INetFwOpenPort openport = ( INetFwOpenPort ) getInstance( "INetOpenPort" );

openport.Port = port;

openport.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP;

openport.Name = "New Open Port";

openports.Add( openport );

}

openports = null;

} // openFirewall

public void closeFirewall()

{

String imageFilename = getImageFilename();

setProfile();

INetFwAuthorizedApplications apps = fwProfile.AuthorizedApplications;

apps.Remove( imageFilename );

apps = null;

INetFwOpenPorts ports = fwProfile.GloballyOpenPorts;

ports.Remove( discoPorts[ 0 ], NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP );

ports.Remove( discoPorts[ 1 ], NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP );

ports = null;

}

protected string getImageFilename()

{

// Get install directory from the registry

RegistryKey pRegKey = Registry.LocalMachine;

pRegKey = pRegKey.OpenSubKey( "SOFTWARE\\Company Directory\\AppDir" );

Object insDir = pRegKey.GetValue( "InstallDir" );

return insDir + "RVP.exe";

}

protected void setProfile()

{

// Access INetFwMgr

INetFwMgr fwMgr = ( INetFwMgr ) getInstance( "INetFwMgr" );

INetFwPolicy fwPolicy = fwMgr.LocalPolicy;

fwProfile = fwPolicy.CurrentProfile;

fwMgr = null;

fwPolicy = null;

}

protected Object getInstance( String typeName )

{

if( typeName == "INetFwMgr" )

{

Type type = Type.GetTypeFromCLSID(

new Guid( "{304CE942-6E39-40D8-943A-B913C40C9CD4}" ) );

return Activator.CreateInstance( type );

}

else if( typeName == "INetAuthApp" )

{

Type type = Type.GetTypeFromCLSID(

new Guid( "{EC9846B3-2762-4A6B-A214-6ACB603462D2}" ) );

return Activator.CreateInstance( type );

}

else if( typeName == "INetOpenPort" )

{

Type type = Type.GetTypeFromCLSID(

new Guid( "{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}" ) );

return Activator.CreateInstance( type );

}

else return null;

}

static void Main( string[] args )

{

Firewall fw = new Firewall();

fw.openFirewall();

fw.closeFirewall();

}

}

}

Once compiled, you're ready to test.  Set a break point on each of the firewall entry methods - openFirewall() and closeFirewall() and step through the program.  Use a DOS box to verify the operations.  The netsh firewall command will verify the operation of the code:

netsh fire show allowed - shows the programs that are allowed

netsh fire show port - shows the ports that are open

Thanks to Moah, Windows XP SP2 Firewall Controller, http://www.codeproject.com/w2k/WinXPSP2Firewall.asp

Thanks too to Dan Agonistes, Windows XP Service Pack 2 and the Windows Firewall, http://danagonistes.blogspot.com/2004/06/windows-xp-service-pack-2-and-windows.html 

 


 

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

Don Hamson



Occupation: Web Developer
Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralDoes it work on vista?memberDavid Engler11:10 29 May '08  
GeneralHelp anybody - very peculiar bugmemberDave Midgley9:13 28 Nov '07  
GeneralRe: Help anybody - very peculiar bug [modified]memberDon Hamson0:31 29 Nov '07  
GeneralSetting things back to nullmemberDave Midgley8:16 28 Nov '07  
GeneralRe: Setting things back to nullmemberDon Hamson23:33 28 Nov '07  
GeneralError checkingmemberDave Midgley6:40 26 Nov '07  
AnswerRe: Error checkingmemberDon Hamson17:51 26 Nov '07  
GeneralRe: Error checkingmemberDave Midgley8:14 28 Nov '07  
GeneralRe: Error checkingmemberDon Hamson23:19 28 Nov '07  
Questionnecessary privsmemberAleRanza6:11 25 May '07  
AnswerRe: necessary privsmemberDon Hamson14:48 25 May '07  
GeneralA different way of doing thingsmemberYiogi6:04 11 Jan '07  
GeneralRe: A different way of doing thingsmembershysan3:29 24 Apr '07  
Generalsecurity caveatmemberkckn4fun3:03 26 Jul '06  
GeneralRe: security caveatmemberDon Hamson9:34 26 Jul '06  
GeneralRe: security caveatmembertverweij22:17 26 Jul '06  
GeneralRe: security caveat [modified]memberDon Hamson4:36 27 Jul '06  
GeneralRe: security caveatmemberJason Barry14:20 26 Jun '08  
GeneralThis is not a feature, but a security leak [modified]membertverweij8:04 25 Jul '06  
GeneralRe: This is not a feature, but a security leakmemberDon Hamson2:36 26 Jul '06  
GeneralRe: This is not a feature, but a security leakmembertverweij22:20 26 Jul '06  
GeneralRe: This is not a feature, but a security leakmemberDon Hamson4:23 27 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Jul 2006
Editor:
Copyright 2006 by Don Hamson
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project