【问题标题】:How is this XSS attack working?这个 XSS 攻击是如何工作的?
【发布时间】:2014-06-10 20:12:12
【问题描述】:

剧透警告:此问题包含对来自Google's XSS Challenge! 的问题之一的回答。如果您现在不想知道答案,请停止继续阅读。


我能够通过level 4 of the challenge,但是,我仍然不知道该漏洞利用的具体情况如何。以下是来自 Google 的 XSS 挑战 - Level 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>

【问题讨论】:

    标签: javascript django xss


    【解决方案1】:

    让您感到困惑的是两种不同语言的混合:HTML 和 JavaScript。 &amp;#39; 是 HTML。它在显示时被翻译成' 字符并被解释为JavaScript。这意味着,从 JavaScript 解释器的角度来看,'&amp;#39; 之间没有区别。代码onload="startTimer('&amp;#39;);alert(&amp;#39;xss');" 实际上与onload="startTimer('');alert('xss');" 相同,尽管乍一看它看起来不应该工作。

    【讨论】:

    • 哦!这真的很令人困惑。我认为字符串&amp;#39;);alert(&amp;#39;xss 被传递给方法startTimer 并在方法中调用警报。
    • +1 为答案。正因为如此,我才能够找到它到底为什么起作用!
    【解决方案2】:

    我明白 XSS 为何起作用了! One of the section in the OWASP XSS cheat sheet 说:

    HTML 实体编码适用于您放入的不受信任的数据 HTML 文档的正文,例如在标签内。它甚至有点 适用于进入属性的不受信任的数据,特别是如果 你对在你的属性周围使用引号很虔诚。但是HTML 如果您将不受信任的数据放入 标记任何地方,或者像 onmouseover 这样的事件处理程序属性, 或在 CSS 中,或在 URL 中。所以即使你使用 HTML 实体编码 方法无处不在,你仍然很可能容易受到 XSS 的攻击。你 必须对您要处理的 HTML 文档部分使用转义语法 将不受信任的数据放入。这就是以下规则的全部内容 关于。

    在这种情况下,用户输入被馈送到事件处理程序中,该处理程序会将其视为 JS 而不是 HTML。而且,输入在 HTML 上下文中被转义(而不是在 JS 上下文中)。因此,JS 会将startTimer('3&amp;#39;) || alert(&amp;#39;1'); 视为startTimer('') || alert('1'); 并简单地运行此脚本。

    PS:JS 转义可能阻止了攻击。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-29
      • 2017-03-30
      • 1970-01-01
      • 2022-01-08
      • 2011-02-16
      • 1970-01-01
      相关资源
      最近更新 更多