本文主要是作者记笔记为主,温故而知新,记录混淆和解混淆的代码
以以下代码为例:
function test(a, b){
const c = "123";
a = a + 1 // a
a = a + 2
a = a * 1234
a = a.toString()
a = a.substring(0, 3)
b = a + "00"
return b;
}
test(2)
console.log(test(1))
将a.toString a.substring 以及console的访问方式改为[]形式,实现混淆代码如下:
const fs = require('fs');
const parser = require("@babel/parser");
const traverse = require("@babel/traverse").default;
const types = require("@babel/types");
const generator = require("@babel/generator").default;
const jscode = fs.readFileSync("./test2.js", {
encoding: "utf-8"
});
let ast = parser.parse(jscode);
// 混淆代码,将访问的属性名改为括号访问形式
const obvarfun = {
MemberExpression(path) {
if (types.isIdentifier(path.node.property)) {
path.node.property = types.stringLiteral(path.node.property.name);
}
path.node.computed = true;
}
}
执行代码后,改变后的代码如下:
var bigarr = [];
function test(a, b) {
const c = "123";
a = a + 1; // a
a = a + 2;
a = a * 1234;
a = a["toString"]();
a = a["substring"](0, 3);
b = a + "00";
return b;
}
test(2);
console["log"](test(1));
若想将代码还原,解混淆代码如下:
const obvarfun = {
MemberExpression(path) {
if (types.isStringLiteral(path.node.property)) {
path.node.property = types.Identifier(path.node.property.value);
}
path.node.computed = false;
}
}
traverse(ast, obvarfun)