技术标签: llvm android ollvm 移动安全 Android
- 平坦化一句话概括就是重组原始代码执行流程,把原本易于阅读的代码流程重组成一个switch case形式的代码执行流程,大大降低代码的直观可读性
- 平坦化这么做的目的就是要大幅度降低代码的可读性,从而达到防破解的目的
- 下面我画一个粗略的图来形象的展示一下:
好了,说了Flattening流程平坦化的大致概念,下面开始进入源码分析,看看代码细节上是怎么实现的吧
Flattening操作的代码粒度是函数级别的,因此Flattening继承自FunctionPass
25 namespace {
26 struct Flattening : public FunctionPass {
27 static char ID; // Pass identification, replacement for typeid
28 bool flag;
29
30 Flattening() : FunctionPass(ID) {}
31 Flattening(bool flag) : FunctionPass(ID) { this->flag = flag; }
32
33 bool runOnFunction(Function &F);
34 bool flatten(Function *f);
35 };
36 }
42 bool Flattening::runOnFunction(Function &F) {
43 Function *tmp = &F;
44 // Do we obfuscate
45 if (toObfuscate(flag, tmp, "fla")) {
46
47 if (flatten(tmp)) {
48 ++Flattened;
49 }
50 }
51
52 return false;
53 }
代码很长,都贴出来看着头昏,我会精简掉其中的不太重要的细节代码,由于精简之后的代码依然有点长,所以为了看起来清晰,我按照逻辑分段的方式来分开来说
55 bool Flattening::flatten(Function *f) {
56 vector<BasicBlock *> origBB;
57 BasicBlock *loopEntry;
58 BasicBlock *loopEnd;
59 LoadInst *load;
60 SwitchInst *switchI;
61 AllocaInst *switchVar;
62
63 // SCRAMBLER
64 char scrambling_key[16];
65 llvm::cryptoutils->get_bytes(scrambling_key, 16);
66 // END OF SCRAMBLER
67
68 // Lower switch
69 FunctionPass *lower = createLowerSwitchPass();
70 lower->runOnFunction(*f);
71
72 // Save all original BB
73 for (Function::iterator i = f->begin(); i != f->end(); ++i) {
74 BasicBlock *tmp = &*i;
75 origBB.push_back(tmp);
76
...这里省略了一些代码
81 }
82
继续往下看代码
88 // Remove first BB
89 origBB.erase(origBB.begin());
90
91 // Get a pointer on the first BB
92 Function::iterator tmp = f->begin(); //++tmp;
93 BasicBlock *insert = &*tmp;
94
95 // If main begin with an if
96 BranchInst *br = NULL;
97 if (isa<BranchInst>(insert->getTerminator())) {
98 br = cast<BranchInst>(insert->getTerminator());
99 }
100
101 if ((br != NULL && br->isConditional()) ||
102 insert->getTerminator()->getNumSuccessors() > 1) {
103 BasicBlock::iterator i = insert->end();
104 --i;
105
106 if (insert->size() > 1) {
107 --i;
108 }
109
110 BasicBlock *tmpBB = insert->splitBasicBlock(i, "first");
111 origBB.insert(origBB.begin(), tmpBB);
112 }
接着的代码开始进行流程平坦化骨架代码的准备,上代码:
117 // Create switch variable and set as it
118 switchVar =
119 new AllocaInst(Type::getInt32Ty(f->getContext()), 0, "switchVar", insert);
120 new StoreInst(
121 ConstantInt::get(Type::getInt32Ty(f->getContext()),
122 llvm::cryptoutils->scramble32(0, scrambling_key)),
123 switchVar, insert);
124
125 // Create main loop
126 loopEntry = BasicBlock::Create(f->getContext(), "loopEntry", f, insert);
127 loopEnd = BasicBlock::Create(f->getContext(), "loopEnd", f, insert);
128
129 load = new LoadInst(switchVar, "switchVar", loopEntry);
130
131 // Move first BB on top
132 insert->moveBefore(loopEntry);
133 BranchInst::Create(loopEntry, insert);
134
135 // loopEnd jump to loopEntry
136 BranchInst::Create(loopEntry, loopEnd);
137
138 BasicBlock *swDefault =
139 BasicBlock::Create(f->getContext(), "switchDefault", f, loopEnd);
140 BranchInst::Create(loopEnd, swDefault);
141
142 // Create switch instruction itself and set condition
143 switchI = SwitchInst::Create(&*f->begin(), swDefault, 0, loopEntry);
144 switchI->setCondition(load);
145
146 // Remove branch jump from 1st BB and make a jump to the while
147 f->begin()->getTerminator()->eraseFromParent();
148
149 BranchInst::Create(loopEntry, &*f->begin());
骨架代码准备好了之后,下面就开始往这个骨架代码中填充剩余的BasicBlock了,都是直接填充到switch case这个框架里面了
151 // Put all BB in the switch
152 for (vector<BasicBlock *>::iterator b = origBB.begin(); b != origBB.end();
153 ++b) {
154 BasicBlock *i = *b;
155 ConstantInt *numCase = NULL;
156
157 // Move the BB inside the switch (only visual, no code logic)
158 i->moveBefore(loopEnd);
159
160 // Add case to switch
161 numCase = cast<ConstantInt>(ConstantInt::get(
162 switchI->getCondition()->getType(),
163 llvm::cryptoutils->scramble32(switchI->getNumCases(), scrambling_key)));
164 switchI->addCase(numCase, i);
165 }
代码都组织好了,留下一个问题,那就是现在简单的把各个代码块放入到switch中,那整个代码逻辑是和未平坦化之前一样的吗?答案是不一样的。先看下到目前为止整个代码流程变成什么样了,直接上图:
剩下代码就是来更新每个case中代码块对caseVar的操作了,怎么更新呢,很简单,就是更新成当前代码块原后续逻辑块在当前这个代码框架下的case值
接下来的操作分三种情况来进行
case中代码块没有后续块,那就是一个返回块,不需要更新caseVar
case中代码块只有一个后续块,也就是一个无条件跳转分支,直接更新成后续块对应的case即可
178 // If it's a non-conditional jump
179 if (i->getTerminator()->getNumSuccessors() == 1) {
180 // Get successor and delete terminator
181 BasicBlock *succ = i->getTerminator()->getSuccessor(0);
182 i->getTerminator()->eraseFromParent();
183
184 // Get next case
185 numCase = switchI->findCaseDest(succ);
186
187 // If next case == default case (switchDefault)
188 if (numCase == NULL) {
189 numCase = cast<ConstantInt>(
190 ConstantInt::get(switchI->getCondition()->getType(),
191 llvm::cryptoutils->scramble32(
192 switchI->getNumCases() - 1, scrambling_key)));
193 }
194
195 // Update switchVar and jump to the end of loop
196 new StoreInst(numCase, load->getPointerOperand(), i);
197 BranchInst::Create(loopEnd, i);
198 continue;
199 }
case中代码块有2个后续块,也就是一个条件跳转分支,看代码:
201 // If it's a conditional jump
202 if (i->getTerminator()->getNumSuccessors() == 2) {
203 // Get next cases
204 ConstantInt *numCaseTrue =
205 switchI->findCaseDest(i->getTerminator()->getSuccessor(0));
206 ConstantInt *numCaseFalse =
207 switchI->findCaseDest(i->getTerminator()->getSuccessor(1));
208
209 // Check if next case == default case (switchDefault)
210 if (numCaseTrue == NULL) {
211 numCaseTrue = cast<ConstantInt>(
212 ConstantInt::get(switchI->getCondition()->getType(),
213 llvm::cryptoutils->scramble32(
214 switchI->getNumCases() - 1, scrambling_key)));
215 }
216
217 if (numCaseFalse == NULL) {
218 numCaseFalse = cast<ConstantInt>(
219 ConstantInt::get(switchI->getCondition()->getType(),
220 llvm::cryptoutils->scramble32(
221 switchI->getNumCases() - 1, scrambling_key)));
222 }
223
224 // Create a SelectInst
225 BranchInst *br = cast<BranchInst>(i->getTerminator());
226 SelectInst *sel =
227 SelectInst::Create(br->getCondition(), numCaseTrue, numCaseFalse, "",
228 i->getTerminator());
229
230 // Erase terminator
231 i->getTerminator()->eraseFromParent();
232
233 // Update switchVar and jump to the end of loop
234 new StoreInst(sel, load->getPointerOperand(), i);
235 BranchInst::Create(loopEnd, i);
236 continue;
237 }
238 }
1
2 #include<stdio.h>
3
4 int add(int a, int b) __attribute__((__annotate__(("fla")))){
5 if(a > b){
6 printf("a>b,print\n");
7 return a + b;
8 }else{
9 printf("a<=b, print2\n");
10 return b;
11 }
12 }
文章浏览阅读2.5w次,点赞6次,收藏50次。官方解释是,docker 容器是机器上的沙盒进程,它与主机上的所有其他进程隔离。所以容器只是操作系统中被隔离开来的一个进程,所谓的容器化,其实也只是对操作系统进行欺骗的一种语法糖。_docker菜鸟教程
文章浏览阅读5.7k次,点赞3次,收藏14次。该如何避免的,今天小编给大家推荐两个下载Windows系统官方软件的资源网站,可以杜绝软件捆绑等行为。该站提供了丰富的Windows官方技术资源,比较重要的有MSDN技术资源文档库、官方工具和资源、应用程序、开发人员工具(Visual Studio 、SQLServer等等)、系统镜像、设计人员工具等。总的来说,这两个都是非常优秀的Windows系统镜像资源站,提供了丰富的Windows系统镜像资源,并且保证了资源的纯净和安全性,有需要的朋友可以去了解一下。这个非常实用的资源网站的创建者是国内的一个网友。_msdn我告诉你
文章浏览阅读1.2k次。vue2封装对话框el-dialog组件_
文章浏览阅读4.7k次,点赞5次,收藏6次。MFC 文本框换行 标签: it mfc 文本框1.将Multiline属性设置为True2.换行是使用"\r\n" (宽字符串为L"\r\n")3.如果需要编辑并且按Enter键换行,还要将 Want Return 设置为 True4.如果需要垂直滚动条的话将Vertical Scroll属性设置为True,需要水平滚动条的话将Horizontal Scroll属性设_c++ mfc同一框内输入二行怎么换行
文章浏览阅读832次。检查Linux是否是否开启所需端口,默认为6379,若未打开,将其开启:以root用户执行iptables -I INPUT -p tcp --dport 6379 -j ACCEPT如果还是未能解决,修改redis.conf,修改主机地址:bind 192.168.85.**;然后使用该配置文件,重新启动Redis服务./redis-server redis.conf..._redis-server doesn't support auth command or ismisconfigured. try
文章浏览阅读4.9k次。济大数电实验报告_数据选择器及其应用
文章浏览阅读236次。1研究内容消费在生产中占据十分重要的地位,是生产的最终目的和动力,是保持省内经济稳定快速发展的核心要素。预测河南省社会消费品零售总额,是进行宏观经济调控和消费体制改变创新的基础,是河南省内人民对美好的全面和谐社会的追求的要求,保持河南省经济稳定和可持续发展具有重要意义。本文建立灰色预测模型,利用MATLAB软件,预测出2019年~2023年河南省社会消费品零售总额预测值分别为21881...._灰色预测模型用什么软件
文章浏览阅读1.2k次。12.4-在Qt中使用Log4Qt输出Log文件,看这一篇就足够了一、为啥要使用第三方Log库,而不用平台自带的Log库二、Log4j系列库的功能介绍与基本概念三、Log4Qt库的基本介绍四、将Log4qt组装成为一个单独模块五、使用配置文件的方式配置Log4Qt六、使用代码的方式配置Log4Qt七、在Qt工程中引入Log4Qt库模块的方法八、获取示例中的源代码一、为啥要使用第三方Log库,而不用平台自带的Log库首先要说明的是,在平时开发和调试中开发平台自带的“打印输出”已经足够了。但_log4qt
文章浏览阅读786次。全局观思维模型,一个教我们由点到线,由线到面,再由面到体,不断的放大格局去思考问题的思维模型。_计算机中对于全局观的
文章浏览阅读330次。一、CountDownLatch介绍CountDownLatch采用减法计算;是一个同步辅助工具类和CyclicBarrier类功能类似,允许一个或多个线程等待,直到在其他线程中执行的一组操作完成。二、CountDownLatch俩种应用场景: 场景一:所有线程在等待开始信号(startSignal.await()),主流程发出开始信号通知,既执行startSignal.countDown()方法后;所有线程才开始执行;每个线程执行完发出做完信号,既执行do..._countdownluach于cyclicbarrier的用法
文章浏览阅读508次。Prometheus 算是一个全能型选手,原生支持容器监控,当然监控传统应用也不是吃干饭的,所以就是容器和非容器他都支持,所有的监控系统都具备这个流程,_-自动化监控系统prometheus&grafana实战
文章浏览阅读4.7k次。输入关键字,可以通过键盘的搜索按钮完成搜索功能。_react search