zoukankan      html  css  js  c++  java
  • 防SQL注入 记 武胜

    /// < summary> 

            /// 分析用户请求是否正常 

            /// < /summary> 

            /// < param name="Str">传入用户提交数据< /param> 

            /// < returns>返回是否含有SQL注入式攻击代码< /returns> 

            private static bool ProcessSqlStr(string Str,int type) 

            { 

                string SqlStr; 

                if(type == 1) 

                    SqlStr = "exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare "; 

                else

                    SqlStr = "'|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare"; 

                bool ReturnValue = true; 

                try

                { 

                    if (Str != "") 

                    { 

                        string[] anySqlStr = SqlStr.Split('|'); 

                        foreach (string ss in anySqlStr) 

                        { 

                            if (Str.IndexOf(ss)>=0) 

                            { 

                                ReturnValue = false; 

                            } 

                        } 

                    }  

                } 

                catch

                { 

                    ReturnValue = false; 

                } 

                return ReturnValue; 

            } 

    //Sql注入时,可能出现的特殊符号,,可根据自己的实际情况进行初始化,每个符号由'|'分隔开来

    //private const string StrRegex = @"-|;|,|/|(|)|[|]|}|{|%|@|*|!|'";

    private const string StrRegex = @"=|!|'";

    //Sql注入时,可能出现的sql关键字,可根据自己的实际情况进行初始化,每个关键字由'|'分隔开来

    private const string StrKeyWord = @"select|insert|delete|from|drop table|update|truncate|exec master|netlocalgroup administrators|:|net user|or|and";

    /// <summary>
      /// 检查_sword是否包涵SQL关键字
      /// </summary>
      /// <param name="_sWord">需要检查的字符串</param>
      /// <returns>存在SQL注入关键字时返回 true,否则返回 false</returns>
      public bool CheckKeyWord(string _sWord)
      {
      bool result = false;
      //模式1 : 对应Sql注入的可能关键字
      string[] patten1 = StrKeyWord.Split('|');
      //模式2 : 对应Sql注入的可能特殊符号
      string[] patten2 = StrRegex.Split('|');
      //开始检查 模式1:Sql注入的可能关键字 的注入情况
      foreach (string sqlKey in patten1)
      {
        if (_sWord.IndexOf(" " + sqlKey) >= 0 || _sWord.IndexOf(sqlKey + " ") >= 0)
        {
        //只要存在一个可能出现Sql注入的参数,则直接退出
        result = true;
        break;
        }
      }
      //开始检查 模式1:Sql注入的可能特殊符号 的注入情况
      foreach (string sqlKey in patten2)
      {
        if (_sWord.IndexOf(sqlKey) >= 0)
        {
        //只要存在一个可能出现Sql注入的参数,则直接退出
        result = true;
        break;
        }
      }
      return result;
      }

  • 相关阅读:
    spring boot + swagger2
    itext7 html转pdf实现
    shell脚本学习
    观察者模式
    sql mode 问题及解决 错误代码:1055 this is incompatible with sql_mode=only_full_group_by
    学生报数算法实现
    git reset 版本回退操作
    struts2方法无法映射问题:There is no Action mapped for namespace [/] and action name [m_hi] associated with context path []
    Vue日历组件的功能
    vue-router 在新窗口打开页面的功能
  • 原文地址:https://www.cnblogs.com/zeroone/p/2484472.html
Copyright © 2011-2022 走看看