Module refinery.units.scripting.js
Expand source code Browse git
from __future__ import annotations
from refinery.lib.scripts.js.analysis.environment import HostEnvironment
from refinery.lib.scripts.js.deobfuscation import deobfuscate
from refinery.lib.scripts.js.model import JsScript
from refinery.lib.scripts.js.parser import JsParser
from refinery.lib.scripts.js.synth import JsSynthesizer
from refinery.lib.types import Param
from refinery.units import Arg
from refinery.units.scripting import IterativeDeobfuscator
class js(IterativeDeobfuscator):
"""
AST-based JavaScript deobfuscator and pretty-printer.
Parses the script into an abstract syntax tree, applies simplifying transformations, and
synthesizes clean output. This deobfuscator iterates until stable; running it twice does
not change the output. By default the input is assumed to run as a classic global script; pass
the module switch when it runs as an ES or CommonJS module instead. Name any entrypoint a host
calls into, or dead-code removal will discard it as unreachable.
"""
def __init__(
self,
*entrypoints: Param[str, Arg.String(metavar='pattern', nargs='*', help=(
'A wildcard pattern naming top-level functions that a host invokes by name, such as the '
'run handler of a JXA script or a Windows Script Host event handler. Such a function has '
'no caller inside the file, so it is otherwise removed as unreachable along with '
'everything only it reached. Give a single asterisk to keep every top-level function.'))],
timeout=500,
module: Param[bool, Arg.Switch('-m', help=(
'Assume the input runs as an ES or CommonJS module (for example, node file.js), where a '
'top-level declaration is scoped to the module and does not attach to the global object, '
'rather than as a classic global script (a browser script tag, Windows Script Host).'))] = False,
environment: Param[str | HostEnvironment, Arg.Option('-e', metavar='E', choices=HostEnvironment,
help=(
'Pin the host the input runs in, so that the global names that host defines are assumed '
'present and the reads that rely on them are resolved. The default is {default}, which '
'assumes only the globals every host shares, so a host-conditional read such as window '
'or global is kept. Options are: {choices}.'))] = HostEnvironment.universal,
):
super().__init__(
timeout=timeout,
module=module,
entrypoints=entrypoints,
environment=Arg.AsOption(environment, HostEnvironment),
)
def parse(self, data: str) -> JsScript:
return JsParser(data).parse()
def transform(self, ast: JsScript) -> int:
return deobfuscate(
ast,
module=self.args.module,
entrypoints=tuple(self.args.entrypoints),
environment=self.args.environment,
)
def synthesize(self, ast: JsScript) -> str:
return JsSynthesizer().convert(ast)
Classes
class js (*entrypoints, timeout=500, module=False, environment=HostEnvironment.universal)-
AST-based JavaScript deobfuscator and pretty-printer.
Parses the script into an abstract syntax tree, applies simplifying transformations, and synthesizes clean output. This deobfuscator iterates until stable; running it twice does not change the output. By default the input is assumed to run as a classic global script; pass the module switch when it runs as an ES or CommonJS module instead. Name any entrypoint a host calls into, or dead-code removal will discard it as unreachable.
Expand source code Browse git
class js(IterativeDeobfuscator): """ AST-based JavaScript deobfuscator and pretty-printer. Parses the script into an abstract syntax tree, applies simplifying transformations, and synthesizes clean output. This deobfuscator iterates until stable; running it twice does not change the output. By default the input is assumed to run as a classic global script; pass the module switch when it runs as an ES or CommonJS module instead. Name any entrypoint a host calls into, or dead-code removal will discard it as unreachable. """ def __init__( self, *entrypoints: Param[str, Arg.String(metavar='pattern', nargs='*', help=( 'A wildcard pattern naming top-level functions that a host invokes by name, such as the ' 'run handler of a JXA script or a Windows Script Host event handler. Such a function has ' 'no caller inside the file, so it is otherwise removed as unreachable along with ' 'everything only it reached. Give a single asterisk to keep every top-level function.'))], timeout=500, module: Param[bool, Arg.Switch('-m', help=( 'Assume the input runs as an ES or CommonJS module (for example, node file.js), where a ' 'top-level declaration is scoped to the module and does not attach to the global object, ' 'rather than as a classic global script (a browser script tag, Windows Script Host).'))] = False, environment: Param[str | HostEnvironment, Arg.Option('-e', metavar='E', choices=HostEnvironment, help=( 'Pin the host the input runs in, so that the global names that host defines are assumed ' 'present and the reads that rely on them are resolved. The default is {default}, which ' 'assumes only the globals every host shares, so a host-conditional read such as window ' 'or global is kept. Options are: {choices}.'))] = HostEnvironment.universal, ): super().__init__( timeout=timeout, module=module, entrypoints=entrypoints, environment=Arg.AsOption(environment, HostEnvironment), ) def parse(self, data: str) -> JsScript: return JsParser(data).parse() def transform(self, ast: JsScript) -> int: return deobfuscate( ast, module=self.args.module, entrypoints=tuple(self.args.entrypoints), environment=self.args.environment, ) def synthesize(self, ast: JsScript) -> str: return JsSynthesizer().convert(ast)Ancestors
Subclasses
Class variables
var reverse-
The type of the None singleton.
Methods
def parse(self, data)-
Expand source code Browse git
def parse(self, data: str) -> JsScript: return JsParser(data).parse() def transform(self, ast)-
Expand source code Browse git
def transform(self, ast: JsScript) -> int: return deobfuscate( ast, module=self.args.module, entrypoints=tuple(self.args.entrypoints), environment=self.args.environment, ) def synthesize(self, ast)-
Expand source code Browse git
def synthesize(self, ast: JsScript) -> str: return JsSynthesizer().convert(ast)
Inherited members