1. 程式人生 > >防cc攻擊策略

防cc攻擊策略

黑客攻擊你的網站,會採取各種各樣的手段,其中為了降低你網站的訪問速度,甚至讓你的伺服器癱瘓,它會不斷的重新整理你的網站,或者模擬很多使用者同一時間大量的訪問你的網站,

這就是所謂的CC攻擊,這就需要我們在程式裡新增一些防CC攻擊的策略程式碼,下面就來介紹一下自己最近寫的一段程式碼,拿來供大家分享:

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.IO; public partial class _Default : System.Web.UI.Page { string getIp = null; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetCC(); } }
//放CC攻擊 public void GetCC() { string FYCC_05 = ""; //'CCLog.txt存放的路徑資料夾!需要手動建立!建議留空 //'如果輸入,請在前面加上符號"/" int FYCC_18 = 1; //'防重新整理頻繁CC攻擊關閉與啟動,1為啟動0為關閉 int FYCC_17 = 1; //'防重新整理禁止IP功能關閉與啟動,1為啟動0為關閉 int FYCC_19 = 6;
//'每分鐘重新整理次數,將會出現提示 string FYCC_20 = "http://www.163.com"; //'被封IP後自動轉入的頁面,建議輸入存放病毒的網址!!! int FYCC_21 = 12; //'惡意重新整理幾次將禁止IP string realip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];//獲得代理ip string proxy = Request.ServerVariables["REMOTE_ADDR"];//獲得普通ip // getIp = GetIP(); if (realip == null) { getIp = proxy; } else { getIp = realip; } string path = Server.MapPath("~/"); if (!System.IO.File.Exists(path + "/CCLOG/CCLOG.txt")) { System.IO.File.CreateText(path + "/CCLOG/CCLOG.txt"); } StreamReader reader = new StreamReader(path + "/CCLOG/CCLOG.txt"); string readFile = reader.ReadToEnd(); reader.Close(); if (readFile.Contains(getIp)) { Response.Write("您的IP" + getIp + "已經被禁止!如需要解封,請聯絡本站管理員')"); Response.End(); } if (Convert.ToInt32(Session["FYCC_01"]) > FYCC_19 && DateTime.Now.Minute != Convert.ToInt32(Session["FYCC_02"])) { Session["FYCC_01"] = "1"; Session["FYCC_02"] = DateTime.Now.Minute.ToString(); } else if ((Convert.ToInt32(Session["FYCC_01"]) > FYCC_21 - 1) && (DateTime.Now.Minute == Convert.ToInt32(Session["FYCC_02"]))) { if (FYCC_17 != 0 & Convert.ToInt32(Session["FYCC_01"]) > FYCC_21 - 1) { OperationFile(); } Response.Redirect("http://www.baidu.com"); } else if ((Convert.ToInt32(Session["FYCC_01"]) > FYCC_19) && (DateTime.Now.Minute == Convert.ToInt32(Session["FYCC_02"]))) { Response.Write("本站啟動防重新整理功能,1分鐘內只能翻" + FYCC_19 + "頁,請在下一分鐘再重新整理本頁面"); Session["FYCC_01"] = (Convert.ToInt32(Session["FYCC_01"]) + 1).ToString(); Response.End(); } else { if (Session["FYCC_01"] == "") { Session["FYCC_01"] = "1"; Session["FYCC_02"] = DateTime.Now.Minute.ToString(); } else { if (DateTime.Now.Minute != Convert.ToInt32(Session["FYCC_02"])) { Session["FYCC_01"] = "1"; Session["FYCC_02"] = DateTime.Now.Minute.ToString(); } else { Session["FYCC_01"] = (Convert.ToInt32(Session["FYCC_01"]) + 1).ToString(); } } } } //向檔案中新增Ip private void OperationFile() { string path = Server.MapPath("~/"); if (!System.IO.File.Exists(path + "/CCLOG/CCLOG.txt")) { System.IO.File.CreateText(path + "/CCLOG/CCLOG.txt"); } StreamWriter w = File.AppendText(path + "/CCLOG/CCLOG.txt"); w.WriteLine(getIp); w.Close(); } }

原理很清晰,簡單的說一下:

當重新整理的時候就記錄他的重新整理數,一分鐘之內達到你設定的值,比如30次就給給予提示,不能頻繁重新整理,過下一分鐘在重新整理就好了,然後重新整理數從頭開始計算,假如惡意重新整理很多次,就記錄她的IP,然後將其封掉,只能聯絡管理員才能解除,這些的話就可以限制惡意的cc攻擊了

上面的程式碼我們可以把一下開關,設定的值寫在web.config中,這樣的話直接修改web.config中值就可以了,不用修改程式程式碼了。

 

 

from:https://www.cnblogs.com/shuang121/archive/2011/03/02/1969369.html