forked from 61bcdefg/Hikari-LLVM15-Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndirectBranch.cpp
272 lines (256 loc) · 10.9 KB
/
IndirectBranch.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// For open-source license, please refer to
// [License](https://github.com/HikariObfuscator/Hikari/wiki/License).
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Obfuscation/IndirectBranch.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/NoFolder.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Obfuscation/CryptoUtils.h"
#include "llvm/Transforms/Obfuscation/Utils.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/LowerSwitch.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
using namespace llvm;
static cl::opt<bool>
UseStack("indibran-use-stack", cl::init(true), cl::NotHidden,
cl::desc("[IndirectBranch]Stack-based indirect jumps"));
static bool UseStackTemp = true;
static cl::opt<bool>
EncryptJumpTarget("indibran-enc-jump-target", cl::init(false),
cl::NotHidden,
cl::desc("[IndirectBranch]Encrypt jump target"));
static bool EncryptJumpTargetTemp = false;
namespace llvm {
struct IndirectBranch : public FunctionPass {
static char ID;
bool flag;
bool initialized;
std::map<BasicBlock *, unsigned long long> indexmap;
std::map<Function *, ConstantInt *> encmap;
IndirectBranch() : FunctionPass(ID) {
this->flag = true;
this->initialized = false;
}
IndirectBranch(bool flag) : FunctionPass(ID) {
this->flag = flag;
this->initialized = false;
}
StringRef getPassName() const override { return "IndirectBranch"; }
bool initialize(Module &M) {
PassBuilder PB;
FunctionAnalysisManager FAM;
FunctionPassManager FPM;
PB.registerFunctionAnalyses(FAM);
FPM.addPass(LowerSwitchPass());
SmallVector<Constant *, 32> BBs;
unsigned long long i = 0;
for (Function &F : M) {
if (!toObfuscate(flag, &F, "indibr"))
continue;
if (!toObfuscateBoolOption(&F, "indibran_use_stack", &UseStackTemp))
UseStackTemp = UseStack;
// See https://github.com/61bcdefg/Hikari-LLVM15/issues/32
FPM.run(F, FAM);
if (!toObfuscateBoolOption(&F, "indibran_enc_jump_target",
&EncryptJumpTargetTemp))
EncryptJumpTargetTemp = EncryptJumpTarget;
if (EncryptJumpTargetTemp)
encmap[&F] = ConstantInt::get(
Type::getInt32Ty(M.getContext()),
cryptoutils->get_range(UINT8_MAX, UINT16_MAX * 2) * 4);
for (BasicBlock &BB : F)
if (!BB.isEntryBlock()) {
indexmap[&BB] = i++;
BBs.emplace_back(EncryptJumpTargetTemp
? ConstantExpr::getGetElementPtr(
Type::getInt8Ty(M.getContext()),
ConstantExpr::getBitCast(
BlockAddress::get(&BB),
Type::getInt8PtrTy(M.getContext())),
encmap[&F])
: BlockAddress::get(&BB));
}
}
ArrayType *AT =
ArrayType::get(Type::getInt8PtrTy(M.getContext()), BBs.size());
Constant *BlockAddressArray =
ConstantArray::get(AT, ArrayRef<Constant *>(BBs));
GlobalVariable *Table = new GlobalVariable(
M, AT, false, GlobalValue::LinkageTypes::PrivateLinkage,
BlockAddressArray, "IndirectBranchingGlobalTable");
appendToCompilerUsed(M, {Table});
this->initialized = true;
return true;
}
bool runOnFunction(Function &Func) override {
if (!toObfuscate(flag, &Func, "indibr"))
return false;
Module *M = Func.getParent();
if (!this->initialized)
initialize(*M);
errs() << "Running IndirectBranch On " << Func.getName() << "\n";
SmallVector<BranchInst *, 32> BIs;
for (Instruction &Inst : instructions(Func))
if (BranchInst *BI = dyn_cast<BranchInst>(&Inst))
BIs.emplace_back(BI);
Type *Int8Ty = Type::getInt8Ty(M->getContext());
Type *Int32Ty = Type::getInt32Ty(M->getContext());
Type *Int8PtrTy = Type::getInt8PtrTy(M->getContext());
Value *zero = ConstantInt::get(Int32Ty, 0);
IRBuilder<NoFolder> *IRBEntry =
new IRBuilder<NoFolder>(&Func.getEntryBlock().front());
for (BranchInst *BI : BIs) {
if (UseStackTemp &&
IRBEntry->GetInsertPoint() !=
(BasicBlock::iterator)Func.getEntryBlock().front())
IRBEntry->SetInsertPoint(Func.getEntryBlock().getTerminator());
IRBuilder<NoFolder> *IRBBI = new IRBuilder<NoFolder>(BI);
SmallVector<BasicBlock *, 16> BBs;
// We use the condition's evaluation result to generate the GEP
// instruction False evaluates to 0 while true evaluates to 1. So here
// we insert the false block first
if (BI->isConditional() && !BI->getSuccessor(1)->isEntryBlock())
BBs.emplace_back(BI->getSuccessor(1));
if (!BI->getSuccessor(0)->isEntryBlock())
BBs.emplace_back(BI->getSuccessor(0));
GlobalVariable *LoadFrom = nullptr;
if (BI->isConditional() ||
indexmap.find(BI->getSuccessor(0)) == indexmap.end()) {
ArrayType *AT = ArrayType::get(Int8PtrTy, BBs.size());
SmallVector<Constant *, 16> BlockAddresses;
for (BasicBlock *BB : BBs)
BlockAddresses.emplace_back(
EncryptJumpTargetTemp ? ConstantExpr::getGetElementPtr(
Int8Ty,
ConstantExpr::getBitCast(
BlockAddress::get(BB), Int8PtrTy),
encmap[&Func])
: BlockAddress::get(BB));
// Create a new GV
Constant *BlockAddressArray =
ConstantArray::get(AT, ArrayRef<Constant *>(BlockAddresses));
LoadFrom = new GlobalVariable(
*M, AT, false, GlobalValue::LinkageTypes::PrivateLinkage,
BlockAddressArray, "HikariConditionalLocalIndirectBranchingTable");
appendToCompilerUsed(*Func.getParent(), {LoadFrom});
} else {
LoadFrom = M->getGlobalVariable("IndirectBranchingGlobalTable", true);
}
AllocaInst *LoadFromAI = nullptr;
if (UseStackTemp) {
LoadFromAI = IRBEntry->CreateAlloca(LoadFrom->getType());
IRBEntry->CreateStore(LoadFrom, LoadFromAI);
}
Value *index, *RealIndex = nullptr;
if (BI->isConditional()) {
Value *condition = BI->getCondition();
Value *zext = IRBBI->CreateZExt(condition, Int32Ty);
if (UseStackTemp) {
AllocaInst *condAI = IRBEntry->CreateAlloca(Int32Ty);
IRBBI->CreateStore(zext, condAI);
index = condAI;
} else {
index = zext;
}
RealIndex = index;
} else {
Value *indexval = nullptr;
ConstantInt *IndexEncKey =
EncryptJumpTargetTemp ? cast<ConstantInt>(ConstantInt::get(
Int32Ty, cryptoutils->get_uint32_t()))
: nullptr;
if (EncryptJumpTargetTemp) {
GlobalVariable *indexgv = new GlobalVariable(
*M, Int32Ty, false, GlobalValue::LinkageTypes::PrivateLinkage,
ConstantInt::get(IndexEncKey->getType(),
IndexEncKey->getValue() ^
indexmap[BI->getSuccessor(0)]),
"IndirectBranchingIndex");
appendToCompilerUsed(*M, {indexgv});
indexval = (UseStackTemp ? IRBEntry : IRBBI)
->CreateLoad(indexgv->getValueType(), indexgv);
} else {
indexval = ConstantInt::get(Int32Ty, indexmap[BI->getSuccessor(0)]);
if (UseStackTemp) {
AllocaInst *indexAI = IRBEntry->CreateAlloca(Int32Ty);
IRBEntry->CreateStore(indexval, indexAI);
indexval = IRBBI->CreateLoad(indexAI->getAllocatedType(), indexAI);
}
}
index = indexval;
RealIndex = EncryptJumpTargetTemp ? IRBBI->CreateXor(index, IndexEncKey)
: index;
}
Value *LI, *enckeyLoad, *gepptr = nullptr;
if (UseStackTemp) {
LoadInst *LILoadFrom =
IRBBI->CreateLoad(LoadFrom->getType(), LoadFromAI);
Value *GEP = IRBBI->CreateGEP(
LoadFrom->getValueType(), LILoadFrom,
{zero, BI->isConditional() ? IRBBI->CreateLoad(Int32Ty, RealIndex)
: RealIndex});
if (!EncryptJumpTargetTemp)
LI = IRBBI->CreateLoad(Int8PtrTy, GEP,
"IndirectBranchingTargetAddress");
else
gepptr = IRBBI->CreateLoad(Int8PtrTy, GEP);
} else {
Value *GEP = IRBBI->CreateGEP(LoadFrom->getValueType(), LoadFrom,
{zero, RealIndex});
if (!EncryptJumpTargetTemp)
LI = IRBBI->CreateLoad(Int8PtrTy, GEP,
"IndirectBranchingTargetAddress");
else
gepptr = IRBBI->CreateLoad(Int8PtrTy, GEP);
}
if (EncryptJumpTargetTemp) {
ConstantInt *encenckey = cast<ConstantInt>(
ConstantInt::get(Int32Ty, cryptoutils->get_uint32_t()));
GlobalVariable *enckeyGV = new GlobalVariable(
*M, Int32Ty, false, GlobalValue::LinkageTypes::PrivateLinkage,
ConstantInt::get(Int32Ty,
encenckey->getValue() ^ encmap[&Func]->getValue()),
"IndirectBranchingAddressEncryptKey");
appendToCompilerUsed(*M, enckeyGV);
enckeyLoad = IRBBI->CreateXor(
IRBBI->CreateLoad(enckeyGV->getValueType(), enckeyGV), encenckey);
LI =
IRBBI->CreateGEP(Int8Ty, gepptr, IRBBI->CreateSub(zero, enckeyLoad),
"IndirectBranchingTargetAddress");
}
IndirectBrInst *indirBr = IndirectBrInst::Create(LI, BBs.size());
for (BasicBlock *BB : BBs)
indirBr->addDestination(BB);
ReplaceInstWithInst(BI, indirBr);
}
shuffleBasicBlocks(Func);
return true;
}
void shuffleBasicBlocks(Function &F) {
SmallVector<BasicBlock *, 32> blocks;
for (BasicBlock &block : F)
if (!block.isEntryBlock())
blocks.emplace_back(&block);
if (blocks.size() < 2)
return;
for (size_t i = blocks.size() - 1; i > 0; i--)
std::swap(blocks[i], blocks[cryptoutils->get_range(i + 1)]);
Function::iterator fi = F.begin();
for (BasicBlock *block : blocks) {
fi++;
block->moveAfter(&*(fi));
}
}
};
} // namespace llvm
FunctionPass *llvm::createIndirectBranchPass(bool flag) {
return new IndirectBranch(flag);
}
char IndirectBranch::ID = 0;
INITIALIZE_PASS(IndirectBranch, "indibran", "IndirectBranching", false, false)