Freigeben über


.NET’s Imaginary quest to rule the world: Whitelisting ala managed style.

At some point, for some reason, maybe because of Episode III (or maybe not), you’re going to want to white list an application within the Windows XP SP2 Windows Firewall from managed code.

You can do this easily enough from VBScript by using this handy VBS snippet. If you want to do this from managed code you can use this blog entry as a resource. All you need is the code below and a reference to c:\windows\hnetcfg.dll.

 

using NetFwTypeLib;

[STAThread]

static void Main()

{

Type fw = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);

object objFwMgr = Activator.CreateInstance(fw);

             

Type app = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication", false);

object objApp = Activator.CreateInstance(app);

((INetFwAuthorizedApplication)objApp).Name = ".NET Test WhiteListing";

((INetFwAuthorizedApplication)objApp).Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;

((INetFwAuthorizedApplication)objApp).IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY;

((INetFwAuthorizedApplication)objApp).Enabled = true;

((INetFwAuthorizedApplication)objApp).ProcessImageFileName = @"C:\test.exe";

((INetFwMgr)objFwMgr).LocalPolicy.CurrentProfile.AuthorizedApplications.Add((INetFwAuthorizedApplication)objApp);

}

Let me know if you need a C# to VB.NET conversion.

Comments