public void AddRule(String name, String Description, NET_FW_ACTION_ Action, NET_FW_RULE_DIRECTION_ Direction, String LocalPort, bool Enabled = true, int Protocole = 6, String RemoteAdresses = "localsubnet", String ApplicationName = "ScreenTask") { Type Policy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2", false); INetFwPolicy2 FwPolicy = (INetFwPolicy2)Activator.CreateInstance(Policy2); INetFwRules rules = FwPolicy.Rules; //Delete if exist to avoid deplicated rules DeleteRule(name); Type RuleType = Type.GetTypeFromProgID("HNetCfg.FWRule"); INetFwRule rule = (INetFwRule)Activator.CreateInstance(RuleType); rule.Name = name; rule.Description = Description; rule.Protocol = Protocole;// TCP/IP rule.LocalPorts = LocalPort; rule.RemoteAddresses = RemoteAdresses; rule.Action = Action; rule.Direction = Direction; rule.ApplicationName = ApplicationName; rule.Enabled = true; //Add Rule rules.Add(rule); }
private Mock <INetFwRule> GetRuleMock( string name, string description, string applicationName, string serviceName, int protocol, string localPorts, string remotePorts, string localAddresses, string remoteAddresses, NET_FW_RULE_DIRECTION_ direction, bool enabled, NET_FW_ACTION_ action) { var rule = new Mock <INetFwRule>(); rule.SetupProperty(p => p.Name, name); rule.SetupProperty(p => p.Description, description); rule.SetupProperty(p => p.ApplicationName, applicationName); rule.SetupProperty(p => p.serviceName, serviceName); rule.SetupProperty(p => p.Protocol, protocol); rule.SetupProperty(p => p.LocalPorts, localPorts); rule.SetupProperty(p => p.LocalAddresses, localAddresses); rule.SetupProperty(p => p.RemotePorts, remotePorts); rule.SetupProperty(p => p.RemoteAddresses, remoteAddresses); rule.SetupProperty(p => p.Direction, direction); rule.SetupProperty(p => p.Enabled, enabled); rule.SetupProperty(p => p.Action, action); return(rule); }
private void FWRule(string path, NET_FW_RULE_DIRECTION_ d, NET_FW_ACTION_ fwaction, string action) { try { INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance( Type.GetTypeFromProgID("HNetCfg.FWRule")); firewallRule.Action = fwaction; firewallRule.Enabled = true; firewallRule.InterfaceTypes = "All"; firewallRule.ApplicationName = path; firewallRule.Name = "Frewall_Blocker: " + Path.GetFileName(path); INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance (Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); firewallRule.Direction = d; if (action == "1") { firewallPolicy.Rules.Add(firewallRule); MessageBox.Show("Program is blocked"); } else { firewallPolicy.Rules.Remove(firewallRule.Name); MessageBox.Show("Program is unblocked"); } } catch (Exception ex) { MessageBox.Show(ex.Message, "ERROR"); } }
/// <summary> /// 查找规则 /// </summary> /// <param name="name">规则名</param> /// <param name="applicationPath">应用程序名</param> /// <returns></returns> public static IList <INetFwRule2> FindRule(string ruleName, string applicationPath, string remotePorts, string localPorts, string direction) { List <INetFwRule2> ret = new List <INetFwRule2>(); NET_FW_RULE_DIRECTION_ dir = GetDirect(direction); INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); foreach (INetFwRule2 firewallRule in firewallPolicy.Rules) { if (!IsFit(ruleName, firewallRule.Name)) { continue; } if (!IsFit(applicationPath, firewallRule.ApplicationName)) { continue; } if (!IsFit(remotePorts, firewallRule.RemotePorts)) { continue; } if (!IsFit(localPorts, firewallRule.LocalPorts)) { continue; } if (dir != firewallRule.Direction) { continue; } ret.Add(firewallRule); } return(ret); }
private FirewallRule GetApplicationPathException(string template, string path, string direction, NET_FW_PROFILE_TYPE2_ fwProfile) { string ruleName = string.Format(CultureInfo.InvariantCulture, template, this.NodeName, GetProfileFriendlyName(fwProfile), direction); NET_FW_RULE_DIRECTION_ fwDirection = direction == FabricNodeFirewallRules.inDirection ? NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN : NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT; return(GetRule(ruleName, path, null, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP, fwDirection, fwProfile)); }
private FirewallRule GetCustomTcpPortException(string template, string port, string direction) { string ruleName = string.Format(CultureInfo.InvariantCulture, template, this.NodeName, GetProfileFriendlyName(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC), direction); NET_FW_RULE_DIRECTION_ fwDirection = direction == FabricNodeFirewallRules.inDirection ? NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN : NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT; return(GetRule(ruleName, null, port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP, fwDirection, NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC)); }
private void BtnCreate_Click(object sender, RoutedEventArgs e) { ComboBoxItem itm = (ComboBoxItem)CmbType.SelectedItem; NET_FW_RULE_DIRECTION_ richting = (NET_FW_RULE_DIRECTION_)itm.Tag; string name = txtFirewallRuleName.Text; string description = txtBeschrijving.Text; int profileValue = 0; if ((bool)chkProfielDomein.IsChecked) { profileValue += 1; } if ((bool)chkProfielPrive.IsChecked) { profileValue += 2; } if ((bool)chkProfielOpenbaar.IsChecked) { profileValue += 4; } bool enabled = (bool)chkIsActief.IsChecked; NET_FW_ACTION_ typeOfRule = NET_FW_ACTION_.NET_FW_ACTION_ALLOW; if ((bool)rdbBlock.IsChecked) { typeOfRule = NET_FW_ACTION_.NET_FW_ACTION_BLOCK; } string application = txtProgramma.Text; if (application.ToString() == "") { application = null; } string localAdresses = txtAdresLokaal.Text; if (localAdresses.Trim() == "") { localAdresses = "*"; } string remoteAdresses = txtAdresExtern.Text; if (remoteAdresses.Trim() == "") { remoteAdresses = "*"; } string localPorts = txtPoortLokaal.Text; string remotePorts = txtPoortExtern.Text; itm = (ComboBoxItem)CmbProtocol.SelectedItem; int protocolValue = (int)itm.Tag; if (WinFireWall.CreateRule(richting, name, description, profileValue, enabled, typeOfRule, application, localAdresses, remoteAdresses, localPorts, remotePorts, protocolValue)) { this.Close(); } }
public static Direction Convert(NET_FW_RULE_DIRECTION_ item) { switch (item) { case NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN: return(Direction.In); case NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT: return(Direction.Out); default: return(Direction.Max); } }
private void VulRules(NET_FW_RULE_DIRECTION_ richting) { lstRules.Items.Clear(); ListBoxItem itm; foreach (INetFwRule regel in WinFireWall.GetAllRules(richting)) { itm = new ListBoxItem(); itm.Content = regel.Name; itm.Tag = regel; lstRules.Items.Add(itm); } }
/// <summary> /// Convert a firewall direction value to the correct payload value /// </summary> /// <param name="ruleDirection">the rule direction</param> /// <returns>the payload value corresponding to the direction</returns> public static FirewallRulePayload.Directions ToIoTValue(this NET_FW_RULE_DIRECTION_ ruleDirection) { switch (ruleDirection) { case NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN: return(FirewallRulePayload.Directions.In); case NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT: return(FirewallRulePayload.Directions.Out); default: throw new ApplicationException($"rule direction of {ruleDirection} cannot be converted"); } }
public static IList <INetFwRule> GetAllRules(NET_FW_RULE_DIRECTION_ richting) { IList <INetFwRule> rules = new List <INetFwRule>(); INetFwPolicy2 fwpoliciy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); foreach (INetFwRule fwrule in fwpoliciy.Rules) { if (fwrule.Direction == richting) { rules.Add(fwrule); } } return(rules); }
private FirewallRule GetLeaseDriverExceptionRule(string direction, NET_FW_PROFILE_TYPE2_ fwProfile) { string ruleName = string.Format( CultureInfo.InvariantCulture, FabricNodeFirewallRules.LeaseDriverExceptionTemplate, this.NodeName, GetProfileFriendlyName(fwProfile), direction); NET_FW_RULE_DIRECTION_ fwDirection = direction == FabricNodeFirewallRules.inDirection ? NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN : NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT; return(GetRule(ruleName, null, this.LeaseDriverPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP, fwDirection, fwProfile)); }
private bool _________(_____________________________ _, NET_FW_RULE_DIRECTION_ __, bool ___) { try { var ____ = __ == NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN ? _.________ : _._________; var ______ = this._______(__, _.____________); if (___) { this.__.Rules.Remove(______); _.______ = true; } else { var x = ____ ?? _______(______); ____ = x ?? (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")); ____.Action = _.________________ ? NET_FW_ACTION_.NET_FW_ACTION_BLOCK : NET_FW_ACTION_.NET_FW_ACTION_ALLOW; ____.Enabled = true; ____.InterfaceTypes = "All"; ____.ApplicationName = _.______________; ____.Direction = __; if (x == null) { ____.Name = ______; this.__.Rules.Add(____); this.____.Add(____); } _.______ = false; if (__ == NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN) { _.________ = ____; } else { _._________ = ____; } } return(true); } catch (Exception) { return(false); } }
private static void AddRule(string name, string ip, NET_FW_RULE_DIRECTION_ direction) { var firewallRule = (INetFwRule)Activator.CreateInstance(RuleType); firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK; firewallRule.Description = $"Used to block all internet access for IP:{ip}"; firewallRule.RemoteAddresses = ip; firewallRule.Direction = direction; firewallRule.Enabled = true; firewallRule.InterfaceTypes = "All"; firewallRule.Name = name; var firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(PolicyType); firewallPolicy.Rules.Add(firewallRule); }
/// <summary> /// 用端口查找规则 /// </summary> /// <param name="name">规则名</param> /// <param name="localPorts">端口</param> /// <returns></returns> public static INetFwRule2 FindRuleByPort(string name, string localPorts, NET_FW_RULE_DIRECTION_ direction) { INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); foreach (INetFwRule2 firewallRule in firewallPolicy.Rules) { string cname = firewallRule.Name; string port = firewallRule.LocalPorts; if (string.Equals(cname, name) && string.Equals(port, localPorts, StringComparison.CurrentCultureIgnoreCase) && direction == firewallRule.Direction) { return(firewallRule); } } return(null); }
private FirewallRule GetHttpGatewayExceptionRule(string template, string port, string direction, NET_FW_PROFILE_TYPE2_ fwProfile) { // Httpgateway exception is a driver level exception for http.sys string ruleName = string.Format( CultureInfo.InvariantCulture, template, this.NodeName, GetProfileFriendlyName(fwProfile), direction); NET_FW_RULE_DIRECTION_ fwDirection = direction == FabricNodeFirewallRules.inDirection ? NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN : NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT; return(GetRule(ruleName, systemApplicationPath, port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP, fwDirection, fwProfile)); }
public static List <Rule> GetFirewallRules(NET_FW_RULE_DIRECTION_ direction) { List <Rule> rules = new List <Rule>(); // Get firewall info INetFwPolicy2 fwPolicy = GetFirewallPolicy(); // Get firewall rules INetFwRules fwRules = fwPolicy.Rules; // Get rule enumerator IEnumerator enumerator = fwRules.GetEnumerator(); // For each firewall rule while (enumerator.MoveNext()) { INetFwRule fwRule = (INetFwRule)enumerator.Current; // If rule is not the direction we're looking for, skip if (fwRule.Direction != direction) { continue; } Rule rule = new Rule(fwRule.Name) { Description = checkString(fwRule.Description), ApplicationName = checkString(fwRule.ApplicationName), ServiceName = checkString(fwRule.serviceName), intProtocol = fwRule.Protocol, LocalPorts = checkString(fwRule.LocalPorts), RemotePorts = checkString(fwRule.RemotePorts), LocalAddresses = checkString(fwRule.LocalAddresses), RemoteAddresses = checkString(fwRule.RemoteAddresses), Enabled = fwRule.Enabled, Group = checkString(fwRule.Grouping), Profile = (NET_FW_PROFILE_TYPE2_)fwRule.Profiles, Action = fwRule.Action, }; rules.Add(rule); } return(rules); }
private void MakeRule(string str, int protNumber, NET_FW_RULE_DIRECTION_ ruleDirection, string ruleName) { Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2"); INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2); // Let's create a new rule INetFwRule2 Rule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")); Rule.Enabled = true; NET_FW_RULE_DIRECTION_ direction = ruleDirection; Rule.Direction = direction; //Inbound Rule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK; Rule.Protocol = protNumber; // ANY/TCP/UDP try { Rule.RemoteAddresses = str; } catch (Exception) { MessageBox.Show("Can't add Rules. Maybe a Format failure?", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } //Rule.LocalPorts = "81"; //Port 81 //Name of rule Rule.Name = ruleName; // ...// //Rule.Profiles = (int)NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_TYPE_MAX; // Now add the rule //INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); try { firewallPolicy.Rules.Add(Rule); } catch (Exception ex) { throw ex; } }
private FirewallRule GetDynamicPortRangeExceptionRule(string direction, string protocol, NET_FW_PROFILE_TYPE2_ fwProfile) { string ruleName = string.Format( CultureInfo.InvariantCulture, FabricNodeFirewallRules.DynamicPortRangeExceptionTemplate, this.NodeName, protocol, GetProfileFriendlyName(fwProfile), direction); NET_FW_IP_PROTOCOL_ fwProtocol = protocol == FabricNodeFirewallRules.ProtocolTcp ? NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP : NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP; NET_FW_RULE_DIRECTION_ fwDirection = direction == FabricNodeFirewallRules.inDirection ? NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN : NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT; return(GetRule(ruleName, null, this.DynamicPorts, fwProtocol, fwDirection, fwProfile)); }
/// <summary> /// /// </summary> /// <param name="direction"></param> /// <returns></returns> private static string GetDirection(NET_FW_RULE_DIRECTION_ direction) { switch (direction) { case NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN: return(Resources.FW_DIR_IN); case NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT: return(Resources.FW_DIR_OUT); case NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_MAX: return(Resources.FW_DIR_BOTH); default: LogHelper.Warning("Unknown direction type: " + direction.ToString()); return("?"); } }
public static bool CreateRule(NET_FW_RULE_DIRECTION_ richting, string name, string description, int profileValue, bool enabled, NET_FW_ACTION_ typeOfRule, string application, string localAdresses, string remoteAdresses, string localPorts, string remotePorts, int protocolValue) { INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); INetFwRule firewallRule = firewallPolicy.Rules.OfType <INetFwRule>().Where(x => x.Name == name).FirstOrDefault(); if (firewallRule != null) { firewallPolicy.Rules.Remove(firewallRule.Name); } try { firewallRule = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")); firewallRule.Name = name; firewallPolicy.Rules.Add(firewallRule); firewallRule.Description = description; firewallRule.Grouping = "testomgeving .Net"; firewallRule.Profiles = profileValue; firewallRule.Protocol = protocolValue; // moet gezet worden vooraleer de poorten gezet worden firewallRule.Direction = richting; firewallRule.Action = typeOfRule; firewallRule.ApplicationName = application; firewallRule.LocalAddresses = localAdresses; if (localPorts != "") { firewallRule.LocalPorts = localPorts; } firewallRule.RemoteAddresses = remoteAdresses; if (remotePorts != "") { firewallRule.RemotePorts = remotePorts; } firewallRule.Enabled = enabled; } catch (Exception fout) { return(false); } return(true); }
public void ______________________() { ______ = __________________(t.Cells[0].Value); ____________ = (string)t.Cells[1].Value; _______________ = _________________(t.Cells[2].Value); ______________ = (string)t.Cells[3].Value; ________________ = __________________(t.Cells[4].Value); var IN = __________________(t.Cells[5].Value); var OUT = __________________(t.Cells[6].Value); __________ = IN ? NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN : 0; __________ = (OUT ? NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT : 0) | __________; var __ = t.Cells[7].Value as string; _ = __ == null ? ______________________________________._ : (__ == "Remove" ? ______________________________________.__ : ______________________________________._); }
public int AddRule(String name, String Description, String RemoteAdresses, String LocalPort, NET_FW_ACTION_ Action, NET_FW_RULE_DIRECTION_ Direction, NET_FW_IP_PROTOCOL_ Protocole, String ApplicationName = "FirewallMan") { Type Policy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2", false); INetFwPolicy2 FwPolicy = (INetFwPolicy2)Activator.CreateInstance(Policy2); INetFwRules rules = FwPolicy.Rules; //Delete if exist to avoid deplicated rules DeleteRule(name); Type RuleType = Type.GetTypeFromProgID("HNetCfg.FWRule"); INetFwRule rule = (INetFwRule)Activator.CreateInstance(RuleType); rule.Name = name; rule.Description = Description; rule.Protocol = (int)Protocole; if (LocalPort != "*") { rule.LocalPorts = LocalPort; } rule.RemoteAddresses = RemoteAdresses; rule.Action = Action; rule.Direction = Direction; rule.ApplicationName = ApplicationName; rule.Grouping = "FirewallManager"; rule.Enabled = true; try { rules.Add(rule); return(0); } catch (Exception ex) { throw (ex); } }
/* Returns direction name */ //Todo: What is Max? static public String getDirectionName(NET_FW_RULE_DIRECTION_ dirEnum) { String dirName = ""; switch (dirEnum) { case NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN: dirName = "In"; break; case NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT: dirName = "Out"; break; case NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_MAX: dirName = "Max"; break; default: dirName = "Unknown"; break; } return(dirName); }
private void CreateRuleOut(String RuleName, String Desc, NET_FW_IP_PROTOCOL_ Protocol, String RemotePorts, NET_FW_RULE_DIRECTION_ Direction, INetFwPolicy2 fwPolicy2) { Type typeFWRule = Type.GetTypeFromProgID("HNetCfg.FwRule", false); INetFwRule newRule = (INetFwRule)Activator.CreateInstance(typeFWRule); newRule.Name = RuleName; newRule.Description = Desc; newRule.Protocol = (int)Protocol; newRule.RemotePorts = RemotePorts; newRule.RemoteAddresses = txtRemoteAddress.Text.Replace(" ", "").Replace("\r\n", ","); newRule.Direction = Direction; newRule.Enabled = true; newRule.Grouping = "Elasticsearch"; //newRule.Profiles = fwPolicy2.CurrentProfileTypes; newRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW; fwPolicy2.Rules.Add(newRule); }
private static void AbrirPorta(INetFwPolicy2 firewallPolicy, string porta, int protocolo, NET_FW_RULE_DIRECTION_ direcao, string nome) { INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")); firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW; firewallRule.Description = "Used to work with Microsoft App Services."; firewallRule.Direction = direcao; firewallRule.Enabled = true; firewallRule.InterfaceTypes = "All"; firewallRule.Protocol = protocolo; firewallRule.LocalPorts = porta; firewallRule.RemotePorts = porta; firewallRule.Name = nome; firewallPolicy.Rules.Remove(firewallRule.Name); firewallPolicy.Rules.Add(firewallRule); }
private FirewallRule GetRule(string ruleName, string applicationPath, string ports, NET_FW_IP_PROTOCOL_ protocol, NET_FW_RULE_DIRECTION_ direction, NET_FW_PROFILE_TYPE2_ profile) { FirewallRule rule = new FirewallRule() { Name = ruleName, Ports = ports, Protocol = protocol, Direction = direction, ApplicationPath = applicationPath, Grouping = FabricNodeFirewallRules.WindowsFabricGrouping, Profile = profile }; return(rule); }
public TestFirewallRule(string name, string ports, NET_FW_IP_PROTOCOL_ protocol, NET_FW_RULE_DIRECTION_ direction, string appPath, string grouping) { this.Name = name; this.Ports = ports; this.Protocol = protocol; this.Direction = direction; this.ApplicationPath = appPath; this.Grouping = grouping; }
private void CreateRuleOut(String RuleName, String Desc, NET_FW_IP_PROTOCOL_ Protocol, String RemotePorts, NET_FW_RULE_DIRECTION_ Direction, INetFwPolicy2 fwPolicy2) { Type typeFWRule = Type.GetTypeFromProgID("HNetCfg.FwRule", false); INetFwRule newRule = (INetFwRule)Activator.CreateInstance(typeFWRule); newRule.Name = RuleName; newRule.Description = Desc; newRule.Protocol = (int)Protocol; newRule.RemotePorts = RemotePorts; newRule.RemoteAddresses = txtRemoteAddress.Text.Replace(" ", "").Replace("\r\n", ","); newRule.Direction = Direction; newRule.Enabled = true; newRule.Grouping = "Elasticsearch"; //newRule.Profiles = fwPolicy2.CurrentProfileTypes; newRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW; fwPolicy2.Rules.Add(newRule); }
public string fwApplicationRule(string ruleName, string filePath, NET_FW_ACTION_ fwAction, NET_FW_RULE_DIRECTION_ fwDirection, bool isAddRule) { if (isAddRule) { if (isApplicationRuleExists(filePath, fwAction, true)) { return("Exclude"); } if (isApplicationRuleExists(filePath, fwAction)) { return(fwAction.ToString().Replace("NET_FW_ACTION_", string.Empty)); } } try { INetFwRule fwRule = (INetFwRule)Activator.CreateInstance( Type.GetTypeFromProgID("HNetCfg.FWRule")); fwRule.Action = fwAction; fwRule.Enabled = true; fwRule.InterfaceTypes = "All"; fwRule.Name = ruleName.ToLower(); fwRule.ApplicationName = filePath.ToLower(); INetFwPolicy2 fwPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); fwRule.Direction = fwDirection; if (isAddRule) { fwPolicy.Rules.Add(fwRule); return(fwRule.Action.ToString().Replace("NET_FW_ACTION_", string.Empty)); } else { fwPolicy.Rules.Remove(fwRule.Name); return("success"); } } catch (Exception ex) { return(ex.Message); } }
public string _______(NET_FW_RULE_DIRECTION_ _, string ___) { return(_ + ______.___ + ___); }