检查计算机上的第三方防火墙

wjh*_*man 13 c# security wmi firewall windows-server-2008-r2

我正在检查防火墙.以下代码非常容易检查默认Windows防火墙的状态:

    INetFwMgr manager = GetFireWallManager();
    bool isFirewallEnabled = manager.LocalPolicy.CurrentProfile.FirewallEnabled;
    if (isFirewallEnabled == false)
    {
      Console.WriteLine("Firewall is not enabled.");
    }
    else
    {
      Consoe.WriteLine("Firewall is enabled.");
    }
    Console.ReadLine();

   private static INetFwMgr GetFireWallManager()
   {
     Type objectType = Type.GetTypeFromCLSID(new Guid(firewallGuid));
     return Activator.CreateInstance(objectType) as INetFwMgr;
   }
Run Code Online (Sandbox Code Playgroud)

然后问题变成:如何查找非Windows防火墙的状态?如果防火墙已正确集成,上面的检查工作是否相同,或者有更好的方法吗?我查过这篇文章:C#Windows安全中心设置和这篇文章:C# - 如果启用了外部防火墙,如何chceck?但两者都证明相对无益.

我一直在研究WMI API,但到目前为止它非常令人困惑,而且通过MSDN的文档并不是很有希望.我也尝试过使用SelectQuery,但到目前为止我还没有成功.任何人都可以帮助我在新的起点或我可以找到更好的有关第三方防火墙的文档/说明吗?

编辑:目前我正在进一步探索WMI,特别FirewallProduct是帖子建议的类.

更新2:我一直在测试以下代码段:

  string wmiNameSpace = "SecurityCenter2";
  ManagementScope scope;
  scope = new ManagementScope(String.Format("\\\\{0}\\root\\{1}", "localhost", wmiNameSpace), null);
  scope.Connect();
  ObjectQuery query = new ObjectQuery("SELECT * FROM FirewallProduct");
  ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
Run Code Online (Sandbox Code Playgroud)

但是运行它会导致以下错误: Exception Invalid namespace 它指向第39行(scope.Connect()).如果我只是错过了一个参数或者格式化了一些不正确的东西,我就不会感到惊讶,我只是不知道它是什么.

更新3 切换SecurityCenter2SecurityCenter静止会产生相同的invalid namespace错误.

更新4 我将控制台应用程序移动到另一个框(win7而不是winserver08r2)并按预期正确报告.因此,我目前一直在测试的VM可能是一个问题.下一步是解析活动/非活动状态

更新5 它在另一个Server08盒子上测试,invalid namespace出现相同的错误.使用SecurityCenterSecurityCenter2不是解决问题.是否有一些基础安全功能Windows Server OS用于防止篡改防火墙,或者服务器操作系统没有附带特定的WMI功能密钥集?

RRU*_*RUZ 10

根据Microsoft 问:Windows安全中心如何检测第三方产品及其状态?

答:Windows安全中心使用双层方法进行检测状态.一层是手动的,另一层是通过Windows Management Instrumentation(WMI)自动完成的.在手动检测模式下,Windows安全中心会搜索由独立软件制造商提供给Microsoft的注册表项和文件.这些注册表项和文件使Windows安全中心可以检测独立软件的状态.在WMI模式下,软件制造商确定自己的产品状态,并通过WMI提供程序将该状态报告回Windows安全中心.在这两种模式下,Windows安全中心都会尝试确定以下内容是否为真:

  • 存在防病毒程序.
  • 防病毒签名是最新的.
  • 为防病毒程序启用实时扫描或按访问扫描.
  • 对于防火墙,Windows安全中心会检测是否安装了第三方防火墙以及防火墙是否已打开.

因此,您可以使用WMI来确定是否安装了第三方防火墙,使用FirewallProduct该类,前一段时间我写了一篇关于此主题的文章,该文章解释了如何使用WMI获取此信息.

尝试使用此示例C#以获取当前安装的第三方防火墙名称和状态.

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                //select the proper wmi namespace depending of the windows version
                string WMINameSpace = System.Environment.OSVersion.Version.Major > 5 ? "SecurityCenter2" : "SecurityCenter";

                ManagementScope Scope;
                Scope = new ManagementScope(String.Format("\\\\{0}\\root\\{1}", "localhost", WMINameSpace), null);

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT * FROM FirewallProduct");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

                foreach (ManagementObject WmiObject in Searcher.Get())
                {

                    Console.WriteLine("{0,-35} {1,-40}","Firewall Name",WmiObject["displayName"]);                      
                    if (System.Environment.OSVersion.Version.Major < 6) //is XP ?
                    {
                    Console.WriteLine("{0,-35} {1,-40}","Enabled",WmiObject["enabled"]);    
                    }
                    else
                    {
                        Console.WriteLine("{0,-35} {1,-40}","State",WmiObject["productState"]); 
                    }   
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)