专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

ASP.NET 修改Global.asax文件,防SQL注入式攻击

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 1:02:04       共计:3682 浏览
只需在Global.asax文件中加入如下方法即可:

#region SQL注入式攻击代码分析
    /// <summary>
    /// 处理用户提交的请求
    /// </summary>
    private void StartProcessRequest()
    {undefined
        try
        {undefined
            string getkeys = "";

            if (System.Web.HttpContext.Current.Request.QueryString != null)
            {undefined

                for (int i = 0; i < System.Web.HttpContext.Current.Request.QueryString.Count; i++)
                {undefined
                    getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];
                    if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys]))
                    {undefined
                        System.Web.HttpContext.Current.Response.Write(" <h3>不能包含执行语句 </h3>");
                        System.Web.HttpContext.Current.Response.End();
                    }
                }
            }
            if (System.Web.HttpContext.Current.Request.Form != null)
            {undefined
                for (int i = 0; i < System.Web.HttpContext.Current.Request.Form.Count; i++)
                {undefined
                    getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i];
                    if (getkeys == "__VIEWSTATE") continue;
                    if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys]))
                    {undefined
                        //jcFAQApp.FAQ_Util.Log.WriteMessage(" <font color:red>注入攻击 </red>", System.Web.HttpContext.Current.Request.UserHostAddress.ToString());
                        System.Web.HttpContext.Current.Response.Write(" <h3>不能包含执行语句 </h3>");
                        System.Web.HttpContext.Current.Response.End();
                    }
                }
            }
        }
        catch
        {undefined

        }
    }
    /// <summary>
    /// 分析用户请求是否正常
    /// </summary>
    /// <param name="Str">传入用户提交数据 </param>
    /// <returns>返回是否含有SQL注入式攻击代码 </returns>
    private bool ProcessSqlStr(string Str)
    {undefined
        bool ReturnValue = true;
        try
        {undefined
            if (Str.Trim() != "")
            {undefined
                //string SqlStr = "and |exec |insert |select |delete |update |count |* |chr |mid |master |truncate |char |declare";
                string SqlStr = "exec |insert |select |delete |update |mid |master |truncate |declare";
                string[] anySqlStr = SqlStr.Split('|');
                foreach (string ss in anySqlStr)
                {undefined
                    if (Str.ToLower().IndexOf(ss) >= 0)
                    {undefined
                        ReturnValue = false;
                        break;
                    }
                }
            }
        }
        catch
        {undefined
            ReturnValue = false;
        }
        return ReturnValue;
    }
    #endregion
版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:小学数学公式大全1到6年级完整版 | ·下一条:网站被挂Global.asax木马的分析和解决办法

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有