Skip to content

ironm00n/unminification

Repository files navigation

Unminification

This is an SWC plugin which aims to reverse many of the effects of code minification. Aims to ease reverse engineering unobfuscated, minified, bundled, JavaScript codebases.

Setup

  • use Rust nightly: rustup override set nightly
  • run: rustup target add wasm32-wasi
  • build using: cargo build-wasi --release

Changes

  • SWC and other transpilers transform literals into equivalent values to reduce the size of code generated
  • The following are reverted back into their literal forms:
    • !0true
    • !1false
    • void 0undefined
    • 1 / 0Infinity
    • -1 / 0-Infinity
    • 0 / 0NaN
  • Note that: undefined, Infinity, and NaN could be potentially inaccurate since they are not reserved words. However this is unlikely to be a concern in a minified context.
  • Transpilers often convert a sequence of statements into a sequence expression to save space
- foo(), bar();
+ foo();
+ bar();

- foo = (baz(), bar);
+ baz();
+ foo = bar;

- return (foo(), bar)
+ foo();
+ return bar;

- throw (foo(), bar)
+ foo();
+ throw bar;

- if ((foo = bar, baz)) {}
+ foo = bar;
+ if (baz) {}

- switch (foo(), bar) {}
+ foo();
+ switch(bar) {}

- for (foo in (baz(), bar)) {}
+ baz();
+ for (foo in bar) {}

- for (foo of (baz(), bar)) {}
+ baz();
+ for (foo of bar) {}

Separates assignments from return statements and function calls:

- return (foo = bar);
+ foo = bar;
+ return foo;

- return (foo = bar)();
+ foo = bar;
+ return foo();

Restores common constants and loop patterns:

- 9007199254740991
+ Number.MAX_SAFE_INTEGER

- -9007199254740991
+ Number.MIN_SAFE_INTEGER

- for (;;) { ... }
+ while (true) { ... }

- for (; cond;) { ... }
+ while (cond) { ... }

Converts logical expressions back to control flow:

- foo && bar();
+ if (foo) bar();

- foo || bar();
+ if (!foo) bar();

- foo ? bar() : baz();
+ if (foo) bar();
+ else baz();

- typeof foo > "u"
+ typeof foo === "undefined"

- 0 === foo
+ foo === 0

Restores modern JavaScript syntax:

- foo == null ? undefined : foo.bar
+ foo?.bar

- foo == null ? undefined : foo.bar()
+ foo?.bar()

- "".concat(a, " world")
+ `${a} world`

- ({ func: function() {} })
+ ({ func() {} })

- function f(x) {
-   var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10;
- }
+ function f(x, y = 10) {}

About

SWC plugin for JavaScript unminification

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors