码迷,mamicode.com
首页 > 数据库 > 详细

过滤sql敏感字符

时间:2015-10-16 20:43:28      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

    public static class SensitiveDataUtil
    {

        private static string ChangeSubStr(string s, string oldstr, string newstr)
        {
            if (s == null || s == "")
                return "";
            string s1 = s.ToLower();
            int i = s1.IndexOf(oldstr);
            while (i != -1)
            {
                string l = s.Substring(0, i);
                string r = s.Substring(i + oldstr.Length);
                s = l + newstr + r;
                s1 = s.ToLower();
                i = s1.IndexOf(oldstr);
            }
            return s;
        }

        public static void CheckForSQLs(HttpRequest Request)
        {
            string[] sql = new string[] { "/*", "*/", "--", "", "declare", "select", "into", "insert", "update", "delete", "drop", "create", "exec", "master" };
            string[] sqlc = new string[] { "/ *", "* /", "- -", "", "declare", "select", "into", "insert", "update", "delete", "drop", "create", "exec", "master" };

            //Form

            if (Request.Form.Count > 0)
            {
                Type type = typeof(System.Collections.Specialized.NameObjectCollectionBase);// Request.Form.GetType();
                PropertyInfo pi = type.GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
                pi.SetValue(Request.Form, false, null);

                for (int i = 0; i < Request.Form.Count; i++)
                {
                    string s = Request.Form[i];
                    for (int j = 0; j < sql.Length; j++)
                        s = ChangeSubStr(s, sql[j], sqlc[j]);
                    Request.Form.Set(Request.Form.GetKey(i), s);
                }
                pi.SetValue(Request.Form, true, null);
            }

            //QueryString
            if (Request.QueryString.Count > 0)
            {
                Type type = typeof(System.Collections.Specialized.NameObjectCollectionBase);// Request.Form.GetType();
                PropertyInfo pi = type.GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
                pi.SetValue(Request.QueryString, false, null);

                for (int i = 0; i < Request.QueryString.Count; i++)
                {
                    string s = Request.QueryString[i];
                    for (int j = 0; j < sql.Length; j++)
                        s = ChangeSubStr(s, sql[j], sqlc[j]);
                    Request.QueryString.Set(Request.QueryString.GetKey(i), s);
                }
                pi.SetValue(Request.QueryString, true, null);
            }

        }

 

过滤sql敏感字符

标签:

原文地址:http://www.cnblogs.com/qiywtc/p/4886138.html

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