English
 电子信箱
 加入收藏

  威盾防火墙 >> 新闻中心 >> 业界动态 >> 一段防SQL注入的正则代码

 

一段防SQL注入的正则代码

威盾防火墙 2015-03-05

 

 //验证是否有SQL注入字符

        private bool ValidateQuery(Hashtable queryConditions)

        {

            //构造SQL的注入关键字符

            #region 字符

            string[] strBadChar = {"and"

                                    ,"exec"

                                    ,"insert"

                                    ,"select"

                                    ,"delete"

                                    ,"update"

                                    ,"count"

                                    ,"or"

                                    //,"*"

                                    ,"%"

                                    ,":"

                                    ,"\'"

                                    ,"\""

                                    ,"chr"

                                    ,"mid"

                                    ,"master"

                                    ,"truncate"

                                    ,"char"

                                    ,"declare"

                                    ,"SiteName"

                                    ,"net user"

                                    ,"xp_cmdshell"

                                    ,"/add"

                                    ,"exec master.dbo.xp_cmdshell"

                                    ,"net localgroup administrators"};

            #endregion

 

            //构造正则表达式

            string str_Regex = ".*(";

            for (int i = 0; i < strBadChar.Length - 1; i++)

            {

                str_Regex += strBadChar[i] + "|";

            }

            str_Regex += strBadChar[strBadChar.Length - 1] + ").*";

            //避免查询条件中_list情况

            foreach (string str in queryConditions.Keys)

            {

                if (str.Substring(str.Length - 5) == "_list")

                {

                    //去掉单引号检验?

                    str_Regex = str_Regex.Replace("|'|", "|");

                }

                string tempStr = queryConditions[str].ToString();

                if (Regex.Matches(tempStr.ToString(), str_Regex).Count > 0)

                {

                    //有SQL注入字符

                    return true;

                }

            }

            return false;

        }

 


相关内容: 最新内容:
防范SQL注入攻击的代码[2015-03-05]
如何从根本上防止 SQL 注入?[2015-03-05]
SQL注入攻击的种类和防范手段[2015-03-05]
如何防止SQL注入[2015-03-05]
好用的asp防SQL注入代码[2015-03-05]
防sql注入代码[2015-03-05]
防范SQL注入攻击的代码[2015-03-05]
如何从根本上防止 SQL 注入?[2015-03-05]
SQL注入攻击的种类和防范手段[2015-03-05]
如何防止SQL注入[2015-03-05]
好用的asp防SQL注入代码[2015-03-05]
asp中最简单的程序引起的最难发现的错误[2015-03-05]