Skip to content

Commit 0f3fdb0

Browse files
authored
Merge pull request #9 from 0x1Avram/dev
replaced slow AST nodes queries
2 parents bd42d0c + 37743b6 commit 0f3fdb0

File tree

1 file changed

+84
-45
lines changed

1 file changed

+84
-45
lines changed

Stages/stage_03_stringarray.js

+84-45
Original file line numberDiff line numberDiff line change
@@ -935,10 +935,14 @@ class StringArrayCallsReplacer{
935935
replace(){
936936
this._initializeVariableAndFunctionWrappers();
937937

938+
var i = 0;
938939
var thisObj = this;
939940
estraverse.traverse(this.ast, {
940941
enter: function(node){
941942
if(astOperations.NodeChecker.nodeHasBodyWithLexicalScope(node)){
943+
i++;
944+
console.log(`Replacing calls in scope nr. ${i}.`);
945+
thisObj.logger.info(`Replacing calls in scope nr. ${i}.`);
942946
thisObj._fixStringArrayCallsInLexicalScope(node);
943947
}
944948
},
@@ -1200,19 +1204,22 @@ class StringArrayCallsReplacer{
12001204
let wrapperNamesSearchedInCurrentScope = this._getVariableWrapperNamesToSearchInCurrentScope(nodeWithBody,
12011205
curentNodeWrapperNames, allWrapperNames);
12021206

1203-
1204-
for(let i = 0; i < wrapperNamesSearchedInCurrentScope.length; i++){
1205-
const identifierNodesFromCalls = esquery(nodeWithBody, `CallExpression[callee.type='Identifier'] > `
1206-
+ `Identifier[name='${wrapperNamesSearchedInCurrentScope[i]}']`);
1207-
identifierNodesFromCalls.forEach((identifierNode) => {
1208-
const lexicalScope = astOperations.ASTRelations.getParentNodeWithLexicalScope(identifierNode);
1209-
if(nodeWithBody == lexicalScope){
1210-
const callExpression = identifierNode.parent;
1211-
wrapperCalls.push(callExpression);
1207+
var firstNode = true;
1208+
estraverse.traverse(nodeWithBody, {
1209+
enter: function(node, parent){
1210+
if(!firstNode && astOperations.NodeChecker.nodeHasBodyWithLexicalScope(node)){
1211+
this.skip();
12121212
}
1213-
});
1214-
}
1213+
firstNode = false;
12151214

1215+
if((node.type == 'Identifier') && (parent.type == 'CallExpression')){
1216+
let identifierName = node.name;
1217+
if(wrapperNamesSearchedInCurrentScope.includes(identifierName)){
1218+
wrapperCalls.push(parent);
1219+
}
1220+
}
1221+
},
1222+
});
12161223
return wrapperCalls;
12171224
}
12181225

@@ -1246,36 +1253,61 @@ class StringArrayCallsReplacer{
12461253
}
12471254

12481255
_wrapperIsRedefinedInCurrentScopeAsNewVariable(wrapperName, nodeWithBody){
1249-
const allVariableDeclarators = esquery(nodeWithBody, `VariableDeclarator[id.type='Identifier'][id.name='${wrapperName}']`);
1250-
for(let i = 0; i < allVariableDeclarators.length; i++){
1251-
const lexicalScope = astOperations.ASTRelations.getParentNodeWithLexicalScope(allVariableDeclarators[i]);
1252-
if(lexicalScope == nodeWithBody){
1253-
return true;
1254-
}
1255-
}
1256-
return false;
1256+
var firstNode = true;
1257+
var foundRedefined = false;
1258+
estraverse.traverse(nodeWithBody, {
1259+
enter: function(node, parent){
1260+
if(!firstNode && astOperations.NodeChecker.nodeHasBodyWithLexicalScope(node)){
1261+
this.skip();
1262+
}
1263+
firstNode = false;
1264+
1265+
if((node.type == 'Identifier') && (node.name == wrapperName) && (parent.type == 'VariableDeclarator')){
1266+
foundRedefined = true;
1267+
this.break();
1268+
}
1269+
},
1270+
});
1271+
return foundRedefined;
12571272
}
12581273

12591274
_wrapperIsRedefinedInCurrentScopeAsNewFunction(wrapperName, nodeWithBody){
1260-
const allFunctionDeclarations = esquery(nodeWithBody, `FunctionDeclaration[id.type='Identifier'][id.name='${wrapperName}']`);
1261-
for(let i = 0; i < allFunctionDeclarations.length; i++){
1262-
const lexicalScope = astOperations.ASTRelations.getParentNodeWithLexicalScope(allFunctionDeclarations[i]);
1263-
if(lexicalScope == nodeWithBody){
1264-
return true;
1265-
}
1266-
}
1267-
return false;
1275+
var firstNode = true;
1276+
var foundRedefined = false;
1277+
estraverse.traverse(nodeWithBody, {
1278+
enter: function(node, parent){
1279+
if(!firstNode && astOperations.NodeChecker.nodeHasBodyWithLexicalScope(node)){
1280+
this.skip();
1281+
}
1282+
firstNode = false;
1283+
1284+
if((node.type == 'Identifier') && (node.name == wrapperName) && (parent.type == 'FunctionDeclaration')){
1285+
foundRedefined = true;
1286+
this.break();
1287+
}
1288+
},
1289+
});
1290+
return foundRedefined;
12681291
}
12691292

12701293
_wrapperIsRedefinedInCurrentScopeAsAssignmentExpression(wrapperName, nodeWithBody){
1271-
const allAssignmentExpressions = esquery(nodeWithBody, `AssignmentExpression[left.type='Identifier'][left.name='${wrapperName}']`);
1272-
for(let i = 0; i < allAssignmentExpressions.length; i++){
1273-
const lexicalScope = astOperations.ASTRelations.getParentNodeWithLexicalScope(allAssignmentExpressions[i]);
1274-
if(lexicalScope == nodeWithBody){
1275-
return true;
1276-
}
1277-
}
1278-
return false;
1294+
var firstNode = true;
1295+
var foundRedefined = false;
1296+
estraverse.traverse(nodeWithBody, {
1297+
enter: function(node, parent){
1298+
if(!firstNode && astOperations.NodeChecker.nodeHasBodyWithLexicalScope(node)){
1299+
this.skip();
1300+
}
1301+
firstNode = false;
1302+
1303+
if((node.type == 'Identifier') && (node.name == wrapperName)
1304+
&& (parent.type == 'AssignmentExpression') && parent.left && (parent.left.name == wrapperName)){
1305+
foundRedefined = true;
1306+
this.break();
1307+
}
1308+
},
1309+
});
1310+
return foundRedefined;
12791311
}
12801312

12811313
_wrapperWasDefinedInAScopeThatIsParentOfCurrentScope(wrapper, nodeWithBody){
@@ -1430,17 +1462,22 @@ class StringArrayCallsReplacer{
14301462
let wrapperNamesForSearching = this._getFunctionWrapperNamesToSearchInCurrentScope(nodeWithBody,
14311463
curentNodeWrapperNames, allWrapperNames);
14321464

1433-
for(let i = 0; i < wrapperNamesForSearching.length; i++){
1434-
const identifierNodesFromCalls = esquery(nodeWithBody, `CallExpression[callee.type='Identifier'] > `
1435-
+ `Identifier[name='${wrapperNamesForSearching[i]}']`);
1436-
identifierNodesFromCalls.forEach((node) => {
1437-
const lexicalScope = astOperations.ASTRelations.getParentNodeWithLexicalScope(node);
1438-
if(nodeWithBody == lexicalScope){
1439-
const callExpression = node.parent;
1440-
wrapperCalls.push(callExpression);
1465+
var firstNode = true;
1466+
estraverse.traverse(nodeWithBody, {
1467+
enter: function(node, parent){
1468+
if(!firstNode && astOperations.NodeChecker.nodeHasBodyWithLexicalScope(node)){
1469+
this.skip();
14411470
}
1442-
});
1443-
}
1471+
firstNode = false;
1472+
1473+
if((node.type == 'Identifier') && (parent.type == 'CallExpression')){
1474+
let identifierName = node.name;
1475+
if(wrapperNamesForSearching.includes(identifierName)){
1476+
wrapperCalls.push(parent);
1477+
}
1478+
}
1479+
},
1480+
});
14441481

14451482
return wrapperCalls;
14461483
}
@@ -1505,9 +1542,11 @@ class StringArraySyntheticTransformer extends stageDeobfuscator.TransformerDeobf
15051542
}
15061543

15071544
_replaceStringArrayCallsWithCorrespondingString(){
1545+
this.logger.info('Replacing stringarray calls with actual strings - START.');
15081546
this.stringArrayCallsReplacer = new StringArrayCallsReplacer(this.logger, this.obfuscatedSourceCode,
15091547
this.ast, this.stringArrayCallWrapperTemplateFunctionNames);
15101548
this.stringArrayCallsReplacer.replace();
1549+
this.logger.info('Replacing stringarray calls with actual strings - FINISH.');
15111550
}
15121551

15131552
}

0 commit comments

Comments
 (0)