Skip to content

Commit

Permalink
merge pull request #7 from lorenzoferre/reconstruct-variable-declarat…
Browse files Browse the repository at this point in the history
…ions

reconstruct varaible declarations
  • Loading branch information
lorenzoferre authored Dec 6, 2023
2 parents b822d37 + cca1d42 commit 3e4164d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/deobfuscator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { transform } from "@babel/core";
import { changed, setChanged } from "./utils/util.js";
import removeDeadCode from "./techniques/remove-dead-code.js";
import renameVariableSameScope from "./techniques/rename-variable-same-scope.js";
import reconstructVariableDeclaration from "./techniques/reconstruct-variable-declaration.js";
import constantPropagation from "./techniques/constant-propagation.js";
import evaluate from "./techniques/evaluate.js";
import replaceSingleConstantViolation from "./techniques/replace-single-constant-violation.js";
Expand All @@ -22,6 +23,7 @@ export default function deobfuscate(code) {
plugins: [
removeDeadCode,
renameVariableSameScope,
reconstructVariableDeclaration,
constantPropagation,
evaluate,
replaceSingleConstantViolation,
Expand Down
23 changes: 23 additions & 0 deletions src/techniques/reconstruct-variable-declaration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { setChanged } from "../utils/util.js";

export default function (babel) {
const { types: t } = babel;

return {
name: "reconstruct-variable-declaration",
visitor: {
VariableDeclaration(path) {
const { node } = path;
const { kind } = node;
let { declarations } = node;
if (declarations.length === 1) return;
declarations.reverse();
for (const declaration of declarations) {
path.insertAfter(t.variableDeclaration(kind, [declaration]));
}
path.remove();
setChanged(true);
},
},
};
}
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ function removeNewLinesAndTabs(pieceOfCode) {
return pieceOfCode.split("\n").join(" ").split(" ").join("");
}

test("reconstruct variable declarations", () => {
assert.strictEqual(
removeNewLinesAndTabs(
deobfuscate(`
var a,b,c;
console.log(a,b,c);`)
),
`var a; var b; var c; console.log(a, b, c);`
);
});

test("hex to value", () => {
assert.strictEqual(deobfuscate(`console.log("\x61\x61\x61")`), `console.log("aaa");`);
});
Expand Down

0 comments on commit 3e4164d

Please sign in to comment.