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 139 140 141 142
| #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 *x = new GlobalVariable(M, val->getType(), false, GlobalValue::PrivateLinkage, constX, "x"); GlobalVariable *y = new GlobalVariable(M, val->getType(), false, GlobalValue::PrivateLinkage, constY, "y"); LoadInst *opX = new LoadInst(val->getType(), x, "", BI); LoadInst *opY = new LoadInst(val->getType(), y, "", BI); BinaryOperator *op1 = BinaryOperator::CreateMul(opX, constA, "", BI); BinaryOperator *op2 = BinaryOperator::CreateMul(opY, constB, "", 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)); unsigned 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 *x = new GlobalVariable(M, val->getType(), false, GlobalValue::PrivateLinkage, constX, "x"); GlobalVariable *y = new GlobalVariable(M, val->getType(), false, GlobalValue::PrivateLinkage, constY, "y"); LoadInst *opX = new LoadInst(val->getType(), x, "", BI); LoadInst *opY = new LoadInst(val->getType(), y, "", BI); BinaryOperator *op1 = BinaryOperator::CreateShl(opX, const5, "", BI); BinaryOperator *op2 = BinaryOperator::CreateLShr(opY, const3, "", BI); BinaryOperator *op3 = BinaryOperator::CreateOr(op1, op2, "", BI); BinaryOperator *op4 = BinaryOperator::CreateXor(op3, constC, "", BI); BI->setOperand(i, op4); }
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; } } } }
char ConstantSubstitution::ID = 0; static RegisterPass<ConstantSubstitution> X("csub", "Replace a constant value with equivalent instructions.");
|