【问题标题】:Change a native function's body while keeping the same "identity"在保持相同“身份”的同时更改本机函数的主体
【发布时间】:2021-02-28 16:31:11
【问题描述】:

我正在寻找一种方法来更改本机 JS 函数体,同时使其无法看到它已被更改。我们以document.hasFocus()为例:

document.hasFocus = ()=>true;

这种方法可以很好地欺骗焦点,但很容易检测到它被修改了:

document.hasFocus.toString() // -> "()=>true"

有什么方法可以修改这样的功能,同时使其无法看到它已被篡改?

【问题讨论】:

    标签: javascript


    【解决方案1】:

    你可以覆盖函数原型中的 toString 方法,然后做类似的事情:

    // https://stackoverflow.com/questions/1833588/javascript-clone-a-function
    Function.prototype.clone = function() {
        var that = this;
        var temp = function temporary() {
            return that.apply(this, arguments);
        };
        for (var key in this) {
            if (this.hasOwnProperty(key)) {
                temp[key] = this[key];
            }
        }
        return temp;
    };
    
    Function.prototype.__oldToString = Function.prototype.toString.clone();
    
    function __toStringHooked() {
        if ((this.name == "")||(this.name == "hasFocus")) // on Firefox, hasFocus doesn't have any name
        {
            return eval+"" // this matches regexp
        } else {
            return this.__oldToString(); // we're returning default value
        }
    }
    
    Function.prototype.toString = __toStringHooked
    document.hasFocus = () => true
    

    以上代码来自 Th3B0r3dD3v3l0p3r 的 GitHub repo,如果需要可以查看:https://github.com/Th3B0r3dD3v3l0p3r/focus-spoofer/

    【讨论】:

      猜你喜欢
      • 2015-02-24
      • 1970-01-01
      • 2017-01-17
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多