zoukankan      html  css  js  c++  java
  • 【荐】PDO防 SQL注入攻击 原理分析 以及 使用PDO的注意事项

    我们都知道,只要合理正确使用PDO,可以基本上防止SQL注入的产生,本文主要回答以下几个问题:

    • 为什么要使用PDO而不是mysql_connect?
    • 为何PDO能防注入?
    • 使用PDO防注入的时候应该特别注意什么?

    一、为何要优先使用PDO?

    PHP手册上说得很清楚:

    Prepared statements and stored procedures
    Many of the more mature databases support the concept of prepared statements. What are they? They can be thought of as a kind of compiled template for the SQL that an application wants to run, that can be customized using variable parameters. Prepared statements offer two major benefits: 

    The query only needs to be parsed (or prepared) once, but can be executed multiple times with the same or different parameters. When the query is prepared, the database will analyze, compile and optimize its plan for executing the query. For complex queries this process can take up enough time that it will noticeably slow down an application if there is a need to repeat the same query many times with different parameters. By using a prepared statement the application avoids repeating the analyze/compile/optimize cycle. This means that prepared statements use fewer resources and thus run faster.

    The parameters to prepared statements don't need to be quoted; the driver automatically handles this. If an application exclusively uses prepared statements, the developer can be sure that no SQL injection will occur(however, if other portions of the query are being built up with unescaped input, SQL injection is still possible).

    即使用PDO的prepare方式,主要是提高 相同SQL模板查询性能、阻止SQL注入

    同时,PHP手册中给出了警告信息

    Prior to PHP 5.3.6, this element was silently ignored. The same behaviour can be partly replicated with the PDO::MYSQL_ATTR_INIT_COMMAND driver option, as the following example shows.

    Warning

    The method in the below example can only be used with character sets that share the same lower 7 bit representation as ASCII, such as ISO-8859-1 and UTF-8. Users using character sets that have different representations (such as UTF-16 or Big5) must use the charset option provided in PHP 5.3.6 and later versions.

    意思是说,在PHP 5.3.6及以前版本中,并不支持在DSN中的charset定义,而应该使用 PDO::MYSQL_ATTR_INIT_COMMAND 设置初始SQL, 即我们常用的 set names gbk 指令。

    我看到一些程序,还在尝试使用 addslashes() 达到防注入的目的,殊不知这样其实问题更多,详情请看 http://www.lorui.com/addslashes-mysql_escape_string-mysql_real_eascape_string.html

    还有一些做法:在执行数据库查询前,将SQL中的 select, union, ....之类的关键词清理掉。这种做法显然是非常错误的处理方式,如果提交的正文中确实包含 the students's union , 替换后将篡改本来的内容,滥杀无辜,不可取。

    二、为何PDO能防SQL注入?

    请先看以下PHP代码:

    <?php
    $pdo = new PDO("mysql:host=192.168.0.1;dbname=test;charset=utf8", "root", "root");
    $st = $pdo->prepare("select * from info where id = ? and name = ?");
    
    $id = 21;
    $name = 'zhangsan';
    
    $st->bindParam(1, $id);
    $st->bindParam(2, $name);
    
    $st->execute();
    $st->fetchAll();
    ?>

    环境如下:

    • PHP 5.4.7
    • Mysql 协议版本 10
    • MySQL Server 5.5.27

    为了彻底搞清楚 PHP 与 MySQL Server 通讯的细节,我特别使用了 wireshark 抓包进行研究之,安装 wireshak 之后,我们设置过滤条件为 tcp.port==3306,如下图所示:

    如此只显示与 MySQL 3306端口的通信数据,避免不必要的干扰。

    特别要注意的是 wireshak 基于 wincap驱动,不支持本地环回接口的侦听(即使用 PHP 连接本地 MySQL 的方法是无法侦听的),请连接其它机器(桥接网络的虚拟机也可)的 MySQL 进行测试。

    然后运行我们的PHP程序,侦听结果如下,我们发现,PHP只是简单地将SQL直接发送给MySQL Server:

    其实,这与我们平时使用 mysql_real_escape_string() 将字符串进行转义,再拼接成SQL语句没有差别(只是由PDO本地驱动完成转义的),显然这种情况下还是有可能造成SQL注入的,也就是说在 PHP 本地调用 pdo prepare 中的 mysql_real_escape_string() 来操作query,使用的是本地单字节字符集,而我们传递多字节编码的变量时,有可能还是会造成SQL注入漏洞(php 5.3.6以前版本的问题之一,这也就解释了为何在使用PDO时,建议升级到php 5.3.6+,并在DSN字符串中指定 charset 的原因。

    针对 PHP 5.3.6 以前版本,以下代码仍然可能造成SQL注入问题:

    $pdo->query('SET NAMES GBK'); 
    
    $var = chr(0xbf) . chr(0x27) . " OR 1=1 /*"; 
    $query = "SELECT * FROM info WHERE name = ?"; 
    
    $stmt = $pdo->prepare($query); 
    $stmt->execute(array($var)); 

    原因与上面的分析是一致的。

    而正确的转义应该是给 MySQL Server 指定字符集,并将变量发送给 MySQL Server 完成根据字符转义。

    那么,如何才能禁止 PHP本地转义 而交由 MySQL Server 转义 呢?

    PDO有一项参数,名为 PDO::ATTR_EMULATE_PREPARES ,表示是否使用 PHP本地模拟prepare,此项参数默认值未知。而且根据我们刚刚抓包分析结果来看,PHP 5.3.6+ 默认还是使用本地变量转,拼接成SQL发送给 MySQL Server 的,我们将这项值设置为 false, 试试效果,如以下代码:

    <?php
    $pdo = new PDO("mysql:host=192.168.0.1;dbname=test;", "root", "root");
    
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $st = $pdo->prepare("select * from info where id = ? and name = ?");
    
    $id = 21;
    $name = 'zhangsan';
    
    $st->bindParam(1, $id);
    $st->bindParam(2, $name);
    
    $st->execute();
    $st->fetchAll();

    红色行是我们刚加入的内容,运行以下程序,使用 wireshark 抓包分析,得出的结果如下:

    看到了吗?这就是神奇之处,可见这次 PHP 是将 SQL模板变量 是分两次发送给 MySQL 的,由 MySQL 完成变量的转义 处理,既然 变量 和 SQL模板 是分两次发送的,那么就不存在SQL注入的问题了,但需要在DSN中指定charset属性,如:

    $pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', 'root');

    如此,即可从根本上杜绝SQL注入的问题。

    三、使用PDO的注意事项

    知道以上几点之后,我们就可以总结使用PDO杜绝SQL注入的几个注意事项:

    1.  PHP升级到5.3.6+,生产环境强烈建议升级到 PHP 5.3.9+ PHP 5.4+,PHP 5.3.8 存在致命的 hash碰撞漏洞。

    2. 若使用 PHP5.3.6+, 请在在PDO的DSN中指定charset属性。

    3. 如果使用了PHP 5.3.6及以前版本,设置 PDO::ATTR_EMULATE_PREPARES 参数为false(即由MySQL进行变量处理),PHP 5.3.6以上版本已经处理了这个问题,无论是使用 本地模拟prepar e还是 调用MySQL Server的prepare均可。在DSN中指定charset是无效的,同时 set names <charset> 的执行是必不可少的。

    4. 如果使用了PHP 5.3.6及以前版本, 因Yii框架默认并未设置 ATTR_EMULATE_PREPARES 的值,请在数据库配置文件中指定 emulatePrepare 的值为 false。

    那么,有个问题,如果在DSN中指定了charset, 是否还需要执行 set names <charset> 呢?

    是的,不能省。set names <charset>其实有两个作用:

    1. 告诉 MySQL Server,客户端(PHP程序)提交给它的编码是什么;
    2. 告诉 MySQL Server,客户端需要的结果的编码是什么;

    也就是说,如果数据表使用gbk字符集,而PHP程序使用UTF-8编码,我们在执行查询前运行 set names utf8, 告诉 MySQL Server 正确编码即可,无须在程序中编码转换。这样我们以utf-8编码提交查询到MySQL Server, 得到的结果也会是utf-8编码。省却了程序中的转换编码问题,不要有疑问,这样做不会产生乱码。

    那么在DSN中指定charset的作用是什么?

    只是告诉PDO,本地驱动转义时使用指定的字符集(并不是设定MySQL Server通信字符集),设置MySQL Server通信字符集,还得使用 set names <charset> 指令。

    不要再尝试自己编写SQL注入过滤函数库了(又繁琐而且很容易产生未知的漏洞)。

    参考:

    http://zhangxugg-163-com.iteye.com/blog/1835721

    PDO::ATTR_EMULATE_PREPARES属性设置为false引发的血案

  • 相关阅读:
    Smobiler实现手机弹窗
    在 ASP.NET Web API 中使用 Attribute 统一处理异常
    VMWare的网络模式说明
    Windows Server2008 IIS通过Nginx实现绑定多个https域名
    Error while Launching activity
    Android 动态渐变按钮
    mysql安装和配置详解以及Navicat连接失败问题
    selenium的基础学习
    使用Pandas.read_csv时出现OSError: Initializing from file failed
    sklearn 机器学习 Pipeline 模板
  • 原文地址:https://www.cnblogs.com/52php/p/6096170.html
Copyright © 2011-2022 走看看