码迷,mamicode.com
首页 > 其他好文 > 详细

Request参数批量过滤,防注入(逐步完善中...)

时间:2015-08-25 15:41:20      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

来公司没有多久,检查了之前程序员写的代码,有些乱,很多Request没有验证。

尝试写了一个通用的Sql过滤的函数放OnInit,临时用下,不讲究效率,仅应急使用。

    /// <summary>
    /// 检查请求参数是否安全
    /// </summary>
    /// <returns>有注入危险的请求参数</returns>
    public string CheckRequstSafe()
    {
        string sErrPara = "";

        foreach (string sRequestKey in Request.QueryString)
        {
            if (!IsSafeToSql(Request.QueryString[sRequestKey]))
            {
                sErrPara = (sErrPara == "" ? sRequestKey : (sErrPara + "" + sRequestKey));
            }
        }

        foreach (string sRequestKey in Request.Form)
        {
            if (!IsSafeToSql(Request.Form[sRequestKey]))
            {
                sErrPara = sErrPara == "" ? sRequestKey : (sErrPara + "" + sRequestKey);
            }
        }

        return sErrPara;
    }

    /// <summary>
    /// 检查是否含有“sql注入”常用字符
    /// </summary>
    /// <param name="_sParaValues">待检查字符串</param>
    /// <returns>true-安全 false-有注入危险</returns>
    public bool IsSafeToSql(string _sRequest)
    {
        bool bIsSafe = true;

        string strTemp = "insert|select|delete|update|drop|create|";
        strTemp += "declare|dbcc|exec|xp_|set|;|‘|--|@";

        string[] arrStr = strTemp.Split(|);

        _sRequest = _sRequest.ToLower();
        

        foreach (string sItem in arrStr)
        {
            if (_sRequest.IndexOf(sItem) != -1)
            {
                bIsSafe = false;
            }
        }

        return bIsSafe;
    }

关于过滤的字符,Sql水平不足,不能确保一定不会被注入,先临时应急使用下。

过滤的方式,个人还是认为分字符型和整型分别过来,字符型过滤的时候根据参数的不同检查长度,毕竟一般情况,我们传递参数的长度不一样。

网上有关于存储过程来防止过滤的方式,暂时没有时间研究,后面有时间研究再补上。

Request参数批量过滤,防注入(逐步完善中...)

标签:

原文地址:http://www.cnblogs.com/dongdong1979/p/4757224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!