本文为看雪论坛精华文章
看雪论坛作者ID:R1mao
一
传统平坦化
1.收集原函数中所有的基本块,并初始化随机数种子。
2.对入口基本块进行处理,切分基本块保证入口基本块只有一个后继。
3.给每一个基本块分配一个随机数字,并新建一个变量var,在入口基本块中赋值为入口基本块后继基本块对应的数字。
4.构造出基本的switch结构和循环框架,使得switch链接所有原有基本块。
5.修正每个原有基本块的后继,使其跳转至switch结构,并在跳转之前根据后继和跳转条件构造对var的赋值语句。
二
支持异常处理
三
增强反反混淆效果
定义如果从起点E出发,要到达节点B必须经过节点A,则可称A支配节点B。
每个节点都存在一个标志位,用于标记每个节点(基本块)是否被访问。
Function *buildUpdateKeyFunc(Module *m)
{
std::vector<Type*> params;
params.push_back(Type::getInt8Ty(m->getContext()));
params.push_back(Type::getInt32Ty(m->getContext()));
params.push_back(Type::getInt32Ty(m->getContext())->getPointerTo());
params.push_back(Type::getInt32Ty(m->getContext())->getPointerTo());
params.push_back(Type::getInt32Ty(m->getContext()));
FunctionType *funcType=FunctionType::get(Type::getVoidTy(m->getContext()),params,false);
Function *func=Function::Create(funcType,GlobalValue::PrivateLinkage,Twine("ollvm"),m);
BasicBlock *entry=BasicBlock::Create(m->getContext(),"entry",func);
BasicBlock *cond=BasicBlock::Create(m->getContext(),"cond",func);
BasicBlock *update=BasicBlock::Create(m->getContext(),"update",func);
BasicBlock *end=BasicBlock::Create(m->getContext(),"end",func);
Function::arg_iterator iter=func->arg_begin();
Value *flag=iter;
Value *len=++iter;
Value *posArray=++iter;
Value *keyArray=++iter;
Value *num=++iter;
IRBuilder<> irb(entry);
Value *i=irb.CreateAlloca(irb.getInt32Ty());
irb.CreateStore(irb.getInt32(0),i);
irb.CreateCondBr(irb.CreateICmpEQ(flag,irb.getInt8(0)),cond,end);
irb.SetInsertPoint(cond);
irb.CreateCondBr(irb.CreateICmpSLT(irb.CreateLoad(i),len),update,end);
irb.SetInsertPoint(update);
Value *pos=irb.CreateLoad(irb.CreateGEP(posArray,irb.CreateLoad(i)));
Value *key=irb.CreateGEP(keyArray,pos);
irb.CreateStore(irb.CreateXor(irb.CreateLoad(key),num),key);
irb.CreateStore(irb.CreateAdd(irb.CreateLoad(i),irb.getInt32(1)),i);
irb.CreateBr(cond);
irb.SetInsertPoint(end);
irb.CreateRetVoid();
return func;
}
IRBuilder<> irb(&*oldEntry->getFirstInsertionPt()); // generate context info key for each block
Value *visitedArray=irb.CreateAlloca(irb.getInt8Ty(),irb.getInt32(origBB.size()));
Value *keyArray=irb.CreateAlloca(irb.getInt32Ty(),irb.getInt32(origBB.size()));
irb.CreateMemSet(visitedArray,irb.getInt8(0),origBB.size(),(MaybeAlign)0);
irb.CreateMemSet(keyArray,irb.getInt8(0),origBB.size()*4,(MaybeAlign)0);
int idx=0;
std::vector<unsigned int> key_list;
DominatorTree tree(*f);
std::map<BasicBlock*,unsigned int> key_map;
std::map<BasicBlock*,unsigned int> index_map;
for(std::vector<BasicBlock *>::iterator b=origBB.begin();b!=origBB.end();b++)
{
BasicBlock *block=*b;
unsigned int num=getUniqueNumber(&key_list);
key_list.push_back(num);
key_map[block]=0;
}
for(std::vector<BasicBlock *>::iterator b=origBB.begin();b!=origBB.end();b++,idx++)
{
BasicBlock *block=*b;
std::vector<Constant*> doms;
int i=0;
for(std::vector<BasicBlock *>::iterator bb=origBB.begin();bb!=origBB.end();bb++,i++)
{
BasicBlock *block0=*bb;
if(block0!=block && tree.dominates(block,block0))
{
doms.push_back(irb.getInt32(i));
key_map[block0]^=key_list[idx];
}
}
irb.SetInsertPoint(block->getTerminator());
Value *ptr=irb.CreateGEP(irb.getInt8Ty(),visitedArray,irb.getInt32(idx));
Value *visited=irb.CreateLoad(ptr);
if(doms.size()!=0)
{
ArrayType *arrayType=ArrayType::get(irb.getInt32Ty(),doms.size());
Constant *doms_array=ConstantArray::get(arrayType,ArrayRef<Constant*>(doms));
GlobalVariable *dom_variable=new GlobalVariable(*(f->getParent()),arrayType,false,GlobalValue::LinkageTypes::PrivateLinkage,doms_array,"doms");
irb.CreateCall(FunctionCallee(updateFunc),{visited,irb.getInt32(doms.size()),irb.CreateGEP(dom_variable,{irb.getInt32(0),irb.getInt32(0)}),keyArray,irb.getInt32(key_list[idx])});
}
irb.CreateStore(irb.getInt8(1),ptr);
index_map[block]=idx;
}
irb.SetInsertPoint(block);
irb.CreateStore(irb.CreateXor(irb.CreateLoad(irb.CreateGEP(keyArray,irb.getInt32(index_map[succ]))),ConstantInt::get(sw->getCondition()->getType(),fixNum)),switchVar);
BranchInst::Create(loopEnd,block);
四
结语
看雪ID:R1mao
https://bbs.pediy.com/user-home-948449.htm
看雪CTF官网:https://ctf.pediy.com/
# 往期推荐
球分享
球点赞
球在看
点击“阅读原文”,了解更多!