13

我正在检查防火墙。以下代码很容易检查默认 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;
   }

那么问题就变成了:如何找到非 Windows 防火墙的状态?如果防火墙已正确集成,上述检查是否会正常工作,还是有更好的方法来做到这一点?我已经检查了这篇文章:C# Windows 安全中心设置和这篇文章:C# - 如何检查是否启用了外部防火墙?但事实证明两者都相对无济于事。

我一直在研究 WMI API,但到目前为止它非常令人困惑,而且通过 MSDN 提供的文档也不太乐观。我也尝试过使用SelectQuery,但到目前为止我一直没有成功。任何人都可以帮助我在一个新的起点或我可以找到关于第 3 方防火墙的更好的文档/说明的地方吗?

编辑:目前我正在进一步探索 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);

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

更新 3SecurityCenter2to切换SecurityCenter仍然会产生相同的invalid namespace错误。

更新 4 我将控制台应用程序移到另一个盒子(win7 不是 winserver08r2),它按预期正确报告。所以这可能是我目前正在测试的虚拟机的问题。下一步是解析活动/非活动状态

更新 5 它在另一个 Server08 机器上进行了测试,并且invalid namespace出现了相同的错误。使用SecurityCenter代替SecurityCenter2不能解决问题。是否有一些 Windows Server OS 用于防止篡改防火墙的底层安全功能,或者 Server OS 是否不附带特定的 WMI 功能密钥集?

4

1 回答 1

11

根据微软问: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();
        }
    }
}
于 2012-11-29T05:03:21.150 回答