在线混淆地址:https://obfuscator.io/
也可自己下载源码本地使用,地址:https://github.com/javascript-obfuscator/javascript-obfuscator
简单翻译一下该工具下载到本地后的使用方法:
安装 npm install --save-dev -g javascript-obfuscator
javascript-obfuscator xx.js 混淆xx.js文件,生成的文件和xx.js同目录
参数解释:
-v, --version 当前版本
-h, --help 帮助
-o, --output 输出目录
--compact <boolean=false> 是否压缩
--config <string=''> 包含混淆器选项的JS / JSON配置文件的名称。这些将被直接传递给CLI的选项覆盖
--control-flow-flattening <boolean=false> 此选项极大地影响了运行速度降低1.5倍的性能。
--control-flow-flattening-threshold <number=0.75 Min: 0 Max: 1> 使用controlFlowFlatteningThreshold设置将受控制流展平影响的节点百分比。启用代码控制流展平。控制流扁平化是源代码的结构转换,阻碍了程序理解
--dead-code-injection <boolean=false> 显着增加混淆代码的大小(最多200%),仅在混淆代码的大小无关紧要时使用。使用deadCodeInjectionThreshold设置将受死代码注入影响的节点百分比。
⚠️此选项强制启用stringArray选项。使用此选项,随机的死代码块将添加到混淆代码中
--dead-code-injection-threshold <number=0.4 Min: 0 Max: 1> 允许设置受deadCodeInjection影响的节点百分比
--debug-protection <boolean=false> 如果您打开开发者工具,可以冻结您的浏览器,用来调试用
--debug-protection-interval <boolean=false> 可以冻结你的浏览器!使用风险自负
--disable-console-output <boolean = false> 通过用空函数替换它们来禁用console.log,console.info,console.error,console.warn,console.debug,console.exception和console.trace。这使得调试器的使用更加困难。
--domain-lock '<list>' (Type: string[] Default: []) 锁定混淆的源代码,使其仅在特定域和/或子域上运行。这使得有人只需复制并粘贴源代码并在别处运行就变得非常困难
--exclude '<list>' (Type: string[] Default: []) 文件名或globs,表示要从混淆中排除的文件。
--identifier-names-generator <string> [hexadecimal, mangled. Default: hexadecimal] 设置标识符名称生成器
--identifiers-prefix <string=''> 设置所有全局标识符的前缀。如果要混淆多个文件,请使用此选项。此选项有助于避免这些文件的全局标识符之间发生冲突。每个文件的前缀应该不同
--log <boolean=false> 允许将信息记录到控制台。
--rename-globals <boolean = false> ⚠️此选项可能会破坏您的代码。只有在您知道它的作用时才启用它!使用声明启用全局变量和函数名称的混淆
--reserved-names '<list>' (Type: string[] Default: []) 禁用模糊处理和生成标识符,这些标识符与传递的RegEx模式匹配
--reserved-strings '<list>' (Type: string[] Default: []) 禁用字符串文字的转换,与传递的RegEx模式匹配
--rotate-string-array <boolean = true> ⚠️必须启用StringArray功能先,将stringArray数组移位固定和随机(在代码混淆时生成)的位置。这使得将删除的字符串的顺序与其原始位置相匹配变得更加困难
--seed <number=0> 此选项为随机生成器设置种子。这对于创建可重复的结果很有用。如果种子为0 - 随机生成器将在没有种子的情况下工作
--self-defending <boolean=false> 使用此选项进行模糊处理后,请勿以任何方式更改模糊代码,因为任何更改代码都会触发自我防御,代码将不再起作用!
⚠️此选项强制将紧凑值设置为true此选项使输出代码对格式化和变量重命名具有弹性。如果试图在混淆代码上使用JavaScript美化器,代码将不再起作用,使其更难理解和修改
--source-map <boolean=false> 为混淆代码启用源映射生成。源映射可以帮助您调试混淆的JavaScript源代码。如果您希望或需要在生产中进行调试,可以将单独的源映射文件上载到秘密位置,然后将浏览器指向该位置
--source-map-base-url <string=''> 当sourceMapMode:'separate'时,将基本URL设置为源映射导入URL
--source-map-file-name <string=''> 当sourceMapMode:'separate'时设置输出源映射的文件名
--source-map-mode <string> [inline, Default=separate] 指定源映射生成模式
--string-array <boolean=true> 删除字符串文字并将它们放在一个特殊的数组中。例如,var m =“Hello World”中的字符串“Hello World”;将被替换为var m = _0x12c456 [0x1]
--string-array-encoding <boolean|string> [true, default=false, default=base64, rc4] 此选项可能会降低脚本速度。使用base64或rc4对stringArray的所有字符串文字进行编码,并插入用于在运行时对其进行解码的特殊代码。
--string-array-threshold <number=0.8 Min: 0 Max: 1> 您可以使用此设置调整字符串文字将插入stringArray的概率(从0到1)。此设置对于大型代码大小特别有用,因为它反复调用字符串数组并可能减慢代码速度
--target <string> [Default=browser, browser-no-eval, node] 允许为混淆代码设置目标环境
--transform-object-keys <boolean=false> 启用对象键的转换
--unicode-escape-sequence <boolean=false> 允许启用/禁用字符串转换为unicode转义序列。Unicode转义序列大大增加了代码大小,并且可以轻松地将字符串恢复为原始视图。建议仅对小型源代码启用此选项
下面是几个常用的混淆配置:
高度混淆,性能低
Performance will 50-100% slower than without obfuscation
{
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
}
中度混淆,性能较好
Performance will 30-35% slower than without obfuscation
{
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
}
低度混淆,高性能
Performance will slightly slower than without obfuscation
{
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
}