简体   繁体   中英

How can I detect if a firewall product is enabled?

How can I detect (from a Windows Forms application written in C#) if a firewall product is enabled?

Here is my code and i am getting error on INetFwMgr that type or namespace could not found

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;

For details see a link.

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

Have a look at this question here about antivirus 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++ the same API call can be used to detect firewall settings using the WSC_SECURITY_PROVIDER_FIREWALL enum. The answer there is actually wrong for that question, but it will give you the answer for non-server computers. That code is in C++, but it's just the windows API call you need, you can call that from C# too.

You'll first need to add the following component to your project

  • INetFwMgr

Then, get the object type from the Home Networking Configuration Manager CLSID which is {304CE942-6E39-40D8-943A-B913C40C9CD4} (Links to C:\\WINDOWS\\system32\\hnetcfg.dll and can be found at HKEY_CLASSES_ROOT\\CLSID\\{304CE942-6E39-40D8-943A-B913C40C9CD4} ) and use the type gathered to create an instance using the type's default constructor as a new INetFwMgr which will be used to detect whether the firewall is enabled or not using INetFwMgr.LocalPolicy.CurrentProfile.FirewallEnabled which returns a 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
    }
}

Thanks,
I hope you find this helpful :)


To add a component to your project,

  • Right-click References from the Solution Explorer under your project name and select Add Reference...
  • Under the tab COM , select the component you'd like to add and click on OK

I know this is a old post but I found a great solution!
Read the registry key of firewall status found in:

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

Key: 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;
        }
    }

Also you can get values for DomainProfile, PublicProfile and StandardProfile. You can get FirewallRules too.

I hope this helps :)

just import the refrences from C://windows/system32/hnetcfg.dll and C://windows/system32/FirewallAPI.dll

then use

using NATUPNPLib;
using NETCONLib;
using NetFwTypeLib;

You can make use of the FwMgr for old Windows versions (XP) and use Windows Firewall with Advanced Security API for Vista and Above.

Here is an example that retrieves the firewall setting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM