如何反制网站的开发者工具限制 - disable-devtool 反制指南

disable-devtool 反制指南 —— 技术分享

背景介绍

现在很多网站为了保护自己的代码和内容,会使用 disable-devtool 等工具来禁用浏览器的开发者工具。这给我们的学习和调试带来了不便。本文将介绍如何通过脚本来反制这类限制。

disable-devtool 的工作原理

disable-devtool 主要通过以下几种方式来检测和阻止开发者工具:

  1. 检测 devtools 对象状态
  2. 监听页面关闭事件
  3. 检测到开发者工具后强制跳转
  4. 使用定时器持续检测

反制方案

1. 核心思路

  • 在网站脚本加载前注入保护代码
  • 劫持关键 API 和事件
  • 清理可疑脚本
  • 阻止强制跳转

2. 具体实现

2.1 前置保护

javascript
复制代码
// 禁用 devtools 检测 Object.defineProperty(window, 'devtools', { get: function() { return { isOpen: false, orientation: undefined }; }, set: function() { return false; } }); // 禁用 DisableDevtool window.DisableDevtool = function(){}; Object.defineProperty(window, 'DisableDevtool', { get: function() { return function(){}; }, set: function() { return false; } });

2.2 阻止强制跳转

javascript
复制代码
// 检查是否是需要阻止的 URL function isBlockedUrl(url) { if (!url) return false; const urlStr = url.toString().toLowerCase(); return urlStr.includes('theajack.github.io') || urlStr.includes('disable-devtool') || urlStr.includes('404.html'); } // 劫持 location 相关 API Object.defineProperty(window, 'location', { get: function() { return _originalLocation; }, set: function(value) { if (isBlockedUrl(value)) { console.log('阻止跳转:', value); return false; } _originalLocation.href = value; } });

2.3 阻止页面关闭

javascript
复制代码
// 阻止关闭事件 EventTarget.prototype.addEventListener = function(type, listener, options) { if (type === 'beforeunload' || type === 'unload' || type === 'visibilitychange') { console.log('阻止事件:', type); return; } return _addEventListener.call(this, type, listener, options); };

2.4 清理可疑脚本

javascript
复制代码
function cleanScripts() { const scripts = document.getElementsByTagName('script'); for (let script of scripts) { if (script.src && ( script.src.includes('disable-devtool') || script.src.includes('theajack.github.io') )) { script.remove(); } } }

注意事项

  1. 本方案仅供学习研究使用
  2. 不同网站可能需要针对性调整
  3. 建议遵守网站使用规则
  4. 脚本可能需要随 disable-devtool 的更新而更新

总结

通过分析 disable-devtool 的工作原理,实现了一个有效的反制方案。该方案通过:

  • 前置注入保护代码
  • API 劫持
  • 事件拦截
  • 脚本清理

等多重手段,成功实现了对 disable-devtool 的破解。这不仅帮助我们更好地学习和调试,也加深了对浏览器安全机制的理解。

参考资料

  1. disable-devtool GitHub 仓库
  2. Tampermonkey 官方文档
  3. 浏览器开发者工具文档

希望这篇文章对你有帮助!欢迎讨论。

0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
安逸
下载 APP