public bool IsEnabled() { #if !DotNetCoreClrLinux && !DotNetCoreClrIOT System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController(FirewallService); return(controller.Status == System.ServiceProcess.ServiceControllerStatus.Running || policy.get_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_DOMAIN) || policy.get_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE) || policy.get_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC)); #else // For now assume firewall is always enabled on Linux return(true); #endif }
private bool TestFirewallEnabled(bool Enable) { if (mFirewallPolicy.get_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE) != Enable) { return(false); } if (mFirewallPolicy.get_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_DOMAIN) != Enable) { return(false); } if (mFirewallPolicy.get_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC) != Enable) { return(false); } return(true); }
/* Checks local and policy setting for enabled firewall by profile */ public Boolean isEnabled(NET_FW_PROFILE_TYPE2_ fwProfile, bool updateProfile = true) { var enabled = false; if (updateProfile) { updateCurrentProfile(); } //something went wrong; abort and say not enabled if (fw == null) { return(enabled); } if (fw.get_FirewallEnabled(fwProfile)) { enabled = true; } //check GPO if (!enabled) { enabled = Convert.ToBoolean(Registry.GetValue(hkeyRoot + "\\" + fwRegKey[fwProfile], "EnableFirewall", false)); } return(enabled); }
/// <summary> /// Is the Windows firewall enabled? /// </summary> /// <returns>Returns True if enabled, or false if disabled.</returns> public static bool IsEnabled() { INetFwPolicy2 policy = GetCurrentPolicy(); NET_FW_PROFILE_TYPE2_ currentProfileTypes; currentProfileTypes = (NET_FW_PROFILE_TYPE2_)policy.CurrentProfileTypes; return(policy.get_FirewallEnabled(currentProfileTypes)); }
/// <summary> /// Throw an exception if Windows firewall is disabled /// </summary> public static void ThrowExceptionIfWindowsFirewallIsDisabled() { // netsh advfirewall show allprofiles state if (!policy.get_FirewallEnabled(NetFwProfileType2.Domain) && !policy.get_FirewallEnabled(NetFwProfileType2.Private) && !policy.get_FirewallEnabled(NetFwProfileType2.Public) && !manager.LocalPolicy.CurrentProfile.FirewallEnabled) { // read firewall state from powershell script ProcessStartInfo psScript = new ProcessStartInfo("powershell", "Get-NetfirewallProfile -PolicyStore ActiveStore") { RedirectStandardOutput = true, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden }; using Process psProcess = Process.Start(psScript); psProcess.WaitForExit(); string text = psProcess.StandardOutput.ReadToEnd(); if (!Regex.IsMatch(text, @"enabled\s*:\s*true", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)) { throw new ApplicationException("Windows firewall is currently disabled, please enable Windows firewall. Public, Private and Domain profiles were checked for active state."); } } }
private FirewallStatus FirewallStatus(FirewallDomain?domain) { // Gets the current firewall profile (domain, public, private, etc.) NET_FW_PROFILE_TYPE2_ fwCurrentProfileTypes; if (domain.HasValue) { fwCurrentProfileTypes = (NET_FW_PROFILE_TYPE2_)domain; } else { fwCurrentProfileTypes = (NET_FW_PROFILE_TYPE2_)policyManager.CurrentProfileTypes; } return((FirewallStatus)Convert.ToInt32(policyManager.get_FirewallEnabled(fwCurrentProfileTypes))); }
static void publicFirewall() { Type netFwPolicy2Type = Type.GetTypeFromProgID("HNetCfg.FwPolicy2"); INetFwPolicy2 mgr = (INetFwPolicy2)Activator.CreateInstance(netFwPolicy2Type); NET_FW_PROFILE_TYPE2_ fwPublicProfileType = NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC; Console.WriteLine("Public Profile Type:"); var firewallEnabled = mgr.get_FirewallEnabled(fwPublicProfileType); var blockAllInboundTraffic = mgr.get_BlockAllInboundTraffic(fwPublicProfileType); var defaultInboundAction = mgr.get_DefaultInboundAction(fwPublicProfileType); var defaultOutboundAction = mgr.get_DefaultOutboundAction(fwPublicProfileType); Console.WriteLine($"Firewall Enabled: {firewallEnabled.ToString()}"); Console.WriteLine($"Block All Inbound Traffic: {blockAllInboundTraffic.ToString()}"); Console.WriteLine($"Default Inbound Action:{defaultInboundAction.ToString()}"); Console.WriteLine($"Default Outbound Action:{defaultOutboundAction.ToString()}"); }
public static bool GetFirewallStatus() { bool result = false; try { INetFwPolicy2 fwPolicy2 = getCurrPolicy(); NET_FW_PROFILE_TYPE2_ fwCurrentProfileTypes; //read Current Profile Types (only to increase Performace) //avoids access on CurrentProfileTypes from each Property fwCurrentProfileTypes = (NET_FW_PROFILE_TYPE2_)fwPolicy2.CurrentProfileTypes; result = (fwPolicy2.get_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC)); } catch (Exception e) { Logger.Log(e.ToString()); } return(result); }
public bool GetFirewallEnabled(FirewallRule.Profiles profileType) { return(NetFwPolicy.get_FirewallEnabled((NET_FW_PROFILE_TYPE2_)profileType)); }