PDO 避免SQL注入的原理
当说到防止SQL注入的办法时,脑海中总是会想到运用PDO绑定参数的办法或许运用mysql_real_eascape_string()来处理(尽管陈旧的 mysql_XXX 这类的函数现已不主张运用)。可是PDO是怎么防止注入的呢?
在手册中,有这样一段:
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 it’s 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).
Prepared statements are so useful that they are the only feature that PDO will emulate for drivers that don’t support them. This ensures that an application will be able to use the same data access paradigm regardless of the capabilities of the database.
大约的翻译是:
许多更老练的数据库都支撑预处理句子的概念。这些是什么?它能够被认为是作为一种经过编译SQL句子模板来运转sql句子的机制。预处理句子能够带来两大优点:
查询只需求被解析(或编译)一次,但能够履行屡次经过相同或不同的参数。当查询处理好后,数据库将剖析,编译和优化它的方案来履行查询。关于杂乱的查询这个进程或许需求满足的时刻,这将显著地使得运用程序变慢,假如有必要,能够屡次运用不同的参数 重复相同的查询。经过运用处理好的句子的运用程序防止重复 【剖析/编译/优化】 周期。这意味着,预处理句子运用更少的资源,并且运转得更快。 绑定的参数不需求运用引号;该驱动程序会主动处理。假如运用程序运用预处理句子,开发人员能够确保不会发作SQL注入(可是,假如查询的其他部分运用了未转义的输入,SQL注入仍然是或许的)。
预处理句子十分有用,PDO能够运用一种本地模仿的办法来为没有预处理功用的数据库系统供给这个功用。这确保了一个运用能够运用一致的拜访办法来拜访数据库。
这儿讲了运用PDO能够带来两个很好的作用,预编译带来查询速度的进步,变量的绑定能够防备 sql injection,其实PDO的防备sql注入的机制也是类似于运用 mysql_real_escape_string 进行转义,PDO 有两种转义的机制,第一种是本地转义,这种转义的办法是运用单字节字符集(PHP < 5.3.6)来转义的( 单字节与多字节 ),来对输入进行转义,可是这种转义办法有一些 危险 。危险主要是:在PHP版别小于5.3.6的时分,本地转义只能转化单字节的字符集,大于 5.3.6 的版别会依据 PDO 衔接中指定的 charset 来转义。PHP官方手册这儿有 阐明 :
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.
所以就是说, 不同的版别的PDO 在本地转义的行为上是有差异的。
第二种办法是PDO,首要将 sql 句子模板发送给Mysql Server,随后将绑定的字符变量再发送给Mysql server,这儿的转义是在Mysql Server做的,它是依据你在衔接PDO的时分,在charset里指定的编码格局来转化的。这样的转义办法更健全,一同还能够在又屡次重复查询的事务场景下,经过复用模板,来进步程序的功能。假如要设置Mysql Server 来转义的话,就要首要履行:
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);下面是经过 wireshark 抓到的数据包,来详细显现PDO 查询的进程:
绑定的变量:
假如不履行 $pdo ->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); PDO 仅仅会将刺进的参数运用本地转义之后和SQL模板组装起来,然后一同发送给Mysql Server。这实际上与运用mysql_real_escape_string()过滤,然后组装这种做法并没有什么不同。
要对数据库的安全做出愈加全面的考量,以下两种办法任选其一:
A. 经过增加(php 5.3.6曾经版别):$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
B. 升级到php 5.3.6 (不必设置PDO::ATTR_EMULATE_PREPARES也能够)
为了程序移植性和一致安全性,主张运用$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false)办法