php sql条件不能用%3c,使用PHP和Zend Framework防止SQL注入 – 如何?

我正试图通过SQL注入来保护我页面上的登录表单.在服务器端,我使用Zend Framework(Zend_Db,Zend_Db_Table_Abstract),但它的内置注入防止功能:quote,quoteInto,quoteIdentifier不能很好地工作(据我知道如何使用它们).其他方式如

mysql_real_escape_string,addslashes似乎根本不起作用……

这就是我正在尝试为防御实现的:

function prevent_from_sql_injection($str) {

if(preg_match('/[\'"]/', $str))

{die('attack1'); exit; }// no quotes

elseif(preg_match('/[\/\\\\]/', $str))

{die('attack2'); exit; }// no slashes

elseif(preg_match('/(and|or|null|not)/i', $str))

{die('attack3'); exit; }// no sqli boolean keywords

elseif(preg_match('/(union|select|from|where)/i', $str))

{die('attack4'); exit; }// no sqli select keywords

elseif(preg_match('/(group|order|having|limit)/i', $str))

{die('attack5'); exit; }// no sqli select keywords

elseif(preg_match('/(into|file|case|LOAD_FILE|DUMPFILE|char|schema|AES_DECRYPT|AES_ENCRYPT)/i', $str))

{die('attack6'); exit; }// no sqli operators

elseif(preg_match('/(--|#|\/\*)/', $str))

{die('attack7'); exit; }// no sqli comments

elseif(preg_match('/(=|&|\|)/', $str))

{die('attack8'); exit; }// no boolean operators

elseif(preg_match('/(UNI\*\*ON|1 OR 1=1|1 AND 1=1|1 EXEC XP_)/', $str))

{die('attack9'); exit; }

elseif(preg_match('/(1|'| |O|R|=|1' OR '1'='1|%31%27%20%4F%52%20%27%31%27%3D%27%31)/', $str))

{ die('attack10'); exit; }

elseif(preg_match('/(SELECT\s[\w\*\)\(\,\s]+\sFROM\s[\w]+)| (UPDATE\s[\w]+\sSET\s[\w\,\'\=]+)| (INSERT\sINTO\s[\d\w]+[\s\w\d\)\(\,]*\sVALUES\s\([\d\w\'\,\)]+)| (DELETE\sFROM\s[\d\w\'\=]+)/', $str))

{ die('attack11'); exit; }

elseif(preg_match('/(script)|(<)|(>)|(%3c)|(%3e)|(SELECT) |(UPDATE) |(INSERT) |(DELETE)|(GRANT) |(REVOKE)|(UNION)|(&lt;)|(&gt;)/', $str))

{ die('attack12'); exit; }

elseif(!preg_match('/^["a-zA-Z0-9\040]+$/', $str))

{ die('attack13'); exit; }

else return $str;

}

至于测试我的结果,我使用Firefox扩展SQL Inject Me,它显示了14个错误(有时21或17,我不知道为什么结果不同):

Server Status Code: 302 Found

Tested value: 1' OR '1'='1

Server Status Code: 302 Found

Tested value: 1 UNI/**/ON SELECT ALL FROM WHERE

Server Status Code: 302 Found

Tested value: 1' OR '1'='1

Server Status Code: 302 Found

Tested value: 1 OR 1=1

Server Status Code: 302 Found

Tested value: 1' OR '1'='1

Server Status Code: 302 Found

Tested value: 1 EXEC XP_

Server Status Code: 302 Found

Tested value: 1 UNION ALL SELECT 1,2,3,4,5,6,name FROM sysObjects WHERE xtype = 'U' --

Server Status Code: 302 Found

Tested value: %31%27%20%4F%52%20%27%31%27%3D%27%31

Server Status Code: 302 Found

Tested value: 1 AND 1=1

Server Status Code: 302 Found

Tested value: 1' OR '1'='1

Server Status Code: 302 Found

Tested value: 1 AND ASCII(LOWER(SUBSTRING((SELECT TOP 1 name FROM sysobjects WHERE xtype='U'), 1, 1))) > 116

那么防止所有这些SQL注入攻击的最佳方法是什么?使用占位符是好的,但在某些情况下它并不好.也许这个扩展是错误的,我有一个偏执狂?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值