Table of Contents
0x02 正确的处理方式
0x03 Dom XSS Vulnerability
0x04 源码分析
0x05 执行测试代码
0x06 修复方案
0x07 VulnTimeline
Home Web Front-end HTML Tutorial 明枪易躲暗箭难防 – JSONView 0day_html/css_WEB-ITnose

明枪易躲暗箭难防 – JSONView 0day_html/css_WEB-ITnose

Jun 24, 2016 am 11:19 AM

0x01 JSONView 介绍

  • Github:  https://github.com/gildas-lormeau/JSONView-for-Chrome

  • ChromeStore:  https://chrome.google.com/w.....bnpoihckbnefhakgolnmc?hl=en-US

JSONView 插件是目前最热门的一款开发者工具插件,它是查看json数据的神器。通常来讲,json 数据一般没有经过格式化或经过了 unicode 编码,没有缩进,没有换行等,给开发者阅读造成了一定困难。而jsonview 插件可以自动对 json 数据转码,缩进,格式化,直接显示出格式化后的数据,使得开发人员可以更好的阅读信息,本文中出现问题的版本为 Chrome 浏览器下的 JSONView 插件,Firefox 下版本不受影响。

0x02 正确的处理方式

我们知道开发人员在使用 JSONP callback 的方式进行跨域请求时,通常会为了方便前端调用 callback 名是可自定义的,例如 function?callback=jQuery14114 ,这时页面将会输出 callback 的参数值到页面中,所以出现了很多 callback 导致的跨站漏洞,解决方案大多由过滤 URL 特殊字符、严格定义 Response 头的 Content-Type:application/json 。

下面这个例子则为 Bilibili API :

http : //api.bilibili.com/x/favourite/folder?callback=jQu%3Ch1%3E163&jsonp=jsonp&_=1461828995783

c334041bjw1f3cq8flqacj20s70h9n1z.jpg

我们可以看到由于 Reponse Headers 严格定义了 Content-Type 类型为 json 数据格式,所以尽管我成功注入了未转义标签代码,但仍然不会得到执行(ChromeView-source 模式下如果正常解析后是会有高亮标识的),JSONView 的故事也是从这个时候开始的。

0x03 Dom XSS Vulnerability

我们之前谈到过 JSONView 插件能够美化原本乱糟糟的 JSON 数据,就像这样:

1.jpg

在使用 JSONView 的过程中我发现它把数据提取进行了渲染,也就导致原本不存在的漏洞在这里得以重现!这里有一个前提,网站使用限制 Content-Type 类型对其的过滤而未过滤特殊字符。所以我们可以看到:

2.jpg

在之后的源码分析中得知是因为通过 DOM 插入数据,直接写入 script 是不加载资源的,所以我们可以使用很多方法来触发恶意代码:

1. <img src=@ onerror=alert(1)>
2. <body onload=alert(1)>
3. ...
Copy after login

通过测试后发现,只要参数中包含 空格、圆括号,插件即使是在开启的情况下,也不会再对 JSON 结果进行数据解析,虽然在黑盒测试过程中我通过斜线等技巧实现了加载恶意代码,但还是要来看看这个插件究竟做了哪些处理。

3.jpg

0x04 源码分析

通过断点调试,寻找到 innerHTML 文件:/master/WebContent/content.js

function displayUI(theme, html) {
    var statusElement, toolboxElement, expandElement, reduceElement, viewSourceElement, optionsElement, content = "";
    content += &#39;<link rel="stylesheet" type="text/css" href="&#39; + chrome.runtime.getURL("jsonview-core.css") + &#39;">&#39;;
    content += "<style>" + theme + "</style>";
    content += html;
    document.body.innerHTML = content;
    ....
}
    
function init(data) {
    port.onMessage.addListener(function(msg) {
        if (msg.oninit) {
            options = msg.options;
            processData(data);
        }
        if (msg.onjsonToHTML)
            if (msg.html) {
                displayUI(msg.theme, msg.html);
            } else if (msg.json)
                port.postMessage({
                    getError : true,
                    json : json,
                    fnName : fnName
                });
        if (msg.ongetError) {
            displayError(msg.error, msg.loc, msg.offset);
        }
    });
    port.postMessage({
        init : true
    });
}
Copy after login

我们看到在 displayUI 函数中 innerHTML 的操作:document.body.innerHTML = content; ,下面来看一下 jsonToHTML 函数在入口文件 /JSONView-for-Chrome/blob/master/WebContent/background.js 中加载:

4.jpg

/master/WebContent/workerFormatter.js 提取核心部分代码:

function htmlEncode(t) {
    return t != null ? t.toString().replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;") : &#39;&#39;;
}
    
function decorateWithSpan(value, className) {
    return &#39;<span class="&#39; + className + &#39;">&#39; + htmlEncode(value) + &#39;</span>&#39;;
}
    
function jsonToHTML(json, fnName) {
    var output = &#39;&#39;;
    if (fnName)
        output += &#39;<div class="callback-function">&#39; + fnName + &#39;(</div>&#39;;
    output += &#39;<div id="json">&#39;;
    output += valueToHTML(json);
    output += &#39;</div>&#39;;
    if (fnName)
        output += &#39;<div class="callback-function">)</div>&#39;;
    return output;
}
    
addEventListener("message", function(event) {
    var object;
    try {
        object = JSON.parse(event.data.json);
    } catch (e) {
        postMessage({
            error : true
        });
        return;
    }
    postMessage({
        onjsonToHTML : true,
        html : jsonToHTML(object, event.data.fnName)
    });
}, false);
Copy after login

之后对 html : jsonToHTML(object, event.data.fnName) 其中的 event 下断点进行追踪,找到 fnName 的赋值代码 /master/WebContent/content.js:

function extractData(rawText) {
    var tokens, text = rawText.trim();
    
    function test(text) {
        return ((text.charAt(0) == "[" && text.charAt(text.length - 1) == "]") || (text.charAt(0) == "{" && text.charAt(text.length - 1) == "}"));
    }
    
    if (test(text))
        return {
            text : rawText,
            offset : 0
        };
    tokens = text.match(/^([^\s\(]*)\s*\(([\s\S]*)\)\s*;?$/);
    if (tokens && tokens[1] && tokens[2]) {
        if (test(tokens[2].trim()))
            return {
                fnName : tokens[1],
                text : tokens[2],
                offset : rawText.indexOf(tokens[2])
            };
    }
}
Copy after login

在 extractData 函数中,我们找到了 fnName 的赋值,tokens 会根据正则获取需要解析的 fnName, text 等值,也就是这个正则导致我们是无法注入圆括号的,因为他被匹配到了 text 中:

4.jpg

0x05 执行测试代码

通过阅读正则,我们发现可以使用 URL编码(HTML 实体编码)+斜线等方式来注入代码并执行:

    1. <img/src=&#39;@&#39;/onerror=alert(window.location)>
    2. <img/src=&#39;@&#39;/onerror=alert(window.location)>
    3. <img/src=&#39;@&#39;/onerror=%3Cimg/src=%27@%27/onerror=%26%2397%3B%26%23108%3B%26%23101%3B%26%23114%3B%26%23116%3B%26%2340%3B%26%23119%3B%26%23105%3B%26%23110%3B%26%23100%3B%26%23111%3B%26%23119%3B%26%2346%3B%26%23108%3B%26%23111%3B%26%2399%3B%26%2397%3B%26%23116%3B%26%23105%3B%26%23111%3B%26%23110%3B%26%2341%3B%3E>
    4. http://api.bilibili.com/x/favourite/folder?callback=jQu11111%3Cimg/src=%27@%27/onerror=%26%2397%3B%26%23108%3B%26%23101%3B%26%23114%3B%26%23116%3B%26%2340%3B%26%23119%3B%26%23105%3B%26%23110%3B%26%23100%3B%26%23111%3B%26%23119%3B%26%2346%3B%26%23108%3B%26%23111%3B%26%2399%3B%26%2397%3B%26%23116%3B%26%23105%3B%26%23111%3B%26%23110%3B%26%2341%3B%3E765386356466327_1461828974439&jsonp=jsonp&_=1461828995783
Copy after login

通过几次变形,我们编写出最终的测试代码以弹出 window.location 地址,就这样原本过滤严谨的接口因为 JSONView 的问题而全面崩塌:

5.jpg

0x06 修复方案

使用 /master/WebContent/workerFormatter.js 文件中的 htmlEncode 函数进行过滤:

    function jsonToHTML(json, fnName) {
        var output = &#39;&#39;;
        if (fnName)
            output += &#39;<div class="callback-function">&#39; + htmlEncode(fnName) + &#39;(</div>&#39;;
        output += &#39;<div id="json">&#39;;
        output += valueToHTML(json);
        output += &#39;</div>&#39;;
        if (fnName)
            output += &#39;<div class="callback-function">)</div>&#39;;
        return output;
    }
Copy after login

0x07 VulnTimeline

  • Find the vulnerability. - 2016/04/28 15:00

  • Because the JSONView plug-in (Chrome platform) has not been updated for a long time, Unable to contact the author to fix the vulnerability. - 2016/04/28 20:15

  • Write the Paper, via @evi1m0. - 2016/04/28 22:32

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

See all articles