サイトをまたいでのシナリオ、Sql注入などの攻撃の処理

5092 ワード

360のセキュリティフォーラムから見つかったコードは、パッケージを整理して、直接サイトGlobal.asaxファイルに書いたり、HttpModuleを書いて悪意の要求を阻止すればいいです。
http://bbs.webscan.360.cn/forum.php?mod=viewthread&tid=711&page=1&extra=#pid1927 
using System.Text.RegularExpressions;
using System.Web;

/// <summary>
/// Web      :       ,Sql     ,  :http://bbs.webscan.360.cn/forum.php?mod=viewthread&tid=711&page=1&extra=#pid1927
///       :
/// 1.Cookie
/// 2.      
/// 3.ReferrerUrl
/// 4.Post  
/// 5.Get  
/// </summary>
public class Safe360
{
    #region       

    /// <summary>
    ///       
    /// </summary>
    public static void Procress()
    {
        const string errmsg =
            "<div style='position:fixed;top:0px;width:100%;height:100%;background-color:white;color:green;font-weight:bold;border-bottom:5px solid #999;'><br>           ,    !<br><br>       :<a href='http://webscan.360.cn'>360      </a></div>";

        if (RawUrl())
        {
            HttpContext.Current.Response.Write(errmsg);
            HttpContext.Current.Response.End();
        }

        if (CookieData())
        {
            HttpContext.Current.Response.Write(errmsg);
            HttpContext.Current.Response.End();
        }

        if (HttpContext.Current.Request.UrlReferrer != null)
        {
            if (Referer())
            {
                HttpContext.Current.Response.Write(errmsg);
                HttpContext.Current.Response.End();
            }
        }

        if (HttpContext.Current.Request.RequestType.ToUpper() == "POST")
        {
            if (PostData())
            {
                HttpContext.Current.Response.Write(errmsg);
                HttpContext.Current.Response.End();
            }
        }
        if (HttpContext.Current.Request.RequestType.ToUpper() == "GET")
        {
            if (GetData())
            {
                HttpContext.Current.Response.Write(errmsg);
                HttpContext.Current.Response.End();
            }
        }
    }

    #endregion

    #region       

    /// <summary>
    ///       
    /// </summary>
    private const string StrRegex =
        @"<[^>]+?style=[\w]+?:expression\(|\b(alert|confirm|prompt)\b|^\+/v(8|9)|<[^>]*?=[^>]*?&#[^>]*?>|\b(and|or)\b.{1,6}?(=|>|<|\bin\b|\blike\b)|/\*.+?\*/|<\s*script\b|<\s*img\b|\bEXEC\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\s+(TABLE|DATABASE)";

    #endregion

    #region   Post  

    /// <summary>
    ///   Post  
    /// </summary>
    /// <returns></returns>
    private static bool PostData()
    {
        bool result = false;

        for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
        {
            result = CheckData(HttpContext.Current.Request.Form[i]);
            if (result)
            {
                break;
            }
        }
        return result;
    }

    #endregion

    #region   Get  

    /// <summary>
    ///   Get  
    /// </summary>
    /// <returns></returns>
    private static bool GetData()
    {
        bool result = false;

        for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++)
        {
            result = CheckData(HttpContext.Current.Request.QueryString[i]);
            if (result)
            {
                break;
            }
        }
        return result;
    }

    #endregion

    #region   Cookie  

    /// <summary>
    ///   Cookie  
    /// </summary>
    /// <returns></returns>
    private static bool CookieData()
    {
        bool result = false;
        for (int i = 0; i < HttpContext.Current.Request.Cookies.Count; i++)
        {
            result = CheckData(HttpContext.Current.Request.Cookies[i].Value.ToLower());
            if (result)
            {
                break;
            }
        }
        return result;
    }

    #endregion

    #region   Referer

    /// <summary>
    ///   Referer
    /// </summary>
    /// <returns></returns>
    private static bool Referer()
    {
        return CheckData(HttpContext.Current.Request.UrlReferrer.ToString());
    }

    #endregion

    #region         

    /// <summary>
    ///         
    /// </summary>
    /// <returns></returns>
    private static bool RawUrl()
    {
        return CheckData(HttpContext.Current.Request.RawUrl);
    }

    #endregion

    #region     

    /// <summary>
    ///     
    /// </summary>
    /// <param name="inputData"></param>
    /// <returns></returns>
    private static bool CheckData(string inputData)
    {
        return Regex.IsMatch(inputData, StrRegex);
    }

    #endregion
}
Global.asaxで呼び出されたコード:
    prvate void Aplication_BegineRequest(object sender、EventArgs e)    {        Safe 360.Procress()
    }