防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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM