private void BindGrid()
        {
            if (txtIPAddress.Text.Length > 0)
            {
                BindSearch();

                return;
            }

            List <BannedIPAddress> bannedIPs = BannedIPAddress.GetPage(pageNumber, pageSize, out totalPages);

            if (totalPages > 1)
            {
                var pageUrl = SiteUtils.GetNavigationSiteRoot() + "/Admin/BannedIPAddresses.aspx?pagenumber={0}";

                pgrBannedIPAddresses.PageURLFormat = pageUrl;
                pgrBannedIPAddresses.ShowFirstLast = true;
                pgrBannedIPAddresses.CurrentIndex  = pageNumber;
                pgrBannedIPAddresses.PageSize      = pageSize;
                pgrBannedIPAddresses.PageCount     = totalPages;
            }
            else
            {
                pgrBannedIPAddresses.Visible = false;
            }

            grdBannedIPAddresses.DataSource = bannedIPs;
            grdBannedIPAddresses.PageIndex  = pageNumber;
            grdBannedIPAddresses.PageSize   = pageSize;

            grdBannedIPAddresses.DataBind();
        }
        private void grdBannedIPAddresses_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var grid = (GridView)sender;

            var rowID           = (int)grid.DataKeys[e.RowIndex].Value;
            var txtBannedIP     = (TextBox)grid.Rows[e.RowIndex].Cells[1].FindControl("txtBannedIP");
            var txtBannedUTC    = (TextBox)grid.Rows[e.RowIndex].Cells[1].FindControl("txtBannedUTC");
            var txtBannedReason = (TextBox)grid.Rows[e.RowIndex].Cells[1].FindControl("txtBannedReason");

            var bannedIPAddress = new BannedIPAddress(rowID)
            {
                BannedIP = txtBannedIP.Text
            };

            DateTime.TryParse(txtBannedUTC.Text, out DateTime bannedTime);

            if (timeZone != null)
            {
                bannedTime = bannedTime.ToUtc(timeZone);
            }

            bannedIPAddress.BannedUtc    = bannedTime;
            bannedIPAddress.BannedReason = txtBannedReason.Text;

            bannedIPAddress.Save();

            WebUtils.SetupRedirect(this, Request.RawUrl);
        }
Esempio n. 3
0
        void grdBannedIPAddresses_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridView grid = (GridView)sender;

            int     rowID           = (int)grid.DataKeys[e.RowIndex].Value;
            TextBox txtBannedIP     = (TextBox)grid.Rows[e.RowIndex].Cells[1].FindControl("txtBannedIP");
            TextBox txtBannedUTC    = (TextBox)grid.Rows[e.RowIndex].Cells[1].FindControl("txtBannedUTC");
            TextBox txtBannedReason = (TextBox)grid.Rows[e.RowIndex].Cells[1].FindControl("txtBannedReason");

            BannedIPAddress bannedIPAddress = new BannedIPAddress(rowID);

            bannedIPAddress.BannedIP = txtBannedIP.Text;
            DateTime bannedTime = DateTime.UtcNow;

            DateTime.TryParse(txtBannedUTC.Text, out bannedTime);

            if (timeZone != null)
            {
                bannedTime = bannedTime.ToUtc(timeZone);
            }

            bannedIPAddress.BannedUtc    = bannedTime;
            bannedIPAddress.BannedReason = txtBannedReason.Text;
            bannedIPAddress.Save();
            //String pathToCacheDependencyFile
            //        = HttpContext.Current.Server.MapPath(
            //    "~/Data/bannedipcachedependency.config");
            //CacheHelper.TouchCacheFile(pathToCacheDependencyFile);

            WebUtils.SetupRedirect(this, Request.RawUrl);
        }
Esempio n. 4
0
        private static void HandleSpam(string queryText, Exception ex)
        {
            bool autoBanSpamBots = ConfigHelper.GetBoolProperty("AutoBanSpambotsOnSearchErrors", false);

            if ((autoBanSpamBots) && (IsSpam(queryText)))
            {
                if ((HttpContext.Current != null) && (HttpContext.Current.Request != null))
                {
                    BannedIPAddress b = new BannedIPAddress();
                    b.BannedIP     = HttpContext.Current.Request.UserHostAddress;
                    b.BannedReason = "spambot autodetected";
                    b.BannedUtc    = DateTime.UtcNow;
                    b.Save();

                    String pathToCacheDependencyFile
                        = HttpContext.Current.Server.MapPath(
                              "~/Data/bannedipcachedependency.config");

                    CacheHelper.TouchCacheFile(pathToCacheDependencyFile);

                    //log.Error(queryText, ex);
                    log.Info("spambot detected, ip address has been banned: " + HttpContext.Current.Request.UserHostAddress);
                }
            }
            else
            {
                //log.Error(queryText, ex);
                log.Info("spambot possibly detected, ip address was: " + HttpContext.Current.Request.UserHostAddress);
            }
        }
Esempio n. 5
0
        void grdBannedIPAddresses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridView grid  = (GridView)sender;
            int      rowID = (int)grid.DataKeys[e.RowIndex].Value;

            BannedIPAddress.Delete(rowID);
            WebUtils.SetupRedirect(this, Request.RawUrl);
        }
Esempio n. 6
0
        private void BindSearch()
        {
            pgrBannedIPAddresses.Visible = false;

            using (IDataReader reader = BannedIPAddress.GeByIpAddress(txtIPAddress.Text))
            {
                grdBannedIPAddresses.DataSource = reader;
                grdBannedIPAddresses.DataBind();
            }
        }
        public void Add_should_be_able_to_add_banned_ip()
        {
            var ip = new BannedIPAddress {
                IPAddress = "127.0.0.1"
            };

            repository.Add(ip);

            Assert.True(ip.Id >= 0);
        }
Esempio n. 8
0
        private bool IsBanned(string ip)
        {
            // 2008-08-13 this list got too large over time
            // better to make a small hit to the db on each request than to cache this huge List

            //List<String> bannedIPs = CacheHelper.GetBannedIPList();
            //if(bannedIPs.Contains(ip))return true;
            //return false;

            return(BannedIPAddress.IsBanned(ip));
        }
        public void IsMatching_should_return_correct_value()
        {
            var ip = new BannedIPAddress {
                IPAddress = "127.0.0.1"
            };

            repository.Add(ip);

            bool ismatched = repository.IsMatching(ip.IPAddress);

            Assert.True(ismatched);
        }
        public void Delete_should_be_able_to_delete_banned_ip()
        {
            var ip = new BannedIPAddress {
                IPAddress = "127.0.0.1"
            };

            repository.Add(ip);

            repository.Delete(ip);

            Assert.False(Database.BannedIPAddresses.Any(b => b.Id == ip.Id));
        }
Esempio n. 11
0
        public AdministrativeActionResult <BannedIPAddress> CreateBannedIPAddress(string ipAddress)
        {
            AdministrativeActionResult <BannedIPAddress> result = Validation.Validate <AdministrativeActionResult <BannedIPAddress> >(() => string.IsNullOrWhiteSpace(ipAddress), "ipAddress", TextMessages.IpAddressCannotBeBlank)
                                                                  .Or(() => !ipAddress.IsIPAddress(), "ipAddress", TextMessages.IpAddressIsNotInValidFormat)
                                                                  .Or(() => bannedIPAddressRepository.IsMatching(ipAddress), "ipAddress", TextMessages.SpecifiedIpAddressAlreadyExists.FormatWith(ipAddress))
                                                                  .Result();

            if (result.RuleViolations.IsEmpty())
            {
                BannedIPAddress banned = new BannedIPAddress {
                    IPAddress = ipAddress
                };

                bannedIPAddressRepository.Add(banned);

                unitOfWork.Commit();

                result = new AdministrativeActionResult <BannedIPAddress>(banned);
            }

            return(result);
        }
Esempio n. 12
0
        public static List <String> GetBannedIPList()
        {
            string cachekey = "bannedipaddresses";

            if (HttpRuntime.Cache[cachekey] == null)
            {
                List <String> bannedIPs = BannedIPAddress.GetAllBannedIPs();

                int cacheTimeout = 120;

                String pathToCacheDependencyFile
                    = HttpContext.Current.Server.MapPath(
                          "~/Data/bannedipcachedependency.config");

                if (pathToCacheDependencyFile != null)
                {
                    EnsureCacheFile(pathToCacheDependencyFile);
                }

                CacheDependency cacheDependency
                    = new CacheDependency(pathToCacheDependencyFile);

                DateTime                 absoluteExpiration = DateTime.Now.AddSeconds(cacheTimeout);
                TimeSpan                 slidingExpiration  = TimeSpan.Zero;
                CacheItemPriority        priority           = CacheItemPriority.Default;
                CacheItemRemovedCallback callback           = null;

                HttpRuntime.Cache.Insert(
                    cachekey,
                    bannedIPs,
                    cacheDependency,
                    absoluteExpiration,
                    slidingExpiration,
                    priority,
                    callback);
            }

            return(HttpRuntime.Cache[cachekey] as List <String>);
        }
        private void AddIPToBanList(HttpContext context, string reason)
        {
            string ip = SiteUtils.GetIP4Address();

            if (IsBanned(ip))
            {
                return;               //already banned
            }
            BannedIPAddress ipToBan = new BannedIPAddress();

            ipToBan.BannedIP     = ip;
            ipToBan.BannedUtc    = DateTime.UtcNow;
            ipToBan.BannedReason = reason;
            ipToBan.Save();

            String pathToCacheDependencyFile
                = HttpContext.Current.Server.MapPath(
                      "~/Data/bannedipcachedependency.config");

            CacheHelper.TouchCacheFile(pathToCacheDependencyFile);

            log.Info("BannedIPBlockingHttpModule banned ip address " + ip + " for reason: " + reason);
        }