zoukankan      html  css  js  c++  java
  • asp.net 防止 跨站脚本

    新建一个类safe_360.cs

    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    using System.Web;
    
    /// <summary>
    /// safe_360 的摘要说明
    /// </summary>
    public class safe_360
    {
    
        private const string StrRegex = @"<[^>]+?style=[w]+?:expression(|(alert|confirm|prompt)|^+/v(8|9)|<[^>]*?=[^>]*?&#[^>]*?>|(and|or).{1,6}?(=|>|<|in|like)|/*.+?*/|<s*script|<s*img|EXEC|UNION.+?SELECT|UPDATE.+?SET|INSERTs+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)s+(TABLE|DATABASE)";
        public static bool PostData()
        {
            bool result = false;
            for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
            {
                result = CheckData(HttpContext.Current.Request.Form[i].ToString());
                if (result)
                {
                    break;
                }
            }
            return result;
        }
    
        public static bool GetData()
        {
            bool result = false;
            for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++)
            {
                result = CheckData(HttpContext.Current.Request.QueryString[i].ToString());
                if (result)
                {
                    break;
                }
            }
            return result;
        }
        public 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;
    
        }
        public static bool referer()
        {
            bool result = false;
            return result = CheckData(HttpContext.Current.Request.UrlReferrer.ToString());
        }
        public static bool CheckData(string inputData)
        {
            if (Regex.IsMatch(inputData, StrRegex))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    View Code

    Global.asax 加入

    void Application_BeginRequest(object sender, EventArgs e)
        {
                if (Request.Cookies != null)
                {
                    if (safe_360.CookieData())
                    {
                        Response.Write("您提交的Cookie数据有恶意字符!");
                        Response.End();
                     
                    }
                
                }
                if (Request.UrlReferrer != null)
                {
                    if (safe_360.referer())
                    {
                        Response.Write("您提交的Referrer数据有恶意字符!");
                        Response.End();
                    }
                }
                if (Request.RequestType.ToUpper() == "POST")
                {
                    if (safe_360.PostData())
                    {
                        Response.Write("您提交的Post数据有恶意字符!");
                        Response.End();
                    }
                }
                if (Request.RequestType.ToUpper() == "GET")
                {
                    if (safe_360.GetData())
                    {
                        Response.Write("您提交的Get数据有恶意字符!");
                        Response.End();
                    }
                }         
        }
    View Code
  • 相关阅读:
    多项式 Wannafly挑战赛22
    L. Twice Equation ACM Nanning 2017
    我们身边的大数据
    js_隔10秒发送验证码(setInterrval定时器)
    js_定时器(setInterval)
    VS调试相关
    afx_msg与消息映射机制
    ON_COMMAND,ON_MESSAGE和ON_NOTIFY的区别
    条款4:确定对象在被使用前已经被初始化
    用doxygen+graphviz生成函数调用流程图
  • 原文地址:https://www.cnblogs.com/xiaoruilin/p/12420612.html
Copyright © 2011-2022 走看看