剧透警告:此问题包含对 Google's XSS Challenge 中问题之一的回答。 !如果您现在不想知道答案,请停止继续阅读。
我能够通过 level 4 of the challenge ,但是,我仍然不知道这个漏洞利用到底是如何工作的。以下是来自 Google 的 XSS 挑战 - 级别 4 的代码:
<!doctype html>
<html>
<head>
<!-- Internal game scripts/styles, mostly boring stuff -->
<script src="/static/game-frame.js"></script>
<link rel="stylesheet" href="/static/game-frame-styles.css" />
<script>
function startTimer(seconds) {
seconds = parseInt(seconds) || 3;
setTimeout(function() {
window.confirm("Time is up!");
window.history.back();
}, seconds * 1000);
}
</script>
</head>
<body id="level4">
<img src="/static/logos/level4.png" />
<br>
<img src="/static/loading.gif" onload="startTimer('{{ timer }}');" />
<br>
<div id="message">Your timer will execute in {{ timer }} seconds.</div>
</body>
</html>
基本上,他们使用的是 Django 框架 ( which uses a bunch of security measure against XSS )。变量 timer
携带来自用户的输入。此事件的目标是通过发送可以绕过 Django 的 XSS 安全性的负载来提醒消息。
我可以使用以下有效负载之一来提醒消息:
');alert('xss
或
3') || alert('1
我可以使用上述有效载荷清除关卡,但我仍然不确定 alert() 方法的确切调用位置?在 onload
处理程序中或在 startTimer()
方法中?
我很困惑,因为如果我在提交负载后检查页面的源 HTML,Django 正在对负载进行编码:
<html>
<head>
<!-- Internal game scripts/styles, mostly boring stuff -->
<script src="/static/game-frame.js"></script>
<link rel="stylesheet" href="/static/game-frame-styles.css" />
<script>
function startTimer(seconds) {
seconds = parseInt(seconds) || 3;
setTimeout(function() {
window.confirm("Time is up!");
window.history.back();
}, seconds * 1000);
}
</script>
</head>
<body id="level4">
<img src="/static/logos/level4.png" />
<br>
<img src="/static/loading.gif" onload="startTimer('');alert('xss');" />
<br>
<div id="message">Your timer will execute in ');alert('xss seconds.</div>
</body>
</html>
最佳答案
似乎让您感到困惑的是两种不同语言的混合:HTML 和 JavaScript。 '
是 HTML。它在显示时被翻译成 '
字符并且被解释为 JavaScript。这意味着,从 JavaScript 解释器的 Angular 来看,'
和 '
之间没有区别。代码 onload="startTimer('');alert('xss');"
实际上与 onload="startTimer('');alert 相同('xss');"
尽管乍一看它看起来不应该工作。
关于javascript - 这种 XSS 攻击是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24122340/