JavaScript AST manipulation with Babel: the first steps

Previously on Trickster Dev:

When it comes to reverse engineering obfuscated JavaScript code there are two major approaches:

  • Dynamic analysis - using debugger to step through the code and observing it’s behaviour over time.
  • Static analysis - performing source code analysis without running it, but parsing and analysing the code itself.

It is bad idea to rely exclusively on regular expressions and naive string manipulation to do web scraping. The same applies to Javascript static analysis. Thus we will parse the JS code into Abstract Syntax Tree. This will enable us to perform manipulations within a data model capable of representing the logical structure of the code. We are going to write code to demonstrate how some of the basic obfuscation techniques could be reverted.

Our deobfuscation code will be based on Babel - a programmable toolkit for manipulating JavaScript code. Typically it is being used in build systems of web-based software projects, but it has also been found to be quite useful by developers working on gray hat scraper/automation stuff (not to be confused Babel from Python world that deals with internationalization/localisation stuff, and with OpenBabel the chemical data processing software).

Deobfuscation code we will be writing will work in three stages:

  1. Parsing JS code into AST.
  2. Modifying AST to undo obfuscations.
  3. Regenerating JS code from AST.

Step 2 is where the bulk of work will be done.

Initially I wanted to do this in Python, but it turned out that there’s no established Python module to regenerate the JS code from AST. Altough some JS static analysis can be done in Python, JavaScript tooling and libraries are best equipped to deal with JS code. However I don’t typically code in JS so bear with me if code quality seems lacking.

Babel can be installed through NPM:

$ npm install --save-dev @babel/core @babel/cli

Let us consider the following code:

fetch('', {
    headers: {
        'authority': '',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
        'cache-control': 'max-age=0',
        'cookie': '_dd_s=',
        'referer': '',
        'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"macOS"',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36'

We use the DigitalOcean Code Minify tool to remove all the whitespace from this snippet:

fetch("",{headers:{authority:"",accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","accept-language":"en-GB,en-US;q=0.9,en;q=0.8","cache-control":"max-age=0",cookie:"_dd_s=",referer:"","sec-ch-ua":'".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',"sec-ch-ua-mobile":"?0","sec-ch-ua-platform":'"macOS"',"sec-fetch-dest":"document","sec-fetch-mode":"navigate","sec-fetch-site":"same-origin","sec-fetch-user":"?1","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36"}});

Screenshot 1

Since minification is not really an obfuscation we can parse and regenerate the code without modifying the AST:

$ node
Welcome to Node.js v18.6.0.
Type ".help" for more information.
> const parser = require("@babel/parser");
> const fs = require("fs");
> const mjs = fs.readFileSync("minified.js", "utf-8");
> mjs
`fetch("",{headers:{authority:"",accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","accept-language":"en-GB,en-US;q=0.9,en;q=0.8","cache-control":"max-age=0",cookie:"_dd_s=",referer:"","sec-ch-ua":'".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',"sec-ch-ua-mobile":"?0","sec-ch-ua-platform":'"macOS"',"sec-fetch-dest":"document","sec-fetch-mode":"navigate","sec-fetch-site":"same-origin","sec-fetch-user":"?1","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36"}});`
> let ast = parser.parse(mjs);
> const generate = require("@babel/generator").default;
> generate(ast).code
'fetch("", {\n' +
  '  headers: {\n' +
  '    authority: "",\n' +
  '    accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",\n' +
  '    "accept-language": "en-GB,en-US;q=0.9,en;q=0.8",\n' +
  '    "cache-control": "max-age=0",\n' +
  '    cookie: "_dd_s=",\n' +
  '    referer: "",\n' +
  `    "sec-ch-ua": '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',\n` +
  '    "sec-ch-ua-mobile": "?0",\n' +
  `    "sec-ch-ua-platform": '"macOS"',\n` +
  '    "sec-fetch-dest": "document",\n' +
  '    "sec-fetch-mode": "navigate",\n' +
  '    "sec-fetch-site": "same-origin",\n' +
  '    "sec-fetch-user": "?1",\n' +
  '    "upgrade-insecure-requests": "1",\n' +
  '    "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36"\n' +
  '  }\n' +

Since Babel automatically formats the generated code by default it was trivial to undo the minification.

Applying hexadecimal string encoding to our test snippet yields the following obfuscated version:


Screenshot 2

Before we proceed with writing code to deobfuscate this, let us do a quick experiment on AST Explorer. Make sure that JavaScript language is chosen in language dropdown and choose @babel/parser in parser dropdown. Then let us check the AST-level difference between the following JS statements:


Screenshot 3

We see that in hex-encoded version of the StringLiteral the extra.raw field is a not the same as extra.raw, whereas they are the same in a normal version of the string (except extra.raw being enclosed in double quotes). This gives us an idea on how to undo the hex encoding: when processing the AST, let us set raw based on the clean form of a string in rawValue field.

To manipulate the Abstract Syntax Tree, we are going to apply the Visitor design pattern that entails some decoupling of data manipulation code from the specifics of the underlying data structure. Babel has us covered here: we don’t need to worry about AST traversal algorithm and merely have to call traverse() with a callback function(s) for the type(s) of AST nodes that we want to modify.

Code that undoes string hex-encoding is as follows:

const fs = require("fs");

const parser = require("@babel/parser");
const generate = require("@babel/generator").default;
const traverse = require("@babel/traverse").default;

let hjs = fs.readFileSync("hexcoded.js", "utf-8");

const ast = parser.parse(hjs);

traverse(ast, {
    StringLiteral: function(path) {
        path.node.extra.raw = "\"" + path.node.extra.rawValue + "\"";

let clean = generate(ast).code;

fs.writeFileSync("clean1.js", clean);

Fairly simple, right?

Now what if we wanted to convert bracket notation into dot notation? That is, we could have statements like this:


And we wanted them to be like this:


For the sake of the example, let us try cleaning up the following code:


let o = {};
o["a"] = 42;

Note that we specifically want to get rid of bracket notation for function calls, but not for object member assignment.

Let us put both statements into AST Explorer and see how do they differ at AST level.

Screenshot 4

We see that in both cases CallExpression has MemberExpression at callee field, but there’s a difference at MemberExpression level: in the case of dot notation there’s Identifier object at property, but in the case of bracket notation it’s a StringLiteral. Furthermore, computed is false when dot notation is used and true when bracket notation is used.

So we write some code to find all parts of AST that match the latter pattern and fix it to be like the former.

const fs = require("fs");

const parser = require("@babel/parser");
const generate = require("@babel/generator").default;
const traverse = require("@babel/traverse").default;
const types = require("@babel/types");

let js = fs.readFileSync("brackets.js", "utf-8");

const ast = parser.parse(js);

traverse(ast, {
    CallExpression: function(path) {
        let prop =;

        if (types.isStringLiteral(prop)) {
 = types.Identifier(prop.value);
          path.node.callee.computed = false;

let clean = generate(ast).code;

fs.writeFileSync("clean2.js", clean);

Running this script converts our sample code into the following:

let o = {};
o["a"] = 42;

Since we did matching on CallExpression it did not touch the last statement that involves assigning member a of JS object.

Lastly, let us consider the following code:


This has way too many semi-colons that we want to remove. Turns out each extra semicolon is represented by EmptyStatement in a Babel AST data model.

Screenshot 5

Thus we simply remove the EmptyStatement nodes when traversing the AST:

const fs = require("fs");

const parser = require("@babel/parser");
const generate = require("@babel/generator").default;
const traverse = require("@babel/traverse").default;

let js = fs.readFileSync("too_many_semicolons.js", "utf-8");

const ast = parser.parse(js);

traverse(ast, {
    EmptyStatement: function(path) {

let clean = generate(ast).code;

fs.writeFileSync("clean3.js", clean);

Running this gives us a cleaned up version of the code:


Trickster Dev

Code level discussion of web scraping, gray hat automation, growth hacking and bounty hunting

By rl1987, 2022-07-20