Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simplifyIfStatements Module and Tests #92

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "restringer",
"version": "1.9.1",
"version": "1.10.0",
"description": "Deobfuscate Javascript with emphasis on reconstructing strings",
"main": "index.js",
"bin": {
Expand Down
1 change: 1 addition & 0 deletions src/modules/safe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = {
resolveRedundantLogicalExpressions: require(__dirname + '/resolveRedundantLogicalExpressions'),
separateChainedDeclarators: require(__dirname + '/separateChainedDeclarators'),
simplifyCalls: require(__dirname + '/simplifyCalls'),
simplifyIfStatements: require(__dirname + '/simplifyIfStatements'),
unwrapFunctionShells: require(__dirname + '/unwrapFunctionShells'),
unwrapIIFEs: require(__dirname + '/unwrapIIFEs'),
unwrapSimpleOperations: require(__dirname + '/unwrapSimpleOperations'),
Expand Down
43 changes: 43 additions & 0 deletions src/modules/safe/simplifyIfStatements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
*
* @param {Arborist} arb
* @param {Function} candidateFilter (optional) a filter to apply on the candidates list
* @return {Arborist}
*/
function simplifyIfStatements(arb, candidateFilter = () => true) {
for (let i = 0; i < arb.ast.length; i++) {
const n = arb.ast[i];
if (n.type === 'IfStatement' && candidateFilter(n)) {
// Empty consequent
if (n.consequent.type === 'EmptyStatement' || (n.consequent.type === 'BlockStatement' && !n.consequent.body.length)) {
// Populated alternate
if (n.alternate && n.alternate.type !== 'EmptyStatement' && !(n.alternate.type === 'BlockStatement' && !n.alternate.body.length)) {
// Wrap the test clause in a logical NOT, replace the consequent with the alternate, and remove the now empty alternate.
arb.markNode(n, {
type: 'IfStatement',
test: {
type: 'UnaryExpression',
operator: '!',
prefix: true,
argument: n.test,
},
consequent: n.alternate,
alternate: null,
});
} else arb.markNode(n, {
type: 'ExpressionStatement',
expression: n.test,
}); // Empty alternate
} else if (n.alternate && (n.alternate.type === 'EmptyStatement' || (n.alternate.type === 'BlockStatement' && !n.alternate.body.length))) {
// Remove the empty alternate clause
arb.markNode(n, {
...n,
alternate: null,
});
}
}
}
return arb;
}

module.exports = simplifyIfStatements;
2 changes: 2 additions & 0 deletions src/restringer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const {
resolveProxyReferences,
rearrangeSequences,
simplifyCalls,
simplifyIfStatements,
rearrangeSwitches,
unwrapIIFEs,
unwrapSimpleOperations,
Expand Down Expand Up @@ -123,6 +124,7 @@ class REstringer {
simplifyCalls,
unwrapFunctionShells,
unwrapIIFEs,
simplifyIfStatements,
];
}

Expand Down
49 changes: 49 additions & 0 deletions tests/modules-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,55 @@ function xor(b, c) {
source: `func1.apply(this, [arg1, arg2]); func2.call(this, arg1, arg2);`,
expected: `func1(arg1, arg2);\nfunc2(arg1, arg2);`,
},
{
enabled: true,
name: 'simplifyIfStatements - TP-1 (empty blocks)',
func: __dirname + '/../src/modules/safe/simplifyIfStatements',
source: `if (J) {} else {}`,
expected: `J;`,
},
{
enabled: true,
name: 'simplifyIfStatements - TP-2 (empty block, empty alternate statement)',
func: __dirname + '/../src/modules/safe/simplifyIfStatements',
source: `if (J) {} else;`,
expected: `J;`,
},
{
enabled: true,
name: 'simplifyIfStatements - TP-3 (empty block, populated alternate expression)',
func: __dirname + '/../src/modules/safe/simplifyIfStatements',
source: `if (J) {} else J();`,
expected: `if (!J)\n J();`,
},
{
enabled: true,
name: 'simplifyIfStatements - TP-4 (empty block, populated alternate block)',
func: __dirname + '/../src/modules/safe/simplifyIfStatements',
source: `if (J) {} else {J()}`,
expected: `if (!J) {\n J();\n}`,
},
{
enabled: true,
name: 'simplifyIfStatements - TP-5 (empty statements)',
func: __dirname + '/../src/modules/safe/simplifyIfStatements',
source: `if (J); else;`,
expected: `J;`,
},
{
enabled: true,
name: 'simplifyIfStatements - TP-6 (empty statement, no alternate)',
func: __dirname + '/../src/modules/safe/simplifyIfStatements',
source: `if (J);`,
expected: `J;`,
},
{
enabled: true,
name: 'simplifyIfStatements - TP-7 (empty block, no alternate)',
func: __dirname + '/../src/modules/safe/simplifyIfStatements',
source: `if (J) {}`,
expected: `J;`,
},

// Unsafe
{
Expand Down