当说到防止SQL注入的办法时,脑海中总是会想到运用PDO绑定参数的办法或许运用mysql_real_eascape_string()来处理(尽管陈旧的 mysql_XXX 这类的函数现已不主张运用)。可是PDO是怎么防止注入的呢?
在手册中,有这样一段:
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 *** yze, 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 *** yze/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句子的机制。预处理句子能够带来两大优点:
预处理句子十分有用,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官方手册这儿有 阐明 :
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)办法
360站长渠道中有一个东西是“官网直达”,经过恳求能够使你的网站在360搜索成果中加上“官网”字样的标识,百度也有这样的东西,不过是收费的,所以趁着360还没收费,有爱好的朋友可认为自己的网站恳求一...
本文我得先从我最近参加的一个安全检测项目开端谈起,本次的客户是一家企业,不得不说,本次咱们的客户的安全防护做得非常好。他们的安全运营中心(SOC)装备了许多先进的内部反常检测东西以及坚强的作业呼应团队...
咱们好!在开端正式的内容之前,请答应我做个简略的毛遂自荐。首要,我要阐明的是我不是什么安全研究人员/安全工程师,切当的来说我是一名安全的爱好者,这始于两年前的Uber。我喜爱触摸新的事物,而且每天都在...
Subversion概述 概述 Subversion,简称SVN,是一个开放源代码的版别控制体系,相对于的RCS、CVS,采用了分支办理体系。 网站更新拓扑结构 ...
为什么有今日这篇文章?原因是我在阅读Twitter时,发现关于长途文件包括RFI的一个奇淫技巧!值得记载一下,思路也很别致!由于它打破我之前认为RFI已死的观念:) 正文 RFI引出 咱们知道php最...
SQL注入原理,在URI页面加参数查询数据库,假如程序没有严厉过滤字符串,就有或许导致SQL注入 咱们能够在前端Nginx过滤URI来避免SQL注入。装备如下 什么是URL和URI,举例说明: 衔接...