逆向之无限debugger那些事

无限debug

原理
1、定时器原理

利用setInterval或setTimeout打开了另一个线程并制定相应的时间,来执行debugger

setInterval(function(){debugger;}, 1000)
setTimeout(function b(){debugger;setTimeout(b, 1000);}, 1000)
2、递归无限循环
function registerRecursion(){
    function a(){debugger;b()}
    function b(){a()}
    b()
}
registerRecursion()
解决方案
1、清除计时器:

使用clearInterval()删除所有的定时器。根据setInterval,线程id递增。来清除

但是有个缺点,会误伤系统本身逻辑。不建议使用

//版本1,过程中出现setInterval,无法清除
function clearAllInverval(){
    var id = setInterval(function(){}, 0)
    while(id > 0){
        clearInterval(id)
        id--
    }
}
clearAllInverval()
//版本2,过程中出现setInterval,彻底清除
var gid = setInterval(clearAllInverval, 1000)
function clearAllInverval(){
	var id = setInterval(function(){}, 0)
    while(id > 0){
        if (id != gid){
            clearInterval(id)
        };
        id--
        console.log(id)
    }
}
2、F12,在debugger地方使用“never pause here”

在这里插入图片描述

比较笨的方法,有时候不一定生效,因为无限调用导致内存崩掉卡死

3、hook:

3.1、hook 定时器函数

// hook 定时器函数 替换debugger
_setInterval = setInterval
setInterval = function setInterval(code, time){
    code = code.toString().replace(/debugger/, "").replace(/function ()/, "return function ()")
    ssssa = new Function("return function(){ console.log(121212) }")
    // ssssa.apply(this, arguments);
    return _setInterval(ssssa() , time)
}

3.2、通过hook Function原型链

//
Function.prototype.before = function(beforefn){
    let that = this
    return function(){
    	arguments[0] = arguments[0].toString().replace(/debugger/, "").replace(/function()/, "function xxx")
        beforefn.apply(this, arguments)
        return that.apply(this, arguments)
    }
}
Function.prototype.after = function(afterfn){
    let that = this
    return function(){
        let ret = that.apply(this, arguments)
        afterfn.apply(this, [ret])
        return ret
    }
}
//debugger
setInterval = setInterval.before(function(){
		console.log('arguments', arguments)
    }).after(function(result){
    	console.log('result', result)
    })
setInterval(function(){
	debugger
}, 1000)

function ddd(){
	return 1+2
}
//debugger
ddd = ddd.before(function(){
	console.log('arguments', arguments)
}).after(function(result){
	console.log('result', result)
})
ddd(1,2)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值