前端反调试实现

2022-09-02  本文已影响0人  会爬虫的小蟒蛇

前端反调试实现

无限Debugger

在讲无限Debugger之前首先要知道三种debugger的实现

// 方法1: 使用关键字
debugger;

// 方法2:使用eval
eval("debugger;")

// 方法3: 借助于Funciton
function a(){}

Function("debugger").call() // call() apply() bind()
a.constructor("debugger").call()
Function.constructor("debugger").call()
(function(){}.constructor("debugger").call())

// 方法4: 前三种方式交叉使用

无限debugger实现

// 方式1:while这种方式在关闭控制台时 浏览器会卡死,因为主线程阻塞
while (true){
    debugger;
}

// 方式2:定时器 setInterval 或 setTimeout (推荐)
setInterval(()=>{
    debugger;
}, 0.5)

解决方法

// 定时器型 解决方案
setInterval_ = setInterval
setInterval = function (fun, timeout){
    if(fun.toString().indexOf("debugger") == -1){
        return setInterval_(fun, timeout);
    }
}

// ob 和 Funciton型 解决方案
Function.prototype.constructor_ = Function.prototype.constructor
Function.prototype.constructor = function (){
    if(arguments === "debugger"){

    }else {
        return Function.prototype.constructor_.apply(this, arguments)
    }
}

控制台反调

禁用控制台

window.document.oncontextmenu = function(){             
    return false;   
} 
document.onkeydown = document.onkeyup = document.onkeypress = function(event) {
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if (e && e.keyCode == 123) {
        e.returnValue = false;
        return (false);
    }
}

解决方法

// 进入网站前 提前打开控制台

监测控制台是否被打开

通过浏览器宽高监测

setInterval(()=>{
    if (window.outerHeight - window.innerHeight > window.outerHeight / 10){
        console.log("请不要打开控制台")
    }
}, 500)

解决方法

// 控制台单独开一个窗口

通过console.log 的 输出时机监测

// 部分浏览器不受限制,例如谷歌91版本后就修复了这一特性
var x = document.createElement("div");
Object.defineProperty(x, 'id', {
    get: function() {
        alert("请不要打开控制台");
    }
})
function use(){
    console.log(x);
    console.clear();
}
setInterval(use, 200);

内存爆破

function bp(){
    var arr = [window]
    for (var index = 0; index < arr.length; index++){
        arr.push(window.toString())
        arr.push(arr.join("="))
    }
}

var flag = false;  // 这里可以通过一些监测 或者 蜜罐, 将flag变为true

if (flag){
    bp()
}
上一篇 下一篇

猜你喜欢

热点阅读