/// <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)
 {
 }
 /// <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)
 {
 }
Пример #3
0
 /// <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();
     }
 }
 /// <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 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="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;
 }
Пример #7
0
        public static void AddApplicationRule(string ruleName, string fileName, FirewallDirection direction,
                                              FirewallProtocol protocol)
        {
            if (Firewall.Instance.IsSupported)
            {
                if (StandardRuleWin8.IsSupported)
                {
                    var rule = new StandardRuleWin8(ruleName, fileName, FirewallAction.Allow,
                                                    direction,
                                                    FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public)
                    {
                        Grouping       = "Celeste Fan Project",
                        Description    = "Auto-generated rules by \"Celeste Fan Project Launcher\"",
                        InterfaceTypes = FirewallInterfaceTypes.Lan | FirewallInterfaceTypes.RemoteAccess |
                                         FirewallInterfaceTypes.Wireless,
                        Protocol = protocol
                    };

                    //if (direction == FirewallDirection.Inbound && (protocol.Equals(FirewallProtocol.TCP) || protocol.Equals(FirewallProtocol.UDP)))
                    //    rule.EdgeTraversalOptions = EdgeTraversalAction.DefferToUser;

                    Firewall.Instance.Rules.Add(rule);
                }
                else if (StandardRuleWin7.IsSupported)
                {
                    var rule = new StandardRuleWin7(ruleName, fileName, FirewallAction.Allow,
                                                    direction,
                                                    FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public)
                    {
                        Grouping       = "Celeste Fan Project",
                        Description    = "Auto-generated rules by \"Celeste Fan Project Launcher\"",
                        InterfaceTypes = FirewallInterfaceTypes.Lan | FirewallInterfaceTypes.RemoteAccess |
                                         FirewallInterfaceTypes.Wireless,
                        Protocol = protocol
                    };

                    //if (direction == FirewallDirection.Inbound && (protocol.Equals(FirewallProtocol.TCP) || protocol.Equals(FirewallProtocol.UDP)))
                    //    rule.EdgeTraversalOptions = EdgeTraversalAction.DefferToUser;

                    Firewall.Instance.Rules.Add(rule);
                }
                else
                {
                    goto default_rule;
                }
                return;
            }
default_rule:
            var defaultRule = FirewallManager.Instance.CreateApplicationRule(
                FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public, ruleName,
                FirewallAction.Allow, fileName, protocol);

            defaultRule.Direction = direction;

            FirewallManager.Instance.Rules.Add(defaultRule);
        }
Пример #8
0
 /// <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 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)
 {
 }
Пример #10
0
 /// <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 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 FirewallWASRule(
     string name,
     string filename,
     FirewallAction action,
     FirewallDirection direction,
     FirewallProfiles profiles,
     COMTypeResolver typeResolver
     ) : this(name, action, direction, profiles, typeResolver)
 {
     ApplicationName = filename;
 }
Пример #13
0
        private static void AddDefaultPortRule(string ruleName, ushort portNumber, FirewallDirection direction,
                                               FirewallProtocol protocol)
        {
            var defaultRule = FirewallManager.Instance.CreatePortRule(
                FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public, ruleName,
                FirewallAction.Allow, portNumber, protocol);

            defaultRule.Direction = direction;

            FirewallManager.Instance.Rules.Add(defaultRule);
        }
 /// <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;
 }
Пример #15
0
        private static IRule CreateFirewallRule(string action, FirewallDirection direction, string filename)
        {
            var file = new FileInfo(filename);
            var rule = FirewallManager.Instance.CreateApplicationRule(
                FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public,
                $"{action} {file.Name}",
                action == "Allow" ? FirewallAction.Allow : FirewallAction.Block,
                filename);

            rule.Direction = direction;

            return(rule);
        }
Пример #16
0
        public static void AddPortRule(string ruleName, ushort portNumber, FirewallDirection direction,
                                       FirewallProtocol protocol)
        {
            if (Firewall.Instance.IsSupported)
            {
                if (StandardRuleWin8.IsSupported)
                {
                    var rule = new StandardRuleWin8(ruleName, portNumber, FirewallAction.Allow,
                                                    direction,
                                                    FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public)
                    {
                        Grouping       = "Celeste Fan Project",
                        Description    = "Auto-generated rules by \"Celeste Fan Project Launcher\"",
                        InterfaceTypes = FirewallInterfaceTypes.Lan | FirewallInterfaceTypes.RemoteAccess |
                                         FirewallInterfaceTypes.Wireless,
                        Protocol = protocol
                    };

                    Firewall.Instance.Rules.Add(rule);
                }
                else if (StandardRuleWin7.IsSupported)
                {
                    var rule = new StandardRuleWin7(ruleName, portNumber, FirewallAction.Allow,
                                                    direction,
                                                    FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public)
                    {
                        Grouping       = "Celeste Fan Project",
                        Description    = "Auto-generated rules by \"Celeste Fan Project Launcher\"",
                        InterfaceTypes = FirewallInterfaceTypes.Lan | FirewallInterfaceTypes.RemoteAccess |
                                         FirewallInterfaceTypes.Wireless,
                        Protocol = protocol
                    };

                    Firewall.Instance.Rules.Add(rule);
                }
                else
                {
                    goto default_rule;
                }

                return;
            }
default_rule:
            var defaultRule = FirewallManager.Instance.CreatePortRule(
                FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public, ruleName,
                FirewallAction.Allow, portNumber, protocol);

            defaultRule.Direction = direction;

            FirewallManager.Instance.Rules.Add(defaultRule);
        }
 /// <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;
 }
Пример #18
0
 /// <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 };
     }
 }
Пример #19
0
        private static void AddDefaultApplicationRule(string ruleName, string fileName, FirewallDirection direction, FirewallProtocol protocol)
        {
            var defaultRule = FirewallManager.Instance.CreateApplicationRule(
                FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public, ruleName,
                FirewallAction.Allow, fileName, protocol);

            defaultRule.Direction = direction;

            FirewallManager.Instance.Rules.Add(defaultRule);
        }
Пример #20
0
        public static void AddRule(String name, FirewallAction action, FirewallProtocol protocol, FirewallDirection direction
                                   , Int32[] localPorts, Int32[] remotePorts, String serviceName = null)
        {
            //Don't add if it already exists
            var policy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));

            if (policy.Rules.Cast <INetFwRule3>().Any(rule => rule.Name == name))
            {
                return;
            }

            //Add new rule
            var newRule = (INetFwRule3)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));

            newRule.Enabled  = true;
            newRule.Action   = (NET_FW_ACTION_)action;
            newRule.Protocol = (Int32)protocol;
            if (localPorts != null)
            {
                newRule.LocalPorts = String.Join(",", localPorts);
            }
            if (remotePorts != null)
            {
                newRule.RemotePorts = String.Join(",", remotePorts);
            }
            newRule.Direction = (NET_FW_RULE_DIRECTION_)direction;
            if (serviceName != null)
            {
                newRule.serviceName = serviceName;
            }
            newRule.Name = name;
            //newRule.Grouping = group;
            newRule.Profiles = (Int32)NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_ALL;

            policy.Rules.Add(newRule);
        }
Пример #21
0
        public static void AddApplicationRule(string nameOfApp, string localOfApp, string groupKey, string description, FirewallDirection direction, FirewallProtocol protocol, string firewallLogNote)
        {
            try
            {
                Log.Info("WINDOWS FIREWALL: Supported Firewall Found");
                var rule = new FirewallWASRuleWin8(localOfApp, FirewallAction.Allow, direction, FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public)
                {
                    ApplicationName       = localOfApp,
                    Name                  = nameOfApp,
                    Grouping              = groupKey,
                    Description           = description,
                    NetworkInterfaceTypes = NetworkInterfaceTypes.Lan | NetworkInterfaceTypes.RemoteAccess |
                                            NetworkInterfaceTypes.Wireless,
                    Protocol = protocol
                };

                if (direction == FirewallDirection.Inbound)
                {
                    rule.EdgeTraversalOptions = EdgeTraversalAction.Allow;
                }

                FirewallManager.Instance.Rules.Add(rule);
                Log.Info("WINDOWS FIREWALL: Finished Adding " + nameOfApp + " to Firewall! {" + firewallLogNote + "}");
            }
            catch (FirewallWASNotSupportedException Error)
            {
                Log.Error("WINDOWS FIREWALL: " + Error.Message);
                AddDefaultApplicationRule(nameOfApp, localOfApp, direction, protocol, firewallLogNote);
            }
        }
Пример #22
0
        public static void CheckIfRuleExists(bool removeFirewallRule, bool firstTimeRun, string nameOfApp, string localOfApp, string groupKey, string description, FirewallDirection direction, FirewallProtocol protocol, string firewallLogNote)
        {
            //Remove Firewall Rules
            if (removeFirewallRule == true && firstTimeRun == false)
            {
                RemoveRules(nameOfApp, firewallLogNote);
            }
            //Add Firewall Rules
            else if (removeFirewallRule == false && firstTimeRun == true)
            {
                AddApplicationRule(nameOfApp, localOfApp, groupKey, description, direction, protocol, firewallLogNote);
            }
            //Removes a Specific Rule from Firewall (When switching locations)
            else if (removeFirewallRule == true && firstTimeRun == true)
            {
                if (RuleExist(nameOfApp) == true)
                {
                    RemoveRules(nameOfApp, firewallLogNote);
                    Log.Info("WINDOWS FIREWALL: Found " + nameOfApp + " {" + firewallLogNote + "} In Firewall");
                }
                else if (RuleExist(nameOfApp) == false)
                {
                    AddApplicationRule(nameOfApp, localOfApp, groupKey, description, direction, protocol, firewallLogNote);
                }
            }
            else if (removeFirewallRule == false && firstTimeRun == false)
            {
                Log.Info("WINDOWS FIREWALL: Already Exlcuded " + nameOfApp + " {" + firewallLogNote + "}");
            }

            else
            {
                Log.Error("WINDOWS FIREWALL: Firewall Error - Check With Visual Studio for Error Debuging");
            }
        }
Пример #23
0
        private static void AddDefaultApplicationRule(string nameOfApp, string localOfApp, FirewallDirection direction, FirewallProtocol protocol, string firewallLogNote)
        {
            try
            {
                Log.Warning("WINDOWS FIREWALL: Falling back to 'LegacyStandard'");
                var defaultRule = FirewallManager.Instance.CreateApplicationRule(
                    FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public,
                    nameOfApp,
                    FirewallAction.Allow,
                    localOfApp, protocol);

                defaultRule.Direction = direction;

                FirewallManager.Instance.Rules.Add(defaultRule);
                Log.Warning("WINDOWS FIREWALL: Finished Adding " + nameOfApp + " to Firewall! {" + firewallLogNote + "}");
            }
            catch (FirewallWASNotSupportedException Error)
            {
                Log.Error("WINDOWS FIREWALL: " + Error.Message);
            }
        }
Пример #24
0
        private static void CheckServerFirewallRules(FirewallDirection direction)
        {
            string serverPath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), ServerLogic.SERVER_EXECUTABLE);

            AddExclusiveFirewallRule(Path.GetFileName(serverPath), serverPath, direction);
        }
Пример #25
0
 private static void CheckFirewallRules(FirewallDirection direction)
 {
     CheckClientFirewallRules(direction);
     CheckServerFirewallRules(direction);
     CheckHamachiFirewallRules(direction);
 }
Пример #26
0
 private static void CheckHamachiFirewallRules(FirewallDirection direction)
 {
Пример #27
0
        private static void CheckClientFirewallRules(FirewallDirection direction)
        {
            string clientPath = Path.Combine(LauncherLogic.Config.SubnauticaPath, GameInfo.Subnautica.ExeName);

            AddExclusiveFirewallRule(Path.GetFileName(clientPath), clientPath, direction);
        }
Пример #28
0
        public static void AddApplicationRule(string nameOfApp, string localOfApp, string groupKey, string description, FirewallDirection direction, FirewallProtocol protocol, string firewallLogNote)
        {
            if (Firewall.Instance.IsSupported)
            {
                Log.Info("WINDOWS FIREWALL: Supported Firewall Found");
                var rule = new StandardRuleWin7(localOfApp, FirewallAction.Allow, direction, FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public)
                {
                    ApplicationName = localOfApp,
                    Name            = nameOfApp,
                    Grouping        = groupKey,
                    Description     = description,
                    InterfaceTypes  = FirewallInterfaceTypes.Lan | FirewallInterfaceTypes.RemoteAccess |
                                      FirewallInterfaceTypes.Wireless,
                    Protocol = protocol
                };

                if (direction == FirewallDirection.Inbound)
                {
                    rule.EdgeTraversalOptions = EdgeTraversalAction.Allow;
                }

                Firewall.Instance.Rules.Add(rule);
                Log.Info("WINDOWS FIREWALL: Finished Adding " + nameOfApp + " to Firewall! {" + firewallLogNote + "}");
            }
            else
            {
                AddDefaultApplicationRule(nameOfApp, localOfApp, direction, protocol, firewallLogNote);
            }
        }