Skip to content

Commit

Permalink
chore: re-add debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Dec 17, 2023
1 parent c7aba12 commit e24147b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
27 changes: 22 additions & 5 deletions packages/webcrack/src/ast-utils/transform.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import traverse, { Node, TraverseOptions, Visitor, visitors } from '@babel/traverse';
import traverse, {
Node,
TraverseOptions,
Visitor,
visitors,
} from '@babel/traverse';
import debug from 'debug';

const logger = debug('webcrack:transforms');

export async function applyTransformAsync<TOptions>(
ast: Node,
transform: AsyncTransform<TOptions>,
options?: TOptions,
): Promise<TransformState> {
logger(`${transform.name}: started`);
const state: TransformState = { changes: 0 };

await transform.run?.(ast, state, options);
if (transform.visitor)
traverse(ast, transform.visitor(options), undefined, state);

logger(`${transform.name}: finished with ${state.changes} changes`);
return state;
}

Expand All @@ -20,24 +30,30 @@ export function applyTransform<TOptions>(
options?: TOptions,
noScopeOverride?: boolean,
): TransformState {
logger(`${transform.name}: started`);
const state: TransformState = { changes: 0 };

transform.run?.(ast, state, options);

if (transform.visitor) {
const visitor = transform.visitor(options) as TraverseOptions<TransformState>;
const visitor = transform.visitor(
options,
) as TraverseOptions<TransformState>;
visitor.noScope = noScopeOverride || !transform.scope;
traverse(ast, visitor, undefined, state);
}

logger(`${transform.name}: finished with ${state.changes} changes`);
return state;
}

export function applyTransforms(
ast: Node,
transforms: Transform[],
noScopeOverride?: boolean,
options: { noScope?: boolean; name?: string; log?: boolean } = {},
): TransformState {
options.log ??= true;
const name = options.name ?? transforms.map((t) => t.name).join(', ');
if (options.log) logger(`${name}: started`);
const state: TransformState = { changes: 0 };

for (const transform of transforms) {
Expand All @@ -48,10 +64,11 @@ export function applyTransforms(
if (traverseOptions.length > 0) {
const visitor: TraverseOptions<TransformState> =
visitors.merge(traverseOptions);
visitor.noScope = noScopeOverride || transforms.every((t) => !t.scope);
visitor.noScope = options.noScope || transforms.every((t) => !t.scope);
traverse(ast, visitor, undefined, state);
}

if (options.log) logger(`${name}: finished with ${state.changes} changes`);
return state;
}

Expand Down
11 changes: 10 additions & 1 deletion packages/webcrack/src/deobfuscate/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import debug from 'debug';
import {
AsyncTransform,
applyTransform,
Expand Down Expand Up @@ -32,12 +33,20 @@ export default {
async run(ast, state, sandbox) {
if (!sandbox) return;

const logger = debug('webcrack:deobfuscate');
const stringArray = findStringArray(ast);
logger(
stringArray
? `String Array: ${stringArray.length} strings`
: 'String Array: no'
);
if (!stringArray) return;

const rotator = findArrayRotator(stringArray);
logger(`String Array Rotate: ${rotator ? 'yes' : 'no'}`);

const decoders = findDecoders(stringArray);
logger(`String Array Encodings: ${decoders.length}`);

state.changes += applyTransform(ast, inlineObjectProps).changes;

Expand All @@ -62,7 +71,7 @@ export default {
state.changes += applyTransforms(
ast,
[mergeStrings, deadCode, controlFlowObject, controlFlowSwitch],
true,
{ noScope: true },
).changes;
},
} satisfies AsyncTransform<Sandbox>;
10 changes: 8 additions & 2 deletions packages/webcrack/src/deobfuscate/vm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NodePath } from '@babel/traverse';
import { CallExpression } from '@babel/types';
import debug from 'debug';
import { generate } from '../ast-utils';
import { ArrayRotator } from './array-rotator';
import { Decoder } from './decoder';
Expand Down Expand Up @@ -67,7 +68,12 @@ export class VMDecoder {
return [${calls.join(',')}]
})()`;

const result = await this.sandbox(code);
return result as unknown[];
try {
const result = await this.sandbox(code);
return result as unknown[];
} catch (error) {
debug('webcrack:deobfuscate')('vm code:', code);
throw error;
}
}
}
13 changes: 6 additions & 7 deletions packages/webcrack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,11 @@ export async function webcrack(
}));
},
() => {
return applyTransforms(ast, [
blockStatements,
sequence,
splitVariableDeclarations,
varFunctions,
]);
return applyTransforms(
ast,
[blockStatements, sequence, splitVariableDeclarations, varFunctions],
{ name: 'prepare' },
);
},
options.deobfuscate &&
(() => applyTransformAsync(ast, deobfuscate, options.sandbox)),
Expand All @@ -153,7 +152,7 @@ export async function webcrack(
options.deobfuscate ? [selfDefending, debugProtection] : [],
options.jsx ? [jsx, jsxNew] : [],
].flat(),
true,
{ noScope: true },
);
}),
options.deobfuscate && (() => applyTransform(ast, mergeObjectAssignments)),
Expand Down
4 changes: 3 additions & 1 deletion packages/webcrack/src/unminify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export const unminify = {
tags: ['safe'],
scope: true,
run(ast, state) {
state.changes += applyTransforms(ast, Object.values(transforms)).changes;
state.changes += applyTransforms(ast, Object.values(transforms), {
log: false,
}).changes;
},
} satisfies Transform;

Expand Down
2 changes: 2 additions & 0 deletions packages/webcrack/src/unpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as m from '@codemod/matchers';
import { unpackBrowserify } from './browserify';
import { Bundle } from './bundle';
import { unpackWebpack } from './webpack';
import debug from 'debug';

export { Bundle } from './bundle';

Expand Down Expand Up @@ -34,6 +35,7 @@ export function unpackAST(
if (options.bundle) {
options.bundle.applyMappings(mappings);
options.bundle.applyTransforms();
debug('webcrack:unpack')('Bundle:', options.bundle.type);
}
return options.bundle;
}

0 comments on commit e24147b

Please sign in to comment.