JAVA动态混淆JS

JAVA动态混淆JS

相关项目依赖

  • javascript-obfuscator
  • nodejs pkg

主要流程

  1. js代码调用javascript-obfuscator实现对指定文件的混淆
  2. 使用pkg将js打包成mac/linux/windows都可以执行的程序
  3. 在java中使用命令行进行js文件混淆

pkg打包

  • 打包命令
     npm install pkg -g
     cd /打包目录 #目录中需要包含pkg配置文件
     pkg ./JavaScriptObfuscator4File.js
    

可执行程序

  • 在新的环境中运行前需要修改文件的权限,让它成为一个可执行的文件
    chmod 755 ./file_name
    
  • 使用打包程序进行js混淆
      #使用绝对路径
      ./bin conf.file input.js output.js
    

相关文件

  • 混淆js的脚本
let JavaScriptObfuscator = require('javascript-obfuscator');
let fs = require('fs');
let path = require('path');

//入参
const argv = process.argv;
if (argv.length <= 2) {
    console.log('请指定待处理的文件地址');
    return
}
const process_conf_file_path = argv[2];
const process_in_file_path = argv[3];
const process_out_file_path = argv[4];


let data = get_js_content();
let conf = get_obfuscate_conf();
let obfuscate_data = obfuscate(data, conf);
output_file(obfuscate_data);
console.log("混淆js成功");

function output_file(data) {
    fs.writeFileSync(path.join(process_out_file_path), data);
}

function get_obfuscate_conf() {
    let data = fs.readFileSync(path.join(process_conf_file_path), "utf8");
    return JSON.parse(data);
}

function get_js_content() {
    let data = fs.readFileSync(path.join(process_in_file_path), "utf8");
    return data;
}

function obfuscate(source_code, conf) {
    let obfuscationResult = JavaScriptObfuscator.obfuscate(source_code, conf);
    return obfuscationResult.getObfuscatedCode();
}

  • 混淆配置文件(conf.file)
{
       "compact": true,
       "controlFlowFlattening": true,
       "controlFlowFlatteningThreshold": 0.75,
       "deadCodeInjection": true,
       "deadCodeInjectionThreshold": 0.4,
       "debugProtection": true,
       "debugProtectionInterval": true,
       "disableConsoleOutput": false,
       "identifierNamesGenerator": "mangled",
       "log": false,
       "renameGlobals": true,
       "rotateStringArray": true,
       "selfDefending": true,
       "stringArray": true,
       "stringArrayEncoding": "base64",
       "stringArrayThreshold": 0.75,
       "unicodeEscapeSequence": false,
       "transformObjectKeys": false
}
  • pkg配置文件(package.json)
    {
      "name": "obfuscate",
      "version": "1.0.0",
      "description": "",
      "bin": "JavaScriptObfuscator4File.js",
      "main": "JavaScriptObfuscator4File.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "dependencies": {
        "javascript-obfuscator": "^0.25.2"
      }
    }
    

你可能感兴趣的:(JAVA动态混淆JS)