zoukankan      html  css  js  c++  java
  • 网站sql注入的技巧与防范

          服务器的安全是一个网站最先考虑的,做为一个服务器,没有放网站之前,可能都已经做好了抵御一些安全问题的修改,可以抵御相当的外部和内部攻击。但是从网站本身进行的一些注入攻击,是通过你的程序进行的,任何的防火墙都无能为力。最近研究了一下这方面的知识,仅针对修改url参数和表单输入的注入攻击,其它的方法不知道。

         提供两篇参考文档,本人的sql注入知识也只是从以下两篇文档中获得,曾按照方法曾尝试进入一个网站,结果相当成功。

           什么是注入式攻击 彻底堵死SQL注入工具漏洞

           不用工具菜鸟也学习查找注入漏洞

         如果发生这种方式的sql注入,一定是程序内部使用的sql拼接,并且没有对用户的输入的表单参数过滤或者没有对url参数过滤。

         对于sql的拼接,基本上有两种方式:

         1,如  sql = “select * from table where id=” + 输入参数;   形式

            此种最好防范,只须要对输入参数进行数字验证,如果忽略此处验证,后果也是最严重,就算你对参数进行 单引号过滤,等号过滤,sql关键字过滤 也于事无补。

            如果没有数字验证,那么sql就有可能变成  

         select * from table where id=10 delete from table
         严重吧,但是我可能过滤了关键字啊
          public static string SafeSql(string str)
    {
    if (string.IsNullOrEmpty(str))
    {
    str = "";
    return str;
    }
    else
    {
    str.Replace("'", "");
    }
    str = new Regex("exec", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("xp_cmdshell", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("select", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("insert", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("update", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("delete", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("drop", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("create", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("rename", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("truncate", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("alter", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("exists", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("master.", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("restore", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("=", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("or", RegexOptions.IgnoreCase).Replace(str, "");
    str = new Regex("and", RegexOptions.IgnoreCase).Replace(str, "");
    return str;
    }

    这个方法够全了吧,还不区分大小写,但是如果我的输入参数是 10 delandete from table 呢??

    哈哈,方法正好被利用了。因为它会把delandete中的and过滤掉,又成了delete了。

    但是黑客怎么知道我的过滤顺序啊,告诉你黑客最常用的也最管用的方法就是尝试,不用几次基本过滤顺序就能尝试出来。

    同样这种利用过滤方法攻击的方法也适用于以下的几种方法。

        2,拼接带单引号的形式
         sql = “select * from table where name = ‘” + 参数 + "' order by id desc ”     
         sql = “select * from table where name = ‘%" + 参数 + "%'”
         输入参数是  feng' delete from table select 1 from table where 1=‘1

    前面单引号终结前面的单引号的作用范围,加入自己的语句,后边自己去拼接吧。

     对于这种的注入,要想插入自己的sql语句,必须加个单引号去终结前面的单引号的作用范围,因此它也就有了自己的死穴了,只需把单引号过滤掉就行了。

    有种更绝的方法是把用户输入的单引号直接替换成两个,让输入的语句没有执行环境,感觉更安全点。

    sql注入如此简单,这造成了非常严重的外部环境,因此我们应该有自己的防护措施。

    建议:

    尽量使用sql参数而不采用sql拼接,因为sql参数不会提供给用户的输入执行的环境。

    尽管如此,也要对输入的参数和url参数进行验证过滤。

    还有自定义错误页,一些数据库信息和网站目录信息就是通过错误页暴露的。

    声明:以上建议仅针对url和用户表单输入的注入攻击有效。其它待研究。

  • 相关阅读:
    LeetCode Find Duplicate File in System
    LeetCode 681. Next Closest Time
    LeetCode 678. Valid Parenthesis String
    LeetCode 616. Add Bold Tag in String
    LeetCode 639. Decode Ways II
    LeetCode 536. Construct Binary Tree from String
    LeetCode 539. Minimum Time Difference
    LeetCode 635. Design Log Storage System
    LeetCode Split Concatenated Strings
    LeetCode 696. Count Binary Substrings
  • 原文地址:https://www.cnblogs.com/forcertain/p/1938885.html
Copyright © 2011-2022 走看看