【问题标题】:VB.NET code adding Windows firewall exception just under private categorie, not publicVB.NET 代码在私有类别下添加 Windows 防火墙异常,而不是公共
【发布时间】:2012-11-08 00:20:21
【问题描述】:

我有一个可以添加防火墙异常的 VB.NET 例程,问题是我必须在所有类型的网络下添加一个异常,无论是私有的还是公共的。但是这个例程在 Windows 防火墙的私有类别下添加了一个例外。

我的代码:

Private Sub AddApp()
        Dim appType As Type = Type.GetTypeFromProgID("HnetCfg.FwAuthorizedApplication")
        Dim app As INetFwAuthorizedApplication
        app = DirectCast(Activator.CreateInstance(appType), INetFwAuthorizedApplication)

        ' Set the application properties
        app.Name = "My App"
        app.ProcessImageFileName = "C:\Users\klein\AppData\Roaming\Microsoft\Windows\MyApp.exe"
        app.Enabled = True

        ' Get the firewall manager, so we can get the list of authorized apps
        Dim fwMgrType As Type = Type.GetTypeFromProgID("HnetCfg.FwMgr")
        Dim fwMgr As INetFwMgr
        fwMgr = DirectCast(Activator.CreateInstance(fwMgrType), INetFwMgr)

        ' Get the list of authorized applications from the Firewall Manager, so we can add our app to that list
        Dim apps As INetFwAuthorizedApplications
        apps = fwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications
        apps.Add(app)
    End Sub

【问题讨论】:

    标签: vb.net windows-firewall-api


    【解决方案1】:

    您是否尝试过修改规则的范围?

    类似的东西;

    app.Scope = 0; 
    

    应该将范围定义为 ALL

    【讨论】:

      【解决方案2】:

      不要将应用程序添加到CurrentProfile,而是尝试使用GetProfileByType

          apps = fwMgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_CURRENT).AuthorizedApplications   ' PUBLIC
          apps = fwMgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_DOMAIN).AuthorizedApplications    ' DOMAIN
          apps = fwMgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_STANDARD).AuthorizedApplications  ' PRIVATE
      

      我使用下面的代码,它工作正常。

      Imports NetFwTypeLib
      Module modMain
      
          Sub Main()
      
              AddApp(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_CURRENT)  'public
              AddApp(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_STANDARD) 'private
      
          End Sub
      
          Private Sub AddApp(ProfileType As NET_FW_PROFILE_TYPE_)
      
              Dim app As INetFwAuthorizedApplication = DirectCast(Activator.CreateInstance(Type.GetTypeFromProgID("HnetCfg.FwAuthorizedApplication")), INetFwAuthorizedApplication)
              app.Name = Application.ProductName
              app.ProcessImageFileName = Application.ExecutablePath
              app.Enabled = True
              Dim fwMgr As INetFwMgr = DirectCast(Activator.CreateInstance(Type.GetTypeFromProgID("HnetCfg.FwMgr")), INetFwMgr)
              fwMgr.LocalPolicy.GetProfileByType(ProfileType).AuthorizedApplications.Add(app)
      
          End Sub 
      End Module
      

      【讨论】:

        【解决方案3】:

        使用 INetFwPolicy2 接口。代码是 c#,但应该不难移植。

        public class Firewall
        {
            public enum ProtocolType
            {
                Tcp = 6,
                Udp = 17, 
                Any = 256
            }
        
            public static bool CheckAddPortRule(String FwRuleTitle, string Ports, ProtocolType Protcol, NET_FW_PROFILE_TYPE2_ Profile2Types)
            {
                try
                {
                    Type Tpolicy2Class = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
                    INetFwPolicy2 policy2Class = (INetFwPolicy2)Activator.CreateInstance(Tpolicy2Class);
                    foreach (INetFwRule itm in policy2Class.Rules)
                    {
                        try
                        {
                            if (itm.Name.ToLower() == FwRuleTitle.ToLower())
                            {
                                itm.Profiles = (int)Profile2Types;
                                itm.Protocol = (int)Protcol;
                                itm.LocalPorts = Ports;
                                return true;
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                    INetFwRule fwRule = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
                    fwRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
                    fwRule.Name = FwRuleTitle;
                    fwRule.Profiles = (int)Profile2Types;
                    fwRule.Protocol = (int)Protcol;
                    fwRule.LocalPorts = Ports;
                    fwRule.Enabled = true;
                    fwRule.InterfaceTypes = "All"; //Acceptable values for this property are "RemoteAccess", "Wireless", "Lan", and "All". 
                    policy2Class.Rules.Add(fwRule);
                    return true;
                }
                catch (Exception ex)
                {
                }
                return false;
            }
        }
        

        你可以这样称呼它。

            NET_FW_PROFILE_TYPE2_ Profile2Types = NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_DOMAIN | NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC;
        Firewall.CheckAddPortRule("Rule title", "1234", Firewall.ProtocolType.Tcp, Profile2Types);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-06-09
          • 2017-10-04
          • 2013-03-02
          • 2016-07-30
          • 1970-01-01
          • 2021-04-26
          • 1970-01-01
          相关资源
          最近更新 更多