Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. const { readFileSync, writeFileSync } = require("fs");
  2. const { parseScript } = require("esprima");
  3. const { js_beautify } = require("js-beautify")
  4.  
  5. class Deobfuscator {
  6. constructor (fileLocation = "") {
  7. this.fileLocation = fileLocation;
  8.  
  9. this.script = "";
  10. this.parsedScript = {};
  11. this.cleanedScript = "";
  12. this.challengeObjects = [];
  13.  
  14. this._ac = [];
  15. this._acNode = {};
  16. this._acObfuscatedObjects = [];
  17. }
  18.  
  19. cleanFile() {
  20. try {
  21. this.script = readFileSync(this.fileLocation).toString();
  22.  
  23. this.parsedScript = parseScript(this.script, null, (node, meta) => {
  24. this.challengeObjects.push({node: node, meta: meta});
  25. });
  26.  
  27. this.challengeObjects.forEach(object => {
  28. if (object.node.type === "VariableDeclaration" && object.node.declarations.length === 1 && object.node.declarations[0].id.name === "_ac") {
  29. this._acNode = object.node;
  30. this.cleanedScript = this.script.slice(object.meta.end.offset, this.script.length);
  31. } else if (object.node.type === "MemberExpression" && object.node.computed === true && object.node.object.name === "_ac") {
  32. let toReplace = this.script.slice(object.meta.start.offset, object.meta.end.offset);
  33. this._acObfuscatedObjects.push({string: toReplace, index: object.node.property.value});
  34. };
  35. });
  36.  
  37. this._acNode.declarations[0].init.elements.forEach(element => {
  38. this._ac.push(element.value);
  39. });
  40.  
  41. this._acObfuscatedObjects.forEach(object => {
  42. this.cleanedScript = this.cleanedScript.replace(object.string, `'${this._ac[object.index]}'`);
  43. });
  44.  
  45. let cleanedPath = this.fileLocation.replace(".js", ".cleaned.js");
  46.  
  47. writeFileSync(cleanedPath, js_beautify(this.cleanedScript), 'utf-8');
  48.  
  49. console.log(`[DEOBFS] [SUCCESS] [${cleanedPath}]`);
  50. } catch (err) {
  51. console.log(`[DEOBFS] [ERR CLEANFILE] [${err}]`);
  52. }
  53. }
  54. };
  55.  
  56. module.exports = Deobfuscator;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement