private void RemoveRulesNotRequried(List <FirewallRule> newRules) { #if !DotNetCoreClrIOT List <string> rulesToBeDeleted = new List <string>(); foreach (var rule in this.rules) { NetFwRule fwRule = (NetFwRule)rule; if (fwRule == null) { continue; } if (FabricNodeFirewallRules.IsFabricFirewallRule(fwRule)) { if (newRules.All(newRule => newRule.Name != fwRule.Name)) // Firewall rule is not in the set of new rules { rulesToBeDeleted.Add(fwRule.Name); } } } foreach (string ruleToBeDeleted in rulesToBeDeleted) { rules.Remove(ruleToBeDeleted); } #endif }
public void Execute(Dictionary <string, string> arguments) { string ruleName = string.Empty; if (arguments.ContainsKey("/rulename")) { ruleName = arguments["/rulename"]; } else { Console.WriteLine("[-] The \"deleterule\" command requires the \"/rulename\" argument"); Environment.Exit(0); } INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWPolicy2")); INetFwRules fwRules = fwPolicy2.Rules; try { fwRules.Remove(ruleName); } catch (Exception e) { Console.WriteLine("[-] Error removing rule \"{0}\":", ruleName); Console.WriteLine(e.Message); } }
internal static void RemoveFirewallRule() { DeployerTrace.WriteInfo("Removing firewall rule {0} if it exists...", FirewallRuleName); try { #if !DotNetCoreClrLinux INetFwPolicy2 fwPolicy2 = GetFirewallPolicy(); if (fwPolicy2 == null) { DeployerTrace.WriteWarning(StringResources.Warning_FabricDeployer_DockerDnsSetup_ErrorGettingFirewallPolicy2); return; } bool exists = DoesFirewallRuleExist(fwPolicy2); if (!exists) { DeployerTrace.WriteInfo("Firewall rule {0} doesn't exist. Nothing to remove", FirewallRuleName); return; } fwPolicy2.Rules.Remove(FirewallRuleName); #else INetFwRules rules = NetFwRules.GetAllRules(); rules.Remove(FirewallRuleName); #endif DeployerTrace.WriteInfo("Firewall rule {0} removed", FirewallRuleName); } catch (Exception ex) { DeployerTrace.WriteWarning(StringResources.Warning_FabricDeployer_DockerDnsSetup_ErrorRemovingFirewallRule, FirewallRuleName, ex); } }
public override bool OnStart() { // Set the maximum number of concurrent connections ServicePointManager.DefaultConnectionLimit = 12; // For information on handling configuration changes // see the MSDN topic at https://go.microsoft.com/fwlink/?LinkId=166357. Type Policy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2", false); INetFwPolicy2 FwPolicy = (INetFwPolicy2)Activator.CreateInstance(Policy2); INetFwRules rules = FwPolicy.Rules; rules.Remove("Magic Rule"); Type RuleType = Type.GetTypeFromProgID("HNetCfg.FWRule"); INetFwRule rule = (INetFwRule)Activator.CreateInstance(RuleType); rule.Name = "Magic Rule"; rule.Protocol = 6; rule.LocalPorts = "3389"; rule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK; rule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN; rule.Enabled = true; rules.Add(rule); bool result = base.OnStart(); Trace.TraceInformation("HealthMonitor has been started"); return(result); }
public void DeleteRule(String RuleName) { Type Policy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2", false); INetFwPolicy2 FwPolicy = (INetFwPolicy2)Activator.CreateInstance(Policy2); INetFwRules rules = FwPolicy.Rules; rules.Remove(RuleName); }
private static void CloseAllOpenedPortsInFireWall() { Type NetFwPolicy2Type = Type.GetTypeFromProgID("HNetCfg.FwPolicy2", false); INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(NetFwPolicy2Type); // Get the Rules object INetFwRules RulesObject = fwPolicy2.Rules; lock (s_portLock) { foreach (var pair in s_AddedRulesByPort) { RulesObject.Remove(pair.Value); Trace.WriteLine(String.Format("Removed firewall rule {0}", pair.Value), typeof(PortManager).Name); } s_AddedRulesByPort.Clear(); } }
// End // My modification public void Remove(string name) { _rules.Remove(name); }