• 防止SQL注入


    一个恐怖的例子:

    注入式攻击的详细解释SQL下面我们将以一个简单的用户登陆为例,结合代码详细解释一下SQL注入式攻击,与及他的防范措施。对于一个简单的用户登陆可能的代码如下:
    try
    {
     string strUserName = this.txtUserName.Text;
     string strPwd = this.txtPwd.Text;
     string strSql = "select * from userinfo where UserName='" + strUserName + "' and Password='" + strPwd + "'";
     SqlConnection objDbConn = new SqlConnection("数据库连接字符串");
     SqlDataAdapter objAdapter = new SqlDataAdapter(strSql,objDbConn);
     DataSet objDataSet = null;
     objAdapter.Fill(objDataSet);//TODO 对获取的数据进行判断。
    }
    catch (System.Exception e)
    {
     this.lblMsg.Text = e.Message;
     this.lblMsg.Visible = true;
    }
      在上面这段代码中,如果用户的输入是正常的用户名和密码的话,那么执行都会比较正常,但是,假如输入用户名的时候,输入的是“johny’--”的话,在 SQLServer里面执行的语句将会是“select * from userinfo where UserName=’johny’--‘ and Password=’密码’”,只要数据库中存在johny这个用户的话,那么不管密码是什么,语句都能够执行成功,并且能够顺利通过登陆。还 有更加厉害的,我们知道SQLServer里面有一些系统的存储过程,能够执行操作系统的很多命令,比如xp_cmdshell,假如上面用户登陆的时 候,用户名部分输入的是“johny’ exec xp_cmdshell ‘format d:/s’--”,大家想想一下后果是什么?有恶意的用户,只要把’format d:/s’这个命令稍加改造就能够做很多不合法的事情。

     

    .NET防SQL注入方法


    1,利用SqlCommand传参数的方法:

    string  strSQL= "SELECT * FROM [user] WHERE user_id=@id" ;
    SqlCommand cmd = new  SqlCommand();
    cmd.CommandText = strSQL;
    cmd.Parameters.Add( "@id" ,SqlDbType.VarChar,20).Value=Request[ "id" ].ToString();

      

    2,过滤禁止运行法:

    /// <summary>
    /// 过滤SQL语句,防止注入
    /// </summary>
    /// <param name="strSql"></param>
    /// <returns>0 - 没有注入, 1 - 有注入 </returns>
    public  int  filterSql( string  sSql)
    {
         int  srcLen, decLen = 0;
         sSql = sSql.ToLower().Trim();
         srcLen = sSql.Length;
         sSql = sSql.Replace("exec", ""); sSql = sSql.Replace("delete", ""); sSql = sSql.Replace("master", ""); sSql = sSql.Replace("truncate", ""); sSql = sSql.Replace("declare", ""); sSql = sSql.Replace("create", ""); sSql = sSql.Replace("xp_", "no"); sSql = sSql.Replace("insert", ""); sSql = sSql.Replace("select", ""); sSql = sSql.Replace("update", ""); sSql = sSql.Replace("count", ""); sSql = sSql.Replace("*", ""); sSql = sSql.Replace("chr", ""); sSql = sSql.Replace("char", ""); sSql = sSql.Replace("drop", ""); sSql = sSql.Replace("or", "");
         decLen = sSql.Length;
         if  (srcLen == decLen) return  0; else  return  1;        
    }

    3,存储过程


    js版的防范SQL注入式攻击代码:

     

    <script language= "javascript" >
    <!--
    var  url = location.search;
    var  re=/^\?(.*)(select%20|insert%20| delete %20from%20|count\(|drop%20table|update%20truncate%20|asc\(|mid\(|char\(|xp_cmdshell|exec%20master|net%20localgroup%20administrators|\ "|:|net%20user|\|%20or%20)(.*)$/gi;
    var e = re.test(url);
    if(e) {
         alert(" 地址中含有非法字符~ ");
         location.href=" error.asp";
    }
    //-->
    <script>
  • 相关阅读:
    Leetcode 238. Product of Array Except Self
    来博客园的第一天
    [LeetCode] 1020. Number of Enclaves
    [LeetCode] 921. Minimum Add to Make Parentheses Valid
    [LeetCode] 1541. Minimum Insertions to Balance a Parentheses String
    [LeetCode] 738. Monotone Increasing Digits
    [LeetCode] 1669. Merge In Between Linked Lists
    [LeetCode] 865. Smallest Subtree with all the Deepest Nodes
    [LeetCode] 376. Wiggle Subsequence
    [LeetCode] 1170. Compare Strings by Frequency of the Smallest Character
  • 原文地址:https://www.cnblogs.com/wybshyy/p/16042711.html
Copyright © 2020-2023  润新知