/// <inheritdoc /> /// <summary> /// Creates a rule about an executable file (application) to be registered to a firewall profile /// </summary> /// <param name="profile">The profile that the rule belongs to</param> /// <param name="name">Name of the rule</param> /// <param name="action">Action of the rule</param> /// <param name="filename">Address of the executable file that the rule applies to</param> /// <param name="protocol">Protocol that the rule applies to</param> /// <returns>Returns the newly created rule object implementing <see cref="T:WindowsFirewallHelper.IRule" /> interface</returns> /// <exception cref="T:System.NotSupportedException">This class is not supported on this machine</exception> /// <exception cref="T:WindowsFirewallHelper.FirewallAPIv1.FirewallAPIv1NotSupportedException"> /// The asked setting is not /// supported with this class /// </exception> // ReSharper disable once TooManyArguments public IRule CreateApplicationRule( FirewallProfiles profile, string name, // ReSharper disable once FlagArgument FirewallAction action, string filename, FirewallProtocol protocol) { if (!IsSupported) { throw new NotSupportedException(); } if (!protocol.Equals(FirewallProtocol.Any)) { throw new FirewallAPIv1NotSupportedException(); } if (action != FirewallAction.Allow) { throw new FirewallAPIv1NotSupportedException(); } return(new ApplicationRule(name, filename, profile)); }
/// <summary> /// Creates a new port rule for Windows Firewall v1 /// </summary> /// <param name="name">Name of the rule</param> /// <param name="port">Port number of the rule</param> /// <param name="profiles">The profiles that this rule belongs to</param> /// <param name="typeResolver">The COM+ object resolver</param> public FirewallLegacyPortRule(string name, ushort port, FirewallProfiles profiles, COMTypeResolver typeResolver) { TypeResolver = typeResolver; if (profiles.HasFlag(FirewallProfiles.Public)) { throw new FirewallLegacyNotSupportedException( "Public profile is not supported when working with Windows Firewall Legacy." ); } UnderlyingObjects = new Dictionary <FirewallProfiles, INetFwOpenPort[]>(); foreach (var profile in Enum.GetValues(typeof(FirewallProfiles)).OfType <FirewallProfiles>()) { if (profiles.HasFlag(profile)) { UnderlyingObjects.Add( profile, new[] { typeResolver.CreateInstance <INetFwOpenPort>() } ); } } if (UnderlyingObjects.Count == 0) { throw new ArgumentException("At least one profile is required.", nameof(profiles)); } Name = name; LocalPort = port; IsEnable = true; Scope = FirewallScope.All; IsEnable = true; }
/// <summary> /// Creates a new application rule for Windows Firewall v1 /// </summary> /// <param name="name">Name of the rule</param> /// <param name="processAddress">Address of the executable file</param> /// <param name="profiles">The profile that this rule belongs to</param> public FirewallLegacyApplicationRule(string name, string processAddress, FirewallProfiles profiles) { if (profiles.HasFlag(FirewallProfiles.Public)) { throw new FirewallLegacyNotSupportedException("Public profile is not supported."); } UnderlyingObjects = new Dictionary <FirewallProfiles, INetFwAuthorizedApplication>(); foreach (var profile in Enum.GetValues(typeof(FirewallProfiles)).OfType <FirewallProfiles>()) { if (profiles.HasFlag(profile)) { UnderlyingObjects.Add( profile, ComHelper.CreateInstance <INetFwAuthorizedApplication>() ); } } if (UnderlyingObjects.Count == 0) { throw new ArgumentException("At least one profile is required.", nameof(profiles)); } Name = name; ExecutableAddress = processAddress; IsEnable = true; Scope = FirewallScope.All; IsEnable = true; }
internal FirewallLegacyProfile(FirewallLegacy firewall, FirewallProfiles profileType) { var localPolicy = firewall.UnderlyingObject.LocalPolicy; UnderlyingObject = localPolicy.GetProfileByType(GetNativeProfileType(profileType)); _firewall = firewall; }
/// <summary> /// Creates a new application rule for Windows Firewall v1 /// </summary> /// <param name="name">Name of the rule</param> /// <param name="processAddress">Address of the executable file</param> /// <param name="profiles">The profile that this rule belongs to</param> public FirewallLegacyApplicationRule(string name, string processAddress, FirewallProfiles profiles) { if (profiles.HasFlag(FirewallProfiles.Public)) { throw new FirewallLegacyNotSupportedException( "Public profile is not supported when working with Windows Firewall Legacy." ); } UnderlyingObjects = new Dictionary <FirewallProfiles, INetFwAuthorizedApplication[]>(); foreach (var profile in Enum.GetValues(typeof(FirewallProfiles)).OfType <FirewallProfiles>()) { if (profiles.HasFlag(profile)) { UnderlyingObjects.Add( profile, new[] { new INetFwAuthorizedApplication() } ); } } if (UnderlyingObjects.Count == 0) { throw new ArgumentException("At least one profile is required.", nameof(profiles)); } Name = name; ApplicationName = processAddress; IsEnable = true; Scope = FirewallScope.All; IsEnable = true; }
/// <inheritdoc /> /// <summary> /// Creates a new port rule for Windows Firewall with Advanced Security /// </summary> /// <param name="name">Name of the rule</param> /// <param name="port">Port number of the rule</param> /// <param name="action">Action that this rule defines</param> /// <param name="direction">Data direction in which this rule applies to</param> /// <param name="profiles">The profile that this rule belongs to</param> // ReSharper disable once TooManyDependencies public FirewallWASRuleWin8( string name, ushort port, FirewallAction action, FirewallDirection direction, FirewallProfiles profiles) : base(name, port, action, direction, profiles) { }
/// <summary> /// Creates a new application rule for Windows Firewall with Advanced Security /// </summary> /// <param name="name">Name of the rule</param> /// <param name="action">Action that this rule defines</param> /// <param name="direction">Data direction in which this rule applies to</param> /// <param name="profiles">The profile that this rule belongs to</param> public StandardRuleWin7(string name, FirewallAction action, FirewallDirection direction, FirewallProfiles profiles) : base(name, action, direction, profiles) { if (UnderlyingObjectV2 == null) { throw new FirewallAPIv2NotSupportedException(); } }
/// <inheritdoc /> /// <summary> /// Creates a rule about an executable file (application) to be registered to a firewall profile /// </summary> /// <param name="profiles">The profile or profiles that the rule belongs to</param> /// <param name="name">Name of the rule</param> /// <param name="action">Action of the rule</param> /// <param name="filename">Address of the executable file that the rule applies to</param> /// <returns>Returns the newly created rule object implementing <see cref="IRule" /> interface</returns> /// <exception cref="NotSupportedException">This class is not supported on this machine</exception> // ReSharper disable once TooManyArguments public IRule CreateApplicationRule( FirewallProfiles profiles, string name, FirewallAction action, string filename) { return(CreateApplicationRule(profiles, name, action, filename, FirewallProtocol.Any)); }
/// <summary> /// Creates a new general rule for Windows Firewall with Advanced Security /// </summary> /// <param name="name">Name of the rule</param> /// <param name="action">Action that this rule defines</param> /// <param name="direction">Data direction in which this rule applies to</param> /// <param name="profiles">The profile that this rule belongs to</param> // ReSharper disable once TooManyDependencies public FirewallWASRuleWin7( string name, FirewallAction action, FirewallDirection direction, FirewallProfiles profiles) : base(name, action, direction, profiles) { }
/// <summary> /// Creates a new application rule for Windows Firewall with Advanced Security /// </summary> /// <param name="name">Name of the rule</param> /// <param name="filename">Address of the executable file</param> /// <param name="action">Action that this rule defines</param> /// <param name="direction">Data direction in which this rule applies to</param> /// <param name="profiles">The profile that this rule belongs to</param> // ReSharper disable once TooManyDependencies public FirewallWASRule( string name, string filename, FirewallAction action, FirewallDirection direction, FirewallProfiles profiles) : this(name, action, direction, profiles) { ApplicationName = filename; }
private static void SetHttpRule(FirewallProfiles firewallProfile, ushort portNumber) { IFirewallRule rule = FirewallManager.Instance.CreateApplicationRule( FirewallManager.Instance.GetProfile(firewallProfile).Type, HTTP_DOT_SYS_INBOUND_RULE_NAME, FirewallAction.Allow, "SYSTEM"); rule.Direction = FirewallDirection.Inbound; rule.Protocol = FirewallProtocol.TCP; rule.LocalPorts = new ushort[1] { portNumber }; // Create an array containing the port number TL.LogMessage("SetHttpSysFireWallRule", "Successfully created inbound rule"); // Add edge traversal permission to the inbound rule so that the rule will apply to packets delivered in encapsulated transmission formats such as VPNs if (rule is FirewallWASRule) { TL.LogMessage("SetHttpSysFireWallRule", "Firewall rule is a standard rule"); ((FirewallWASRule)rule).EdgeTraversal = true; ((FirewallWASRule)rule).Grouping = GROUP_NAME; TL.LogMessage("SetHttpSysFireWallRule", $"Edge traversal set {true}, Group name set to: {GROUP_NAME}"); } else { TL.LogMessage("SetHttpSysFireWallRule", "Firewall rule is not a standard rule"); } if (rule is FirewallWASRuleWin7) { TL.LogMessage("SetHttpSysFireWallRule", "Firewall rule is a WIN7 rule"); ((FirewallWASRuleWin7)rule).EdgeTraversalOptions = EdgeTraversalAction.Allow; ((FirewallWASRuleWin7)rule).Grouping = GROUP_NAME; TL.LogMessage("SetHttpSysFireWallRule", $"Edge traversal set {true}, Group name set to: {GROUP_NAME}"); } else { TL.LogMessage("SetHttpSysFireWallRule", "Firewall rule is not a WIN7 rule"); } if (rule is FirewallWASRuleWin8) { TL.LogMessage("SetHttpSysFireWallRule", "Firewall rule is a WIN8 rule"); ((FirewallWASRuleWin8)rule).EdgeTraversalOptions = EdgeTraversalAction.Allow; ((FirewallWASRuleWin8)rule).Grouping = GROUP_NAME; TL.LogMessage("SetHttpSysFireWallRule", $"Edge traversal set {true}, Group name set to: {GROUP_NAME}"); } else { TL.LogMessage("SetHttpSysFireWallRule", "Firewall rule is not a WIN8 rule"); } TL.LogMessage("SetHttpSysFireWallRule", "Successfully created inbound firewall rule"); FirewallManager.Instance.Rules.Add(rule); TL.LogMessage("SetHttpSysFireWallRule", $"Successfully added inbound rule for HTTP.SYS permitting listening on port {portNumber} for {firewallProfile}"); TL.BlankLine(); }
/// <summary> /// Creates a new general rule for Windows Firewall with Advanced Security /// </summary> /// <param name="name">Name of the rule</param> /// <param name="action">Action that this rule defines</param> /// <param name="direction">Data direction in which this rule applies to</param> /// <param name="profiles">The profile that this rule belongs to</param> // ReSharper disable once TooManyDependencies public FirewallWASRule( string name, FirewallAction action, FirewallDirection direction, FirewallProfiles profiles ) : this(name, action, direction, profiles, new COMTypeResolver()) { }
/// <summary> /// Creates a new application rule for Windows Firewall with Advanced Security /// </summary> /// <param name="name">Name of the rule</param> /// <param name="action">Action that this rule defines</param> /// <param name="direction">Data direction in which this rule applies to</param> /// <param name="profiles">The profile that this rule belongs to</param> public StandardRule(string name, FirewallAction action, FirewallDirection direction, FirewallProfiles profiles) { UnderlyingObject = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID(@"HNetCfg.FWRule")); Name = name; Action = action; Direction = direction; IsEnable = true; Profiles = profiles; }
/// <summary> /// Creates a new port rule for Windows Firewall with Advanced Security /// </summary> /// <param name="name">Name of the rule</param> /// <param name="port">Port number of the rule</param> /// <param name="action">Action that this rule defines</param> /// <param name="direction">Data direction in which this rule applies to</param> /// <param name="profiles">The profile that this rule belongs to</param> /// <param name="typeResolver">The COM+ object resolver</param> // ReSharper disable once TooManyDependencies public FirewallWASRuleWin8( string name, ushort port, FirewallAction action, FirewallDirection direction, FirewallProfiles profiles, COMTypeResolver typeResolver ) : base(name, port, action, direction, profiles, typeResolver) { }
/// <summary> /// Creates a new port rule for Windows Firewall with Advanced Security /// </summary> /// <param name="name">Name of the rule</param> /// <param name="port">Port number of the rule</param> /// <param name="action">Action that this rule defines</param> /// <param name="direction">Data direction in which this rule applies to</param> /// <param name="profiles">The profile that this rule belongs to</param> // ReSharper disable once TooManyDependencies public FirewallWASRule( string name, ushort port, FirewallAction action, FirewallDirection direction, FirewallProfiles profiles) : this(name, action, direction, profiles) { Protocol = FirewallProtocol.TCP; LocalPorts = new[] { port }; }
/// <summary> /// Creates a new application rule for Windows Firewall with Advanced Security /// </summary> /// <param name="name">Name of the rule</param> /// <param name="filename">Address of the executable file</param> /// <param name="action">Action that this rule defines</param> /// <param name="direction">Data direction in which this rule applies to</param> /// <param name="profiles">The profile that this rule belongs to</param> /// <param name="typeResolver">The COM+ object resolver</param> // ReSharper disable once TooManyDependencies public FirewallWASRuleWin7( string name, string filename, FirewallAction action, FirewallDirection direction, FirewallProfiles profiles, COMTypeResolver typeResolver ) : base(name, filename, action, direction, profiles, typeResolver) { }
/// <summary> /// Creates a new port rule for Windows Firewall with Advanced Security /// </summary> /// <param name="name">Name of the rule</param> /// <param name="port">Port number of the rule</param> /// <param name="action">Action that this rule defines</param> /// <param name="direction">Data direction in which this rule applies to</param> /// <param name="profiles">The profile that this rule belongs to</param> public StandardRuleWin8(string name, ushort port, FirewallAction action, FirewallDirection direction, FirewallProfiles profiles) : base(name, port, action, direction, profiles) { UnderlyingObjectV3 = UnderlyingObject as INetFwRule3; if (UnderlyingObjectV3 == null) { throw new FirewallAPIv2NotSupportedException(); } }
/// <summary> /// Creates a new port rule for Windows Firewall v1 /// </summary> /// <param name="name">Name of the rule</param> /// <param name="port">Port number of the rule</param> /// <param name="profile">The profile that this rule belongs to</param> public PortRule(string name, ushort port, FirewallProfiles profile) { UnderlyingObject = (INetFwOpenPort)Activator.CreateInstance(Type.GetTypeFromProgID(@"HNetCfg.FwOpenPort")); Name = name; LocalPorts = new[] { port }; IsEnable = true; Scope = FirewallScope.All; Profiles = profile; IsEnable = true; }
/// <summary> /// Creates a new application rule for Windows Firewall v1 /// </summary> /// <param name="name">Name of the rule</param> /// <param name="processAddress">Address of the executable file</param> /// <param name="profile">The profile that this rule belongs to</param> public ApplicationRule(string name, string processAddress, FirewallProfiles profile) { UnderlyingObject = (INetFwAuthorizedApplication)Activator.CreateInstance( Type.GetTypeFromProgID(@"HNetCfg.FwAuthorizedApplication")); Name = name; ExecutableAddress = processAddress; IsEnable = true; Scope = FirewallScope.All; Profiles = profile; IsEnable = true; }
/// <summary> /// Creates a new application rule for Windows Firewall with Advanced Security /// </summary> /// <param name="name">Name of the rule</param> /// <param name="filename">Address of the executable file</param> /// <param name="action">Action that this rule defines</param> /// <param name="direction">Data direction in which this rule applies to</param> /// <param name="profiles">The profile that this rule belongs to</param> /// <param name="typeResolver">The COM+ object resolver</param> // ReSharper disable once TooManyDependencies public FirewallWASRule( string name, string filename, FirewallAction action, FirewallDirection direction, FirewallProfiles profiles, COMTypeResolver typeResolver ) : this(name, action, direction, profiles, typeResolver) { ApplicationName = filename; }
/// <summary> /// Creates a rule about an executable file (application) to be registered to a firewall profile /// </summary> /// <param name="profiles">The profile or profiles that the rule belongs to</param> /// <param name="name">Name of the rule</param> /// <param name="action">Action of the rule</param> /// <param name="filename">Address of the executable file that the rule applies to</param> /// <param name="protocol">Protocol that the rule applies to</param> /// <returns>Returns the newly created rule object implementing <see cref="IRule" /> interface</returns> /// <exception cref="NotSupportedException">This class is not supported on this machine</exception> public IRule CreateApplicationRule(FirewallProfiles profiles, string name, FirewallAction action, string filename, FirewallProtocol protocol) { if (!IsSupported) { throw new NotSupportedException(); } return(new StandardRule(name, filename, action, FirewallDirection.Inbound, profiles) { Protocol = protocol }); }
/// <summary> /// Creates a rule about a port to be registered to a firewall profile /// </summary> /// <param name="profiles">The profile or profiles that the rule belongs to</param> /// <param name="name">Name of the rule</param> /// <param name="action">Action of the rule</param> /// <param name="portNumber">Port number that the rule applies to</param> /// <param name="protocol">Protocol that the rule applies to</param> /// <returns>Returns the newly created rule object implementing <see cref="IRule" /> interface</returns> /// <exception cref="NotSupportedException">This class is not supported on this machine</exception> public IRule CreatePortRule(FirewallProfiles profiles, string name, FirewallAction action, ushort portNumber, FirewallProtocol protocol) { if (!IsSupported) { throw new NotSupportedException(); } return(new StandardRule(name, portNumber, action, FirewallDirection.Inbound, profiles) { Protocol = protocol }); }
/// <summary> /// Creates a new general rule for Windows Firewall with Advanced Security /// </summary> /// <param name="name">Name of the rule</param> /// <param name="action">Action that this rule defines</param> /// <param name="direction">Data direction in which this rule applies to</param> /// <param name="profiles">The profile that this rule belongs to</param> // ReSharper disable once TooManyDependencies public FirewallWASRule( string name, FirewallAction action, FirewallDirection direction, FirewallProfiles profiles) : this(ComHelper.CreateInstance <INetFwRule>()) { Name = name; Action = action; Direction = direction; IsEnable = true; Profiles = profiles; }
/// <summary> /// Creates a new general rule for Windows Firewall with Advanced Security /// </summary> /// <param name="name">Name of the rule</param> /// <param name="action">Action that this rule defines</param> /// <param name="direction">Data direction in which this rule applies to</param> /// <param name="profiles">The profile that this rule belongs to</param> /// <param name="typeResolver">The COM+ object resolver</param> // ReSharper disable once TooManyDependencies public FirewallWASRule( string name, FirewallAction action, FirewallDirection direction, FirewallProfiles profiles, COMTypeResolver typeResolver ) : this(typeResolver.CreateInstance <INetFwRule>()) { Name = name; Action = action; Direction = direction; IsEnable = true; Profiles = profiles; }
/// <summary> /// Returns a specific firewall profile /// </summary> /// <param name="profile">Requested firewall profile</param> /// <returns>Firewall profile object implementing <see cref="IProfile" /> interface</returns> /// <exception cref="NotSupportedException">This class is not supported on this machine</exception> /// <exception cref="FirewallAPIv2NotSupportedException">The asked profile is not supported with this class</exception> public IProfile GetProfile(FirewallProfiles profile) { if (!IsSupported) { throw new NotSupportedException(); } foreach (var p in Profiles) { if (p.Type == profile) { return(p); } } throw new FirewallAPIv2NotSupportedException(); }
/// <summary> /// Creates a rule about a port to be registered to a firewall profile /// </summary> /// <param name="profile">The profile that the rule belongs to</param> /// <param name="name">Name of the rule</param> /// <param name="action">Action of the rule</param> /// <param name="portNumber">Port number that the rule applies to</param> /// <param name="protocol">Protocol that the rule applies to</param> /// <returns>Returns the newly created rule object implementing <see cref="IRule" /> interface</returns> /// <exception cref="NotSupportedException">This class is not supported on this machine</exception> /// <exception cref="FirewallAPIv1NotSupportedException">The asked setting is not supported with this class</exception> public IRule CreatePortRule(FirewallProfiles profile, string name, FirewallAction action, ushort portNumber, FirewallProtocol protocol) { if (!IsSupported) { throw new NotSupportedException(); } if (action != FirewallAction.Allow) { throw new FirewallAPIv1NotSupportedException(); } return(new PortRule(name, portNumber, profile) { Protocol = protocol }); }
/// <summary> /// Creates a rule about a port to be registered to a firewall profile /// </summary> /// <param name="profiles">The profile or profiles that the rule belongs to</param> /// <param name="name">Name of the rule</param> /// <param name="action">Action of the rule</param> /// <param name="portNumber">Port number that the rule applies to</param> /// <param name="protocol">Protocol that the rule applies to</param> /// <returns>Returns the newly created rule object implementing <see cref="IRule" /> interface</returns> /// <exception cref="NotSupportedException">This class is not supported on this machine</exception> public IRule CreatePortRule(FirewallProfiles profiles, string name, FirewallAction action, ushort portNumber, FirewallProtocol protocol) { if (!IsSupported) { throw new NotSupportedException(); } if (!protocol.Equals(FirewallProtocol.TCP) && protocol.Equals(FirewallProtocol.UDP)) { throw new FirewallAPIv2InvalidProtocolException( "Invalid protocol selected; rule's protocol should be TCP or UDP."); } return(new StandardRule(name, portNumber, action, FirewallDirection.Inbound, profiles) { Protocol = protocol }); }
/// <summary> /// Creates a new port rule for Windows Firewall with Advanced Security /// </summary> /// <param name="name">Name of the rule</param> /// <param name="port">Port number of the rule</param> /// <param name="action">Action that this rule defines</param> /// <param name="direction">Data direction in which this rule applies to</param> /// <param name="profiles">The profile that this rule belongs to</param> public StandardRule(string name, ushort port, FirewallAction action, FirewallDirection direction, FirewallProfiles profiles) { UnderlyingObject = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID(@"HNetCfg.FWRule")); Name = name; Action = action; Direction = direction; Protocol = FirewallProtocol.TCP; IsEnable = true; Profiles = profiles; if (direction == FirewallDirection.Inbound) { LocalPorts = new[] { port }; } else { RemotePorts = new[] { port }; } }
private static void Main() { ConsoleWriter.Default.PrintMessage("Firewall Control"); // Информация о версии брандмауэра ConsoleWriter.Default.PrintMessage($"Firewall Version: {FirewallManager.Version}"); // Экземпляр ('пульт') управления брандмауэром var firewallInstance = FirewallManager.Instance; ConsoleWriter.Default.PrintMessage($"Type of control: {firewallInstance.Name}"); // Если версия брандмауэра неизвестная или недействительна if (FirewallManager.Version == FirewallAPIVersion.None) { ConsoleWriter.Default.PrintMessage("Press any key to exit."); Console.ReadKey(); // То выход return; } // Панель навигации // Генеральная консоль ConsoleNavigation.Default.PrintNavigation(new[] { // Консоль для профилей new ConsoleNavigationItem("Profiles", (i, item) => { // Консоль подпунктов профилей ConsoleNavigation.Default.PrintNavigation( // Объект кладем в массив firewallInstance.Profiles.ToArray(), (i1, profile) => { // Выводим данные о конкретном профиле ConsoleWriter.Default.WriteObject(profile); ConsoleWriter.Default.PrintMessage("Press any key to get one step back."); // Возврат Console.ReadKey(); }, "Select a profile to view its settings." ); }), // Консоль для правил new ConsoleNavigationItem("Rules", (i, item) => { // Консоль всех правил ConsoleNavigation.Default.PrintNavigation( // Сортируем правила в алфавитном порядке // Каждое правило (объект) кладем в массив firewallInstance.Rules.OrderBy((rule) => rule.FriendlyName).ToArray(), (i1, rule) => { // Вывод правила ConsoleWriter.Default.WriteObject(rule); ConsoleWriter.Default.PrintMessage("Press any key to get one step back."); // Возврат Console.ReadKey(); }, "Select a rule to view its settings." ); }), new ConsoleNavigationItem("Create rule", (i, item) => { // Задание правилу профиля string profile = ConsoleWriter.Default.PrintQuestion("Enter type of profile"); if (profile.ToUpper() == "PUBLIC" || profile.ToUpper() == "DOMAIN" || profile.ToUpper() == "PRIVATE") { FirewallProfiles firewallProfile = FirewallProfiles.Public; if (profile.ToUpper() == "PUBLIC") { firewallProfile = FirewallProfiles.Public; } if (profile.ToUpper() == "DOMAIN") { firewallProfile = FirewallProfiles.Domain; } if (profile.ToUpper() == "PRIVATE") { firewallProfile = FirewallProfiles.Private; } // Имя правила string name = ConsoleWriter.Default.PrintQuestion("Enter name of rule"); if (name != "") { // Тип доступа FirewallAction firewallaction = FirewallAction.Block; string action = ConsoleWriter.Default.PrintQuestion("Enter type of acces"); if (action.ToUpper() == "ALLOW" || action.ToUpper() == "BLOCK") { if (action.ToUpper() == "ALLOW") { firewallaction = FirewallAction.Allow; } if (action.ToUpper() == "BLOCK") { firewallaction = FirewallAction.Block; } string fullPath = ConsoleWriter.Default.PrintQuestion("Enter full path of exe file"); var rule = firewallInstance.CreateApplicationRule( firewallProfile, @$ "{name}", firewallaction, @$ "{fullPath}" ); rule.Direction = FirewallDirection.Outbound; firewallInstance.Rules.Add(rule); ConsoleWriter.Default.PrintSuccess("Rule successfully aded"); } else { ConsoleWriter.Default.PrintError("This type of acces is invalid"); } } else { ConsoleWriter.Default.PrintError("This name of rule is invalid"); } } else { ConsoleWriter.Default.PrintError("This profile name is invalid"); } ConsoleWriter.Default.PrintMessage("Press any key to get one step back."); // Вsозврат Console.ReadKey(); }), new ConsoleNavigationItem("Create port rule", (i, item) => { string name = ConsoleWriter.Default.PrintQuestion("Enter name of port rule"); if (name != "") { string action = ConsoleWriter.Default.PrintQuestion("Enter type of access"); FirewallAction firewallAction = FirewallAction.Block; if (action.ToUpper() == "ALLOW" || action.ToUpper() == "BLOCK") { if (action.ToUpper() == "ALLOW") { firewallAction = FirewallAction.Allow; } if (action.ToUpper() == "BLOCK") { firewallAction = FirewallAction.Block; } string port = ConsoleWriter.Default.PrintQuestion("Enter port"); ushort finallyPort = Convert.ToUInt16(port); string protocol = ConsoleWriter.Default.PrintQuestion("Enter protocol"); FirewallProtocol firewallProtocol = FirewallProtocol.TCP; if (protocol.ToUpper() == "UDP" || protocol.ToUpper() == "TCP") { if (protocol.ToUpper() == "UDP") { firewallProtocol = FirewallProtocol.UDP; } if (protocol.ToUpper() == "TCP") { firewallProtocol = FirewallProtocol.TCP; } var rule = firewallInstance.CreatePortRule( @$ "{name}", firewallAction, finallyPort, firewallProtocol ); firewallInstance.Rules.Add(rule); ConsoleWriter.Default.PrintSuccess("Protocol rule successfully aded"); } else { ConsoleWriter.Default.PrintError("This protocol is invalid"); } } else { ConsoleWriter.Default.PrintError("This type of acces is invalid"); } } else { ConsoleWriter.Default.PrintError("This name of port rule is invalid"); } // Возврат Console.ReadKey(); }), new ConsoleNavigationItem("Delete rule", (i, item) => { string deleted = ConsoleWriter.Default.PrintQuestion("Enter name of rule"); var rule = firewallInstance.Rules.SingleOrDefault(r => r.Name == deleted); if (rule != null) { firewallInstance.Rules.Remove(rule); ConsoleWriter.Default.PrintSuccess("Rule was successfully deleted"); } else { ConsoleWriter.Default.PrintError("This name rule is invalid"); } }) }, "Select an execution path."); }
/// <summary> /// Creates a new port rule for Windows Firewall v1 /// </summary> /// <param name="name">Name of the rule</param> /// <param name="port">Port number of the rule</param> /// <param name="profiles">The profiles that this rule belongs to</param> public FirewallLegacyPortRule(string name, ushort port, FirewallProfiles profiles) : this(name, port, profiles, new COMTypeResolver()) { }