| 关键词: nbsp email str 函数 replace 代码 Discuz mailto 评论 漏洞 |
Author: RickGrayDate: 2015-12-09之前(2015-03-07)乌云上报了 《Discuz全版本存储型DOM XSS(可打管理员)附Discuz官方开发4大坑&验证脚本》 ,但是在 Discuz版本(2015-06-09)的修复中却因为修复不全导致漏洞仍然可以触发。 一、原理分析Discuz 在用户评论处设置了帖子管理员编辑评论的功能,由于前端 JS 代码处理不当导致了经过恶意构造的评论内容在经过交互后形成 XSS 。下面通过 payload 的调试过程来解释该漏洞的形成过程。 首先,在评论处提交评论内容: [ email = 2 "onmouseover=" alert ( 2 ) ] 2 [ / email ] 由于服务器对引号等有过滤,所以提交后,查看源码会发现引号已经被实体编码了。 对于普通用户提交的评论,管理员或者版主都有权利对其发表的评论进行管理。 当管理或版主对用户的评论点击管理时,前端 JS 代码就开始处理,弹出一个编辑框供管理或版主操作。在 JS 代码处理的过程中,首先获取用户评论的内容,代码位于当前页面中: 而 $ ( ) 函数原型位于 /static/js/common.js 中:
使用了原生的 document . getElementById ( ) 函数来获取页面中的对应对象,此处获取的是标有 id = ” e _ textarea” 的对象,其对应的值为用户评论的内容。 而由于 JS 原生函数的原因,被服务器后端转义的引号会被重新是渲染回引号: 获取到 id = ” e _ textarea” 对象后,代码对浏览器进行了判断,并将结果赋值给变量var wysiwyg 。 在页面上另一处JS代码判断了变量 wysiwyg 的值,然后开始渲染编辑框:
这里使用了 Firfox 浏览器进行测试,在前面 wysiwyg 变量的值为1,所以会执行如下代码: newEditor(1, bbcode2html(textobj.value)) newEditor(1,bbcode2html(textobj.value)) 其中 textobj.value 的值为: [ email = 2 "onmouseover=" alert ( 2 ) ] 2 [ / email ](经过 document.getElementById() 获取的对象解析了实体编码) 在进行 newEditor ( ) 时,会对传入的内容使用函数 bbcode2html ( ) 进行编码过滤,其函数原型位于 /static/js/bbcode.js,下面是Discuz对程序所支持的 shortcode 进行处理的部分代码。 程序匹配其支持的 shortcode 然后正则替换为相应的前端格式代码,因此次测试的 payload 为 [ email = 2 ” onmouseover = ” alert ( 2 ) ] 2 [ / email ] ,因此图中红色标注的代码会得到执行。 str = str.replace(/\[email=(.[^\[]*)\](.*?)\[\/email\]/ig, '<a href="mailto:$1" target="_blank">$2</a>'); str=str.replace(/\[email=(.[^\[]*)\](.*?)\[\/email\]/ig,'<a href="mailto:$1" target="_blank">$2</a>'); 经过正则匹配替换后,str 的值会变为: < a href = "mailto:2" onmouseover ="alert(2)" target = "_blank" > 2 < / a > :
最终 bbcode2html ( ) 函数会返回经过转换后的 textobj.value,值为: < a href ="mailto:2" onmouseover = "alert(2)" target = "_blank" > 2 < / a > 然后调用 newEditor ( ) 函数进编辑框的渲染,其函数原型位于 /static/js/editor.js: 从函数原型可以看到,代码再次判断浏览器类型然后开始渲染,由于 wysiwyq 变量的值为1,最终会执行: writeEditorContents(isUndefined(initialtext) ? textobj.value: initialtext);writeEditorContents(isUndefined(initialtext)?textobj.value:initialtext);
而调用 newEditor ( ) 函数时,传递了 initialtext 参数,其值就为经过 bbcode2html ( )函数处理后的 textobj.value 的值。 前端 JS 最终使用 writeEditorContents ( ) 函数对页面进行渲染,其过程中会将 initialtext 变量的值直接写入到页面中,最终形成XSS。 渲染成功后,查看页面源代码:
当管理员或者版主对其进行交互时就会触发 alert ( 2 ) : 即使后段服务器对输入内容进行了过滤和转义,但是在前段渲染的时候依然有可能形成 XSS。 二、补丁绕过Discuz版本(2015-06-09)中修复了在 《Discuz全版本存储型DOM XSS(可打管理员)附Discuz官方开发4大坑&验证脚本》 中所提到触发点。 但是通过分析补丁,发现仍有其他可用的 shortcode 可以在同一个点触发 XSS。 通过 diff 前后两个版本可以得到 bbcode.js 文件变化: 可以注意到在上图红色标注部分 shortcode 正则替换仍能够形成 XSS。 新的 Payload 为: [ email ] 2 "onmouseover=" alert ( 2 ) [ / email ] 发布新帖或者提交评论,在其内容中包含如下 payload: [email]2"onmouseover="alert(2)[/email] [email]2"onmouseover="alert(2)[/email] 然后对帖子或者评论进行编辑时,与页面进行一定交互时即可触发 XSS: 漏洞跟踪
官方针对 bbcode.js 的补丁如下: diff @@ -78,7 +78,7 @@
str = clearcode(str);
str = str.replace(/\[url\]\s*((https?|ftp|gopher|news|telnet|rtsp|mms|callto|bctp|thunder|qqdl|synacast){1}:\/\/|www\.)([^\[\"']+?)\s*\[\/url\]/ig, function($1, $
str = str.replace(/\[url=((https?|ftp|gopher|news|telnet|rtsp|mms|callto|bctp|thunder|qqdl|synacast){1}:\/\/|www\.|mailto:)?([^\r\n\[\"']+?)\]([\s\S]+?)\[\/url\]/
- str = str.replace(/\[email\](.*?)\[\/email\]/ig, '<a href="mailto:$1">$1</a>');
+ str = str.replace(/\[email\](.[^\\=[]*)\[\/email\]/ig, '<a href="mailto:$1">$1</a>');
str = str.replace(/\[email=(.[^\\=[]*)\](.*?)\[\/email\]/ig, '<a href="mailto:$1" target="_blank">$2</a>');
str = str.replace(/\[postbg\]\s*([^\[\<\r\n;'\"\?\(\)]+?)\s*\[\/postbg\]/ig, function($1, $2) {
addCSS = ''; @@ -78,7 +78,7 @@
str = clearcode(str);
str = str.replace(/\[url\]\s*((https?|ftp|gopher|news|telnet|rtsp|mms|callto|bctp|thunder|qqdl|synacast){1}:\/\/|www\.)([^\[\"']+?)\s*\[\/url\]/ig, function($1, $
str = str.replace(/\[url=((https?|ftp|gopher|news|telnet|rtsp|mms|callto|bctp|thunder|qqdl|synacast){1}:\/\/|www\.|mailto:)?([^\r\n\[\"']+?)\]([\s\S]+?)\[\/url\]/
- str = str.replace(/\[email\](.*?)\[\/email\]/ig, '<a href="mailto:$1">$1</a>');
+ str = str.replace(/\[email\](.[^\\=[]*)\[\/email\]/ig, '<a href="mailto:$1">$1</a>');
str = str.replace(/\[email=(.[^\\=[]*)\](.*?)\[\/email\]/ig, '<a href="mailto:$1" target="_blank">$2</a>');
str = str.replace(/\[postbg\]\s*([^\[\<\r\n;'\"\?\(\)]+?)\s*\[\/postbg\]/ig, function($1, $2) {
addCSS = ''; |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|