javascript - 这种 XSS 攻击是如何工作的?

标签 javascript django xss

剧透警告:此问题包含对 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('&#39;);alert(&#39;xss');" />
    <br>
    <div id="message">Your timer will execute in &#39;);alert(&#39;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/

相关文章:

javascript - 可以使用 DOM 的 setAttribute 函数设置多个属性吗?

javascript - 使用SinonJS 欺骗API?

django - 如何在 Django 表单中将标签设置为粗体?

xss - XSS 是如何工作的?

javascript - 将可能不安全的用户输入从隐藏输入字段传输到 DOM

javascript - 如何在 calc 函数中使用变量值

javascript - 如何从图表js中的图表中删除轴线

javascript - 使用 Jquery 附加 Django 模板标签

python - 我应该把 models.py 放在 Django 的什么地方?

regex - XSS 过滤器避免表单注入(inject)匹配一个它不应该匹配的字符串