C# 检查计算机上的第三方防火墙
我正在检查防火墙。以下代码很容易检查默认Windows防火墙的状态:C# 检查计算机上的第三方防火墙,c#,security,wmi,firewall,windows-server-2008-r2,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 enabl
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防火墙的状态?
如果防火墙正确集成,上述检查是否也能正常工作,或者是否有更好的方法?
我检查了这篇文章:和这篇文章:但两者都证明相对没有帮助
我一直在研究WMI API,但到目前为止它非常混乱,而且通过MSDN提供的文档也不太有希望。
我也试过和他鬼混,但到目前为止我都没有成功。
是否有人可以帮助我找到一个新的起点,或者帮助我找到关于第三方防火墙的更好的文档/说明
编辑:目前,我正在进一步探索WMI,特别是一篇帖子建议的类防火墙产品
更新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()
)。如果我只是错过了一个参数或者格式化不正确,我一点也不会感到惊讶,我只是不知道它是什么
UPDATE 3从SecurityCenter 2
切换到SecurityCenter
仍然会产生相同的无效命名空间
错误
更新4我将控制台应用程序移到另一个框中(win7而不是winserver08r2),并按预期正确报告。所以这可能是我目前正在测试的VM的一个问题。下一步是解析出活动/非活动状态
更新5它已在另一个Server08框上测试,出现相同的无效命名空间
错误。使用SecurityCenter
而不是SecurityCenter 2
无法解决问题。Windows Server OS是否有一些底层安全功能可用于防止篡改防火墙,或者Server OS是否没有特定的WMI功能密钥集
根据微软的说法问:Windows是如何运行的
安全中心检测第三方产品及其状态?
A:
Windows安全中心使用两层方法进行检测
地位一层是手动的,另一层是自动的
Windows管理检测(WMI)。在手动检测模式下,
Windows安全中心搜索注册表项和不可用的文件
由独立软件制造商提供给微软。这些
注册表项和文件允许Windows安全中心检测状态
独立软件的开发。在WMI模式下,软件制造商决定
他们自己的产品状态,并向Windows报告该状态
通过WMI提供程序访问安全中心。在这两种模式下,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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用制度管理;
使用系统文本;
命名空间GetWMI\u信息
{
班级计划
{
静态void Main(字符串[]参数)
{
尝试
{
//根据windows版本选择适当的wmi命名空间
字符串WMINameSpace=System.Environment.OSVersion.Version.Major>5?“SecurityCenter 2”:“SecurityCenter”;
管理范围;
Scope=newmanagementscope(String.Format(“\\\{0}\\root\{1}”,“localhost”,WMINameSpace),null);
Scope.Connect();
ObjectQuery=新建ObjectQuery(“从FirewallProduct中选择*);
ManagementObjectSearcher search=新的ManagementObjectSearcher(范围、查询);
foreach(Searcher.Get()中的ManagementObject WMIOObject)
{
WriteLine(“{0,-35}{1,-40}”,“防火墙名称”,wmioobject[“displayName]”);
if(System.Environment.OSVersion.Version.Major<6)//是XP吗?
{
WriteLine(“{0,-35}{1,-40}”,“Enabled”,wmioobject[“Enabled”]);
}
其他的
{
WriteLine(“{0,-35}{1,-40}”,“State”,wmioobject[“productState]”);
}
}
}
捕获(例外e)
{
WriteLine(String.Format(“异常{0}跟踪{1}”,e.Message,e.StackTrace));
}
控制台写入线(“按回车键退出”);
Console.Read();
}
}
}
您可能希望浏览此页面底部的三个参考链接也指向一些有用的页面。SecurityCenter 2
命名空间适用于Windows Vista、7和8,对于XP,您必须使用SecurityCenter
命名空间。@wjjguitarman-为什么需要检测是否安装了防火墙?@Ramhound它是健康分析器的一部分,用于检查客户端框上的设置和统计信息。我们的软件需要至少打开一个例外,所以报告哪种类型的防火墙处于活动状态会很有帮助哇,你肯定知道你的东西!我是