asp.net防類似DDOS攻擊(CC攻擊)代碼001 Web.config
002
003 <httpModules>
004 <!–Url重寫–>
005 <add type=”UrlRewriter.RewriterHttpModule, UrlRewriter” name=”UrlRewriter”/>
006 <!–防類似DDOS攻擊–>
007 <add type=”UrlRewriter.DDosAttackModule, UrlRewriter” name=”DDosAttackModule”/>
008 </httpModules>
009
010 using System;
011 using System.Web;
012 using System.Collections.Generic;
013 using System.Collections.Specialized;
014 using System.Timers;
015
016 namespace UrlRewriter
017 {
018 /// <summary>
019 /// 阻止攻擊IP地址的迴應
020 /// </summary>
021 public class DosAttackModule : IHttpModule
022 {
023 void IHttpModule.Dispose() { }
024
025 void IHttpModule.Init(HttpApplication context)
026 {
027 context.BeginRequest += new EventHandler(context_BeginRequest);
028 }
029
030 private static Dictionary<string, short> _IpAdresses = new Dictionary<string, short>();
031 private static Stack<string> _Banned = new Stack<string>();
032 private static Timer _Timer = CreateTimer();
033 private static Timer _BannedTimer = CreateBanningTimer();
034
035 private const int BANNED_REQUESTS = 1; //規定時間內訪問的最大次數
036 private const int REDUCTION_INTERVAL = 1000; // 1 秒(檢查訪問次數的時間段)
037 private const int RELEASE_INTERVAL = 5 * 60 * 1000; // 5 分鐘(清除一個禁止IP的時間段)
038
039 private void context_BeginRequest(object sender, EventArgs e)
040 {
041 string ip = HttpContext.Current.Request.UserHostAddress;
042 if (_Banned.Contains(ip))
043 {
044 HttpContext.Current.Response.StatusCode = 403;
045 HttpContext.Current.Response.End();
046 }
047
048 CheckIpAddress(ip);
049 }
050
051 /// <summary>
052 /// 檢查訪問IP
053 /// </summary>
054 private static void CheckIpAddress(string ip)
055 {
056 if (!_IpAdresses.ContainsKey(ip)) //如果沒有當前訪問IP的記錄就將訪問次數設爲1
057 {
058 _IpAdresses[ip] = 1;
059 }
060 else if (_IpAdresses[ip] == BANNED_REQUESTS) //如果當前IP訪問次數等於規定時間段的最大訪問次數就拉於“黑名單”
061 {
062 _Banned.Push(ip);
063 _IpAdresses.Remove(ip);
064 }
065 else //正常訪問就加次數 1
066 {
067 _IpAdresses[ip]++;
068 }
069 }
070
071 #region Timers
072
073 /// <summary>
074 /// 創建計時器,從_IpAddress減去一個請求。
075 /// </summary>
076 private static Timer CreateTimer()
077 {
078 Timer timer = GetTimer(REDUCTION_INTERVAL);
079 timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
080 return timer;
081 }
082
083 /// <summary>
084 /// 創建定時器,消除一個禁止的IP地址
085 /// </summary>
086 /// <returns></returns>
087 private static Timer CreateBanningTimer()
088 {
089 Timer timer = GetTimer(RELEASE_INTERVAL);
090 timer.Elapsed += delegate { _Banned.Pop(); }; //消除一個禁止IP
091 return timer;
092 }
093
094 /// <summary>
095 /// 創建一個時間器,並啓動它
096 /// </summary>
097 /// <param name="interval">以毫秒爲單位的時間間隔</param>
098 private static Timer GetTimer(int interval)
099 {
100 Timer timer = new Timer();
101 timer.Interval = interval;
102 timer.Start();
103
104 return timer;
105 }
106
107 /// <summary>
108 /// 減去從集合中的每個IP地址的請求
109 /// </summary>
110 private static void TimerElapsed(object sender, ElapsedEventArgs e)
111 {
112 foreach (string key in _IpAdresses.Keys)
113 {
114 _IpAdresses[key]--;
115 if (_IpAdresses[key] == 0)
116 _IpAdresses.Remove(key);
117 }
118 }
119
120 #endregion
121
122 }
123 }
asp.net 防ddos(cc)攻擊代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.