JavaScript Obfuscator Tool 的設定

 

使用 JavaScript Obfuscator Tool 時,除了要先安裝 plugin

$ npm install --save-dev webpack-obfuscator

之外,還要在 webpack.config.js 裡加上 require("webpack-obfuscator");

才會有作用,否則編譯時會出現類似 JavaScriptObfuscator is not defined 訊息。

 

一個只有 remove comment 與 rename identifier 功能如下

const path = require('path');
const JavaScriptObfuscator = require("webpack-obfuscator");

module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'main.js',
        path: path.resolve(__dirname, 'dist'),
    },
    plugins: [
        new JavaScriptObfuscator({
            compact: false,
            controlFlowFlattening: false,
            controlFlowFlatteningThreshold: 0.75,
            deadCodeInjection: false,
            deadCodeInjectionThreshold: 0.4,
            debugProtection: false,
            debugProtectionInterval: false,
            disableConsoleOutput: false,
            domainLock: [],
            identifierNamesGenerator: 'mangled',
            identifiersPrefix: '',
            inputFileName: '',
            log: false,
            renameGlobals: false,
            reservedNames: [],
            reservedStrings: [],
            rotateStringArray: false,
            seed: 0,
            selfDefending: false,
            sourceMap: false,
            sourceMapBaseUrl: '',
            sourceMapFileName: '',
            sourceMapMode: 'separate',
            stringArray: false,
            stringArrayEncoding: false,
            stringArrayThreshold: 0.8,
            target: 'browser',
            transformObjectKeys: false,
            unicodeEscapeSequence: false
        }, [''])
    ]
};

 

一個客製化的設定檔如下,視情況還可以再調整

const path = require('path');
const JavaScriptObfuscator = require("webpack-obfuscator");

module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'main.js',
        path: path.resolve(__dirname, 'dist'),
    },
    plugins: [
        new JavaScriptObfuscator({
            compact: true,
            controlFlowFlattening: false,
            controlFlowFlatteningThreshold: 0.75,
            deadCodeInjection: false,
            deadCodeInjectionThreshold: 0.4,
            debugProtection: false,
            debugProtectionInterval: false,
            disableConsoleOutput: false,
            domainLock: [],
            identifierNamesGenerator: 'hexadecimal',
            identifiersPrefix: '',
            inputFileName: '',
            log: false,
            renameGlobals: false,
            reservedNames: [],
            reservedStrings: [],
            rotateStringArray: true,
            seed: 0,
            selfDefending: true,
            sourceMap: false,
            sourceMapBaseUrl: '',
            sourceMapFileName: '',
            sourceMapMode: 'separate',
            stringArray: true,
            stringArrayEncoding: false,
            stringArrayThreshold: 0.8,
            target: 'browser',
            transformObjectKeys: false,
            unicodeEscapeSequence: false
        }, [''])
    ]
};

 

參考資料:

javascript-obfuscator plugin for Webpack

Options for javascript-obfuscator

前端混淆--JavaScript Obfuscator