1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| #include "llvm/IR/Function.h" #include "llvm/Pass.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/Transforms/IPO/PassManagerBuilder.h" #include "llvm/Support/raw_ostream.h" #include "llvm/IR/Instructions.h" #include "llvm/Transforms/Utils.h" #include "llvm/Support/CommandLine.h" #include "llvm/Transforms/Utils/Local.h" #include "SplitBasicBlock.h" #include "Utils.h" #include <vector> #include <cstdlib> #include <ctime> #define MAX_RAND 32767 #define NUMBER_CONST_SUBST 2 using namespace llvm; using std::vector; static cl::opt<int> obfuTimes("csub_loop",cl::init(1),cl::desc("Obfucase a function <obfu_loop> time(s).")); namespace{ class ConstantSubstitution :public FunctionPass{ public : static char ID; ConstantSubstitution():FunctionPass(ID){ srand(time(NULL)); } bool runOnFunction(Function &F); void substitute(BinaryOperator *BI); void linearSubstitute(BinaryOperator *BI,int i); void bitwiseSubstitute(BinaryOperator *BI,int i); }; } bool ConstantSubstitution::runOnFunction(Function &F){ INIT_CONTEXT(F); for(int i=0;i<obfuTimes;i++){ for(BasicBlock &BB:F){ vector<Instruction*>origInst; for(Instruction &I:BB){ origInst.push_back(&I); } for(Instruction *I:origInst){ if(BinaryOperator *BI=dyn_cast<BinaryOperator>(I)){ if(BI->getType()->isIntegerTy(32)){ substitute(BI); } } } } } } void ConstantSubstitution::substitute(BinaryOperator *BI){ int operandNum =BI ->getNumOperands(); for(int i=0;i<operandNum;i++){ if(isa<ConstantInt>(BI->getOperand(i))){ int choice =rand()%NUMBER_CONST_SUBST; switch (choice) { case 0: linearSubstitute(BI,i); break; case 1: bitwiseSubstitute(BI,i); break; default: break; } } }
} void ConstantSubstitution::linearSubstitute(BinaryOperator *BI,int i){ Module *M=BI->getModule(); ConstantInt *val=cast<ConstantInt>(BI->getOperand(i)); int randx=rand()%MAX_RAND,randy=rand()%MAX_RAND; int randa=rand()%MAX_RAND,randb=rand()%MAX_RAND; APInt c = val->getValue()-(randx*randa+randy*randb); GlobalVariable *xptr=new GlobalVariable(*M,TYPE_I32,false,GlobalValue::PrivateLinkage,CONST_I32(randx),"x"); GlobalVariable *yptr=new GlobalVariable(*M,TYPE_I32,false,GlobalValue::PrivateLinkage,CONST_I32(randy),"y"); LoadInst *x=new LoadInst(TYPE_I32,xptr,"",BI); LoadInst *y=new LoadInst(TYPE_I32,yptr,"",BI); BinaryOperator *op1=BinaryOperator::CreateMul(CONST_I32(randa),x,"",BI); BinaryOperator *op2=BinaryOperator::CreateMul(CONST_I32(randb),y,"",BI); BinaryOperator *op3=BinaryOperator::CreateAdd(op1,op2,"",BI); BinaryOperator *op4=BinaryOperator::CreateAdd(op3,CONST_I32(c.getSExtValue()),"",BI); BI->setOperand(i,op4); } void ConstantSubstitution::bitwiseSubstitute(BinaryOperator *BI,int i){ Module *M=BI->getModule(); ConstantInt *val=cast<ConstantInt>(BI->getOperand(i)); int randx=rand()%MAX_RAND,randy=rand()%MAX_RAND; APInt c = val->getValue()^(randx<<5|randy>>3); GlobalVariable *xptr=new GlobalVariable(*M,TYPE_I32,false,GlobalValue::PrivateLinkage,CONST_I32(randx),"x"); GlobalVariable *yptr=new GlobalVariable(*M,TYPE_I32,false,GlobalValue::PrivateLinkage,CONST_I32(randy),"y"); LoadInst *x=new LoadInst(TYPE_I32,xptr,"",BI); LoadInst *y=new LoadInst(TYPE_I32,yptr,"",BI); BinaryOperator *op1=BinaryOperator::CreateShl(x,CONST_I32(5),"",BI); BinaryOperator *op2=BinaryOperator::CreateLShr(y,CONST_I32(3),"",BI); BinaryOperator *op3=BinaryOperator::CreateOr(op1,op2,"",BI); BinaryOperator *op4=BinaryOperator::CreateXor(op3,CONST_I32(c.getSExtValue()),"",BI); BI->setOperand(i,op4); } char ConstantSubstitution::ID=0; static RegisterPass<ConstantSubstitution> X("csub","Replace a constant value with equivalent instructions");
|