我想计算用于动态解释 JavaScript 代码(例如eval和setTimeout)的函数调用的数量。我的主要目的是找到作为参数传递给 eval 和 setTimeout 的字符串。这是因为恶意脚本通常使用 eval 函数动态评估复杂代码。找到它的一种方法是挂钩该函数,以便可以触发对 eval 函数的每次调用。但我不知道该怎么做。请举例说明这一点。
我通常为 setTimeout 尝试过,但我没有得到正确的结果。
我的代码如下
var sto = setTimeout;
  setTimeout = function setTimeout(str){
  console.log(str);
  }
我的实际用户脚本是:
// ==UserScript==
// @encoding    UTF-8 
// @name        Count_setTimeout
// @run-at      document-start
// ==/UserScript==
addJS_Node("var count=0;");
function Log_StimeoutCreations ()
{
  var sto = setTimeout;
  setTimeout = function(arg, time) 
  {
     if(typeof arg == "string")
        console.log("Insecure setTimeout: ", arg);
     else
        console.log("setTimeout:",arg);   
     count++;
     sto(arg, time);    
  }
}
var waitForDomInterval = setInterval (
function () {
    var domPresentNode;
    if (typeof document.head == "undefined")
        domPresentNode = document.querySelector ("head, body");
    else
        domPresentNode = document.head;
    if (domPresentNode) {
        clearInterval (waitForDomInterval);
        addJS_Node (null, null, Log_StimeoutCreations);           
    }
},
1
);
function addJS_Node (text, s_URL, funcToRun) {
var D                                   = document;
var scriptNode                          = D.createElement ('script');
scriptNode.type                         = "text/javascript";
if (text)       scriptNode.textContent  = text;
if (s_URL)      scriptNode.src          = s_URL;
if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
targ.appendChild (scriptNode);
}
addJS_Node("document.onreadystatechange = function (){if (document.readyState == 'complete'){if(typeof(unsafeWindow)=='undefined') { unsafeWindow=window; } unsafeWindow.console.log('count--'+count);}}");
但我什至没有在源代码中查看 setTimeout 函数。此外,我如何实现这一点通过这我将获得内部 eval 函数,即,eval(eval());