+
95
-

回答

可以使用JavaScript Obfuscator 

JavaScript Obfuscator 是一款功能强大的免费 JavaScript 混淆器,包含多种功能,可为您的源代码提供保护。

主要特征:

变量重命名

字符串提取和加密

死代码注入

控制流扁平化

各种代码转换

两种安装方式

一、cdn

不仅可以在nodejs中使用,还可以自己在浏览器中运行对js代码实时混淆。

在浏览器中混淆JS代码示例

<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum=1.0,minimum=1.0,user-scalable=0" />
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/javascript-obfuscator.browser.js"></script>
</script>

<script type="text/javascript">
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());
</script>
<style>
</style>
</head>

<body>
<div id="picker">
颜色选择器
</div>
</body>

</html>

二、通过npm安装

npm install --save-dev javascript-obfuscator


javascript-obfuscator的用法

默认不用参数的加密直接执行

javascript-obfuscator input.js --output output.js


也可以使用部分自定义参数

--target 'browser-no-eval'
--disable-console-output true
--debug-protection true
--debug-protection-interval true
--identifier-names-generator 'hexadecimal'
--string-array true
--rotate-string-array true
--string-array-encoding 'rc4'
--string-array-threshold 0.8

当然还能直接读取配置json文件

javascript-obfuscator a.js --config test.json --output b.js

官方推荐的三种配置

性能将比没有混淆的情况下慢50-100%

{
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 1,
deadCodeInjection: true,
deadCodeInjectionThreshold: 1,
debugProtection: true,
debugProtectionInterval: true,
disableConsoleOutput: true,
identifierNamesGenerator: 'hexadecimal',
log: false,
renameGlobals: false,
rotateStringArray: true,
selfDefending: true,
stringArray: true,
stringArrayEncoding: 'rc4',
stringArrayThreshold: 1,
transformObjectKeys: true,
unicodeEscapeSequence: false
}

性能将比没有混淆的情况下降低30-35%

{
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 0.75,
deadCodeInjection: true,
deadCodeInjectionThreshold: 0.4,
debugProtection: false,
debugProtectionInterval: false,
disableConsoleOutput: true,
identifierNamesGenerator: 'hexadecimal',
log: false,
renameGlobals: false,
rotateStringArray: true,
selfDefending: true,
stringArray: true,
stringArrayEncoding: 'base64',
stringArrayThreshold: 0.75,
transformObjectKeys: true,
unicodeEscapeSequence: false
}

性能会比没有混淆的情况稍微慢一些

{
compact: true,
controlFlowFlattening: false,
deadCodeInjection: false,
debugProtection: false,
debugProtectionInterval: false,
disableConsoleOutput: true,
identifierNamesGenerator: 'hexadecimal',
log: false,
renameGlobals: false,
rotateStringArray: true,
selfDefending: true,
stringArray: true,
stringArrayEncoding: false,
stringArrayThreshold: 0.75,
unicodeEscapeSequence: false
}

选择混淆

可以设置某些代码段不混淆

只要在代码上一行注释一下

disable: // javascript-obfuscator:disable or /* javascript-obfuscator:disable */;
enable: // javascript-obfuscator:enable or /* javascript-obfuscator:enable */.

例子

//输入
var foo = 1;
// javascript-obfuscator:disable
var bar = 2;

// 输出
var _0xabc123 = 0x1;
var bar = 2;


参数讲解

controlFlowFlattening

默认 false。设为 true,表示开启代码控制流展平,这是源代码的一种结构转换,使代码增大且变得难以理解。

controlFlowFlatteningThreshold

和 controlFlowFlattening配合,表示代码控制流展平的概率,此设置对于大代码影响较大,大量的控制流转换会减慢代码速度并增加代码大小。

controlFlowFlatteningThreshold的值范围是从0到1,如果为0等同于 controlFlowFlattening为false。

deadCodeInjection

默认false。设为true,表示将添加随机废代码到被混淆代码中。

该选项会显著增加代码大小(高达200%)

deadCodeInjectionThreshold

设置废代码注入的百分比。值范围是从0到1,如果为0等同于 deadCodeInjection为false。

此选项强制启用stringArray选项。

stringArray

删除字符串文字并将其放置在特殊数组中。例如,var m=“Hello World”中的字符串“Hello World”,将被替换为var m=x12c456[0x1];

stringArrayEncoding

使用 base64或 rc4对stringArray影响的所有字符串文本进行编码,并插入用于在运行时对其进行解码的特殊代码。

此选项会减慢脚本的速度。

stringArrayThreshold

此设置调整将字符串文本插入stringArray的概率(从0到1)。

debugProtection: false,

是否禁止开发者打开f12调试

debugProtectionInterval: 0,

检测调试时间

disableConsoleOutput: false,

是否禁止console.log输出

domainLock: [],

锁定运行的域名,这是个数组

domainLockRedirectUrl: 'about:blank',

其他域名访问跳转

完整官方网站请点击

网友回复

我知道答案,我要回答