console.log("not found arg index with name "+arg.name +" at "+arg.loc?.start.line);
}
} else{
returnarg;
}
}
let doFlatten ={
FunctionDeclaration(path) {
let refBinding =path.scope.getBinding(path.node.id?.name);
if(!refBinding.referenced) {
path.remove(); //如果函数没有被引用则直接删除并更新作用域
path.scope.crawl();
return;
}
if(path.node.body.body.length !=1) return;
let body =path.node.body.body[0];
if(!btypes.isReturnStatement(body)) return;
let callExp =body.argument;
if(!btypes.isCallExpression(callExp)) return;
//以上三个判断是否满足混淆函数的格式
let calleeArgs =callExp.arguments; //混淆函数里面调用函数的参数
let funcArgs =path.node.params; //混淆函数的参数
for(let arg of calleeArgs) {
let type=arg.type;
switch (arg.type) {
case "BinaryExpression":
replaceArgsToIndex(funcArgs, (arg as btypes.BinaryExpression).left as btypes.Identifier); //这里可以不case,已经在replaceArgsToIndex中实现了递归,这里case是为了防止有未预期的形式,但是经过测试不存在该情况
break;
case "Identifier":
replaceArgsToIndex(funcArgs, arg as btypes.Identifier);
break;
default:
console.log("callee arg not recognizable at line: "+path.node.loc?.start.line);
return;
}
}
let { id} =path.node;
let binding =path.scope.getBinding((idas btypes.Identifier).name);
for(let refer_path of binding!.referencePaths) {
//获取所有调用
if(!btypes.isCallExpression(refer_path.parent)) {
console.log("abnormal reference at line: "+refer_path.node.loc?.start.line);
continue;
}
let args =(refer_path.parent as btypes.CallExpression).arguments;
let newArgs: btypes.Expression[] =[]; //重组的表调用参数
let argExp: btypes.Expression;
for(let arg of calleeArgs) {
let type=arg.type;
switch (arg.type) {
case "BinaryExpression":
argExp =convertIndexToArg(args, (arg as btypes.BinaryExpression).left as btypes.Identifier);
let exp =btypes.binaryExpression((arg as btypes.BinaryExpression).operator, argExp, (arg as btypes.BinaryExpression).right)
newArgs.push(exp);
//处理重组,按照嵌套二值表达式的方式组装并把变量参数放在最左边
break;
case "Identifier":
argExp =convertIndexToArg(args, arg as btypes.Identifier);
newArgs.push(argExp);
break;
}
}
let newCallExp =btypes.callExpression(callExp.callee, newArgs);
let refBinding =path.scope.getBinding(path.node.id?.name);
if(!refBinding.referenced) {
path.remove(); //如果函数没有被引用则直接删除并更新作用域
path.scope.crawl();
return;
}
if(path.node.body.body.length !=1) return;
let body =path.node.body.body[0];
if(!btypes.isReturnStatement(body)) return;
let callExp =body.argument;
if(!btypes.isCallExpression(callExp)) return;
//以上三个判断是否满足混淆函数的格式
let calleeArgs =callExp.arguments; //混淆函数里面调用函数的参数
let funcArgs =path.node.params; //混淆函数的参数
for(let arg of calleeArgs) {
let type=arg.type;
switch (arg.type) {
case "BinaryExpression":
replaceArgsToIndex(funcArgs, (arg as btypes.BinaryExpression).left as btypes.Identifier); //这里可以不case,已经在replaceArgsToIndex中实现了递归,这里case是为了防止有未预期的形式,但是经过测试不存在该情况
break;
case "Identifier":
replaceArgsToIndex(funcArgs, arg as btypes.Identifier);
break;
default:
console.log("callee arg not recognizable at line: "+path.node.loc?.start.line);
return;
}
}
let { id} =path.node;
let binding =path.scope.getBinding((idas btypes.Identifier).name);
for(let refer_path of binding!.referencePaths) {
//获取所有调用
if(!btypes.isCallExpression(refer_path.parent)) {
console.log("abnormal reference at line: "+refer_path.node.loc?.start.line);
continue;
}
let args =(refer_path.parent as btypes.CallExpression).arguments;
let newArgs: btypes.Expression[] =[]; //重组的表调用参数
let argExp: btypes.Expression;
for(let arg of calleeArgs) {
let type=arg.type;
switch (arg.type) {
case "BinaryExpression":
argExp =convertIndexToArg(args, (arg as btypes.BinaryExpression).left as btypes.Identifier);
let exp =btypes.binaryExpression((arg as btypes.BinaryExpression).operator, argExp, (arg as btypes.BinaryExpression).right)
newArgs.push(exp);
//处理重组,按照嵌套二值表达式的方式组装并把变量参数放在最左边
break;
case "Identifier":
argExp =convertIndexToArg(args, arg as btypes.Identifier);
newArgs.push(argExp);
break;
}
}
let newCallExp =btypes.callExpression(callExp.callee, newArgs);