如何使用Node.js混淆JavaScript代码

本文概述

  • 1.安装JavaScript Obfuscator模块
  • 2.使用混淆器
  • 例子
分析以下JavaScript代码:
(function(_0x1f87a3, _0x3799c5) {var _0x17ebae = function(_0x55b076) {while (--_0x55b076) {_0x1f87a3['push'](_0x1f87a3['shift']()); }}; _0x17ebae(++_0x3799c5); }(_0xaec5, 0x67)); var _0x5aec = function(_0x3dd15e, _0x1f0015) {_0x3dd15e = _0x3dd15e - 0x0; var _0x231fd0 = _0xaec5[_0x3dd15e]; return _0x231fd0; }; (function() {var _0x1e950d = '5' - 0x3; var _0x137c62 = '5' + 0x3; var _0x277681 = '5' + -'2'; var _0x531dc9 = ['10', '10', '10', '10', '10'][_0x5aec('0x0')](parseInt); var _0x1db9e4 = 'foo\x20' + 0x1 + 0x1; console[_0x5aec('0x1')](_0x1e950d); console[_0x5aec('0x1')](_0x137c62); console[_0x5aec('0x1')](_0x277681); console[_0x5aec('0x1')](_0x531dc9); console[_0x5aec('0x1')](_0x1db9e4); }());

只是开个玩笑, 除非你是计算机或特权编程天才/计算机, 否则你将在一两秒钟之内无法理解以前的JS代码的含义或作用。好吧, 先前的总有效JavaScript代码是以下代码的混淆版本:
(function(){var variable1 = '5' - 3; var variable2 = '5' + 3; var variable3 = '5' + - '2'; var variable4 = ['10', '10', '10', '10', '10'].map(parseInt); var variable5 = 'foo ' + 1 + 1; console.log(variable1); console.log(variable2); console.log(variable3); console.log(variable4); console.log(variable5); })();

看起来很不一样吗?对JavaScript代码的混淆可以帮助你避免在其他地方使用你的代码。这对于在诸如Themeforest之类的平台上出售代码的人来说非常有用, 在该平台上可以轻松地从浏览器复制你的代码, 但是使用混淆器可以复制但难以阅读(甚至比最小化还差)。在本文中, 我们将向你展示如何使用JavaScript模糊处理模块在Node.js中对代码进行模糊处理。
1.安装JavaScript Obfuscator模块要使用Node.js进行任何JS代码(对于浏览器, node.js等)的混淆处理, 你将需要依赖JavaScript混淆器模块。 JavaScript混淆器是JavaScript和Node.js的强大免费混淆器, 具有多种功能, 可为你的源代码提供保护。该模块:
  • 没有限制或限制。
  • 在本地计算机上运行-不将数据发送到服务器。
  • 与es2015, es2016和部分es2017兼容。
你可以使用以下命令安装此模块:
npm install javascript-obfuscator

安装后, 你将可以使用require(” javascript-obfuscator” )在脚本中要求该模块。混淆器是免费且开放源代码(BSD-2-Clause许可), 并且使用TypeScript编写, 你可以在此处查看该模块的在线实现。有关此库的更多信息, 请访问Github上的官方存储库。
2.使用混淆器使用模块混淆一些代码的逻辑非常简单。你创建模块的实例, 从该实例可以使用混淆方法, 该方法将要混淆的代码作为第一个参数。此方法同步返回混淆。通过一系列转换, 例如变量/函数/参数重命名, 字符串删除等, 你的源代码被转换为不可读的内容, 而工作方式与以前完全相同:
// Require the JavaScript obfuscatorvar JavaScriptObfuscator = require('javascript-obfuscator'); // Obfuscate the code providen as first argumentvar obfuscationResult = JavaScriptObfuscator.obfuscate(`(function(){var variable1 = '5' - 3; var variable2 = '5' + 3; var variable3 = '5' + - '2'; var variable4 = ['10', '10', '10', '10', '10'].map(parseInt); var variable5 = 'foo ' + 1 + 1; console.log(variable1); console.log(variable2); console.log(variable3); console.log(variable4); console.log(variable5); })(); `); // Display obfuscated resultconsole.log(obfuscationResult.getObfuscatedCode());

混淆器选项
如果在混淆方法中将配置对象作为第二个参数提供, 则可以自定义混淆器。以下代码段显示了模块上所有可用的属性:
JavaScriptObfuscator.obfuscate(YourCode, {compact: true, controlFlowFlattening: false, controlFlowFlatteningThreshold: 0.75, deadCodeInjection: false, deadCodeInjectionThreshold: 0.4, debugProtection: false, debugProtectionInterval: false, disableConsoleOutput: false, domainLock: [], log: false, mangle: false, renameGlobals: false, reservedNames: [], rotateStringArray: true, seed: 0, selfDefending: false, sourceMap: false, sourceMapBaseUrl: '', sourceMapFileName: '', sourceMapMode: 'separate', stringArray: true, stringArrayEncoding: false, stringArrayThreshold: 0.75, target: 'browser', unicodeEscapeSequence: false});

值得阅读该库的文档, 因为将来可能会出现新的选项。官方存储库提供了已经进行的预设, 以特殊的选项组合提供” 低” , “ 中” 或” 高” 混淆的感觉。请注意, 混淆效果越好, 处理步骤就越慢:
A.低混淆
{ compact: true, controlFlowFlattening: false, deadCodeInjection: false, debugProtection: false, debugProtectionInterval: false, disableConsoleOutput: true, log: false, mangle: true, renameGlobals: false, rotateStringArray: true, selfDefending: true, stringArray: true, stringArrayEncoding: false, stringArrayThreshold: 0.75, unicodeEscapeSequence: false}

B.中度混淆
{ compact: true, controlFlowFlattening: true, controlFlowFlatteningThreshold: 0.75, deadCodeInjection: true, deadCodeInjectionThreshold: 0.4, debugProtection: false, debugProtectionInterval: false, disableConsoleOutput: true, log: false, mangle: false, renameGlobals: false, rotateStringArray: true, selfDefending: true, stringArray: true, stringArrayEncoding: 'base64', stringArrayThreshold: 0.75, unicodeEscapeSequence: false}

C.高度混淆
{ compact: true, controlFlowFlattening: true, controlFlowFlatteningThreshold: 1, deadCodeInjection: true, deadCodeInjectionThreshold: 1, debugProtection: true, debugProtectionInterval: true, disableConsoleOutput: true, log: false, mangle: false, renameGlobals: false, rotateStringArray: true, selfDefending: true, stringArray: true, stringArrayEncoding: 'rc4', stringArrayThreshold: 1, unicodeEscapeSequence: false}

例子在下面的示例中, 我们将读取一个JS文件的内容, 并将使用代码的混淆版本编写一个新文件:
// Require Filesystem modulevar fs = require("fs"); // Require the Obfuscator Modulevar JavaScriptObfuscator = require('javascript-obfuscator'); // Read the file of your original JavaScript Code as textfs.readFile('./your-original-code.js', "UTF-8", function(err, data) {if (err) {throw err; }// Obfuscate content of the JS filevar obfuscationResult = JavaScriptObfuscator.obfuscate(data); // Write the obfuscated code into a new filefs.writeFile('./your-code-obfuscated.js', obfuscationResult.getObfuscatedCode() , function(err) {if(err) {return console.log(err); }console.log("The file was saved!"); }); });

请记住, 虽然几乎不可能从混淆后的版本中恢复确切的原始源代码, 但是有时间, 知识和耐心的人可以对其进行反向工程。该示例不使用特殊的混淆, 而是使用默认的混淆, 因此, 如果要自定义混淆, 请提供配置对象。
【如何使用Node.js混淆JavaScript代码】编码愉快!

    推荐阅读