PDO 避免SQL注入的原理

访客5年前黑客资讯950

 当说到防止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 *** 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句子的机制。预处理句子能够带来两大优点:

查询只需求被解析(或编译)一次,但能够履行屡次经过相同或不同的参数。当查询处理好后,数据库将剖析,编译和优化它的方案来履行查询。关于杂乱的查询这个进程或许需求满足的时刻,这将显著地使得运用程序变慢,假如有必要,能够屡次运用不同的参数 重复相同的查询。经过运用处理好的句子的运用程序防止重复 【剖析/编译/优化】 周期。这意味着,预处理句子运用更少的资源,并且运转得更快。 绑定的参数不需求运用引号;该驱动程序会主动处理。假如运用程序运用预处理句子,开发人员能够确保不会发作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)办法

相关文章

谁看了你的Instagram账户?又是谁盗取了你的暗码?

简介 移动运用程序现在现已成为最有用的进犯向量之一,这些网络罪犯最喜欢的一种办法就是盛行运用程序的乱用。自己审视下是否在装置一款需求衔接到交际运用账户凭据,电子邮件账户,云存储服务的运用时有静下来细细...

网站检测提示的“Flash装备不妥”是什么缝隙?

 360站长渠道中有一个东西是“官网直达”,经过恳求能够使你的网站在360搜索成果中加上“官网”字样的标识,百度也有这样的东西,不过是收费的,所以趁着360还没收费,有爱好的朋友可认为自己的网站恳求一...

记载一次使用事务规划缺点缝隙的精彩实战测验

前语 前次的那篇文章《一名代码审计新手的实战阅历与感悟》得到了不少读者的支撑,也得到了FreeBuf这个渠道的必定,这给了我这个菜的不能再菜的小菜鸟很大的决心。可是,不足之处仍是许多,比方文章中呈现的...

安全研究人员发现 Hacking Team 新开发不易发觉的Mac歹意软件

研究者在HackingTeam上发现了新开发的Mac歹意软件,这项发现促进了投机活动。自从上一年七月以来,这款臭名远扬的歹意软件造成了数Gbytes集团私家邮件和源代码的流出,现在这款软件作者再次出...

Web 服务器基准测验,nginx+php vs Apache+php

本次测验nginx+php与apache+php哪种组合的核算性能及稳定性更佳 操作系统:Centos6.4 x64 硬件环境:   服务器IP 硬件装备 人物 192.168.1.2 4中心 8G...

XSS终结者-CSP理论与实践

 CSP 全称为 Content Security Policy,即内容安全战略。首要以白名单的方法装备可信任的内容来历,在网页中,能够使白名单中的内容正常履行(包括 JS,CSS,Image 等等)...