/// <summary>
        /// Constructor
        /// </summary>
        public IPBanConfig()
        {
            ConfigurationManager.RefreshSection("appSettings");
            ConfigurationManager.RefreshSection("configSections");
            ConfigurationManager.RefreshSection("nlog");
            ConfigurationManager.RefreshSection("ExpressionsToBlock");

            string value = ConfigurationManager.AppSettings["FailedLoginAttemptsBeforeBan"];

            failedLoginAttemptsBeforeBan = int.Parse(value, CultureInfo.InvariantCulture);

            value   = ConfigurationManager.AppSettings["BanTime"];
            banTime = TimeSpan.Parse(value, CultureInfo.InvariantCulture);

            value   = ConfigurationManager.AppSettings["BanFile"];
            banFile = value;
            if (!Path.IsPathRooted(banFile))
            {
                banFile = Path.GetFullPath(banFile);
            }
            value = ConfigurationManager.AppSettings["BanFileClearOnRestart"];
            if (!bool.TryParse(value, out banFileClearOnRestart))
            {
                banFileClearOnRestart = true;
            }

            value      = ConfigurationManager.AppSettings["ExpireTime"];
            expireTime = TimeSpan.Parse(value, CultureInfo.InvariantCulture);

            value     = ConfigurationManager.AppSettings["CycleTime"];
            cycleTime = TimeSpan.Parse(value, CultureInfo.InvariantCulture);

            value    = ConfigurationManager.AppSettings["RuleName"];
            ruleName = value;

            value        = ConfigurationManager.AppSettings["windSFTPlog"];
            windFileName = value;

            value         = ConfigurationManager.AppSettings["windSFTPusers"];
            windSFTPusers = value;

            PopulateList(whiteList, ref whiteListRegex, ConfigurationManager.AppSettings["Whitelist"], ConfigurationManager.AppSettings["WhitelistRegex"]);
            PopulateList(blackList, ref blackListRegex, ConfigurationManager.AppSettings["Blacklist"], ConfigurationManager.AppSettings["BlacklistRegex"]);
            Regex ignored = null;

            PopulateList(allowedUserNames, ref ignored, ConfigurationManager.AppSettings["AllowedUserNames"], null);
            expressions = (ExpressionsToBlock)System.Configuration.ConfigurationManager.GetSection("ExpressionsToBlock");

            foreach (ExpressionsToBlockGroup group in expressions.Groups)
            {
                foreach (ExpressionToBlock expression in group.Expressions)
                {
                    expression.Regex       = (expression.Regex ?? string.Empty).Trim();
                    expression.RegexObject = new Regex(expression.Regex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                }
            }
        }
        /// <summary>
        /// Constructor
        /// </summary>
        public IPBanConfig()
        {
            ConfigurationManager.RefreshSection("appSettings");
            ConfigurationManager.RefreshSection("configSections");
            ConfigurationManager.RefreshSection("nlog");
            ConfigurationManager.RefreshSection("ExpressionsToBlock");

            string value = ConfigurationManager.AppSettings["FailedLoginAttemptsBeforeBan"];
            failedLoginAttemptsBeforeBan = int.Parse(value);

            value = ConfigurationManager.AppSettings["BanTime"];
            banTime = TimeSpan.Parse(value);

            value = ConfigurationManager.AppSettings["BanFile"];
            banFile = value;
            if (!Path.IsPathRooted(banFile))
            {
                banFile = Path.GetFullPath(banFile);
            }
            value = ConfigurationManager.AppSettings["BanFileClearOnRestart"];
            if (!bool.TryParse(value, out banFileClearOnRestart))
            {
                banFileClearOnRestart = true;
            }

            value = ConfigurationManager.AppSettings["ExpireTime"];
            expireTime = TimeSpan.Parse(value);

            value = ConfigurationManager.AppSettings["CycleTime"];
            cycleTime = TimeSpan.Parse(value);

            value = ConfigurationManager.AppSettings["RuleName"];
            ruleName = value;

            PopulateList(whiteList, ref whiteListRegex, ConfigurationManager.AppSettings["Whitelist"], ConfigurationManager.AppSettings["WhitelistRegex"]);
            PopulateList(blackList, ref blackListRegex, ConfigurationManager.AppSettings["Blacklist"], ConfigurationManager.AppSettings["BlacklistRegex"]);
            Regex ignored = null;
            PopulateList(allowedUserNames, ref ignored, ConfigurationManager.AppSettings["AllowedUserNames"], null);
            expressions = (ExpressionsToBlock)System.Configuration.ConfigurationManager.GetSection("ExpressionsToBlock");

            foreach (ExpressionsToBlockGroup group in expressions.Groups)
            {
                foreach (ExpressionToBlock expression in group.Expressions)
                {
                    expression.Regex = (expression.Regex ?? string.Empty).Trim();
                    expression.RegexObject = new Regex(expression.Regex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                }
            }
        }
        public object Create(object parent, object configContext, XmlNode section)
        {
            string config = section.SelectSingleNode("//" + sectionName).OuterXml;

            if (!string.IsNullOrWhiteSpace(config))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(ExpressionsToBlock));
                MemoryStream  ms         = new MemoryStream(Encoding.UTF8.GetBytes(config));
                ms.Position = 0;
                ExpressionsToBlock expressions = serializer.Deserialize(ms) as ExpressionsToBlock;
                return(expressions);
            }

            return(null);
        }
        /// <summary>
        /// Constructor
        /// </summary>
        public IPBanConfig()
        {
            string value = ConfigurationManager.AppSettings["FailedLoginAttemptsBeforeBan"];
            failedLoginAttemptsBeforeBan = int.Parse(value);

            value = ConfigurationManager.AppSettings["BanTime"];
            banTime = TimeSpan.Parse(value);

            value = ConfigurationManager.AppSettings["BanFile"];
            banFile = value;
            if (!Path.IsPathRooted(banFile))
            {
                banFile = Path.GetFullPath(banFile);
            }

            value = ConfigurationManager.AppSettings["ExpireTime"];
            expireTime = TimeSpan.Parse(value);

            value = ConfigurationManager.AppSettings["CycleTime"];
            cycleTime = TimeSpan.Parse(value);

            value = ConfigurationManager.AppSettings["RuleName"];
            ruleName = value;

            value = ConfigurationManager.AppSettings["Whitelist"];
            whiteList.Clear();
            if (!string.IsNullOrWhiteSpace(value))
            {
                foreach (string ip in value.Split(','))
                {
                    whiteList.Add(ip.Trim());
                }
            }

            value = (ConfigurationManager.AppSettings["WhitelistRegex"] ?? string.Empty).Replace("*", "[0-255]").Trim();
            if (value.Length != 0)
            {
                whiteListRegex = new Regex(value, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);
            }

            expressions = (ExpressionsToBlock)System.Configuration.ConfigurationManager.GetSection("ExpressionsToBlock");
        }
示例#5
0
        private IPBanConfig(string xml)
        {
            // deserialize with XmlDocument, the .net core Configuration class is quite buggy
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xml);
            foreach (XmlNode node in doc.SelectNodes("//appSettings/add"))
            {
                appSettings[node.Attributes["key"].Value] = node.Attributes["value"].Value;
            }

            GetConfig <int>("FailedLoginAttemptsBeforeBan", ref failedLoginAttemptsBeforeBan);
            GetConfig <TimeSpan>("BanTime", ref banTime);
            GetConfig <bool>("ClearBannedIPAddressesOnRestart", ref clearBannedIPAddressesOnRestart);
            GetConfig <TimeSpan>("ExpireTime", ref expireTime);
            GetConfig <TimeSpan>("CycleTime", ref cycleTime);
            GetConfig <TimeSpan>("MinimumTimeBetweenFailedLoginAttempts", ref minimumTimeBetweenFailedLoginAttempts);
            GetConfig <string>("FirewallRulePrefix", ref firewallRulePrefix);
            GetConfig <bool>("CreateWhitelistFirewallRule", ref createWhitelistFirewallRule);

            string whiteListString      = GetConfig <string>("Whitelist", string.Empty);
            string whiteListRegexString = GetConfig <string>("WhitelistRegex", string.Empty);
            string blacklistString      = GetConfig <string>("Blacklist", string.Empty);
            string blacklistRegexString = GetConfig <string>("BlacklistRegex", string.Empty);

            PopulateList(whiteList, ref whiteListRegex, whiteListString, whiteListRegexString);
            PopulateList(blackList, ref blackListRegex, blacklistString, blacklistRegexString);
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                expressions = new XmlSerializer(typeof(ExpressionsToBlock)).Deserialize(new XmlNodeReader(doc.SelectSingleNode("//ExpressionsToBlock"))) as ExpressionsToBlock;
                if (expressions != null)
                {
                    foreach (ExpressionsToBlockGroup group in expressions.Groups)
                    {
                        foreach (ExpressionToBlock expression in group.Expressions)
                        {
                            expression.Regex = (expression.Regex ?? string.Empty).Trim();
                            if (expression.Regex.Length != 0)
                            {
                                if (expression.Regex[0] == '^')
                                {
                                    expression.Regex = "^\\s*?" + expression.Regex.Substring(1) + "\\s*?";
                                }
                                else
                                {
                                    expression.Regex = "\\s*?" + expression.Regex + "\\s*?";
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                expressions = new ExpressionsToBlock {
                    Groups = new ExpressionsToBlockGroup[0]
                };
            }
            try
            {
                LogFilesToParse logFilesToParse = new XmlSerializer(typeof(LogFilesToParse)).Deserialize(new XmlNodeReader(doc.SelectSingleNode("//LogFilesToParse"))) as LogFilesToParse;
                logFiles = (logFilesToParse == null ? new LogFileToParse[0] : logFilesToParse.LogFiles);
            }
            catch
            {
                logFiles = new LogFileToParse[0];
            }
            GetConfig <string>("ProcessToRunOnBan", ref processToRunOnBan);

            // retrieve firewall configuration
            string[] firewallTypes = GetConfig <string>("FirewallType", string.Empty).Split(',', StringSplitOptions.RemoveEmptyEntries);
            foreach (string firewallOSAndType in firewallTypes)
            {
                string[] pieces = firewallOSAndType.Split(':');
                if (pieces.Length == 2)
                {
                    osAndFirewallType[pieces[0]] = pieces[1];
                }
            }

            string userNameWhiteListString = GetConfig <string>("UserNameWhiteList", string.Empty);

            foreach (string userName in userNameWhiteListString.Split(','))
            {
                string userNameTrimmed = userName.Normalize().Trim();
                if (userNameTrimmed.Length > 0)
                {
                    userNameWhitelist.Add(userNameTrimmed);
                }
            }
            GetConfig <int>("UserNameWhiteListMinimumEditDistance", ref userNameWhitelistMaximumEditDistance);
            GetConfig <int>("FailedLoginAttemptsBeforeBanUserNameWhitelist", ref failedLoginAttemptsBeforeBanUserNameWhitelist);
            GetConfig <string>("GetUrlUpdate", ref getUrlUpdate);
            GetConfig <string>("GetUrlStart", ref getUrlStart);
            GetConfig <string>("GetUrlStop", ref getUrlStop);
            GetConfig <string>("GetUrlConfig", ref getUrlConfig);
            GetConfig <string>("ExternalIPAddressUrl", ref externalIPAddressUrl);
        }
示例#6
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="configFilePath">Config file path</param>
        public IPBanConfig(string configFilePath)
        {
            if (configFilePath != AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
            {
                File.Copy(configFilePath, AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, true);
            }

            ConfigurationManager.RefreshSection("appSettings");
            ConfigurationManager.RefreshSection("configSections");
            ConfigurationManager.RefreshSection("nlog");
            ConfigurationManager.RefreshSection("ExpressionsToBlock");

            string value = ConfigurationManager.AppSettings["FailedLoginAttemptsBeforeBan"];

            failedLoginAttemptsBeforeBan = int.Parse(value, CultureInfo.InvariantCulture);

            value   = ConfigurationManager.AppSettings["BanTime"];
            banTime = TimeSpan.Parse(value, CultureInfo.InvariantCulture);

            value   = ConfigurationManager.AppSettings["BanFile"];
            banFile = value;
            if (!Path.IsPathRooted(banFile))
            {
                banFile = Path.GetFullPath(banFile);
            }
            value = ConfigurationManager.AppSettings["BanFileClearOnRestart"];
            if (!bool.TryParse(value, out banFileClearOnRestart))
            {
                banFileClearOnRestart = true;
            }

            value      = ConfigurationManager.AppSettings["ExpireTime"];
            expireTime = TimeSpan.Parse(value, CultureInfo.InvariantCulture);

            value     = ConfigurationManager.AppSettings["CycleTime"];
            cycleTime = TimeSpan.Parse(value, CultureInfo.InvariantCulture);

            value    = ConfigurationManager.AppSettings["RuleName"];
            ruleName = value;

            PopulateList(whiteList, ref whiteListRegex, ConfigurationManager.AppSettings["Whitelist"], ConfigurationManager.AppSettings["WhitelistRegex"]);
            PopulateList(blackList, ref blackListRegex, ConfigurationManager.AppSettings["Blacklist"], ConfigurationManager.AppSettings["BlacklistRegex"]);
            Regex ignored = null;

            PopulateList(allowedUserNames, ref ignored, ConfigurationManager.AppSettings["AllowedUserNames"], null);
            expressions = (ExpressionsToBlock)System.Configuration.ConfigurationManager.GetSection("ExpressionsToBlock");

            foreach (ExpressionsToBlockGroup group in expressions.Groups)
            {
                foreach (ExpressionToBlock expression in group.Expressions)
                {
                    expression.Regex = (expression.Regex ?? string.Empty).Trim();
                    if (expression.Regex.Length != 0)
                    {
                        if (expression.Regex[0] == '^')
                        {
                            expression.Regex = "^\\s*?" + expression.Regex.Substring(1) + "\\s*?";
                        }
                        else
                        {
                            expression.Regex = "\\s*?" + expression.Regex + "\\s*?";
                        }
                    }
                    expression.RegexObject = new Regex(expression.Regex, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.Compiled);
                }
            }

            processToRunOnBan    = ConfigurationManager.AppSettings["ProcessToRunOnBan"];
            getUrlUpdate         = ConfigurationManager.AppSettings["GetUrlUpdate"];
            getUrlStart          = ConfigurationManager.AppSettings["GetUrlStart"];
            getUrlStop           = ConfigurationManager.AppSettings["GetUrlStop"];
            getUrlConfig         = ConfigurationManager.AppSettings["GetUrlConfig"];
            externalIPAddressUrl = ConfigurationManager.AppSettings["ExternalIPAddressUrl"];
        }