360漏洞解决

using QBYL.Common;
using System.Text.RegularExpressions;
using System.Web;

/// 
/// 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数据
/// 
public class Safe360
{
    #region 执行安全检查

    /// 
    /// 执行安全检查
    /// 
    public static void Procress()
    {
        const string errmsg ="您的提交带有不合法参数!";

        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 安全检查正则

    /// 
    /// 安全检查正则
    /// 
    private const string StrRegex =
        @"<[^>]+?style=[\w]+?:expression\(|\b(alert|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数据

    /// 
    /// 检查Post数据
    /// 
    /// 
    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数据

    /// 
    /// 检查Get数据
    /// 
    /// 
    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数据

    /// 
    /// 检查Cookie数据
    /// 
    /// 
    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

    /// 
    /// 检查Referer
    /// 
    /// 
    private static bool Referer()
    {
        return CheckData(HttpContext.Current.Request.UrlReferrer.ToString());
    }

    #endregion

    #region 检查当前请求路径

    /// 
    /// 检查当前请求路径
    /// 
    /// 
    private static bool RawUrl()
    {
        return CheckData(HttpContext.Current.Request.RawUrl);
    }

    #endregion

    #region 正则匹配

    /// 
    /// 正则匹配
    /// 
    /// 
    /// 
    private static bool CheckData(string inputData)
    {
        //LogUtils.WriteLog(LogUtils.LogType.DEBUG, "inputData:" + inputData);
        return Regex.IsMatch(inputData, StrRegex);
    }

    #endregion
}

 

转载于:https://www.cnblogs.com/qbq01/p/9913516.html

你可能感兴趣的:(git)