bluontt/code-obfuscator

GitHub: bluontt/code-obfuscator

一款统一的多语言代码混淆与编译 CLI 工具,通过标识符重命名、字符串编码和二进制编译来保护源代码免受逆向分析。

Stars: 0 | Forks: 0

# code-obfuscator 一个用于混淆或编译代码的 CLI 工具:传入文件路径(或在对话框中选择文件),输出为 `-secured.` 或二进制文件。语言通过扩展名或 shebang 自动检测。 ![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg) ![Node](https://img.shields.io/badge/node-%3E%3D18-green) ## 目录 - [快速开始](#quick-start) - [支持的语言](#supported-languages) - [环境要求](#requirements) - [安装](#install) - [用法](#usage) - [选项](#options) - [功能说明](#what-it-does) - [验证与错误](#validation-and-errors) - [工作原理](#how-it-works) - [贡献](#contributing) - [许可证](#license) ## 快速开始 ``` # 从仓库 npm install && npm run build node dist/cli.js samples/sample.js # → 写入 samples/sample-secured.js # 不带参数:打开文件选择器(支持多选) npm start ``` 全局安装:`npm install -g code-obfuscator`,然后运行 `obfuscate path/to/file.js`。不安装直接运行:`npx code-obfuscator path/to/file.js`。 ## 支持的语言 | 语言 | 扩展名 | 保护方式 | |------------|--------------------------------------------------|-------------------------------| | JavaScript | `.js`, `.mjs`, `.cjs` | 内置混淆器 (Babel) | | TypeScript | `.ts`, `.mts`, `.cts` | 同 JavaScript | | Python | `.py` | PyArmor (如缺失会提示安装) | | Rust | `.rs` | `rustc` 编译 | | C | `.c`, `.h` | `gcc` 编译 | | C++ | `.cpp`, `.cxx`, `.cc`, `.hpp`, `.hxx` | `g++` 编译 | | C# | `.cs` | `csc` / `dotnet` | 语言也可以通过 shebang 推断(例如 `#!/usr/bin/env node`, `#!/usr/bin/env python3`)。如果缺少所需的外部工具,应用程序会在可能的情况下提示安装。 ## 环境要求 - **Node.js** ≥ 18 - 对于非 JS/TS 语言,会使用外部工具,并在需要时提示安装: - **Python:** PyArmor (`pip install pyarmor`) - **Rust:** rustc (例如 rustup) - **C/C++:** gcc/g++ (例如 Windows 上的 MSYS2,Linux 上的 build-essential) - **C#:** csc 或 .NET SDK **Windows:** CLI 会在 MSYS2 下查找 gcc/g++(`C:\msys64\ucrt64\bin` 等)。如果缺少 Rust MSVC linker,它会建议切换到 GNU 工具链:`rustup default stable-x86_64-pc-windows-gnu`。 ## 安装 **本地 (克隆或下载):** ``` npm install npm run build ``` **全局 (安装 CLI):** ``` npm install -g code-obfuscator ``` 示例文件位于 `samples/`(例如 `samples/sample.js`, `samples/sample.py`)。 ## 用法 ``` # 无参数 → 打开文件选择器,然后写入 -secured. 或每个文件对应的二进制文件 npm start # 或者:node dist/cli.js 或:obfuscate 或:npx code-obfuscator # 单个文件(语言来自扩展名或 shebang) node dist/cli.js path/to/script.js node dist/cli.js path/to/script.js -o out.js # 多个文件:每个文件生成各自的输出;Rust/C/C++ 可将多个源文件编译为一个二进制文件 node dist/cli.js main.rs mod.rs node dist/cli.js file1.js file2.py # 强制语言 node dist/cli.js path/to/file --lang javascript # 仅 JS/TS:禁用重命名或字符串编码 node dist/cli.js script.js --no-rename --no-encode-strings ``` ### 选项 | 选项 | 描述 | |------------------------|-------------| | `-o`, `--out ` | 输出文件。默认:`-secured.` 或二进制文件。对于多个输入,仅用于将 Rust/C/C++ 一起编译为一个二进制文件时。 | | `--lang ` | 强制指定语言:`javascript`, `typescript`, `python`, `rust`, `c`, `csharp`, `cpp`。 | | `--no-rename` | (仅限 JS/TS) 不重命名变量/函数。 | | `--no-encode-strings` | (仅限 JS/TS) 不编码字符串字面量。 | | `-h`, `--help` | 显示帮助。 | ## 功能说明 - **JavaScript/TypeScript:** 重命名标识符(例如重命名为 `_0x4a2f`),将字符串字面量编码为 `atob("base64...")`,并进行压缩。保留的名称和属性键保持不变。 - **Python:** 运行 PyArmor (`pyarmor gen`);如果缺少 PyArmor 则提示安装。 - **Rust / C / C++ / C#:** 编译为二进制文件(Windows 上默认为 `.exe`;使用 `-o` 选择路径)。如果缺少编译器/SDK 则提示安装。**多文件:** 传入多个源文件(例如 `obfuscate main.rs mod.rs`)以生成一个二进制文件。对于使用 dotnet 的 C#,`.cs` 文件必须是包含 `static void Main` 的控制台应用程序。 ## 验证与错误 CLI 会验证输入和输出,并在以下情况下退出并给出明确提示: - **输入路径是目录** → `Error: path is a directory, not a file: ` - **输出路径是目录** (使用 `-o` 时) → `Error: output path is a directory, please specify a file path: ` - **无法检测语言** → 建议使用 `--lang` 或支持的扩展名。 - **JS/TS 解析错误** → `Failed to parse JavaScript/TypeScript:
` ## 工作原理 ``` +-------------+ | Start | +------+------+ | v +-------------+ | Parse argv | (inputFile, -o, --lang, --no-rename, --no-encode-strings) +------+------+ | v +------------------+ No +------------------+ | Input path given? |------------>| Open file picker | or exit +--------+---------+ +------------------+ | Yes v +------------------+ No +------------------+ | File exists and |------------>| File not found |--> Exit 1 | is a file? | +------------------+ +--------+---------+ | Yes v +------------------+ | Detect language | (extension or shebang) +--------+---------+ | v +------------------+ No +------------------+ | Language valid? |------------>| Error, use --lang|--> Exit 1 +--------+---------+ +------------------+ | Yes v +---------------------------+ | JS or TypeScript? | +----+------------------+----+ | Yes | No (Python/Rust/C/C++/C#) v v +------------------------+ +------------------+ | obfuscate() | | ensureDependency |--> runSecureCommand | - Parse → AST | +------------------+ | - Rename ids | | - Encode strings | | - Minify | +------------------------+ | v +------------------+ | Write to file | (-secured. or binary, or -o path) +--------+---------+ | v Exit 0 ``` ## 贡献 提交 Issue 或 Pull Request。要在本地测试,请针对 `samples/` 中的文件运行(例如 `node dist/cli.js samples/sample.js`)。 ## 许可证 MIT
标签:Babel, C#编译, C++编译, DNS 反向解析, GCC, GNU通用公共许可证, IPv6支持, JavaScript混淆, MITM代理, Node.js, PyArmor, Python混淆, Rust编译, 二进制编译, 代码压缩, 代码混淆, 多语言支持, 字符串编码, 安全测试框架, 标识符重命名, 源码保护, 知识产权保护, 网络安全, 网络调试, 自动化, 自动化攻击, 防御性技术, 隐私保护, 静态分析对抗