检查机器上的第三方防火墙

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 Security Center Settings 和此帖子:C# - How to chceck if external firewall is enabled? 但两者都没有提供太多帮助。
我一直在研究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);

但运行此代码会导致以下错误:异常:无效的命名空间,并且指向第39行(scope.Connect())。我不会感到惊讶,如果我只是错过了一个参数或格式化了一些东西,我只是不知道是什么。 更新3:从SecurityCenter2切换到SecurityCenter仍然会出现相同的无效命名空间错误。 更新4:我将控制台应用程序移动到另一台计算机上(win7而不是winserver08r2),并且它像预期的那样正确地报告回来。因此,可能存在问题,我目前一直在测试的VM。下一步是解析活动/非活动状态 更新5:在另一台Server08框上进行了测试,并且出现了相同的无效命名空间错误。使用SecurityCenter而不是SecurityCenter2无法解决此问题。Windows Server OS使用某些基础安全功能以防止篡改防火墙,还是Server OS没有特定的WMI功能密钥集?

1
您可能想要探索此页面 http://www.codeproject.com/Articles/37714/Software-Development-Build-your-own-Windows-Securi底部的三个参考链接也指向了一些有用的页面。 - Gary
1
SecurityCenter2 命名空间适用于 Windows Vista、7 和 8,对于 XP,您必须使用 SecurityCenter 命名空间。 - RRUZ
@wjhguitarman - 为什么需要检测防火墙是否已安装? - Security Hound
@Ramhound 这是健康分析仪的一部分,用于检查客户端的设置和统计信息。我们的软件需要至少打开一个异常,以便报告活动的防火墙类型可能会有所帮助。 - wjhguitarman
1个回答

11
根据微软的说法:Windows安全中心如何检测第三方产品及其状态? Windows安全中心采用两级检测状态的方法。一级是手动,另一级是通过Windows管理工具(WMI)自动进行。在手动检测模式下,Windows安全中心搜索独立软件制造商提供给微软的注册表键和文件。这些注册表键和文件让Windows安全中心检测独立软件的状态。在WMI模式下,软件制造商确定他们自己的产品状态,并通过WMI提供程序将该状态报告给Windows安全中心。在两种模式下,Windows安全中心都试图确定以下是否正确:1.防病毒程序是否存在;2.防病毒签名是否更新;3.防病毒程序是否开启实时扫描或即时扫描;4.对于防火墙,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();
        }
    }
}

哇,你肯定很懂行啊!我正在测试你的例子,但是为了不仅仅复制粘贴,我有一个问题,Console.WriteLine中的"{0,-35} {1,-40}"是什么意思?它的作用是什么? - wjhguitarman
1
@wjhguitarman,那些只是格式选项,以便它显示得漂亮一些。 - Daniel A. White
我在你发布的博客链接上再次阅读了更多内容,我应该早些这样做,这段时间我一直试图让它在Server 2008上运行,不知道它只适用于桌面版 :( - wjhguitarman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接