【问题标题】:Adding an application firewall rule to both private and public networks via win7 FirewallAPI通过 win7 FirewallAPI 向私有网络和公共网络添加应用程序防火墙规则
【发布时间】:2013-03-02 19:58:57
【问题描述】:

一点背景知识:基本上我想为私有网络和公共网络添加一个程序防火墙访问规则。

我以前用这个- "netsh firewall add allowedprogram program= "Path.." name=AppName ENABLE scope=ALL profile=CURRENT"

但现在我想使用 COM 对象稍微自动化这个过程。 找到了这段闪亮的代码 - http://web.archive.org/web/20070707110141/http://www.dot.net.nz/Default.aspx?tabid=42&mid=404&ctl=Details&ItemID=8

在实现了我一直在尝试使用的类之后—— FirewallHelper.Instance.GrantAuthorization(@"Path...","AppName",NET_FW_SCOPE_.NET_FW_SCOPE_ALL,NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY);

我面临的问题是,GrantAuthorization 方法只会为公共或专用网络添加一条规则,而我的旧 netsh 命令将为每个网络添加 2 条规则。

这些命令实际上看起来非常相似,所以对我来说有点困惑。

那么...如何添加两个网络规则?

肖恩

【问题讨论】:

    标签: c# firewall windows-firewall windows-firewall-api firewall-access


    【解决方案1】:

    我的回答来自大卫的回答,但更详细。并修复有关设置 Localports 的问题。您需要在设置 Localports 之前设置协议。详情如下:

    首先,需要导入参考FirewallAPI.dll。它在“C:\Windows\System32\FirewallAPI.dll”中 那么:

    using NetFwTypeLib;
    

    并将代码插入您的:

            Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
            INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
            var currentProfiles = fwPolicy2.CurrentProfileTypes;
    
            // Let's create a new rule
            INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
            inboundRule.Enabled = true;
            //Allow through firewall
            inboundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
            //Using protocol TCP
            inboundRule.Protocol = 6; // TCP
            //Port 81
            inboundRule.LocalPorts = "81";
            //Name of rule
            inboundRule.Name = "MyRule";
            // ...//
            inboundRule.Profiles = currentProfiles;
    
            // Now add the rule
            INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
            firewallPolicy.Rules.Add(inboundRule);
    

    【讨论】:

      【解决方案2】:

      我认为您最好的选择是与Windows Firewall with Advanced Security API 交谈。

      “C# INetFwRule2”的快速 google 将向您展示如何注册或更新防火墙规则的大量示例。

      为了增加公共和私人政策,我使用了类似

      的东西
      Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
      INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
      var currentProfiles = fwPolicy2.CurrentProfileTypes;
      
      // Let's create a new rule
      
      INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
      inboundRule.Enabled = true;
      inboundRule.LocalPorts = "1234";
      inboundRule.Protocol = 6; // TCP
      // ...
      inboundRule.Profiles = currentProfiles;
      
      // Now add the rule
      
      INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
      firewallPolicy.Rules.Add(inboundRule);
      

      【讨论】:

        【解决方案3】:

        这个页面并没有说这个已经被回答并且是旧的,所以以防万一,以备将来使用,我会回答这个。

        首先,导入位于“C:\Windows\System32\FirewallAPI.dll”的引用FirewallAPI.dll,然后添加using指令

        using NetFwTypeLib;
        

        inboundRule.Profiles 属性似乎被归类为具有以下值的一组标志(该属性的类型是 int,所以我做了一个枚举):

        public enum FirewallProfiles
        {
            Domain = 1,
            Private = 2,
            Public = 4
        }
        

        因此,使用该代码,我们可以将配置文件更改为以下内容:

        // Create a new rule
        INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwRule"));
        // Enable the rule
        inboundRule.Enabled = true;
        // Allow through firewall
        inboundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
        // Using protocol TCP
        inboundRule.Protocol = 6; // TCP
        // Set port number
        inboundRule.LocalPorts = "1234";
        // Name of rule
        inboundRule.Name = "Name Of Firewall Rule";
        // Set profiles
        inboundRule.Profiles = (int)(FirewallProfiles.Private | FirewallProfiles.Public);
        
        // Add the rule
        INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
        firewallPolicy.Rules.Add(inboundRule);
        

        或者您可以将 inboundRule.Profiles 更改为 int 值。

        两个音符:

        1:如果您不以管理权限运行此代码,

        firewallPolicty.Rules.Add(inboundRule);
        

        会抛出异常。

        2:inboundRule.Profiles 必须介于 1 和 7 之间,否则会抛出异常

        【讨论】:

          【解决方案4】:

          以防万一你们想要 Outbound 规则:

          inboundRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
          

          【讨论】:

            猜你喜欢
            • 2014-01-14
            • 2022-08-12
            • 1970-01-01
            • 2020-06-09
            • 2014-10-22
            • 1970-01-01
            • 2018-11-08
            • 2021-04-26
            • 1970-01-01
            相关资源
            最近更新 更多