AST反混淆进阶-try-catch-finally简化

2023-12-15 18:03:42
实现目的:try-catch-finally简化,删除catch部分,其他顺序排列,减少无用代码,增强可读性

处理前 demo.js

try {
	a = 1;
	b = 2;
} catch (_0x2aff1a) {
	c = 3;
	d = 4;
} finally {
	e = 5;
	f = 6;
}

处理后 demoNew.js

a = 1;
b = 2;
e = 5;
f = 6;

主逻辑源码 ObDecryFuMain.js

const fs = require("fs");//文件读写
const parse = require("@babel/parser"); //解析为ast
const traverse = require('@babel/traverse').default;//遍历节点
const t = require('@babel/types');//类型
const generator = require('@babel/generator').default;//ast解析为代码


//读取js文件
const jscode = fs.readFileSync(
    './demo.js', {
        encoding: 'utf-8'
    }
);
let ast = parse.parse(jscode);//js转ast

function del_TryStatement(path) {
	// try-catch-finally 语句简化
	if (path.node.block) {//
		if (path.node.finalizer) {
			//获取try中的body长度
			blocklength = path.node.block.body.length;
			//获取finally中的body长度
			finalizerlength = path.node.finalizer.body.length;
			//遍历finally中的内容,添加
			for (var i = 0; i < finalizerlength; i++) {
				path.node.block.body[blocklength + i] = path.node.finalizer.body[i];
			}
			//节点替换
			path.replaceInline(path.node.block.body);
		} else {
			path.replaceInline(path.node.block.body);
		}


	}
}
traverse(ast, {TryStatement: {exit: [del_TryStatement]},});
let {code} = generator(ast,opts = {jsescOption:{"minimal":true}})
//文件保存
fs.writeFile('./demoNew.js', code, (err) => {
});

文章来源:https://blog.csdn.net/jia666666/article/details/135021267
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。