簡體   English   中英

如何檢測防火牆產品是否已啟用?

[英]How can I detect if a firewall product is enabled?

如何啟用防火牆產品,如何檢測(從使用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.");
           }

        }
    }
}
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

在這里看一下這個關於防病毒的問題如何使用WMI或其他隨后的CMI在C ++中檢測安裝在Windows 2003服務器和2008服務器2003服務器R2和2008服務器R2上的防病毒,可以使用相同的API調用來使用WSC_SECURITY_PROVIDER_FIREWALL枚舉來檢測防火牆設置。 這個問題的答案實際上是錯誤的,但它會為您提供非服務器計算機的答案。 該代碼使用的是C ++,但它只是您需要的Windows API調用,您也可以從C#調用它。

您首先需要將以下組件添加到項目中

  • 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
    }
}

謝謝,
我希望這個對你有用 :)


要向項目添加組件,

  • 右鍵單擊項目名稱下解決方案資源管理器中的 引用 ,然后選擇添加引用...
  • 在選項卡COM下 ,選擇您要添加的組件,然后單擊“ 確定”

我知道這是一個老帖子,但我找到了一個很好的解決方案!
在以下位置讀取防火牆狀態的注冊表項:

HKEY_LOCAL_MACHINE \\系統\\ CurrentControlSet \\服務\\ SharedAccess \\參數\\ 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。

我希望這有幫助 :)

只需從C://windows/system32/hnetcfg.dll和C://windows/system32/FirewallAPI.dll導入引用

然后用

using NATUPNPLib;
using NETCONLib;
using NetFwTypeLib;

您可以使用FwMgr用於舊版Windows(XP)並使用Windows防火牆和高級安全API用於Vista及更高版本。

以下是檢索防火牆設置的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM