Javascript Obfuscator with Node.js

1. Install JavaScript Obfuscator Module
Javascript Obfuscator with Node.js:

To proceed with the obfuscation of any JS code (for the browser, node.js etc) with Node.js, you will need to rely on the JavaScript obfuscator module. JavaScript obfuscator is a powerful free obfuscator for JavaScript and Node.js with a wide number of features which provides protection for your source code. This module:

npm install javascript-obfuscator
2. Create a new JS file

In the following example we’re going to read the content of a JS file and we’ll write a new one with the obfuscated version of the code:

// Require Filesystem module
var fs = require("fs");

// Require the Obfuscator Module
var JavaScriptObfuscator = require('javascript-obfuscator');

// Read the file of your original JavaScript Code as text
fs.readFile('./your-original-code.js', "UTF-8", function(err, data) {
if (err) {
throw err;
}

// Obfuscate content of the JS file
var obfuscationResult = JavaScriptObfuscator.obfuscate(data);

// Write the obfuscated code into a new file
fs.writeFile('./your-code-obfuscated.js', obfuscationResult.getObfuscatedCode() , function(err) {
if(err) {
return console.log(err);
}

console.log("The file was saved!");
});
});
3. Run the Command to Obfuscate

You have to run the js file that you configured. (step no: 2)

node obfuscate.js
Obfuscator Options

The obfuscator can be customized if you provide the configuration object as second argument in the obfuscate method. The following snippet shows all the available properties on the module:

JavaScriptObfuscator.obfuscate(YourCode, {
compact: true,
controlFlowFlattening: false,
controlFlowFlatteningThreshold: 0.75,
deadCodeInjection: false,
deadCodeInjectionThreshold: 0.4,
debugProtection: false,
debugProtectionInterval: false,
disableConsoleOutput: false,
domainLock: [],
log: false,
mangle: false,
renameGlobals: false,
reservedNames: [],
rotateStringArray: true,
seed: 0,
selfDefending: false,
sourceMap: false,
sourceMapBaseUrl: '',
sourceMapFileName: '',
sourceMapMode: 'separate',
stringArray: true,
stringArrayEncoding: false,
stringArrayThreshold: 0.75,
target: 'browser',
unicodeEscapeSequence: false
});
A. Low Obfuscation
{
compact: true,
controlFlowFlattening: false,
deadCodeInjection: false,
debugProtection: false,
debugProtectionInterval: false,
disableConsoleOutput: true,
log: false,
mangle: true,
renameGlobals: false,
rotateStringArray: true,
selfDefending: true,
stringArray: true,
stringArrayEncoding: false,
stringArrayThreshold: 0.75,
unicodeEscapeSequence: false
}
B. Medium Obfuscation
{
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 0.75,
deadCodeInjection: true,
deadCodeInjectionThreshold: 0.4,
debugProtection: false,
debugProtectionInterval: false,
disableConsoleOutput: true,
log: false,
mangle: false,
renameGlobals: false,
rotateStringArray: true,
selfDefending: true,
stringArray: true,
stringArrayEncoding: 'base64',
stringArrayThreshold: 0.75,
unicodeEscapeSequence: false
}
C. High Obfuscation
{
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 1,
deadCodeInjection: true,
deadCodeInjectionThreshold: 1,
debugProtection: true,
debugProtectionInterval: true,
disableConsoleOutput: true,
log: false,
mangle: false,
renameGlobals: false,
rotateStringArray: true,
selfDefending: true,
stringArray: true,
stringArrayEncoding: 'rc4',
stringArrayThreshold: 1,
unicodeEscapeSequence: false
}
* Install Node.js
project your javascript file with obfuscation

Leave a Reply

Your email address will not be published. Required fields are marked *