/// <summary> /// 将指定的端口添加到防火墙例外值列表 /// </summary> /// <param name="port">需要例外的TCP端口号</param> /// <param name="ruleName">防火墙规则列表中的显示名称</param> /// <param name="protocol">协议类型</param> private static void AllowPort(int port, string ruleName, NET_FW_IP_PROTOCOL protocol) { Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2"); INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2); try { // 检查规则是否已存在 if (fwPolicy2.Rules.Item(ruleName) != null) { return; } } catch {// 如果规则不存在,会抛出异常,这里就直接吃掉异常 } // 创建一个入站规则实例 INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")); inboundRule.Enabled = true; //设置为允许 inboundRule.Action = NET_FW_ACTION.NET_FW_ACTION_ALLOW; //指定使用TCP协议 inboundRule.Protocol = (int)protocol; inboundRule.LocalPorts = port.ToString(); //规则名称 inboundRule.Name = ruleName; // 规则影响范围(配置文件) inboundRule.Profiles = (int)NET_FW_PROFILE_TYPE2.NET_FW_PROFILE2_ALL; // 添加规则到防火墙 fwPolicy2.Rules.Add(inboundRule); }
/// <summary> /// Removes a port on a the Windows Firewall /// </summary> /// <param name="title">Name of Firewall Rule</param> /// <param name="portNo">Port number</param> /// <param name="protocol">TCP, UDP</param> /// <returns>True if successful</returns> public static bool DeAuthorizePort(string title, int portNo, NET_FW_IP_PROTOCOL protocol) { try { if (OSVersion.GetOSVersion() <= OSVersion.OS.WIN_2003) { dynamic fwMgr = CreateCOMObject(PROGID_FIREWALL_MANAGER); dynamic profile = fwMgr.LocalPolicy.CurrentProfile; profile.GloballyOpenPorts.Remove(portNo, protocol); } else { dynamic firewallPolicy = CreateCOMObject(PROGID_FW_POLICY); firewallPolicy.Rules.Remove(title); } } catch (Exception e) { Log.WriteSystemEventLog("Error DeAuthorizing firewall port -> " + e.ToString(), System.Diagnostics.EventLogEntryType.Warning); return(false); } return(true); }
/// <summary> /// Removes all references in the firewall for the specified ports/protocol combination and named rules entries /// </summary> /// <param name="appTitle">Name of Firewall Rule for Authorizing application</param> /// <param name="applicationPath">Path to the executable</param> /// <param name="portTitle">Name of Firewall Rule for Authorizing port</param> /// <param name="portNo">Port number</param> /// <param name="protocol">TCP, UDP</param> /// <returns>True if successful</returns> public static bool CleanUpFirewall(string appTitle, string applicationPath, string portTitle, int portNo, NET_FW_IP_PROTOCOL protocol) { try { if (OSVersion.GetOSVersion() <= OSVersion.OS.WIN_2003) { // Windows XP always only makes one entry even if multiple calls are made to add. For some reason getting list (Item) throws an exception DeAuthorizePort(portTitle, portNo, protocol); DeAuthorizeApplication(appTitle, applicationPath); } else { dynamic firewallPolicy = CreateCOMObject(PROGID_FW_POLICY); try { while (true) { firewallPolicy.Rules.Item(portTitle); // Get the item firewallPolicy.Rules.Remove(portTitle); // Keep removing all entries for open ports (duplicates) } } catch { } // When the entries run out, Item throws an exception, H_RESULT_NOT_FOUND try { while (true) { firewallPolicy.Rules.Item(appTitle); // Get the item firewallPolicy.Rules.Remove(appTitle); // Keep removing all entries for Authorized apps (duplicates) } } catch { } // When the entries run out, Item throws an exception, H_RESULT_NOT_FOUND } } catch (Exception e) { Log.WriteSystemEventLog("Error cleaning up firewall entries -> " + 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); }
public FirewallLegacyPortCollectionKey(int portNumber, NET_FW_IP_PROTOCOL protocolType) { PortNumber = portNumber; ProtocolType = protocolType; }
/// <summary> /// Removes all references in the firewall for the specified ports/protocol combination and named rules entries /// </summary> /// <param name="appTitle">Name of Firewall Rule for Authorizing application</param> /// <param name="applicationPath">Path to the executable</param> /// <param name="portTitle">Name of Firewall Rule for Authorizing port</param> /// <param name="portNo">Port number</param> /// <param name="protocol">TCP, UDP</param> /// <returns>True if successful</returns> public static bool CleanUpFirewall(string appTitle, string applicationPath, string portTitle, int portNo, NET_FW_IP_PROTOCOL protocol) { try { if (OSVersion.GetOSVersion() <= OSVersion.OS.WIN_2003) { // Windows XP always only makes one entry even if multiple calls are made to add. For some reason getting list (Item) throws an exception DeAuthorizePort(portTitle, portNo, protocol); DeAuthorizeApplication(appTitle, applicationPath); } else { dynamic firewallPolicy = CreateCOMObject(PROGID_FW_POLICY); try { while (true) { firewallPolicy.Rules.Item(portTitle); // Get the item firewallPolicy.Rules.Remove(portTitle); // Keep removing all entries for open ports (duplicates) } } catch { } // When the entries run out, Item throws an exception, H_RESULT_NOT_FOUND try { while (true) { firewallPolicy.Rules.Item(appTitle); // Get the item firewallPolicy.Rules.Remove(appTitle); // Keep removing all entries for Authorized apps (duplicates) } } catch { } // When the entries run out, Item throws an exception, H_RESULT_NOT_FOUND } } catch (Exception e) { Log.WriteSystemEventLog("Error cleaning up firewall entries -> " + e.ToString(), System.Diagnostics.EventLogEntryType.Warning); return false; } return true; }
/// <summary> /// Removes a port on a the Windows Firewall /// </summary> /// <param name="title">Name of Firewall Rule</param> /// <param name="portNo">Port number</param> /// <param name="protocol">TCP, UDP</param> /// <returns>True if successful</returns> public static bool DeAuthorizePort(string title, int portNo, NET_FW_IP_PROTOCOL protocol) { try { if (OSVersion.GetOSVersion() <= OSVersion.OS.WIN_2003) { dynamic fwMgr = CreateCOMObject(PROGID_FIREWALL_MANAGER); dynamic profile = fwMgr.LocalPolicy.CurrentProfile; profile.GloballyOpenPorts.Remove(portNo, protocol); } else { dynamic firewallPolicy = CreateCOMObject(PROGID_FW_POLICY); firewallPolicy.Rules.Remove(title); } } catch (Exception e) { Log.WriteSystemEventLog("Error DeAuthorizing 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; }