/// <summary> /// Authorize an application to work through the firewall /// </summary> /// <param name="title">Name of Firewall Rule</param> /// <param name="applicationPath">Path to executable</param> /// <param name="scope">Scope (All, Local, Custom)</param> /// <param name="ipVersion">IPv4, IpV6 or both</param> /// <returns>True if it succeeds</returns> public static bool AuthorizeApplication(string title, string applicationPath, NET_FW_SCOPE scope, NET_FW_ACTION action, NET_FW_IP_VERSION ipVersion) { try { if (OSVersion.GetOSVersion() <= OSVersion.OS.WIN_2003) { dynamic fwMgr = CreateCOMObject(PROGID_FIREWALL_MANAGER); dynamic profile = fwMgr.LocalPolicy.CurrentProfile; dynamic authApp = CreateCOMObject(PROGID_AUTHORIZED_APPLICATION); authApp.Name = title; authApp.ProcessImageFileName = applicationPath; authApp.Scope = scope; authApp.IpVersion = ipVersion; authApp.Enabled = true; profile.AuthorizedApplications.Add(authApp); } else { dynamic firewallRule = CreateCOMObject(PROGID_FW_RULE); firewallRule.Action = action; firewallRule.Name = title; firewallRule.ApplicationName = applicationPath; firewallRule.Enabled = true; firewallRule.InterfaceTypes = "All"; firewallRule.EdgeTraversal = true; dynamic firewallPolicy = CreateCOMObject(PROGID_FW_POLICY); firewallPolicy.Rules.Add(firewallRule); } } catch (Exception e) { Log.WriteSystemEventLog("Error authorizing firewall application -> " + e.ToString(), System.Diagnostics.EventLogEntryType.Warning); return false; } return true; }
/// <summary> /// Authorize an application to work through the firewall /// </summary> /// <param name="title">Name of Firewall Rule</param> /// <param name="applicationPath">Path to executable</param> /// <param name="scope">Scope (All, Local, Custom)</param> /// <param name="ipVersion">IPv4, IpV6 or both</param> /// <returns>True if it succeeds</returns> public static bool AuthorizeApplication(string title, string applicationPath, NET_FW_SCOPE scope, NET_FW_ACTION action, NET_FW_IP_VERSION ipVersion) { try { if (OSVersion.GetOSVersion() <= OSVersion.OS.WIN_2003) { dynamic fwMgr = CreateCOMObject(PROGID_FIREWALL_MANAGER); dynamic profile = fwMgr.LocalPolicy.CurrentProfile; dynamic authApp = CreateCOMObject(PROGID_AUTHORIZED_APPLICATION); authApp.Name = title; authApp.ProcessImageFileName = applicationPath; authApp.Scope = scope; authApp.IpVersion = ipVersion; authApp.Enabled = true; profile.AuthorizedApplications.Add(authApp); } else { dynamic firewallRule = CreateCOMObject(PROGID_FW_RULE); firewallRule.Action = action; firewallRule.Name = title; firewallRule.ApplicationName = applicationPath; firewallRule.Enabled = true; firewallRule.InterfaceTypes = "All"; firewallRule.EdgeTraversal = true; dynamic firewallPolicy = CreateCOMObject(PROGID_FW_POLICY); firewallPolicy.Rules.Add(firewallRule); } } catch (Exception e) { Log.WriteSystemEventLog("Error authorizing firewall application -> " + e.ToString(), System.Diagnostics.EventLogEntryType.Warning); return(false); } return(true); }
/// <summary> /// Opens a port on a the Windows Firewall /// </summary> /// <param name="title">Name of Firewall Rule</param> /// <param name="portNo">Port number</param> /// <param name="scope">All, Subnet, Custom</param> /// <param name="protocol">TCP, UDP</param> /// <param name="ipVersion">IPv4, IPv6, Both</param> /// <returns>True if successful</returns> public static bool AuthorizePort(string title, int portNo, NET_FW_SCOPE scope, NET_FW_IP_PROTOCOL protocol, NET_FW_IP_VERSION ipVersion) { try { if (OSVersion.GetOSVersion() <= OSVersion.OS.WIN_2003) { dynamic fwMgr = CreateCOMObject(PROGID_FIREWALL_MANAGER); dynamic profile = fwMgr.LocalPolicy.CurrentProfile; dynamic port = CreateCOMObject(PROGID_OPEN_PORT); port.Name = title; port.Port = portNo; port.Scope = scope; port.Protocol = protocol; port.IpVersion = ipVersion; profile.GloballyOpenPorts.Add(port); } else { dynamic firewallRule = CreateCOMObject(PROGID_FW_RULE); firewallRule.Name = title; firewallRule.Protocol = protocol; firewallRule.LocalPorts = portNo.ToString(); firewallRule.Enabled = true; firewallRule.InterfaceTypes = "All"; firewallRule.EdgeTraversal = true; dynamic firewallPolicy = CreateCOMObject(PROGID_FW_POLICY); firewallPolicy.Rules.Add(firewallRule); } } catch (Exception e) { Log.WriteSystemEventLog("Error enabling firewall port -> " + e.ToString(), System.Diagnostics.EventLogEntryType.Warning); return(false); } return(true); }
/// <summary> /// Opens a port on a the Windows Firewall /// </summary> /// <param name="title">Name of Firewall Rule</param> /// <param name="portNo">Port number</param> /// <param name="scope">All, Subnet, Custom</param> /// <param name="protocol">TCP, UDP</param> /// <param name="ipVersion">IPv4, IPv6, Both</param> /// <returns>True if successful</returns> public static bool AuthorizePort(string title, int portNo, NET_FW_SCOPE scope, NET_FW_IP_PROTOCOL protocol, NET_FW_IP_VERSION ipVersion) { try { if (OSVersion.GetOSVersion() <= OSVersion.OS.WIN_2003) { dynamic fwMgr = CreateCOMObject(PROGID_FIREWALL_MANAGER); dynamic profile = fwMgr.LocalPolicy.CurrentProfile; dynamic port = CreateCOMObject(PROGID_OPEN_PORT); port.Name = title; port.Port = portNo; port.Scope = scope; port.Protocol = protocol; port.IpVersion = ipVersion; profile.GloballyOpenPorts.Add(port); } else { dynamic firewallRule = CreateCOMObject(PROGID_FW_RULE); firewallRule.Name = title; firewallRule.Protocol = protocol; firewallRule.LocalPorts = portNo.ToString(); firewallRule.Enabled = true; firewallRule.InterfaceTypes = "All"; firewallRule.EdgeTraversal = true; dynamic firewallPolicy = CreateCOMObject(PROGID_FW_POLICY); firewallPolicy.Rules.Add(firewallRule); } } catch (Exception e) { Log.WriteSystemEventLog("Error enabling firewall port -> " + e.ToString(), System.Diagnostics.EventLogEntryType.Warning); return false; } return true; }