【问题标题】:How can I detect if a firewall product is enabled?如何检测是否启用了防火墙产品?
【发布时间】:2012-11-27 12:46:01
【问题描述】:

如果启用了防火墙产品,我如何(从用 C# 编写的 Windows 窗体应用程序)检测?

这是我的代码,我在 INetFwMgr 上遇到错误,找不到类型或命名空间

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private const string CLSID_FIREWALL_MANAGER = "{304CE942-6E39-40D8-943A-B913C40C9CD4}"; 

       INetFwMgr manager = GetFireWallManager();
       bool isFirewallEnabled = manager.LocalPolicy.CurrentProfile.FirewallEnabled;



       private static INetFwMgr GetFireWallManager()
       {
           Type objectType = Type.GetTypeFromCLSID(new Guid(CLSID_FIREWALL_MANAGER));
           return Activator.CreateInstance(objectType) as INetFwMgr;
       }
        private void button1_Click(object sender, EventArgs e)
        {



            if (isFirewallEnabled == false)
           {
                MessageBox.Show("Firewall is not enabled.");
           }
           else
           {
                MessageBox.Show("Firewall is enabled.");
           }

        }
    }
}

【问题讨论】:

标签: c# windows-applications windows-firewall windows-firewall-api


【解决方案1】:

在此处查看有关防病毒How to detect antivirus installed on windows 2003 server and 2008 server 2003 server R2and 2008 server R2 using WMI or other then WMI in C++ 的这个问题,使用WSC_SECURITY_PROVIDER_FIREWALL 枚举可以使用相同的 API 调用来检测防火墙设置。该问题的答案实际上是错误的,但它会为您提供非服务器计算机的答案。该代码是用 C++ 编写的,但它只是您需要的 windows API 调用,您也可以从 C# 调用它。

【讨论】:

  • 请注意,这只会检测本地运行的防火墙应用程序。它不会(也不能)检测防火墙设备等。
  • 不,它不会,但考虑到这个问题的标签(windows-firewall 等)我认为 OP 意味着本地防火墙
  • 可能,我只是想完整。
  • 我只想检查本地机器,当我点击 ny 窗口窗体中的按钮时,它会检查防火墙状态的代码,所以你能帮助如何编写代码吗?
  • @PavanAnadkat 您能否澄清一下,您是否要检查互联网上的计算机是否可以连接到计算机上的端口,或者只是本地计算机是否运行防火墙?这是两个非常不同的问题,有不同的解决方案。
【解决方案2】:
NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); 
INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
bool Firewallenabled = mgr.LocalPolicy.CurrentProfile.FirewallEnabled;

详情见链接。

http://technet.microsoft.com/en-us/library/cc737845%28WS.10%29.aspx

http://blogs.msdn.com/b/securitytools/archive/2009/08/21/automating-windows-firewall-settings-with-c.aspx

【讨论】:

  • 请参阅下面 Jeetendra negi 的回答,了解如何编译此代码。
【解决方案3】:

对于旧 Windows 版本 (XP),您可以使用 FwMgr,对于 Vista 及更高版本,您可以使用 Windows Firewall with Advanced Security API

这是检索防火墙设置的example

【讨论】:

    【解决方案4】:

    您首先需要将以下组件添加到您的项目中

    • INetFwMgr

    然后,从家庭网络配置管理器 CLSID 获取对象类型 {304CE942-6E39-40D8-943A-B913C40C9CD4}(链接到 C:\WINDOWS\system32\hnetcfg.dll,可以在 HKEY_CLASSES_ROOT\CLSID\{304CE942-6E39-40D8-943A-B913C40C9CD4} 找到)并使用收集到的类型使用该类型的默认构造函数创建实例作为一个新的INetFwMgr,它将用于检测防火墙是否启用,使用INetFwMgr.LocalPolicy.CurrentProfile.FirewallEnabled返回bool

    private const string CLSID_FIREWALL_MANAGER = "{304CE942-6E39-40D8-943A-B913C40C9CD4}"; //This is the CLSID of Home Networking Configuration Manager. We'll use this to detect whether the Firewall is enabled or not
    private static NetFwTypeLib.INetFwMgr GetHNCMType()
    {
        Type objectType = Type.GetTypeFromCLSID(new Guid(CLSID_FIREWALL_MANAGER)); //Creates a new GUID from CLSID_FIREWALL_MANAGER getting its type as objectType
        return Activator.CreateInstance(objectType) as NetFwTypeLib.INetFwMgr; //Creates an instance from the object type we gathered as an INetFwMgr object
    }
    static void Main(string[] args)
    {
        INetFwMgr manager = GetHNCMType(); //Initializes a new INetFwMgr of name manager from GetHNCMType
        if (manager.LocalPolicy.CurrentProfile.FirewallEnabled == false) //Continue if the firewall is not enabled
        {
            //The firewall is not enabled
            Console.WriteLine("OFF"); //Writes OFF to the Console in a new line
        }
        else //Otherwise:
        {
            //The fire wall is enabled
            Console.WriteLine("ON"); //Writes ON to the Console in a new line
        }
    }
    

    谢谢,
    希望对您有所帮助:)


    要将组件添加到您的项目中,

    • 右键单击 Solution Explorer 下的 References 项目名称并选择添加参考...
    • 在标签 COM 下,选择您要添加的组件,然后点击 OK

    【讨论】:

    • 我没有在 COM 下找到 INetFwMgr。现在我该如何添加这个组件?
    • 错误 1 ​​找不到类型或命名空间名称“INetFwMgr”(您是否缺少 using 指令或程序集引用?) c:\users\administrator.mustuspune\documents\visual studio 2010\ Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 我收到此错误
    【解决方案5】:

    我知道这是一篇旧帖子,但我找到了一个很好的解决方案!
    读取防火墙状态的注册表项:

    HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile

    键:EnableFirewall

    public static bool isFirewallEnabled() {
            try {
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile")) {
                    if (key == null) {
                        return false;
                    } else { 
                        Object o = key.GetValue("EnableFirewall");
                        if (o == null) {
                            return false;
                        } else {
                            int firewall = (int)o;
                            if (firewall == 1) {
                                return true;
                            } else {
                                return false;
                            }
                        }
                    }
                }
            } catch {
                return false;
            }
        }
    

    您还可以获得 DomainProfile、PublicProfile 和 StandardProfile 的值。您也可以获得 FirewallRules。

    我希望这会有所帮助:)

    【讨论】:

      【解决方案6】:

      只需从 C://windows/system32/hnetcfg.dll 和 C://windows/system32/FirewallAPI.dll 导入引用

      然后使用

      using NATUPNPLib;
      using NETCONLib;
      using NetFwTypeLib;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-20
        • 2012-07-06
        • 2011-01-04
        • 2016-09-10
        • 1970-01-01
        • 1970-01-01
        • 2014-10-05
        • 2015-06-24
        相关资源
        最近更新 更多