### 简要描述: Thinksaas SQL注入#2 ### 详细说明: Thinksaas 最新版在小组发表帖子时,帖子内容中可以@其他用户,这里在处理@的用户时,只在前台过滤,未在后台过滤,而且没有通过sql安全检查,直接拼接到sql语句中,导致sql注入。还有一处是在编辑补贴内容处,title和content都么过滤 来看看发帖的地方,参数的传递: ``` // 执行发布帖子 case "do" : if ($_POST ['token'] != $_SESSION ['token']) { tsNotice ( '非法操作!' ); } $authcode = strtolower ( $_POST ['authcode'] ); if ($TS_SITE ['base'] ['isauthcode']) { if ($authcode != $_SESSION ['verify']) { tsNotice ( "验证码输入有误,请重新输入!" ); } } $groupid = intval ( $_POST ['groupid'] ); $title = tsClean ( $_POST ['title'] ); $content = tsClean ( $_POST ['content'] ); $typeid = intval ( $_POST ['typeid'] ); $tag = tsClean ( $_POST ['tag'] ); ``` 跟进tdClean函数: ``` function cleanJs($text) { $text = trim ( $text ); $text = stripslashes ( $text ); // 完全过滤注释 $text = preg_replace ( '/<!--?.*-->/', '', $text ); // 完全过滤动态代码 $text = preg_replace ( '/<\?|\?>/', '', $text ); // 完全过滤js $text = preg_replace ( '/<script?.*\/script>/', '', $text ); // 过滤多余html $text = preg_replace (...
### 简要描述: Thinksaas SQL注入#2 ### 详细说明: Thinksaas 最新版在小组发表帖子时,帖子内容中可以@其他用户,这里在处理@的用户时,只在前台过滤,未在后台过滤,而且没有通过sql安全检查,直接拼接到sql语句中,导致sql注入。还有一处是在编辑补贴内容处,title和content都么过滤 来看看发帖的地方,参数的传递: ``` // 执行发布帖子 case "do" : if ($_POST ['token'] != $_SESSION ['token']) { tsNotice ( '非法操作!' ); } $authcode = strtolower ( $_POST ['authcode'] ); if ($TS_SITE ['base'] ['isauthcode']) { if ($authcode != $_SESSION ['verify']) { tsNotice ( "验证码输入有误,请重新输入!" ); } } $groupid = intval ( $_POST ['groupid'] ); $title = tsClean ( $_POST ['title'] ); $content = tsClean ( $_POST ['content'] ); $typeid = intval ( $_POST ['typeid'] ); $tag = tsClean ( $_POST ['tag'] ); ``` 跟进tdClean函数: ``` function cleanJs($text) { $text = trim ( $text ); $text = stripslashes ( $text ); // 完全过滤注释 $text = preg_replace ( '/<!--?.*-->/', '', $text ); // 完全过滤动态代码 $text = preg_replace ( '/<\?|\?>/', '', $text ); // 完全过滤js $text = preg_replace ( '/<script?.*\/script>/', '', $text ); // 过滤多余html $text = preg_replace ( '/<\/?(html|head|meta|link|base|body|title|style|script|form|iframe|frame|frameset)[^><]*>/i', '', $text ); // 过滤on事件lang js while ( preg_match ( '/(<[^><]+)(lang|onfinish|onmouse|onexit|onerror|onclick|onkey|onload|onchange|onfocus|onblur)[^><]+/i', $text, $mat ) ) { $text = str_replace ( $mat [0], $mat [1], $text ); } while ( preg_match ( '/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i', $text, $mat ) ) { $text = str_replace ( $mat [0], $mat [1] . $mat [3], $text ); } return $text; } /** * 输入安全过滤 * @param unknown $text * @return mixed */ function tsClean($text) { $text = cleanJs ( $text ); return $text; } ``` 只是将content内容对js相关内容进行了过滤。 再来看看处理@用户时的操作: ``` // 处理@用户名 if (preg_match_all ( '/@/', $content, $at )) { preg_match_all ( "/@(.+?)([\s|:]|$)/is", $content, $matches ); $unames = $matches [1]; $ns = "'" . implode ( "','", $unames ) . "'"; $csql = "username IN($ns)"; if ($unames) { $query = $db->fetch_all_assoc ( "select userid,username from " . dbprefix . "user_info where $csql" );//注入点在这里 foreach ( $query as $v ) { $content = str_replace ( '@' . $v ['username'] . '', '[@' . $v ['username'] . ':' . $v ['userid'] . ']', $content ); $msg_content = '我在帖子中提到了你<br />去看看:' . tsUrl ( 'group', 'topic', array ( 'id' => $topicid ) ); aac ( 'message' )->sendmsg ( $userid, $v ['userid'], $msg_content ); } $new ['group']->update ( 'group_topic', array ( 'topicid' => $topicid ), array ( 'content' => $content ) ); } ``` 匹配到@用户时的内容后,通过空格:冒号进行匹配,然后用都好分割,去第二个,放到sql语句中,最后带入查询,造成sql注入。 ### 漏洞证明: 我们发表帖子,然后抓包: [<img src="https://images.seebug.org/upload/201312/19161622bec5bf0d556f7a060ecdb71d1091da30.png" alt="5.png" width="600" onerror="javascript:errimg(this);">](https://images.seebug.org/upload/201312/19161622bec5bf0d556f7a060ecdb71d1091da30.png) [<img src="https://images.seebug.org/upload/201312/19161633921270c49c3c75b2e54a6d62661511ef.png" alt="6.png" width="600" onerror="javascript:errimg(this);">](https://images.seebug.org/upload/201312/19161633921270c49c3c75b2e54a6d62661511ef.png) 这里的content内容在前台进行了过滤,但是我们抓包含可以进行修改,如上图修改。 我们的sql语句会被执行,延迟10秒,帖子发表成功。 看看数据库的执行日志,以及此语句的执行结果: [<img src="https://images.seebug.org/upload/201312/191616441ba2219790362423bf5406d5152c539e.png" alt="7.png" width="600" onerror="javascript:errimg(this);">](https://images.seebug.org/upload/201312/191616441ba2219790362423bf5406d5152c539e.png)