阻止百度统计和嵌入窗口

使用Hook技术阻止百度统计和嵌入窗口

  1. // ==UserScript==
  2. // @name 阻止百度统计和嵌入窗口
  3. // @namespace http://www.infosec-wiki.com/
  4. // @version 1.3
  5. // @description 使用Hook技术阻止百度统计和嵌入窗口
  6. // @author infosec-wiki
  7. // @match *
  8. // @run-at document-start
  9. // @grant unsafeWindow
  10. // @noframes
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. /**
  17. Code by pnig0s[Knownsec&FreeBuf]
  18. Date 20130214
  19. 一個通用的js任意函數鉤子
  20. A simple hooks to Javascript functions
  21.  
  22. [bool]hook:params{
  23. realFunc[String|must]:用於保存原始的目標函數名,用於unHook;
  24. hookFunc[Function|must]:替換的hook函數;
  25. context[Object|opt]:目標函數實例的對象,用於hook非window對象下的函數,如String.protype.slice,carInstance1
  26. methodName[String|opt]:用於hook匿名函數eg:this.Begin = function(){....};
  27. }
  28. [bool]unhook:params{
  29. realFunc[String|must]:用於保存原始的目標函數名,用於unHook;
  30. funcName[String|must]:被Hook的函數名稱
  31. context[Object|opt]:目標函數實例的對象,用於hook非window對象下的函數,如String.protype.slice,carInstance1
  32. }
  33.  
  34. **/
  35.  
  36. function Hooks() {
  37. return {
  38. initEnv:function () {
  39. Function.prototype.hook = function (realFunc,hookFunc, run, context,funcName) {
  40. var _context = null; //函數上下文
  41. var _funcName = null; //函數名
  42.  
  43. _context = context || window;
  44. //_context = context || unsafeWindow;
  45.  
  46. _funcName = funcName || getFuncName(this);
  47. _context[realFunc] = this;
  48.  
  49. if(_context[_funcName].prototype && _context[_funcName].prototype.isHooked)
  50. {
  51. console.log("Already has been hooked,unhook first");
  52. return false;
  53. }
  54.  
  55. function getFuncName (fn) {
  56. // 獲取函數名稱
  57. var strFunc = fn.toString();
  58. var _regex = /function\s+(\w+)\s*\(/;
  59. var patten = strFunc.match(_regex);
  60. if (patten) {
  61. return patten[1];
  62. };
  63. return '';
  64. }
  65.  
  66. try {
  67. if (run) {
  68. eval('_context[_funcName] = function ' + _funcName + '(){\n' +
  69. 'var args = Array.prototype.slice.call(arguments,0);\n' +
  70. 'var obj = this;\n' +
  71. 'hookFunc.apply(obj,args)\n' +
  72. 'return _context[realFunc].apply(obj,args);\n' +
  73. '};');
  74. }else {
  75. eval('_context[_funcName] = function ' + _funcName + '(){\n' +
  76. 'var args = Array.prototype.slice.call(arguments,0);\n' +
  77. 'var obj = this;\n' +
  78. 'hookFunc.apply(obj,args)\n' +
  79. '};');
  80. }
  81. _context[_funcName].prototype.isHooked = true;
  82. return true;
  83. }catch (e)
  84. {
  85. console.log("Hook failed,check the params.");
  86. return false;
  87. }
  88. };
  89. Function.prototype.unhook = function (realFunc,funcName,context) {
  90. var _context = null;
  91. var _funcName = null;
  92. _context = context || window;
  93. _funcName = funcName;
  94. if (!_context[_funcName].prototype.isHooked)
  95. {
  96. console.log("No function is hooked on");
  97. return false;
  98. }
  99. _context[_funcName] = _context[realFunc];
  100. delete _context[realFunc];
  101. return true;
  102. };
  103. },
  104. cleanEnv:function () {
  105. if(Function.prototype.hasOwnProperty("hook"))
  106. {
  107. delete Function.prototype.hook;
  108. }
  109. if(Function.prototype.hasOwnProperty("unhook"))
  110. {
  111. delete Function.prototype.unhook;
  112. }
  113. return true;
  114. }
  115. };
  116. }
  117. var myHook = new Hooks();
  118. myHook.initEnv();
  119.  
  120. var _createElement = null;
  121. function createElement_hook(tag_name){
  122. console.log("createElement: " + tag_name);
  123. if(tag_name.toLowerCase() == "script") {
  124. console.log("注意.....");
  125. }
  126. }
  127. Document.prototype.createElement.hook("_createElement", createElement_hook, true, Document.prototype);
  128.  
  129. var _insertBefore = null;
  130. function insertBefore_hook(son, par){
  131. var tagName = son.tagName;
  132. if(tagName.toLowerCase() == "script"){
  133. var src = son.src;
  134. console.log("insertBefore: " + src);
  135. if(src.indexOf("hm.baidu.com") > -1){
  136. console.log("阻止百度统计.... ");
  137. return false;
  138. }
  139. }
  140. HTMLElement.prototype["_insertBefore"].call(this, son, par);
  141. }
  142. HTMLElement.prototype.insertBefore.hook("_insertBefore", insertBefore_hook, false, HTMLElement.prototype);
  143.  
  144.  
  145. var _writeln = null;
  146. function writeln_hook(s){
  147. console.log("writeln: " + s);
  148. if(s.toLowerCase().indexOf("frameset") > -1 || s.toLowerCase().indexOf("iframe") > -1){
  149. console.log("阻止创建Frame窗口.....");
  150. }
  151. else{
  152. Document.prototype["_writeln"].call(document, s);
  153. //console.log(Document.prototype);
  154. }
  155. }
  156. Document.prototype.writeln.hook("_writeln", writeln_hook, false, Document.prototype);
  157.  
  158. var _write = null;
  159. function write_hook(s){
  160. console.log("write: " + s);
  161. if(s.toLowerCase().indexOf("cnzz.com") > -1){
  162. console.log("阻止创建cnzz.com统计.....");
  163. }
  164. else{
  165. Document.prototype["_write"].call(document, s);
  166. //console.log(Document.prototype);
  167. }
  168. }
  169. Document.prototype.write.hook("_write", write_hook, false, Document.prototype);
  170.  
  171.  
  172. //myHook.cleanEnv(); //clear hooks
  173.  
  174. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址