黑客攻擊你的網(wǎng)站,會采取各種各樣的手段,其中為了降低你網(wǎng)站的訪問速度,甚至讓你的服務(wù)器癱瘓,它會不斷的刷新你的網(wǎng)站,或者模擬很多用戶同一時(shí)間大量的訪問你的網(wǎng)站,
這就是所謂的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存放的路徑文件夾!需要手動創(chuàng)建!建議留空
//'如果輸入,請?jiān)谇懊婕由戏?/"
int FYCC_18 = 1;
//'防刷新頻繁CC攻擊關(guān)閉與啟動,1為啟動0為關(guān)閉
int FYCC_17 = 1;
//'防刷新禁止IP功能關(guān)閉與啟動,1為啟動0為關(guān)閉
int FYCC_19 = 6;
//'每分鐘刷新次數(shù),將會出現(xiàn)提示
string FYCC_20 = "http://www.163.com";
//'被封IP后自動轉(zhuǎn)入的頁面,建議輸入存放病毒的網(wǎng)址!!!
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 + "已經(jīng)被禁止!如需要解封,請聯(lián)系本站管理員')");
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分鐘內(nèi)只能翻" + FYCC_19 + "頁,請?jiān)谙乱环昼娫偎⑿卤卷撁?);
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();
}
}
原理很清晰,簡單的說一下:
當(dāng)刷新的時(shí)候就記錄他的刷新數(shù),一分鐘之內(nèi)達(dá)到你設(shè)定的值,比如30次就給給予提示,不能頻繁刷新,過下一分鐘在刷新就好了,然后刷新數(shù)從頭開始計(jì)算,假如惡意刷新很多次,就記錄她的IP,然后將其封掉,只能聯(lián)系管理員才能解除,這些的話就可以限制惡意的cc攻擊了
上面的代碼我們可以把一下開關(guān),設(shè)定的值寫在web.config中,這樣的話直接修改web.config中值就可以了,不用修改程序代碼了。
如對本文有疑問,請?zhí)峤坏浇涣髡搲瑥V大熱心網(wǎng)友會為你解答?。?點(diǎn)擊進(jìn)入論壇