zoukankan      html  css  js  c++  java
  • 全站防止SQL注入类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// 防SQL注入检查器
    /// </summary>
    public class SqlChecker
    {
        //当前请求对象
        private HttpRequest request;
        //当前响应对象
        private HttpResponse response;
        //安全Url,当出现Sql注入时,将导向到的安全页面,如果没赋值,则停留在当前页面
        private string safeUrl = String.Empty;
    
        //Sql注入时,可能出现的sql关键字,可根据自己的实际情况进行初始化,每个关键字由'|'分隔开来
        //private const string StrKeyWord = @"select|insert|delete|from|count(|drop table|update|truncate|asc(|mid(|char(|xp_cmdshell|exec master|netlocalgroup administrators|:|net user|""|or|and";
        private const string StrKeyWord = @"select|insert|delete|from|drop table|update|truncate|exec master|netlocalgroup administrators|:|net user|or|and";
        //Sql注入时,可能出现的特殊符号,,可根据自己的实际情况进行初始化,每个符号由'|'分隔开来
        //private const string StrRegex = @"-|;|,|/|(|)|[|]|}|{|%|@|*|!|'";
        private const string StrRegex = @"=|!|'";
        public SqlChecker()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 由此构造函数创建的对象,在验证Sql注入之后将停留在原来页面上
        /// </summary>
        /// <param name="_request">当前请求的 Request 对象</param>
        /// <param name="_response">当前请求的 Response 对象</param>
        public SqlChecker(HttpRequest _request, HttpResponse _response)
        {
            this.request = _request;
            this.response = _response;
        }
        /// <summary>
        /// 由此构造函数创建的对象,在验证Sql注入之后将请求将导向由 _safeUrl 指定的安全url页面上
        /// </summary>
        /// <param name="_request">当前请求的 Request 对象</param>
        /// <param name="_response">当前请求的 Response 对象</param>
        /// <param name="_safeUrl">验证Sql注入之后将导向的安全 url</param>
        public SqlChecker(HttpRequest _request, HttpResponse _response, string _safeUrl)
        {
            this.request = _request;
            this.response = _response;
            this.safeUrl = _safeUrl;
        }
        /// <summary>
        /// 只读属性 SQL关键字
        /// </summary>
        public string KeyWord
        {
            get
            {
                return StrKeyWord;
            }
        }
        /// <summary>
        /// 只读属性过滤特殊字符
        /// </summary>
        public string RegexString
        {
            get
            {
                return StrRegex;
            }
        }
        /// <summary>
        /// 当出现Sql注入时需要提示的错误信息(主要是运行一些客户端的脚本)
        /// </summary>
        public string Msg
        {
            get
            {
                string msg = "<script type='text/javascript'> "
                + " alert('请勿输入非法字符!'); ";
    
                if (this.safeUrl == String.Empty)
                    msg += " window.location.href = '" + request.RawUrl + "'";
                else
                    msg += " window.location.href = '" + safeUrl + "'";
    
                msg += "</script>";
                return msg;
            }
        }
        /// <summary>
        /// 检查URL参数中是否带有SQL注入的可能关键字。
        /// </summary>
        /// <returns>存在SQL注入关键字时返回 true,否则返回 false</returns>
        public bool CheckRequestQuery()
        {
            bool result = false;
            if (request.QueryString.Count != 0)
            {
                //若URL中参数存在,则逐个检验参数。
                foreach (string queryName in this.request.QueryString)
                {
                    //过虑一些特殊的请求状态值,主要是一些有关页面视图状态的参数
                    if (queryName == "__VIEWSTATE" || queryName == "__EVENTVALIDATION")
                        continue;
                    //开始检查请求参数值是否合法
                    if (CheckKeyWord(request.QueryString[queryName]))
                    {
                        //只要存在一个可能出现Sql注入的参数,则直接退出
                        result = true;
                        break;
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 检查提交表单中是否存在SQL注入的可能关键字
        /// </summary>
        /// <returns>存在SQL注入关键字时返回 true,否则返回 false</returns>
        public bool CheckRequestForm()
        {
            bool result = false;
            if (request.Form.Count > 0)
            {
                //若获取提交的表单项个数不为0,则逐个比较参数
                foreach (string queryName in this.request.Form)
                {
                    //过虑一些特殊的请求状态值,主要是一些有关页面视图状态的参数
                    if (queryName == "__VIEWSTATE" || queryName == "__EVENTVALIDATION")
                        continue;
                    //开始检查提交的表单参数值是否合法
                    if (CheckKeyWord(request.Form[queryName]))
                    {
                        //只要存在一个可能出现Sql注入的参数,则直接退出
                        result = true;
                        break;
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 检查_sword是否包涵SQL关键字
        /// </summary>
        /// <param name="_sWord">需要检查的字符串</param>
        /// <returns>存在SQL注入关键字时返回 true,否则返回 false</returns>
        public bool CheckKeyWord(string _sWord)
        {
            bool result = false;
            //模式1 : 对应Sql注入的可能关键字
            string[] patten1 = StrKeyWord.Split('|');
            //模式2 : 对应Sql注入的可能特殊符号
            string[] patten2 = StrRegex.Split('|');
            //开始检查 模式1:Sql注入的可能关键字 的注入情况
            foreach (string sqlKey in patten1)
            {
                if (_sWord.IndexOf(" " + sqlKey) >= 0 || _sWord.IndexOf(sqlKey + " ") >= 0)
                {
                    //只要存在一个可能出现Sql注入的参数,则直接退出
                    result = true;
                    break;
                }
            }
            //开始检查 模式1:Sql注入的可能特殊符号 的注入情况
            foreach (string sqlKey in patten2)
            {
                if (_sWord.IndexOf(sqlKey) >= 0)
                {
                    //只要存在一个可能出现Sql注入的参数,则直接退出
                    result = true;
                    break;
                }
            }
            return result;
        }
        /// <summary>
        /// 执行Sql注入验证
        /// </summary>
        public void Check()
        {
            if (CheckRequestQuery() || CheckRequestForm())
            {
                response.Write(Msg);
                response.End();
            }
        }
    }
    /// 使用说明 ///
    // 使用时可以根据需要决定是要进行全局性(即针对整个应用程序)的Sql注入检查
    // ,还是局部性(即在针对某个页面)的Sql注入检查
    /*=========== 全局性设置:在Global.asax.cs 中加上以下代码 =============
    
    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
    SqlChecker SqlChecker = new SqlChecker(this.Request,this.Response);
    //或 SqlChecker SqlChecker = new SqlChecker(this.Request,this.Response,safeUrl);
    SqlChecker.Check();
    }
    
    ======================================================================*/
    /*============ 局部性:在任何时候都可直接用以下代码来实现Sql注入检验 ===============
    
    SqlChecker SqlChecker = new SqlChecker(this.Request,this.Response);
    //或 SqlChecker SqlChecker = new SqlChecker(this.Request,this.Response,safeUrl);
    SqlChecker.Check();
    

     转载:http://makoo.blogbus.com/logs/61067550.html

    天行健,君子以自强不息
  • 相关阅读:
    xxl-job docker版分布式任务
    nginx学习http_access_module模块
    nginx学习sub_filter模块
    nginx学习首页随机模块
    mysql使用命令
    laravel数据填充
    LINUX下统计代码行数
    检查字符串结尾 判断一个字符串(str)是否以指定的字符串(target)结尾。
    右边大数组中包含了4个小数组,分别找到每个小数组中的最大值,然后把它们串联起来,形成一个新数组。
    确保字符串的每个单词首字母都大写,其余部分小写。
  • 原文地址:https://www.cnblogs.com/halo/p/2953941.html
Copyright © 2011-2022 走看看