Articles

Deobfuscating JavaScript with AI: webcrack, source maps and honest limits

How to reverse minified, bundled and obfuscated JavaScript with an AI agent: recover source maps first, unpack bundles with webcrack, format with prettier, then let the model rename and explain — and where it stops working.

· 7 min read

An old-master oil painting of a tangled knot of old rope on a table, its loose end unravelled and smouldering.

Reading someone's production JavaScript is a common job: auditing a web app you're testing, understanding how a client talks to its API, checking what a browser extension actually does, or recovering the logic of your own app after the source was lost. The code arrives minified, bundled into a few huge files, and sometimes deliberately obfuscated. A language model is genuinely good at the last mile of this — giving a, b and c real names and explaining what a function does. It's bad at the first mile, which is mechanical and should be done by tools.

The rule that makes this work: deterministic tools first, the model last. Recover what was shipped, undo the transforms that have known inverses, and only then ask the model to read. A model asked to deobfuscate raw obfuscator output will paraphrase it confidently and get details wrong; a model reading webcrack's output with a source map's file layout gets most of it right.

Deobfuscating JavaScript, from shipped bundle to readable codeStart with the shipped JavaScript. First check for a source map; if there is one, recover the original sources and you're mostly done. If not, unpack the bundle and undo the obfuscation with webcrack, then format with prettier. Only then does the model rename variables and explain functions, and every claim it makes is checked by running the code in a debugger.YesNoThe shipped JavaScriptA source mapavailable?Recover theoriginal sourceswebcrack: unpackand deobfuscateprettier: formatThe model renamesand explainsCheck claimsin a debugger
Deobfuscating JavaScript, from shipped bundle to readable code

Before you start: whose code is it

Client-side JavaScript is delivered to your browser, but that doesn't make every use of it fair game. Reversing it to test an app you're authorized to test, to audit an extension you're deciding whether to install, for interoperability, or to recover your own code is ordinary work. Lifting someone's proprietary code into your product, or using what you learn against a service you have no permission to test, isn't. Know which one you're doing, and if it's a target, keep to the scope you were given.

Step 1: look for a source map

The cheapest deobfuscation is the one the developer left behind. Many production bundles end with a comment pointing to a source map:

tail -c 300 app.3f9a1c.js
# //# sourceMappingURL=app.3f9a1c.js.map

The map may also be named in a SourceMap HTTP response header. If the .map file is served, it often carries the original sources verbatim in its sourcesContent field — file names, folder structure, comments and all. A short Bun script writes them back out:

// unmap.ts — bun unmap.ts app.js.map out/
import { mkdirSync, writeFileSync } from 'node:fs';
import { dirname, join, normalize } from 'node:path';

const [mapPath, outDir] = process.argv.slice(2);
const map = await Bun.file(mapPath).json();
map.sources.forEach((src: string, i: number) => {
  const body = map.sourcesContent?.[i];
  if (body == null) return;
  // strip webpack:// prefixes and any ../ so nothing escapes outDir
  const rel = normalize(src.replace(/^[a-z]+:\/\/\/?/i, '')).replace(/^(\.\.[\/\\])+/, '');
  const dest = join(outDir, rel);
  mkdirSync(dirname(dest), { recursive: true });
  writeFileSync(dest, body);
});

If sourcesContent is missing, the map still gives you the original file names and the mapping from minified positions back to original lines and identifiers, which is worth handing to the model as context. Browser DevTools also apply maps automatically — the Sources panel shows the original tree when the map loads.

When there's a usable map, you're mostly done: you have something close to the real source, and the model can read it like any codebase.

Step 2: unpack and deobfuscate with webcrack

No map? webcrack is the tool to reach for. It undoes the transforms of obfuscator.io-style obfuscation (string arrays, rotated string tables, control-flow flattening it can recognize, dead code), unminifies common patterns, and unpacks webpack and browserify bundles into one file per module:

bunx webcrack app.3f9a1c.js -o out/

The output folder holds the deobfuscated code and, for a recognized bundle, the modules split out. Splitting matters more than it sounds: a 3 MB bundle is useless to a model, but 400 modules of a few hundred lines each are perfect — the agent can grep for an API path, find the three modules that touch it, and read only those.

For older "packers" — eval(function(p,a,c,k,e,d){…}) and similar — the payload is a string the packer decodes and evals. Tools in the de4js family unpack the common ones. The manual equivalent is to replace the final eval with a print and run it in an isolated environment (a container or a throwaway VM, not your main browser profile), since you're executing code you don't trust.

Step 3: format it

Whatever came out, run it through a formatter so the model and you see the same consistent structure:

bunx prettier --write "out/**/*.js"

This is also the point to commit the output to a scratch git repo. Every rename the model makes from here on becomes a diff you can review and revert.

Step 4: let the model rename and explain

Now the model earns its place. Minified code has lost its names, and recovering names is guessing from context — which is what language models are good at. Give the agent the formatted modules and a narrow task:

These are webcrack-unpacked modules from a web app I'm authorized to test. Find the module(s) that build requests to /api/v2/orders. Rename variables and functions in those modules to descriptive names, add a short comment above each function saying what it does, and list anything you're unsure of. Don't change behaviour — renames and comments only.

A few things make this much more reliable:

  • Rename with a tool, not by rewriting. Asking the model to re-emit a whole file invites silent changes. Have it produce a rename map ({ "a": "orderId", "Qe": "buildOrderRequest" }) and apply it with a scope-aware tool, or a tool built on Babel's scope tracking. humanify takes this approach: the model proposes names, and the AST transform applies them safely.
  • Work module by module. Context is precious. One module, its imports' names, and the question.
  • Give it anchors. String literals, API paths, error messages and library code it can recognize (React, axios, lodash) are what let it name the surrounding code correctly. Point it at those first.
  • Diff every pass. Run the app's tests if you have them, or at least run the code, after a rename pass.

Step 5: confirm in a debugger

A model's explanation of minified code is a hypothesis. Check the claims that matter by running the code: set a breakpoint in DevTools on the function it named buildOrderRequest, trigger the action, and see whether the arguments look like what it said. Pretty-printing in the Sources panel, conditional breakpoints and the Network panel's initiator stack cover most of it. When the question is "what does this send," the network capture is the ground truth — the undocumented-API guide covers capturing it.

The model's explanation is a hypothesis until the code runsThe model names a function and says what it does. Set a breakpoint on it in DevTools and trigger the action. If the arguments and behaviour match what the model said, mark it confirmed in your notes. If not, tell the model what you saw and have it revise the name and comment, then check again.YesNoThe model namesa functionBreakpoint in DevTools,trigger the actionMatches whatit said?Confirmed,in your notesTell the modelwhat you saw
The model's explanation is a hypothesis until the code runs

Where it stops working

Be honest with yourself about the ceiling:

  • Virtualized code. Some commercial protectors compile functions into bytecode for a custom interpreter shipped in the bundle. webcrack won't undo that, and a model reading the interpreter won't magically recover the original. It becomes a real reversing job: understand the VM, write a disassembler for its bytecode, and have the model help with each piece.
  • Server-side logic. Client code shows you what the client does. Validation, pricing, authorization — whatever the server decides — isn't in the bundle, however well you deobfuscate it.
  • Names are guesses. A recovered name is plausible, not true. Two functions can look alike and do different things; keep "confirmed" and "inferred" separate in your notes.
  • Huge apps. Even split into modules, a large SPA is far more than any context window holds. The agent has to search and read selectively, which is a workflow question, not a model one.
  • WebAssembly. If the interesting logic is in a .wasm file, this is native-style reversing; the Ghidra/IDA/radare2 guide is the better starting point.

Which AI to use

Any agent that can run shell commands handles this workflow — the tools do the heavy lifting, and the model needs a long context and decent code-reading more than anything security-specific. Two things do differ. Mainstream assistants sometimes refuse or hedge on "deobfuscate this," especially when the code looks like a protection or anti-bot script, even when the work is legitimate (what to do about that). And an agent that can also drive a browser saves a lot of copying: caveira ships the rea MCP server, which handles JavaScript and Electron apps and websites, and its optional Docker box has a headless Chromium behind mitmproxy that the agent can drive, so it can load the page, pull the bundle and its map, and capture what the code sends without anything running in your own browser. Whatever you use, the order is the same: maps, tools, formatter, then the model, then the debugger.