Beispiel #1
0
 public void Teardown()
 {
     failedEvents.Clear();
     successfulEvents.Clear();
     IPBanService.DisposeIPBanTestService(service);
     service = null;
 }
Beispiel #2
0
 public void Setup()
 {
     // ensure a clean start
     IPBanService.UtcNow = DateTime.UtcNow;
     service             = IPBanService.CreateAndStartIPBanTestService <IPBanService>();
     Assert.AreNotEqual(typeof(IPBanMemoryFirewall), service.Firewall.GetType());
 }
Beispiel #3
0
        public void TestDefaultConfig()
        {
            // ensure config file is read properly
            using (IPBanService service = IPBanService.CreateAndStartIPBanTestService <IPBanService>())
            {
                IPBanConfig cfg = service.Config;
                Assert.IsNotNull(cfg);
                Assert.AreEqual(TimeSpan.FromDays(1.0), cfg.BanTime);
                Assert.IsEmpty(cfg.BlackList);
                Assert.IsEmpty(cfg.BlackListRegex);
                Assert.IsFalse(cfg.ClearBannedIPAddressesOnRestart);
                Assert.IsFalse(cfg.CreateWhitelistFirewallRule);
                Assert.AreEqual(TimeSpan.FromSeconds(15.0), cfg.CycleTime);
                Assert.AreEqual(TimeSpan.FromDays(1.0), cfg.ExpireTime);
                Assert.AreEqual("https://checkip.amazonaws.com/", cfg.ExternalIPAddressUrl);
                Assert.AreEqual(5, cfg.FailedLoginAttemptsBeforeBan);
                Assert.AreEqual(20, cfg.FailedLoginAttemptsBeforeBanUserNameWhitelist);
                Assert.IsEmpty(cfg.FirewallOSAndType);
                Assert.AreEqual("IPBan_", cfg.FirewallRulePrefix);
                Assert.AreEqual(TimeSpan.FromSeconds(1.0), cfg.MinimumTimeBetweenFailedLoginAttempts);
                Assert.IsEmpty(cfg.ProcessToRunOnBan);
                Assert.IsEmpty(cfg.UserNameWhitelist);
                Assert.IsEmpty(cfg.WhiteList);
                Assert.IsEmpty(cfg.WhiteListRegex);

                AssertLogFilesToParse(cfg);
                AssertEventViewer(cfg);
            }
        }
Beispiel #4
0
 public void TearDown()
 {
     service.Firewall.BlockIPAddresses(new string[0]);
     service.Stop();
     service.Dispose();
     service = null;
 }
Beispiel #5
0
        public async Task TestDefaultConfig()
        {
            // ensure config file is read properly
            IPBanService service = IPBanService.CreateAndStartIPBanTestService <IPBanService>();

            try
            {
                IPBanConfig cfg = service.Config;
                Assert.IsNotNull(cfg);
                Assert.AreEqual(TimeSpan.FromDays(1.0), cfg.BanTimes.First());
                Assert.AreEqual(1, cfg.BanTimes.Length);
                Assert.IsEmpty(cfg.BlacklistFilter.IPAddressRanges);
                Assert.IsTrue(string.IsNullOrEmpty(cfg.BlacklistFilter.Regex?.ToString()));
                Assert.IsFalse(cfg.ClearBannedIPAddressesOnRestart);
                Assert.IsFalse(cfg.ClearFailedLoginsOnSuccessfulLogin);
                Assert.IsFalse(cfg.ProcessInternalIPAddresses);
                Assert.AreEqual(TimeSpan.FromSeconds(15.0), cfg.CycleTime);
                Assert.AreEqual(TimeSpan.FromDays(1.0), cfg.ExpireTime);
                Assert.AreEqual("https://checkip.amazonaws.com/", cfg.ExternalIPAddressUrl);
                Assert.AreEqual(5, cfg.FailedLoginAttemptsBeforeBan);
                Assert.AreEqual(20, cfg.FailedLoginAttemptsBeforeBanUserNameWhitelist);
                Assert.AreEqual("IPBan_", cfg.FirewallRulePrefix);
                Assert.AreEqual(TimeSpan.FromSeconds(1.0), cfg.MinimumTimeBetweenFailedLoginAttempts);
                Assert.IsEmpty(cfg.ProcessToRunOnBan);
                Assert.IsEmpty(cfg.ProcessToRunOnUnban);
                Assert.IsFalse(cfg.ResetFailedLoginCountForUnbannedIPAddresses);
                Assert.IsTrue(cfg.UseDefaultBannedIPAddressHandler);
                Assert.IsEmpty(cfg.UserNameWhitelist);
                Assert.IsEmpty(cfg.UserNameWhitelistRegex);
                Assert.IsEmpty(cfg.WhitelistFilter.IPAddressRanges);
                Assert.IsTrue(string.IsNullOrEmpty(cfg.WhitelistFilter.Regex?.ToString()));
                Assert.AreEqual(0, cfg.ExtraRules.Count);
                Assert.AreEqual(cfg.FirewallUriRules.Trim(), "EmergingThreats,01:00:00:00,https://rules.emergingthreats.net/fwrules/emerging-Block-IPs.txt");

                AssertLogFilesToParse(cfg);
                AssertEventViewer(cfg);
                string xml = await service.ConfigReaderWriter.ReadConfigAsync();

                IPBanConfig prod = IPBanConfig.LoadFromXml(xml);
                Assert.IsTrue(prod.UseDefaultBannedIPAddressHandler);
            }
            finally
            {
                IPBanService.DisposeIPBanTestService(service);
            }
        }
Beispiel #6
0
        public async Task TestDefaultConfig()
        {
            // ensure config file is read properly
            IPBanService service = IPBanService.CreateAndStartIPBanTestService <IPBanService>();

            try
            {
                IPBanConfig cfg = service.Config;
                Assert.IsNotNull(cfg);
                Assert.AreEqual(TimeSpan.FromDays(1.0), cfg.BanTimes.First());
                Assert.AreEqual(1, cfg.BanTimes.Length);
                Assert.IsEmpty(cfg.BlackList);
                Assert.IsEmpty(cfg.BlackListRegex);
                Assert.IsFalse(cfg.ClearBannedIPAddressesOnRestart);
                Assert.AreEqual(TimeSpan.FromSeconds(15.0), cfg.CycleTime);
                Assert.AreEqual(TimeSpan.FromDays(1.0), cfg.ExpireTime);
                Assert.AreEqual("https://checkip.amazonaws.com/", cfg.ExternalIPAddressUrl);
                Assert.AreEqual(5, cfg.FailedLoginAttemptsBeforeBan);
                Assert.AreEqual(20, cfg.FailedLoginAttemptsBeforeBanUserNameWhitelist);
                Assert.AreEqual(1, cfg.FirewallOSAndType.Count);
                Assert.AreEqual("*:Default", cfg.FirewallOSAndType.Keys.First() + ":" + cfg.FirewallOSAndType.Values.First());
                Assert.AreEqual("IPBan_", cfg.FirewallRulePrefix);
                Assert.AreEqual(TimeSpan.FromSeconds(1.0), cfg.MinimumTimeBetweenFailedLoginAttempts);
                Assert.IsEmpty(cfg.ProcessToRunOnBan);
                Assert.IsFalse(cfg.ResetFailedLoginCountForUnbannedIPAddresses);
                Assert.IsTrue(cfg.UseDefaultBannedIPAddressHandler);
                Assert.IsEmpty(cfg.UserNameWhitelist);
                Assert.IsEmpty(cfg.UserNameWhitelistRegex);
                Assert.IsEmpty(cfg.WhiteList);
                Assert.IsEmpty(cfg.WhiteListRegex);
                Assert.AreEqual(0, cfg.ExtraRules.Count);

                AssertLogFilesToParse(cfg);
                AssertEventViewer(cfg);
                string xml = await service.ReadConfigAsync();

                IPBanConfig prod = IPBanConfig.LoadFromXml(xml, null);
                Assert.IsTrue(prod.UseDefaultBannedIPAddressHandler);
            }
            finally
            {
                IPBanService.DisposeIPBanTestService(service);
            }
        }
Beispiel #7
0
        public void TestDefaultConfig()
        {
            // ensure config file is read properly
            IPBanService service = IPBanService.CreateAndStartIPBanTestService <IPBanService>();

            try
            {
                IPBanConfig cfg = service.Config;
                Assert.IsNotNull(cfg);
                Assert.AreEqual(TimeSpan.FromDays(1.0), cfg.BanTime);
                Assert.IsEmpty(cfg.BlackList);
                Assert.IsEmpty(cfg.BlackListRegex);
                Assert.IsFalse(cfg.ClearBannedIPAddressesOnRestart);
                Assert.IsFalse(cfg.CreateWhitelistFirewallRule);
                Assert.AreEqual(TimeSpan.FromSeconds(15.0), cfg.CycleTime);
                Assert.AreEqual(TimeSpan.FromDays(1.0), cfg.ExpireTime);
                Assert.AreEqual("https://checkip.amazonaws.com/", cfg.ExternalIPAddressUrl);
                Assert.AreEqual(5, cfg.FailedLoginAttemptsBeforeBan);
                Assert.AreEqual(20, cfg.FailedLoginAttemptsBeforeBanUserNameWhitelist);
                Assert.AreEqual(1, cfg.FirewallOSAndType.Count);
                Assert.AreEqual("*:Default", cfg.FirewallOSAndType.Keys.First() + ":" + cfg.FirewallOSAndType.Values.First());
                Assert.AreEqual("IPBan_", cfg.FirewallRulePrefix);
                Assert.AreEqual(TimeSpan.FromSeconds(1.0), cfg.MinimumTimeBetweenFailedLoginAttempts);
                Assert.IsEmpty(cfg.ProcessToRunOnBan);
                Assert.IsFalse(cfg.UseDefaultBannedIPAddressHandler); // the create and start test service forces this false, it is true otherwise in production by default
                Assert.IsEmpty(cfg.UserNameWhitelist);
                Assert.IsEmpty(cfg.WhiteList);
                Assert.IsEmpty(cfg.WhiteListRegex);

                AssertLogFilesToParse(cfg);
                AssertEventViewer(cfg);

                IPBanConfig prod = IPBanConfig.LoadFromFile(service.ConfigFilePath.Replace(".tmp", string.Empty), null);
                Assert.IsTrue(prod.UseDefaultBannedIPAddressHandler);
            }
            finally
            {
                IPBanService.DisposeIPBanTestService(service);
            }
        }
Beispiel #8
0
        private async Task RunTest(string pathAndMaskXPath, string pathAndMaskOverride)
        {
            // create a test service with log file path/mask overriden
            pathAndMaskOverride = Path.Combine(AppContext.BaseDirectory, pathAndMaskOverride).Replace('\\', '/');
            service             = IPBanService.CreateAndStartIPBanTestService <IPBanService>(configFileModifier: config =>
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(config);
                XmlNode exchange      = doc.SelectSingleNode(pathAndMaskXPath);
                XmlNode pathAndMask   = exchange["PathAndMask"];
                pathAndMask.InnerText = pathAndMaskOverride;
                return(doc.OuterXml);
            });
            service.IPBanDelegate = this;

            // read all the files, save contents in memory temporarily
            Dictionary <string, string> files = new Dictionary <string, string>();

            foreach (var file in LogFileScanner.GetFiles(pathAndMaskOverride))
            {
                files[file.FileName] = File.ReadAllText(file.FileName);
                File.WriteAllText(file.FileName, string.Empty);
            }

            // force service to read all files as empty, the service always starts at the end of the file
            await service.RunCycle();

            // now write the full file contents, service will pick-up all the new text and parse it
            foreach (var file in files)
            {
                File.WriteAllText(file.Key, file.Value);
            }
            files = null;

            // run cycle again to kick off the parse of all the new text
            await service.RunCycle();
        }
Beispiel #9
0
 public void TearDown()
 {
     IPBanService.DisposeIPBanTestService(service);
     successEvents.Clear();
 }
Beispiel #10
0
 public void Setup()
 {
     service = IPBanService.CreateAndStartIPBanTestService <IPBanService>();
     service.IPBanDelegate = this;
     service.Firewall.Truncate();
 }
Beispiel #11
0
 public void Teardown()
 {
     IPBanService.DisposeIPBanTestService(service);
 }
Beispiel #12
0
 public void Teardown()
 {
     IPBanService.DisposeIPBanTestService(service);
     events.Clear();
 }
Beispiel #13
0
 public void Setup()
 {
     // ensure a clean start
     service = IPBanService.CreateAndStartIPBanTestService <IPBanService>();
     service.IPBanDelegate = this;
 }
 public void Setup()
 {
     service = IPBanService.CreateAndStartIPBanTestService <IPBanService>();
     service.IPBanDelegate = this;
     service.Firewall.BlockIPAddresses(new string[0]);
 }