dedeCMS /data/mysql_error_trace.php DB error raised PHP Code Injection Via /include/dedesql.class.php Log FIle Without Access Validation

时间:2023-01-29 09:02:16

目录

. 漏洞描述
. 漏洞触发条件
. 漏洞影响范围
. 漏洞代码分析
. 防御方法
. 攻防思考

1. 漏洞描述

dedecms采用面向对象封装的方式实现了功能操作的模块集中化,例如对于数据库管理

. /include/dedesql.class.php: mysql数据库操作
. /include/dedesqli.class.php: mysqli数据库操作

当发生数据库操作语句执行错误的时候,框架代码会集中对错误相关信息进行收集,并保存到一个指定的文件中(/data/mysql_error_trace.php),但是这里存在一个安全上的漏洞

. MySQL字段数值采用了C语言同样的定义,当传入的数值大于字段定义的类型的时候,将引发整型上溢出错误

. /data/mysql_error_trace.php没有进行正确的访问认证
/*
security practice
在不需要被访问的日志文件头应该加上以下代码
die('Request Error!');
or
exit();
*/ . 日志文件最好使用例如".txt"的文本文件进行保存

真正导致漏洞的原因是数据库处理库(/include/dedesql.class.php),而触发这个漏洞的文件有很多,攻击向量路径很多,理论上只要调用了如下代码即存在漏洞

/*
$dsql->ExecuteNoneQuery("Update `$maintable` set scores = scores + {$cfg_caicai_add},goodpost=goodpost+1,lastpost=".time()." where id=$id");
*/

目前已知的可以成为攻击向量的文件有

. /plus/digg_frame.php
. /plus/digg_ajax.php
. /plus/comments_frame.php

Relevant Link:

http://icarusli.iteye.com/blog/610715
http://www.007hack.com/?p=522

2. 漏洞触发条件
3. 漏洞影响范围

. < dede 5.7

4. 漏洞代码分析

/include/dedesql.class.php

//显示数据链接错误信息
function DisplayError($msg)
{
$errorTrackFile = dirname(__FILE__).'/../data/mysql_error_trace.inc';
//这里将日志文件的后缀改为了.inc,是一个好的防御方法
if( file_exists(dirname(__FILE__).'/../data/mysql_error_trace.php') )
{
@unlink(dirname(__FILE__).'/../data/mysql_error_trace.php');
}
$emsg = '';
$emsg .= "<div><h3>DedeCMS Error Warning!</h3>\r\n";
$emsg .= "<div><a href='http://bbs.dedecms.com' target='_blank' style='color:red'>Technical Support: http://bbs.dedecms.com</a></div>";
$emsg .= "<div style='line-helght:160%;font-size:14px;color:green'>\r\n";
$emsg .= "<div style='color:blue'><br />Error page: <font color='red'>".$this->GetCurUrl()."</font></div>\r\n";
$emsg .= "<div>Error infos: {$msg}</div>\r\n";
$emsg .= "<br /></div></div>\r\n"; echo $emsg; $savemsg = 'Page: '.$this->GetCurUrl()."\r\nError: ".$msg;
//保存MySql错误日志
$fp = @fopen($errorTrackFile, 'a');
//直接将错误信息写入了可执行的.PHP文件中
@fwrite($fp, '<'.'?php'."\r\n/*\r\n{$savemsg}\r\n*/\r\n?".">\r\n");
@fclose($fp);
}

5. 防御方法

//显示数据链接错误信息
function DisplayError($msg)
{
$errorTrackFile = dirname(__FILE__).'/../data/mysql_error_trace.inc';
if( file_exists(dirname(__FILE__).'/../data/mysql_error_trace.php') )
{
@unlink(dirname(__FILE__).'/../data/mysql_error_trace.php');
}
$emsg = '';
$emsg .= "<div><h3>DedeCMS Error Warning!</h3>\r\n";
$emsg .= "<div><a href='http://bbs.dedecms.com' target='_blank' style='color:red'>Technical Support: http://bbs.dedecms.com</a></div>";
$emsg .= "<div style='line-helght:160%;font-size:14px;color:green'>\r\n";
$emsg .= "<div style='color:blue'><br />Error page: <font color='red'>".$this->GetCurUrl()."</font></div>\r\n";
$emsg .= "<div>Error infos: {$msg}</div>\r\n";
$emsg .= "<br /></div></div>\r\n"; echo $emsg; $savemsg = 'Page: '.$this->GetCurUrl()."\r\nError: ".$msg."\r\nTime".date('Y-m-d H:i:s');
//保存MySql错误日志
$fp = @fopen($errorTrackFile, 'a');
@fwrite($fp, '<'.'?php' . "\r\n" . "die('Request Error!');" . "\r\n/*\r\n{$savemsg}\r\n*/\r\n?".">\r\n");
@fclose($fp);
}

6. 攻防思考

Copyright (c) 2014 LittleHann All rights reserved