Technique:Recognizing code obfuscation/JavaScript obfuscator output
What output of javascript-obfuscator looks like: a string array, a rotation function run at start, accessor wrappers, hexadecimal identifiers, and the option names that control each feature.
javascript-obfuscator (BSD-2-Clause, version 5.9.0 read) is the most used open JavaScript obfuscator; the commercial pro VM mode (vm* options) is also described in its README.
Shape of the output
- A string array: every string literal is moved into one array, and each use becomes a call such as
_0x1a2b(0x1f3)(index as a number; withstringArrayIndexesTypeset tohexadecimal-numeric-stringthe index is a string). The array is often returned from a function (a0a-style name in the examples) that is replaced after rotation. - Rotation and shuffle:
stringArrayRotate(default true) shifts the array by a fixed amount chosen at obfuscation time, andstringArrayShuffle(default true) shuffles it; a self-invoking function at the top of the file rotates the array until a generated expression equals a constant:src/custom-code-helpers/string-array/templates/string-array-rotate-function/StringArrayRotateFunctionTemplate.ts(commit 67005e2b6ee9e3e48592f964a28e3e8fa9aa9111) is(function (stringArrayFunction, comparisonValue) { const stringArray = stringArrayFunction(); while (true) { try { const expression = {comparisonExpressionCode}; if (expression === comparisonValue) break; else stringArray['push'](stringArray['shift']()); } catch (e) { stringArray['push'](stringArray['shift']()); } } })({stringArrayFunctionName}, {comparisonValue}), so an exception while evaluating the expression also rotates by one. Unverified: that the generated expression is built from parseInt of decoded entries (its builder was not read). - Encoding of array values:
stringArrayEncodingacceptsnone,base64,rc4; entries look like base64 text, and an rc4 or base64 decoder function sits next to the array. - Index shift (
stringArrayIndexShift, default true): the call subtracts a constant from the index. - Wrappers:
stringArrayWrappersCount,stringArrayWrappersType(variableorfunction),stringArrayWrappersChainedCalls,stringArrayCallsTransform: extra accessor functions that chain to the main one, so there are several decoders. - Identifiers:
identifierNamesGeneratordefaulthexadecimalgives_0xabc123; alternativesdictionary,mangled(a, b, c) andmangled-shuffled. - Control flow flattening (
controlFlowFlattening, threshold default 0.75): object tables of function wrappers andswitchinside awhiledriven by a string of|separated indexes. - Dead code injection (
deadCodeInjection): size can grow up to 200 percent. - splitStrings (chunk length 10 by default when enabled), transformObjectKeys, selfDefending (breaks if the code is beautified), debugProtection (freezes DevTools, optional interval).
Defaults worth knowing
stringArrayThreshold 0.75, stringArrayCallsTransformThreshold 0.5, deadCodeInjectionThreshold 0.4, controlFlowFlatteningThreshold 0.75, all from the README default options.
Analyst approach
Find the array and the decoder, run the decoder in a sandboxed JavaScript engine (not the page) to resolve all indexes, and replace calls with literals. Do not format a selfDefending file before the first run.
See also Technique:Recognizing code obfuscation.