Skip to content

Instantly share code, notes, and snippets.

@amol9
Last active March 1, 2019 07:18
Show Gist options
  • Save amol9/dac4f93c232696d76551c44411ced51e to your computer and use it in GitHub Desktop.
Save amol9/dac4f93c232696d76551c44411ced51e to your computer and use it in GitHub Desktop.
Beautify / Deobfuscate Javascript via various sources (nodejs script)
var beautify = require('js-beautify').js;
var fs = require('fs');
var ArgParser = require('argparse').ArgumentParser;
var http = require('http');
var ap = ArgParser({});
ap.addArgument('filename', {
'help': 'input js filename'
});
ap.addArgument(['-s', '--source'], {
'help': 'source: "b" (js-beautify, node module) or "n" (jsnice.org, website)'
});
args = ap.parseArgs();
var filename = args.filename;
var source = args.source;
//source: jsnice.org
function unpackJS(input_text) {
var code = input_text;
if (input_text.startsWith("eval(")) {
input_text = "code = (" + input_text.substr(5);
eval(input_text);
}
return code;
}
jsfile = fs.readFileSync(filename, 'utf8');
var code = unpackJS(jsfile);
if (source == 'b') {
console.log(beautify(code, {
indent_size: 2,
space_in_empty_paren: true
}));
} else if (source == 'n') {
//ref: https://flaviocopes.com/node-http-post/
const options = {
hostname: 'jsnice.org',
port: 80,
path: '/beautify?pretty=1&rename=1&types=1&packers=1&transpile=0&suggest=0',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': code.length
}
};
const req = http.request(options, (res) => {
//console.log(`statusCode: ${res.statusCode}`)
res.on('data', (d) => {
o = JSON.parse(d);
process.stdout.write(o.js)
})
});
req.on('error', (error) => {
console.error(error)
});
req.write(code);
req.end();
} else {
console.log("invalid source");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment