• Vue项目前端代码防止被调试


    项目背景

    安全测试针对了,总是调试我这不太安全的代码。前端代码深度混淆转成十六进制还不行,仍然找到加密方法,对后端数据进行解密。这次就修改了思路换种方法: 

    我承认阁下很强,但假如, 我是说假如打开控制台是空白页面,阁下又该如何应对呢?

    解决办法

    前端代码防止被调试,无非就是打开控制台,卡住页面不让他往下走。有以下几种办法

    1、打开控制台,无限debugger

    2、打开控制台,无限加载

    3、打开控制台,重定向到新的页面

    网上有类似的插件可以使用,推荐两个 disable-devtoolconsole-ban,我这就是用console-ban内部的js文件做的。

    console-ban.min.js

    将这个文件放到public文件夹下,然后再index.html文件里引用

    1. /*!
    2. * console-ban v5.0.0
    3. * (c) 2020-2023 fz6m
    4. * Released under the MIT License.
    5. */
    6. !(function (e, t) {
    7. typeof exports == 'object' && typeof module != 'undefined'
    8. ? t(exports)
    9. : typeof define == 'function' && define.amd
    10. ? define(['exports'], t)
    11. : t(((e = typeof globalThis != 'undefined' ? globalThis : e || self).ConsoleBan = {}))
    12. })(this, function (e) {
    13. 'use strict'
    14. var t = function () {
    15. return (
    16. (t =
    17. Object.assign ||
    18. function (e) {
    19. for (var t, n = 1, i = arguments.length; n < i; n++) {
    20. for (var o in (t = arguments[n])) {
    21. Object.prototype.hasOwnProperty.call(t, o) && (e[o] = t[o])
    22. }
    23. }
    24. return e
    25. }),
    26. t.apply(this, arguments)
    27. )
    28. }
    29. var n = { clear: !0, debug: !0, debugTime: 3e3, bfcache: !0 }
    30. var i = 2
    31. var o = function (e) {
    32. return ~navigator.userAgent.toLowerCase().indexOf(e)
    33. }
    34. var r = function (e, t) {
    35. t !== i ? (location.href = e) : location.replace(e)
    36. }
    37. var c = 0
    38. var a = 0
    39. var f = function (e) {
    40. var t = 0
    41. var n = 1 << c++
    42. return function () {
    43. ;(!a || a & n) && ++t === 2 && ((a |= n), e(), (t = 1))
    44. }
    45. }
    46. var s = function (e) {
    47. var t = /./
    48. t.toString = f(e)
    49. var n = function () {
    50. return t
    51. }
    52. n.toString = f(e)
    53. var i = new Date()
    54. ;(i.toString = f(e)), console.log('%c', n, n(), i)
    55. var o
    56. var r
    57. var c = f(e)
    58. ;(o = c),
    59. (r = new Error()),
    60. Object.defineProperty(r, 'message', {
    61. get: function () {
    62. o()
    63. }
    64. }),
    65. console.log(r)
    66. }
    67. var u = (function () {
    68. function e(e) {
    69. var i = t(t({}, n), e)
    70. var o = i.clear
    71. var r = i.debug
    72. var c = i.debugTime
    73. var a = i.callback
    74. var f = i.redirect
    75. var s = i.write
    76. var u = i.bfcache
    77. ;(this._debug = r),
    78. (this._debugTime = c),
    79. (this._clear = o),
    80. (this._bfcache = u),
    81. (this._callback = a),
    82. (this._redirect = f),
    83. (this._write = s)
    84. }
    85. return (
    86. (e.prototype.clear = function () {
    87. this._clear && (console.clear = function () {})
    88. }),
    89. (e.prototype.bfcache = function () {
    90. this._bfcache &&
    91. (window.addEventListener('unload', function () {}),
    92. window.addEventListener('beforeunload', function () {}))
    93. }),
    94. (e.prototype.debug = function () {
    95. if (this._debug) {
    96. var e = new Function('debugger')
    97. setInterval(e, this._debugTime)
    98. }
    99. }),
    100. (e.prototype.redirect = function (e) {
    101. var t = this._redirect
    102. if (t) {
    103. if (t.indexOf('http') !== 0) {
    104. var n
    105. var i = location.pathname + location.search
    106. if (((n = t) ? (n[0] !== '/' ? '/'.concat(n) : n) : '/') !== i) r(t, e)
    107. } else location.href !== t && r(t, e)
    108. }
    109. }),
    110. (e.prototype.callback = function () {
    111. if ((this._callback || this._redirect || this._write) && window) {
    112. var e
    113. var t = this.fire.bind(this)
    114. var n = window.chrome || o('chrome')
    115. var r = o('firefox')
    116. if (!n) {
    117. return r
    118. ? (((e = /./).toString = t), void console.log(e))
    119. : void (function (e) {
    120. var t = new Image()
    121. Object.defineProperty(t, 'id', {
    122. get: function () {
    123. e(i)
    124. }
    125. }),
    126. console.log(t)
    127. })(t)
    128. }
    129. s(t)
    130. }
    131. }),
    132. (e.prototype.write = function () {
    133. var e = this._write
    134. e && (document.body.innerHTML = typeof e == 'string' ? e : e.innerHTML)
    135. }),
    136. (e.prototype.fire = function (e) {
    137. this._callback
    138. ? this._callback.call(null)
    139. : (this.redirect(e), this._redirect || this.write())
    140. }),
    141. (e.prototype.prepare = function () {
    142. this.clear(), this.bfcache(), this.debug()
    143. }),
    144. (e.prototype.ban = function () {
    145. this.prepare(), this.callback()
    146. }),
    147. e
    148. )
    149. })()
    150. e.init = function (e) {
    151. new u(e).ban()
    152. }
    153. })

    Vue2.6、vue-cli5.0、webpack5

    index.html

    在index.html内部引入console-ban.min.js

    index.html和console-ban.min.js都是同一个public文件夹下

    1. <script src="console-ban.min.js"></script>
    2. <% if (process.env.NODE_ENV === 'production' ) { %>
    3. <script>
    4. ConsoleBan.init({
    5. redirect: 'about:blank'
    6. })
    7. </script>
    8. <% } %>

    Vue3.2、vite.4.4.8

    vite不能使用process.env,如果要使用的话,需要引入外部插件 vite-plugin-html-env

    .env.development

    VITE_APP_NODE_ENV=development

    .env.production

    VITE_APP_NODE_ENV=production

    .env.sit

    VITE_APP_NODE_ENV=sit

    vite-plugin-html-env

    pnpm add vite-plugin-html-env -D

    vite.config.js

    1. import VitePluginHtmlEnv from 'vite-plugin-html-env'
    2. plugins: [
    3. VitePluginHtmlEnv(),
    4. VitePluginHtmlEnv({
    5. compiler: true
    6. }),
    7. ]

    index.html

    这个时候index.html已经移到根目录下去了,所以需要稍微改一下

    1. <script src="/console-ban.min.js"></script>
    2. <script type="text/javascript" vite-if="<{ VITE_APP_NODE_ENV }> === development">
    3. ConsoleBan.init({
    4. redirect: 'about:blank'
    5. })
    6. </script>

     效果

  • 相关阅读:
    一文搞懂前端兼容问题
    基于SpringBoot+Vue的餐饮管理系统设计与实现
    数组问题之《下一个排列》、《旋转图像》以及二分查找之《搜索二维矩阵》
    vue3+scss开启写轮眼
    用python把所有出现snprintf的c文件添加_snprintf
    【vue】@、@/、../和./的区别
    提升服务器性能相关
    在线渲染3d怎么用?3d快速渲染步骤设置
    【Docker】基于Dockerfile构建镜像介绍
    c语言入门——三子棋(N子棋)
  • 原文地址:https://blog.csdn.net/chenacxz/article/details/133138162