humanifyjs
Version:
> Deobfuscate Javascript code using LLMs ("AI")
63 lines (62 loc) • 2.2 kB
JavaScript
import * as t from "@babel/types";
import { transformWithPlugins } from "../../babel-utils.js";
import bautifier from "babel-plugin-transform-beautifier";
const convertVoidToUndefined = {
visitor: {
// Convert void 0 to undefined
UnaryExpression(path) {
if (path.node.operator === "void" &&
path.node.argument.type === "NumericLiteral") {
path.replaceWith({
type: "Identifier",
name: "undefined"
});
}
}
}
};
const flipComparisonsTheRightWayAround = {
visitor: {
// If a variable is compared to a literal, flip the comparison around so that the literal is on the right-hand side
BinaryExpression(path) {
const node = path.node;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mappings = {
"==": "==",
"!=": "!=",
"===": "===",
"!==": "!==",
"<": ">",
"<=": ">=",
">": "<",
">=": "<="
};
if (t.isLiteral(node.left) &&
!t.isLiteral(node.right) &&
mappings[node.operator]) {
path.replaceWith(Object.assign(Object.assign({}, node), { left: node.right, right: node.left, operator: mappings[node.operator] }));
}
}
}
};
const makeNumbersLonger = {
visitor: {
// Convert 5e3 to 5000
NumericLiteral(path) {
var _a, _b, _c;
if (typeof ((_a = path.node.extra) === null || _a === void 0 ? void 0 : _a.raw) === "string" &&
((_c = (_b = path.node.extra) === null || _b === void 0 ? void 0 : _b.raw) === null || _c === void 0 ? void 0 : _c.includes("e"))) {
path.replaceWith({
type: "NumericLiteral",
value: Number(path.node.extra.raw)
});
}
}
}
};
export default async (code) => transformWithPlugins(code, [
convertVoidToUndefined,
flipComparisonsTheRightWayAround,
makeNumbersLonger,
bautifier
]);