7

Come posso rilevare (da un'applicazione Windows Form scritta in C#) se un prodotto firewall è abilitato?Come posso rilevare se un prodotto firewall è abilitato?

Ecco il mio codice e sto ottenendo errore sul INetFwMgr quel tipo o dello spazio dei nomi non è stato trovato

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

     } 
    } 
} 
+0

Ti manca una direttiva using? – CRoshanLG

+0

Sì. Come risolvere questo? –

+0

Aggiungi lo spazio dei nomi Microsoft.TeamFoundation.Common al tuo codice. Vedi l'aggiunta nella mia risposta. – CRoshanLG

risposta

2

Date un'occhiata a questa domanda qui circa 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++ la stessa chiamata API può essere utilizzato per rilevare impostazioni del firewall utilizzando l'enum WSC_SECURITY_PROVIDER_FIREWALL. La risposta è effettivamente sbagliata per quella domanda, ma ti darà la risposta per i computer non server. Quel codice è in C++, ma è solo la chiamata API di Windows che ti serve, puoi anche chiamarla da C#.

+0

Si noti che questo rileverà solo le applicazioni firewall in esecuzione locale. Non (e non può) rilevare dispositivi firewall e simili. – Donnie

+0

No, non sarà dato il tag su questa domanda (Windows-firewall, ecc.). Ho pensato che l'OP significasse il firewall locale –

+0

Probabilmente, stavo solo cercando di essere completo. – Donnie

3
NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); 
INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType); 
bool Firewallenabled = mgr.LocalPolicy.CurrentProfile.FirewallEnabled; 

Per i dettagli vedere un collegamento.

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

+0

Grazie funziona per me –

+0

Vedere la risposta di Jeetendra negi in basso su come ottenere questo codice da compilare. –

1

Per prima cosa è necessario aggiungere il seguente componente al progetto

  • INetFwMgr

Quindi, ottenere il tipo di oggetto dalla Home Networking Configuration Manager CLSID che è {304CE942-6E39-40D8-943A-B913C40C9CD4} (Link a C:\WINDOWS\system32\hnetcfg.dll e può essere trovato a HKEY_CLASSES_ROOT\CLSID\{304CE942-6E39-40D8-943A-B913C40C9CD4}) e utilizzare il tipo raccolto per creare un'istanza utilizzando il costruttore predefinito del tipo come nuovo INetFwMgr che verrà utilizzato per rilevare se il firewall è enab portato o non utilizzo di INetFwMgr.LocalPolicy.CurrentProfile.FirewallEnabled che restituisce un 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 
    } 
} 

Grazie,
spero che è risultato utile :)


Per aggiungere un componente al progetto,

  • destro -click Riferimenti dal Solution Explorer sotto il nome del progetto e selezionare Aggiungi riferimento ...
  • Nella scheda COM, selezionare il componente che si vuoi aggiungere e cliccare su OK
+1

non ho trovato INetFwMgr sotto COM. Ora come posso aggiungere questo componente? –

+0

Errore Impossibile trovare il nome dello spazio dei nomi "INetFwMgr" (manca una direttiva using o un riferimento all'assembly?) \t c: \ users \ administrator.mustuspune \ documents \ visual studio 2010 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs \t Ho ricevuto questo errore –

1

So che questo è un vecchio post ma ho trovato un'ottima soluzione!
Leggi la chiave di registro di stato del firewall si trovano in:

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

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

Inoltre è possibile ottenere valori per DomainProfile, PublicProfile e StandardProfile. Puoi anche ottenere FirewallRules.

Spero che questo aiuta :)

1

Basta importare i refrences da C: //windows/system32/hnetcfg.dll e C: //windows/system32/FirewallAPI.dll

quindi utilizzare

using NATUPNPLib; 
using NETCONLib; 
using NetFwTypeLib;