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 138
| #include "llvm/IR/Function.h" #include "llvm/IR/Module.h" #include "llvm/Pass.h" #include "llvm/Support/raw_ostream.h" #include "llvm/IR/Instructions.h" #include "llvm/Support/CommandLine.h" #include <vector> #include <cstdlib> #include <ctime> #include "Utils.h" using namespace llvm; using std::vector;
#define MAX_RAND 32767 #define NUMBER_CONST_SUBST 2
static cl::opt<int> obfuTimes("csub_loop", cl::init(1), cl::desc("Obfuscate a function <obfu_time> 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::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() - randA * randX - randB * randY; ConstantInt *constX = CONST(val->getType(), randX); ConstantInt *constY = CONST(val->getType(), randY); ConstantInt *constA = CONST(val->getType(), randA); ConstantInt *constB = CONST(val->getType(), randB); ConstantInt *constC = (ConstantInt*)CONST(val->getType(), c); GlobalVariable *xptr = new GlobalVariable(*M, TYPE_I32, false, GlobalValue::PrivateLinkage, constX, "x"); GlobalVariable *yptr = new GlobalVariable(*M, TYPE_I32, false, GlobalValue::PrivateLinkage, constY, "y"); LoadInst *x = new LoadInst(TYPE_I32, xptr, "", BI); LoadInst *y = new LoadInst(TYPE_I32, yptr, "", BI); BinaryOperator *op1 = BinaryOperator::CreateMul(constA, x, "", BI); BinaryOperator *op2 = BinaryOperator::CreateMul(constB, y, "", BI); BinaryOperator *op3 = BinaryOperator::CreateAdd(op1, op2, "", BI); BinaryOperator *op4 = BinaryOperator::CreateAdd(op3, constC, "", 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); ConstantInt *constX = CONST(val->getType(), randX); ConstantInt *constY = CONST(val->getType(), randY); ConstantInt *const5 = CONST(val->getType(), 5); ConstantInt *const3 = CONST(val->getType(), 3); ConstantInt *constC = (ConstantInt*)CONST(val->getType(), c); GlobalVariable *xptr = new GlobalVariable(*M, TYPE_I32, false, GlobalValue::PrivateLinkage, constX, "x"); GlobalVariable *yptr = new GlobalVariable(*M, TYPE_I32, false, GlobalValue::PrivateLinkage, constY, "y"); LoadInst *x = new LoadInst(TYPE_I32, xptr, "", BI); LoadInst *y = new LoadInst(TYPE_I32, yptr, "", BI); BinaryOperator *op1 = BinaryOperator::CreateShl(x, const5, "", BI); BinaryOperator *op2 = BinaryOperator::CreateLShr(y, const3, "", BI); BinaryOperator *op3 = BinaryOperator::CreateOr(op1, op2, "", BI); BinaryOperator *op4 = BinaryOperator::CreateXor(constC, op3, "", BI); BI->setOperand(i, op4); }
void ConstantSubstitution::substitute(BinaryOperator *BI) { int operateNum = BI->getNumOperands(); for(int i = 0; i < operateNum; i++) { if(isa<ConstantInt>(BI->getOperand(i))) { int opt = rand() % NUMBER_CONST_SUBST; switch(opt) { case 0: linearSubstitute(BI, i); break; case 1: bitwiseSubstitute(BI, i); break; default: break; } } } }
char ConstantSubstitution::ID = 0; static RegisterPass<ConstantSubstitution> X("csub", "Replace a constant value with equivalent instructions.");
|