当前位置: 首页 > news >正文

AST入门与实战(三):if节点转switch节点(瑞数5)

原文地址:https://zhuoyue360.com/jsnx/110.html

1. 期望

这是一个瑞数5代解混淆的案例,我们本章节需要做的是把if节点的内容转换成switch-case内容.以此来熟悉AST对JS混淆的对抗.

原始代码:

function whileState() {while (1) {aV = cA[wU++];if (aV < 4) {if (aV < 1) {zT = window, kD = String, bO = Array, xX = document, nZ = Date;} else if (aV < 2) {iG = zT['ab'] = {};} else if (aV < 3) {iG = zT['ab'];} else {mM = !iG;}} else {if (aV < 5) {xT(0);} else if (aV < 6) {if (!mM) wU += 1;} else if (aV < 7) {lG = [4, 16, 64, 256, 1024, 4096, 16384, 65536];} else {return;}}}
}

期望代码:

function whileState() {while (1) {switch (cA[wU++]) {case 0:zT = window, kD = String, bO = Array, xX = document, nZ = Date;break;case 1:iG = zT['ab'] = {};break;case 2:iG = zT['ab'];break;case 3:mM = !iG;break;case 4:xT(0);break;case 5:if (!mM) wU += 1;break;case 6:lG = [4, 16, 64, 256, 1024, 4096, 16384, 65536];break;case 7:return;break;}}
}

2. 思路分析

首先,我们需要明确一点,aV 的索引是从0开始的,它是不可能为负数的.

那么也就可以有如下的转换:

if (aV < 1) {zT = window, kD = String, bO = Array, xX = document, nZ = Date;
}

转换成

if (aV == 0) {zT = window, kD = String, bO = Array, xX = document, nZ = Date;
}

这是蔡老板所说的夹逼原理 ,奈何文化低,我不懂.知道有这么一个回事就行.

思路如下(更加详细的看代码注释):

  1. while循环的参数是NumericLiteral ,且内容为1.
  2. body中只有2个节点
  3. 提取出aV
  4. 找到WhileStatement
  5. 枚举WhileStatement下的IfStatement节点.
  6. leftname应该为我们提取出的aV
  7. operator<
  8. right类型不能为IfStatement, 因为它有嵌套.
  9. 记录下了所有符合条件的body
  10. 生成switch 节点

image-20230811172025893

3. 代码

function collectSwitchCase(whilePath,name){// 菜老板知识星球获得.let ifNodes = [];// 遍历WhilePathwhilePath.traverse({"IfStatement"(path){//遍历所有的ifStatement;let {test,consequent,alternate} = path.node; //获取子节点let {left,operator,right} = test; // 必定是BinaryExpressionif (!types.isIdentifier(left,{name:name}) || operator != '<' || !types.isNumericLiteral(right)) {//条件过滤return;}let value = right.value;//保存整个body,记得生成switchCase节点的时候加上break节点。ifNodes[right.value-1] = consequent.body;   if (!types.isIfStatement(alternate)){ifNodes[right.value] = alternate.body;  //最后一个else,其实就是上一个else-if 的 test.right的值}	}})return ifNodes;}const if2switchReplace = {WhileStatement(path){let {test,body} = path.node;// `while`循环的参数是`NumericLiteral` ,且内容为`1`.  body中只有2个节点if(!types.isNumericLiteral(test,{value:1}) || body.body.length != 2){return}// 判断while循环格式, 条件过滤let blockBody = body.body;if (!types.isExpressionStatement(blockBody[0]) || !types.isIfStatement(blockBody[1])){return;}// left 左边的节点就是我们需要的变量名let {left,right} = blockBody[0].expression; //或者左右节点       aV = cA[wU++];let name = left.name;// 获取到了变量名称后, 就需要收集使用了aV的caselet ifNodes = collectSwitchCase(path,name);   //收集case//无case,直接返回。if (ifNodes.length == 0) return;   let len = ifNodes.length;for (let i=0; i < len; i++){//每一个case最后都加breakifNodes[i].push(types.BreakStatement()); ifNodes[i] = types.SwitchCase(test = types.valueToNode(i),consequent = ifNodes[i]);  //生成SwitchCase节点}//生成SwitchCase节点let switchNode = types.SwitchStatement(right,ifNodes);   path.node.body.body = [switchNode]; //最后的while节点只有一个Switch Node;}   
}traverse(ast, if2switchReplace);
http://www.lryc.cn/news/119162.html

相关文章:

  • 小白到运维工程师自学之路 第七十一集 (kubernetes网络设置)
  • day17 enum abstract interface 枚举 抽象 接口
  • c刷题(二)
  • 【leetcode】15. 三数之和(medium)
  • 【css】属性选择器
  • Redis_概述
  • 【从零学习python 】16. Python字符串的format方法(一)
  • python re 模块 正则表达式
  • c#设计模式-创建型模式 之 单例模式
  • K-01BFS(2023河南萌新联赛第(五)场:郑州轻工业大学)
  • CSP复习每日一题(四)
  • dubbo之整合SpringBoot
  • UE 5 GAS 在项目中处理AttributeSet相关
  • JDBC数据库连接
  • gitee分支合并
  • Python小白入门:文件、异常处理和json格式存储数据
  • 16bit、8 通道、500kSPS、 SAR 型 ADC——MS5188N
  • Chapter 12: Regular expressions | Python for Everybody 讲义笔记_En
  • Android javaMail mergeDebugJavaResource FAILED解决
  • 【ArcGIS Pro二次开发】(57):地图系列
  • 秋招打卡015(20230811)
  • 如何使用Word转PDF转换器在线工具?在线Word转PDF使用方法
  • 自然语言处理从入门到应用——LangChain:记忆(Memory)-[记忆的类型Ⅰ]
  • Camunda 7.x 系列【7】Spring Boot 集成 Camunda 7.19
  • 24华东交通软件工程837考研题库
  • nginx 以及nginx优化
  • cesium学习记录04-坐标系
  • P5737 【深基7.例3】闰年展示
  • Nacos的安装使用教程Linux
  • 数据结构-学习