如何检查防火墙规则是否存在

时间:2016-05-14 11:26:54

标签: c# windows-firewall

我有一个单击按钮,它会向防火墙添加新规则。但问题是可以多次点击并添加许多规则。

如何检查防火墙中是否存在该规则? (或者可以检查规则吗?)

这是我添加规则的代码:

ProcessStartInfo run = new ProcessStartInfo();
run.FileName = "cmd.exe";
run.Verb = "runas";
run.Arguments = "/C netsh advfirewall firewall add rule name=\"Block IP Rule\" dir=in interface=any action=block remoteip=x.x.x.x";
run.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(run);

3 个答案:

答案 0 :(得分:3)

    public static void RemoveFirewallRules(string RuleName = "BreakermindCom")
{
    try
    {
        Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
        INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
        var currentProfiles = fwPolicy2.CurrentProfileTypes;               

        // Lista rules
        List<INetFwRule> RuleList = new List<INetFwRule>();

        foreach (INetFwRule rule in fwPolicy2.Rules)
        {
            // Add rule to list
            //RuleList.Add(rule);
            // Console.WriteLine(rule.Name);
            if (rule.Name.IndexOf(RuleName) != -1)
            {
                // Now add the rule
                INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));                     
                firewallPolicy.Rules.Remove(rule.Name);
                Console.WriteLine(rule.Name + " has been deleted from Firewall Policy");
            }
        }
    }
    catch (Exception r)
    {
        Console.WriteLine("Error delete rule from firewall");
    }}

作品......:}

答案 1 :(得分:1)

您可以在将防火墙规则初始化为选择性/特定时使用 linq。

对于多个同名的防火墙规则:

    INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); 
    List<INetFwRule> firewallRules = firewallPolicy.Rules.OfType<INetFwRule>().Where(x => x.Name.Contains(fwRuleName)).ToList();

    foreach (INetFwRule rule in firewallRules)
    {
        firewallPolicy.Rules.Remove(rule.Name);
    }

对于单个防火墙规则:

    INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
    INetFwRule firewallRule = firewallPolicy.Rules.OfType<INetFwRule>().Where(x => x.Name == fwRuleName).FirstOrDefault();
    firewallPolicy.Rules.Remove(firewallRule.Name);

但是,如果您已经知道防火墙规则名称,您也可以这样做(未经测试,但没有 Where 子句):

    INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
    firewallPolicy.Rules.Remove(fwRuleName);

重点是展示 linq 语法以及能够使用它搜索特定名称、模式等的灵活性。

答案 2 :(得分:0)

WindowsFirewallHelper 类。作为 VS 的 NuGet 包提供。 我寻找了几天的解决方案并找到了这个。一个非常重要的项目改变了我的生活。

这是执行您想要的操作的代码:

  private void initFWrule(object sender, EventArgs e)
        {
            Console.WriteLine("CHECKING FIREWALL RULE EXISTENCE");
            var myRule = FirewallManager.Instance.Rules.SingleOrDefault(r => r.Name == "BlockUTG_Port-26881");
             //substitute your rule name in place of BlockUTG_Port-26881 above
            try
            {
                if (myRule != null)
                {
                    Console.WriteLine("Rules DOES Exist");
                }

                else
                {
                    Console.WriteLine("Rules DOES NOT Exist");
                    //run your code here to create rule
 
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }