不知道你遇没遇到过以下情况
甲方要源代码???
甲方要私有化部署????
甲方要来一套测试环境?????
前端代码可以利用 webpack、gulp 构建打包,后端 node.js 代码是透明的,甲方提出上面无理的要求,这样你自己写的价值千万级别的代码岂不是就泄露了!!! 那么这样就需要对 js 代码进行混淆打包。
说一说方案
- 混淆
- JavaScript obfuscator 一个强大的 JS 混淆器,详情可移步 👉🏻 官方网站
- 萨满科技 一套JS代码安全问题解决方案, 充钱 💰 让你变得更强大 !!!!
- UglifyJS2 仅支持ECMAScript 5,更高的版本需要用Babel转换,就是有点那么局限。 🐶
- 编译
- bytenode,一个极简的Node.js字节码编译器。
- 打包
本文简单做一下混淆
- 先安装 javascript-obfuscator 包
输出 demo ```json var JavaScriptObfuscator = require(‘javascript-obfuscator’);npm i javascript-obfuscator --save
var 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);
})();
,
{
compact: false,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 1,
numbersToExpressions: true,
simplify: true,
stringArrayShuffle: true,
splitStrings: true,
stringArrayThreshold: 1
}
);
console.log(obfuscationResult.getObfuscatedCode()); // function _0x4b3b(_0x17de7c, _0x2475af) { // var _0x167250 = _0xf437(); // return _0x4b3b = function (_0xd4f42c, _0x28331f) { // _0xd4f42c = _0xd4f42c - (0x1fa3 + -0x2 -0x1090 + -0x3f7b); // var _0x3c382d = _0x167250[_0xd4f42c]; // return _0x3c382d; // }, _0x4b3b(_0x17de7c, _0x2475af); // } // function _0xf437() { // var _0x24ab8b = [ // ‘foo\x20’, // ‘map’, // ‘183640WpndQG’, // ‘5406816LjHYju’, // ‘oJgiq’, // ‘LOADB’, // ‘119NZfSJc’, // ‘1041528CGywSf’, // ‘EcVxF’, // ‘Ztgwh’, // ‘1884RZbqvq’, // ‘8894223vxtBLT’, // ‘0|9|6|8|3’, // ‘5|4|2|7|1|’, // ‘813LbhUSh’, // ‘log’, // ‘8406712dUeRYz’, // ‘sbOGF’, // ‘split’, // ‘82878coZghU’ // ]; // _0xf437 = function () { // return _0x24ab8b; // }; // return _0xf437(); // } // (function (_0x30f9f9, _0x4030b3) { // var _0x1784bf = _0x4b3b, _0xdf655a = _0x30f9f9(); // while (!![]) { // try { // var _0x390903 = -parseInt(_0x1784bf(0x148)) / (-0x1808 + 0x246 + 0x15c3) (-parseInt(_0x1784bf(0x158)) / (-0x1e81 + -0xbe9 + 0x2a6c)) + -parseInt(_0x1784bf(0x155)) / (0x679 + 0x1f79 + -0x25ef) + -parseInt(_0x1784bf(0x151)) / (-0xd82 + -0x119 -0x5 + 0x1 0x809) + parseInt(_0x1784bf(0x150)) / (0x1 -0x1d7f + 0x453 0x3 + 0x4d 0x37) + -parseInt(_0x1784bf(0x14d)) / (-0x3 -0x9c7 + 0x4 -0x779 + 0x95) (parseInt(_0x1784bf(0x154)) / (-0x243b + 0x2008 + -0x2 -0x21d)) + parseInt(_0x1784bf(0x14a)) / (-0xdf + 0x1 0x1051 + -0xf6a) + parseInt(_0x1784bf(0x159)) / (0x26 -0x3f + 0x1af + 0x22 0x3a); // if (_0x390903 === _0x4030b3) // break; // else // _0xdf655a‘push’); // } catch (_0x4566bf) { // _0xdf655a‘push’); // } // } // }(_0xf437, -0x5602b + -0xc7c90 + 0x1fb772), (function () { // var _0x29f3f2 = _0x4b3b, _0x4fe11d = { // ‘oJgiq’: _0x29f3f2(0x15b) + _0x29f3f2(0x15a), // ‘LOADB’: function (_0x6c3a6a, _0x4b2c6d) { // return _0x6c3a6a + _0x4b2c6d; // }, // ‘Ztgwh’: _0x29f3f2(0x14e), // ‘EcVxF’: function (_0x55933d, _0x5f5065) { // return _0x55933d + _0x5f5065; // }, // ‘sbOGF’: function (_0x21d1e7, _0x1ab17e) { // return _0x21d1e7 - _0x1ab17e; // } // }, _0x41feda = _0x4fe11d[_0x29f3f2(0x152)]_0x29f3f2(0x14c), _0x1113aa = -0x16c + -0x1a9e + 0x1c0a; // while (!![]) { // switch (_0x41feda[_0x1113aa++]) { // case ‘0’: // console_0x29f3f2(0x149); // continue; // case ‘1’: // var _0x2a2bf7 = _0x4fe11d_0x29f3f2(0x153)](_0x4fe11d[_0x29f3f2(0x157)], 0x1 -0x793 + -0x57a -0x2 + -0x90 0x6), 0x11bf + 0x1 -0x295 + 0xf29 * -0x1); // continue; // case ‘2’: // var _0x28b9d2 = _0x4fe11d_0x29f3f2(0x156); // continue; // case ‘3’: // console_0x29f3f2(0x149); // continue; // case ‘4’: // var _0x5c21fd = _0x4fe11d_0x29f3f2(0x153); // continue; // case ‘5’: // var _0x8ade48 = _0x4fe11d_0x29f3f2(0x14b); // continue; // case ‘6’: // console_0x29f3f2(0x149); // continue; // case ‘7’: // var _0x4f871f = [ // ‘10’, // ‘10’, // ‘10’, // ‘10’, // ‘10’ // ]_0x29f3f2(0x14f); // continue; // case ‘8’: // console_0x29f3f2(0x149); // continue; // case ‘9’: // console_0x29f3f2(0x149); // continue; // } // break; // } // }()));
- express 项目
```json
npm i javascript-obfuscator -g
在根目录配置 javascript-obfuscator.json
{
"compact": true,
"controlFlowFlattening": true,
"controlFlowFlatteningThreshold": 0.75,
"deadCodeInjection": true,
"deadCodeInjectionThreshold": 0.4,
"debugProtection": false,
"debugProtectionInterval": 1,
"disableConsoleOutput": true,
"identifierNamesGenerator": "hexadecimal",
"log": false,
"renameGlobals": false,
"rotateStringArray": true,
"selfDefending": true,
"stringArray": true,
"stringArrayEncoding": ["base64"],
"stringArrayThreshold": 0.75,
"transformObjectKeys": true,
"unicodeEscapeSequence": false
}
打包
javascript-obfuscator ./ --output ./obfuscated --exclude node_modules --config javascript-obfuscator.json