Skip to content

Commit

Permalink
Small Fixes (#109)
Browse files Browse the repository at this point in the history
* Refactor redundant code into a single statement (modules/safe/unwrapFunctionShells)

* Fix issue where cycle changes counter did not reset between cycles (modules/utils/runLoop)

* Fix logger line not printing (restringer.js)

* Add version logging (restringer.js)

* Replace Node.js v16 with Node.js v20 (.github/workflows/node.js.yml)
  • Loading branch information
BenBaryoPX authored Jan 14, 2024
1 parent 9b90243 commit 8b2075f
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [16.x, 18.x]
node-version: [18.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
3 changes: 1 addition & 2 deletions src/modules/safe/unwrapFunctionShells.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ function unwrapFunctionShells(arb, candidateFilter = () => true) {
for (let i = 0; i < arb.ast.length; i++) {
const n = arb.ast[i];
if (['FunctionDeclaration', 'FunctionExpression'].includes(n.type) &&
n.body?.body?.length === 1 &&
n.body.body[0].type === 'ReturnStatement' &&
n.body?.body?.[0]?.type === 'ReturnStatement' &&
(n.body.body[0].argument?.callee?.property?.name || n.body.body[0].argument?.callee?.property?.value) === 'apply' &&
n.body.body[0].argument.arguments?.length === 2 &&
n.body.body[0].argument.callee.object.type === 'FunctionExpression' &&
Expand Down
3 changes: 2 additions & 1 deletion src/modules/utils/runLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ function hasGlobalMaxIterationBeenReached() {
function runLoop(script, funcs, maxIterations = defaultMaxIterations) {
let scriptSnapshot = '';
let currentIteration = 0;
let changesCounter = 0;
try {
let scriptHash = generateHash(script);
let changesCounter = 0;
let arborist = new Arborist(script, logger.log);
while (arborist.ast?.length && scriptSnapshot !== script && currentIteration < maxIterations && !hasGlobalMaxIterationBeenReached()) {
const cycleStartTime = Date.now();
Expand Down Expand Up @@ -60,6 +60,7 @@ function runLoop(script, funcs, maxIterations = defaultMaxIterations) {
++globalIterationsCounter;
logger.log(`[+] ==> Cycle ${globalIterationsCounter} completed in ${(Date.now() - cycleStartTime) / 1000} seconds` +
` with ${changesCounter ? changesCounter : 'no'} changes (${arborist.ast?.length || '???'} nodes)`);
changesCounter = 0;
}
if (changesCounter) script = arborist.script;
} catch (e) {
Expand Down
3 changes: 2 additions & 1 deletion src/restringer.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,12 @@ if (require.main === module) {
const fs = require('node:fs');
let content = fs.readFileSync(args.inputFilename, 'utf-8');
const startTime = Date.now();
logger.log(`[!] Deobfuscating ${args.inputFilename}...\n`);

const restringer = new REstringer(content);
if (args.quiet) restringer.logger.setLogLevel(logger.logLevels.NONE);
else if (args.verbose) restringer.logger.setLogLevel(logger.logLevels.DEBUG);
logger.log(`[!] REstringer v${REstringer.__version__}`);
logger.log(`[!] Deobfuscating ${args.inputFilename}...`);
if (args.maxIterations) {
setGlobalMaxIterations(args.maxIterations);
restringer.logger.log(`[!] Running at most ${args.maxIterations} iterations`);
Expand Down

0 comments on commit 8b2075f

Please sign in to comment.