使用字符过滤的方法防止ASP.NET网站被注入

在Global.asax文件里面写入代码:

        protected void Application_BeginRequest(Object sender, EventArgs e)
        {
               StartProcessRequest();
        }            

 

复制代码
        private void StartProcessRequest()
        {
            try
            {
                string getKeys = "";
                string sqlErrorPage = "/error";
                if (Request.QueryString != null)
                {
                    for (int i = 0; i < Request.QueryString.Count; i++)
                    {
                        getKeys = Request.QueryString.Keys[i];
                        string val = Request.QueryString[getKeys];
                        if (StringHelper.CheckValidationUrl(val) || StringHelper.CheckValidationKeyword(val) || StringHelper.CheckValidationKeywordJS(val))
                        {
                            Response.Redirect(sqlErrorPage);
                            Response.End();
                        }
                    }
                }
                if (Request.Form != null)
                {
                    for (int i = 0; i < Request.Form.Count; i++)
                    {
                        getKeys = Request.Form.Keys[i];
                        if (getKeys == "_VIEWSTATE")
                            continue;
                        string val = Request.Form[getKeys];
                        if (StringHelper.CheckValidationKeyword(val) || StringHelper.CheckValidationKeywordJS(val))
                        {
                            Response.Redirect(sqlErrorPage);
                            Response.End();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/error");
                return;
            }
        }    
复制代码

 

StringHelper.cs中的方法参考:

复制代码
        /// <summary>
        /// 去除符号字符,防止SQL注入(URL)
        /// </summary>
        /// <param name="val">检查的对象</param>
        /// <returns>True:包含非法字符;False:不包含非法字符</returns>
        public static bool CheckValidationUrl(string val)
        {
            string str = "'<>~!$^*();|/\"";//%
            foreach (char ch in str)
            {
                if (val.IndexOf(ch) >= 0)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 去除SQL关键字,防止SQL注入
        /// </summary>
        /// <param name="val">检查的对象</param>
        /// <returns>True:包含SQL关键字;False:不包含SQL关键字</returns>
        public static bool CheckValidationKeyword(string val)
        {
            val = " " + val;
            string sql = " exec | insert | select | delete | update | count | chr | master | truncate | char | declare | drop | create | and | or ";//|mid
            string[] sql_c = sql.Split('|');
            foreach (var sl in sql_c)
            {
                if (val.ToLower().IndexOf(sl) >= 0)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 去除脚本注入关键字
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static bool CheckValidationKeywordJS(string val)
        {
            val = " " + val;
            string sql = " script | alert | href | location ";
            string[] sql_c = sql.Split('|');
            foreach (var sl in sql_c)
            {
                if (val.ToLower().IndexOf(sl) >= 0)
                {
                    return true;
                }
            }
            return false;
        }
复制代码

 

posted @   海角之上  阅读(199)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2016-03-11 从一个集合中过滤另一个集合中存在的项(类似in)
点击右上角即可分享
微信分享提示