Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { readFileSync, writeFileSync } = require("fs");
- const { parseScript } = require("esprima");
- const { js_beautify } = require("js-beautify")
- class Deobfuscator {
- constructor (fileLocation = "") {
- this.fileLocation = fileLocation;
- this.script = "";
- this.parsedScript = {};
- this.cleanedScript = "";
- this.challengeObjects = [];
- this._ac = [];
- this._acNode = {};
- this._acObfuscatedObjects = [];
- }
- cleanFile() {
- try {
- this.script = readFileSync(this.fileLocation).toString();
- this.parsedScript = parseScript(this.script, null, (node, meta) => {
- this.challengeObjects.push({node: node, meta: meta});
- });
- this.challengeObjects.forEach(object => {
- if (object.node.type === "VariableDeclaration" && object.node.declarations.length === 1 && object.node.declarations[0].id.name === "_ac") {
- this._acNode = object.node;
- this.cleanedScript = this.script.slice(object.meta.end.offset, this.script.length);
- } else if (object.node.type === "MemberExpression" && object.node.computed === true && object.node.object.name === "_ac") {
- let toReplace = this.script.slice(object.meta.start.offset, object.meta.end.offset);
- this._acObfuscatedObjects.push({string: toReplace, index: object.node.property.value});
- };
- });
- this._acNode.declarations[0].init.elements.forEach(element => {
- this._ac.push(element.value);
- });
- this._acObfuscatedObjects.forEach(object => {
- this.cleanedScript = this.cleanedScript.replace(object.string, `'${this._ac[object.index]}'`);
- });
- let cleanedPath = this.fileLocation.replace(".js", ".cleaned.js");
- writeFileSync(cleanedPath, js_beautify(this.cleanedScript), 'utf-8');
- console.log(`[DEOBFS] [SUCCESS] [${cleanedPath}]`);
- } catch (err) {
- console.log(`[DEOBFS] [ERR CLEANFILE] [${err}]`);
- }
- }
- };
- module.exports = Deobfuscator;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement