日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

asp.net防类似DDOS攻击(CC攻击)代码

發布時間:2024/4/15 asp.net 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 asp.net防类似DDOS攻击(CC攻击)代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

Web.config

<httpModules>
<!–Url重寫–>
<add type=”UrlRewriter.RewriterHttpModule, UrlRewriter” name=”UrlRewriter”/>
<!–防類似DDOS攻擊–>
<add type=”UrlRewriter.DDosAttackModule, UrlRewriter” name=”DDosAttackModule”/>
</httpModules>

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(); }; //消除一個禁止IPreturn 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é2?±????ddos??????£??/#more-118

轉載于:https://www.cnblogs.com/jamin/archive/2011/04/11/2012254.html

總結

以上是生活随笔為你收集整理的asp.net防类似DDOS攻击(CC攻击)代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。