Ejemplo n.º 1
0
 /// <summary>
 ///     Attempts to get the list of all registered third party firewalls.
 /// </summary>
 /// <param name="collection">Outputs the collection of third party firewalls, if successful.</param>
 /// <returns>Returns true if successful, otherwise false.</returns>
 public static bool TryGetRegisteredProducts(out IFirewallProductsCollection collection)
 {
     try
     {
         collection = RegisteredProducts;
         return(true);
     }
     catch
     {
         collection = null;
         return(false);
     }
 }
Ejemplo n.º 2
0
        private static void SetHttpSysFireWallInboundRule(string portNumberString)
        {
            try                                                                                                             // Make sure that we still try and set the firewall rules even if we bomb out trying to get information on the firewall configuration
            {
                TL.LogMessage("QueryFireWall", string.Format("Firewall version: {0}", FirewallManager.Version.ToString())); // Log the firewall version in use
                foreach (IFirewallProfile profile in FirewallManager.Instance.Profiles)
                {
                    TL.LogMessage("QueryFireWall", string.Format("Found current firewall profile {0}, enabled: {1}", profile.Type.ToString(), profile.IsActive));
                }

                COMTypeResolver             cOMTypeResolver     = new COMTypeResolver();
                IFirewallProductsCollection thirdPartyFirewalls = FirewallManager.GetRegisteredProducts(cOMTypeResolver);
                TL.LogMessage("QueryFireWall", string.Format("number of third party firewalls: {0}", thirdPartyFirewalls.Count));
                foreach (FirewallProduct firewall in thirdPartyFirewalls)
                {
                    TL.LogMessage("QueryFireWall", $"Found third party firewall: {firewall.Name} - {firewall.FriendlyName}");
                    //foreach (IFirewallProfile profile in firewall.Profiles)
                    //{
                    //    TL.LogMessage("QueryFireWall", string.Format("Found third party firewall profile {0}, enabled: {1}", profile.Type.ToString(), profile.IsActive));
                    //}
                }
            }
            catch (Exception ex)
            {
                TL.LogMessageCrLf("QueryFireWall", "Exception: " + ex.ToString());
            }
            TL.BlankLine();

            try
            {
                if ((new WindowsPrincipal(WindowsIdentity.GetCurrent())).IsInRole(WindowsBuiltInRole.Administrator)) // Application is being run with Administrator privilege so go ahead and set the firewall rules
                {
                    TL.LogMessage("SetHttpSysFireWallRule", $"Supplied HTTP.SYS port: {portNumberString}");

                    if (ushort.TryParse(portNumberString, out ushort portNumber)) // Make sure the supplied port number is a valid value before processing it
                    {
                        // Clear up redundant firewall rules left over from previous versions (ASCOM Remote Server - Inbound and Outbound)
                        IEnumerable <IFirewallRule> queryRedundant     = FirewallManager.Instance.Rules.Where(ruleName => ruleName.Name.ToUpperInvariant().StartsWith(REMOTE_SERVER_RULE_NAME_BASE.ToUpperInvariant()));
                        List <IFirewallRule>        queryRedundantCopy = queryRedundant.ToList();
                        foreach (IFirewallRule existingRule in queryRedundantCopy)
                        {
                            TL.LogMessage("SetHttpSysFireWallRule", string.Format("Found redundant rule: {0}", existingRule.Name));
                            FirewallManager.Instance.Rules.Remove(existingRule); // Delete the rule
                            TL.LogMessage("SetHttpSysFireWallRule", string.Format("Deleted redundant rule: {0}", existingRule.Name));
                        }

                        // Check whether the specified file exists and if so delete it
                        IEnumerable <IFirewallRule> query     = FirewallManager.Instance.Rules.Where(ruleName => ruleName.Name.ToUpperInvariant().Equals(HTTP_DOT_SYS_INBOUND_RULE_NAME.ToUpperInvariant()));
                        List <IFirewallRule>        queryCopy = query.ToList();
                        foreach (IFirewallRule existingRule in queryCopy)
                        {
                            TL.LogMessage("SetHttpSysFireWallRule", string.Format("Found rule: {0}", existingRule.Name));
                            FirewallManager.Instance.Rules.Remove(existingRule); // Delete the rule
                            TL.LogMessage("SetHttpSysFireWallRule", string.Format("Deleted rule: {0}", existingRule.Name));
                        }

                        SetHttpRule(FirewallProfiles.Private, portNumber);
                        SetHttpRule(FirewallProfiles.Public, portNumber);
                        SetHttpRule(FirewallProfiles.Domain, portNumber);
                    }
                    else
                    {
                        TL.LogMessage("SetHttpSysFireWallRule", $"Supplied port number {portNumberString} is not valid so can't set permission for HTTP.SYS");
                        Console.WriteLine($"Supplied port number: \"{portNumberString}\" is not valid so can't set permission for HTTP.SYS");
                    }
                }
                else
                {
                    TL.LogMessage("SetHttpSysFireWallRule", "Not running as Administrator so unable to set firewall rules.");
                    Console.WriteLine("Not running as Administrator so unable to set firewall rules.");
                }
                TL.BlankLine();
            }
            catch (Exception ex)
            {
                TL.LogMessageCrLf("SetHttpSysFireWallRule", "Exception: " + ex.ToString());
                Console.WriteLine("SetHttpSysFireWallRule threw an exception: " + ex.Message);
            }
        }
Ejemplo n.º 3
0
        private static void SetLocalServerFireWallOutboundRule(string applicationPath)
        {
            try                                                                                                             // Make sure that we still try and set the firewall rules even if we bomb out trying to get information on the firewall configuration
            {
                TL.LogMessage("QueryFireWall", string.Format("Firewall version: {0}", FirewallManager.Version.ToString())); // Log the firewall version in use
                foreach (IFirewallProfile profile in FirewallManager.Instance.Profiles)
                {
                    TL.LogMessage("QueryFireWall", string.Format("Found current firewall profile {0}, enabled: {1}", profile.Type.ToString(), profile.IsActive));
                }

                COMTypeResolver             cOMTypeResolver     = new COMTypeResolver();
                IFirewallProductsCollection thirdPartyFirewalls = FirewallManager.GetRegisteredProducts(cOMTypeResolver);
                TL.LogMessage("QueryFireWall", string.Format("number of third party firewalls: {0}", thirdPartyFirewalls.Count));
                foreach (FirewallProduct firewall in thirdPartyFirewalls)
                {
                    TL.LogMessage("QueryFireWall", $"Found third party firewall: {firewall.Name} - {firewall.FriendlyName}");
                    //foreach (IFirewallProfile profile in firewall.)
                    //{
                    //    TL.LogMessage("QueryFireWall", string.Format("Found third party firewall profile {0}, enabled: {1}", profile.Type.ToString(), profile.IsActive));
                    //}
                }
            }
            catch (Exception ex)
            {
                TL.LogMessageCrLf("QueryFireWall", "Exception: " + ex.ToString());
            }
            TL.BlankLine();

            try
            {
                if ((new WindowsPrincipal(WindowsIdentity.GetCurrent())).IsInRole(WindowsBuiltInRole.Administrator)) // Application is being run with Administrator privilege so go ahead and set the firewall rules
                {
                    // Check whether the specified file exists
                    if (File.Exists(applicationPath)) // The file does exist so process it
                    {
                        string applicationPathFull = Path.GetFullPath(applicationPath);
                        TL.LogMessage("SetFireWallOutboundRule", string.Format("Supplied path: {0}, full path: {1}", applicationPath, applicationPathFull));

                        // Now clear up previous instances of this rule
                        IEnumerable <IFirewallRule> query     = FirewallManager.Instance.Rules.Where(ruleName => ruleName.Name.ToUpperInvariant().StartsWith(LOCAL_SERVER_OUTBOUND_RULE_NAME.ToUpperInvariant()));
                        List <IFirewallRule>        queryCopy = query.ToList();
                        foreach (IFirewallRule existingRule in queryCopy)
                        {
                            TL.LogMessage("SetFireWallOutboundRule", string.Format("Found rule: {0}", existingRule.Name));
                            FirewallManager.Instance.Rules.Remove(existingRule); // Delete the rule
                            TL.LogMessage("SetFireWallOutboundRule", string.Format("Deleted rule: {0}", existingRule.Name));
                        }

                        IFirewallRule rule = FirewallManager.Instance.CreateApplicationRule(FirewallManager.Instance.GetProfile(FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public).Type, LOCAL_SERVER_OUTBOUND_RULE_NAME, FirewallAction.Allow, applicationPathFull);
                        rule.Direction = FirewallDirection.Outbound;

                        // Add the group name to the outbound rule
                        if (rule is FirewallWASRule) //Rules.StandardRule)
                        {
                            TL.LogMessage("SetHttpSysFireWallRule", "Firewall rule is a standard rule");
                            ((FirewallWASRule)rule).Grouping = GROUP_NAME;
                            TL.LogMessage("SetHttpSysFireWallRule", $"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).Grouping = GROUP_NAME;
                            TL.LogMessage("SetHttpSysFireWallRule", $"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).Grouping = GROUP_NAME;
                            TL.LogMessage("SetHttpSysFireWallRule", $"Group name set to: {GROUP_NAME}");
                        }
                        else
                        {
                            TL.LogMessage("SetHttpSysFireWallRule", "Firewall rule is not a WIN8 rule");
                        }

                        TL.LogMessage("SetFireWallOutboundRule", "Successfully created outbound rule");
                        FirewallManager.Instance.Rules.Add(rule);
                        TL.LogMessage("SetFireWallOutboundRule", string.Format("Successfully added outbound rule for {0}", applicationPathFull));
                    }
                    else
                    {
                        TL.LogMessage("SetFireWallOutboundRule", string.Format("The specified file does not exist: {0}", applicationPath));
                        Console.WriteLine("The specified file does not exist: {0}", applicationPath);
                    }
                }
                else
                {
                    TL.LogMessage("SetFireWallOutboundRule", "Not running as Administrator so unable to set firewall rules.");
                    Console.WriteLine("Not running as Administrator so unable to set firewall rules.");
                }
                TL.BlankLine();
            }
            catch (Exception ex)
            {
                TL.LogMessageCrLf("SetFireWallOutboundRule", "Exception: " + ex.ToString());
                Console.WriteLine("SetFireWallOutboundRule threw an exception: " + ex.Message);
            }
        }