首页 > 技术文章 > 防止页面被调试

IT-study 2019-07-10 18:14 原文


实现原理


执行以下方法,会进入断点,如果断点的时间 >10毫秒,那么我就认为你正在调试。


JS代码中执行

 1 (function noDebuger() {
 2 
 3     function testDebuger() {
 4         var d = new Date();
 5         debugger;
 6         if (new Date() - d > 10) {
 7             document.body.innerHTML = '<div style="width: 100%;height: 50px;font-size: 30px;text-align: center;font-weight: bold;">年轻人,不要太好奇</div>';
 8             return true;
 9         }
10         return false;
11     }
12 
13     function start() {
14         while (testDebuger()) {
15             testDebuger();
16         }
17     }
18 
19     if (!testDebuger()) {
20         window.onblur = function () {
21             setTimeout(function () {
22                 start();
23             }, 500)
24         }
25     }else {
26         start();
27     }
28 
29 
30 })();

 

JSP页面中执行:建议使用这种

使用的时候,可以将js方法压缩  http://tool.chinaz.com/js.aspx

 1 <%--禁用调试--%>
 2 <c:if test="${!debug}">
 3     <script type="text/javascript">
 4         (function noDebuger() {
 5 
 6             function testDebuger() {
 7                 var d = new Date();
 8                 debugger;
 9                 if (new Date() - d > 100) {
10                     document.body.innerHTML = '<div style="width: 100%;height: 50px;font-size: 30px;text-align: center;font-weight: bold;">年轻人,不要太好奇</div>';
11                     return true;
12                 }
13                 return false;
14             }
15 
16             function start() {
17                 while (testDebuger()) {
18                     testDebuger();
19                 }
20             }
21 
22             if (!testDebuger()) {
23                 window.onblur = function () {
24                     setTimeout(function () {
25                         start();
26                     }, 500)
27                 }
28             } else {
29                 start();
30             }
31 
32 
33         })();
34     </script>
35 </c:if>

 

 

加密后执行

1 <c:if test="${!debug}">
2     <script type="text/javascript">
3         eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('(0 k(){0 1(){j d=6 7();i;9(6 7()-d>8){l.h.b=\'<3 c="a: 8%;f: e;5-o: y;x-w: z;5-A: v;">r,q</3>\';4 u}4 t}0 2(){s(1()){1()}}9(!1()){p.B=0(){g(0(){2()},m)}}n{2()}})();',38,38,'function|testDebuger|start|div|return|font|new|Date|100|if|width|innerHTML|style||50px|height|setTimeout|body|debugger|var|noDebuger|document|500|else|size|window|不要太好奇|年轻人|while|false|true|bold|align|text|30px|center|weight|onblur'.split('|'),0,{}))
4     </script>
5 </c:if>

 

推荐阅读