asp.net防類似DDOS攻擊(CC攻擊)代碼

來源:互聯網
上載者:User
< DOCTYPE html PUBLIC -WCDTD XHTML StrictEN httpwwwworgTRxhtmlDTDxhtml-strictdtd>

Web.config
 
<httpModules>
 <!-Url重寫->
 <add type=”UrlRewriter.RewriterHttpModule, UrlRewriter” name=”UrlRewriter”/>
 <!-防類似DDOS攻擊->
 <add type=”UrlRewriter.DDosAttackModule, UrlRewriter” name=”DDosAttackModule”/>
 </httpModules>
 
 
 
代碼:
 
?[Copy to clipboard]View Code JAVA

 

using System;
using System.Web;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Timers;
 
namespace UrlRewriter
{
    /// <summary>
    /// 阻止攻擊IP地址的回應
    /// </summary>
    public class DosAttackModule : IHttpModule
    {
        void IHttpModule.Dispose() { }
 
        void IHttpModule.Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
 
        private static Dictionary<string, short> _IpAdresses = new Dictionary<string, short>();
        private static Stack<string> _Banned = new Stack<string>();
        private static Timer _Timer = CreateTimer();
        private static Timer _BannedTimer = CreateBanningTimer();
 
        private const int BANNED_REQUESTS = 1; //規定時間內訪問的最大次數
        private const int REDUCTION_INTERVAL = 1000; // 1 秒(檢查訪問次數的時間段)
        private const int RELEASE_INTERVAL = 5 * 60 * 1000; // 5 分鐘(清除一個禁止IP的時間段)
 
        private void context_BeginRequest(object sender, EventArgs e)
        {
            string ip = HttpContext.Current.Request.UserHostAddress;
            if (_Banned.Contains(ip))
            {
                HttpContext.Current.Response.StatusCode = 403;
                HttpContext.Current.Response.End();
            }
 
            CheckIpAddress(ip);
        }
 
        /// <summary>
        /// 檢查訪問IP
        /// </summary>
        private static void CheckIpAddress(string ip)
        {
            if (!_IpAdresses.ContainsKey(ip)) //如果沒有當前訪問IP的記錄就將訪問次數設為1
            {
                _IpAdresses[ip] = 1;
            }
            else if (_IpAdresses[ip] == BANNED_REQUESTS) //如果當前IP訪問次數等於規定時間段的最大訪問次數就拉於“黑名單”
            {
                _Banned.Push(ip);
                _IpAdresses.Remove(ip);
            }
            else //正常訪問就加次數 1
            {
                _IpAdresses[ip]++;
            }
        }
 
        #region Timers
 
        /// <summary>
        /// 建立計時器,從_IpAddress減去一個請求。
        /// </summary>
        private static Timer CreateTimer()
        {
            Timer timer = GetTimer(REDUCTION_INTERVAL);
            timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
            return timer;
        }
 
        /// <summary>
        /// 建立定時器,消除一個禁止的IP地址
        /// </summary>
        /// <returns></returns>
        private static Timer CreateBanningTimer()
        {
            Timer timer = GetTimer(RELEASE_INTERVAL);
            timer.Elapsed += delegate { _Banned.Pop(); }; //消除一個禁止IP
            return timer;
        }
 
        /// <summary>
        /// 建立一個時間器,並啟動它
        /// </summary>
        /// <param name="interval">以毫秒為單位的時間間隔</param>
        private static Timer GetTimer(int interval)
        {
            Timer timer = new Timer();
            timer.Interval = interval;
            timer.Start();
 
            return timer;
        }
 
        /// <summary>
        /// 減去從集合中的每個IP地址的請求
        /// </summary>
        private static void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            foreach (string key in _IpAdresses.Keys)
            {
                _IpAdresses[key]--;
                if (_IpAdresses[key] == 0)
                    _IpAdresses.Remove(key);
            }
        }
 
        #endregion
 
    }
}

轉載保留 : http://blog.wuaiwei.com/2011/04/02/asp-net%e9%98%b2%e7%b1%bb%e4%bc%bcddos%e6%94%bb%e5%87%bb%e4%bb%a3%e7%a0%81/

#c#專欄

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.