Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.");
           }

        }
    }
}
like image 845
Pavan Anadkat Avatar asked Dec 10 '12 05:12

Pavan Anadkat


2 Answers

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

like image 148
4b0 Avatar answered Sep 23 '22 04:09

4b0


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.

like image 30
Glenn Slaven Avatar answered Sep 24 '22 04:09

Glenn Slaven