This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
StringEncryption.cpp
341 lines (333 loc) · 14.8 KB
/
StringEncryption.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// For open-source license, please refer to [License](https://github.com/HikariObfuscator/Hikari/wiki/License).
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Obfuscation/StringEncryption.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Value.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/Transforms/Obfuscation/CryptoUtils.h"
#include "llvm/Transforms/Obfuscation/Obfuscation.h"
#include "llvm/Transforms/Obfuscation/Utils.h"
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace llvm;
using namespace std;
namespace llvm {
struct StringEncryption : public ModulePass {
static char ID;
map<Function * /*Function*/, GlobalVariable * /*Decryption Status*/>
encstatus;
bool flag;
StringEncryption() : ModulePass(ID) { this->flag = true; }
StringEncryption(bool flag) : ModulePass(ID) { this->flag = flag; }
StringRef getPassName() const override {
return StringRef("StringEncryption");
}
bool runOnModule(Module &M) override {
// in runOnModule. We simple iterate function list and dispatch functions
// to handlers
for (Module::iterator iter = M.begin(); iter != M.end(); iter++) {
Function *F = &(*iter);
if (toObfuscate(flag, F, "strenc")) {
errs() << "Running StringEncryption On " << F->getName() << "\n";
Constant *S = ConstantInt::get(Type::getInt32Ty(M.getContext()), 0);
GlobalVariable *GV = new GlobalVariable(
M, S->getType(), false, GlobalValue::LinkageTypes::PrivateLinkage,
S, "");
encstatus[F] = GV;
HandleFunction(F);
}
}
return true;
} // End runOnModule
void HandleFunction(Function *Func) {
FixFunctionConstantExpr(Func);
set<GlobalVariable *> Globals;
set<User *> Users;
for (BasicBlock &BB : *Func) {
for (Instruction &I : BB) {
for (Value *Op : I.operands()) {
if (GlobalVariable *G = dyn_cast<GlobalVariable>(Op->stripPointerCasts())) {
if(User* U=dyn_cast<User>(Op)){
Users.insert(U);
}
Users.insert(&I);
Globals.insert(G);
}
}
}
}
set<GlobalVariable *> rawStrings;
set<GlobalVariable *> objCStrings;
map<GlobalVariable *, pair<Constant *, GlobalVariable *>> GV2Keys;
map<GlobalVariable * /*old*/, pair<GlobalVariable * /*encrypted*/, GlobalVariable * /*decrypt space*/>> old2new;
for (GlobalVariable *GV : Globals) {
if (GV->hasInitializer() &&
GV->getSection() != StringRef("llvm.metadata") &&
GV->getSection().find(StringRef("__objc")) == string::npos &&
GV->getName().find("OBJC") == string::npos) {
if (GV->getInitializer()->getType() ==
Func->getParent()->getTypeByName("struct.__NSConstantString_tag")) {
objCStrings.insert(GV);
rawStrings.insert(
cast<GlobalVariable>(cast<ConstantStruct>(GV->getInitializer())
->getOperand(2)
->stripPointerCasts()));
} else if (isa<ConstantDataSequential>(GV->getInitializer())) {
rawStrings.insert(GV);
}else if(isa<ConstantArray>(GV->getInitializer())){
ConstantArray* CA=cast<ConstantArray>(GV->getInitializer());
for(unsigned i=0;i<CA->getNumOperands();i++){
Value* op=CA->getOperand(i)->stripPointerCasts();
if(GlobalVariable* GV=dyn_cast<GlobalVariable>(op)){
Globals.insert(GV);
}
}
}
}
}
for (GlobalVariable *GV : rawStrings) {
if (GV->getInitializer()->isZeroValue() ||
GV->getInitializer()->isNullValue()) {
continue;
}
ConstantDataSequential *CDS =
cast<ConstantDataSequential>(GV->getInitializer());
Type *memberType = CDS->getElementType();
// Ignore non-IntegerType
if (!isa<IntegerType>(memberType)) {
continue;
}
IntegerType *intType = cast<IntegerType>(memberType);
Constant *KeyConst = NULL;
Constant *EncryptedConst = NULL;
Constant *DummyConst = NULL;
if (intType == Type::getInt8Ty(GV->getParent()->getContext())) {
vector<uint8_t> keys;
vector<uint8_t> encry;
vector<uint8_t> dummy;
for (unsigned i = 0; i < CDS->getNumElements(); i++) {
uint8_t K = cryptoutils->get_uint8_t();
uint64_t V = CDS->getElementAsInteger(i);
keys.push_back(K);
encry.push_back(K ^ V);
dummy.push_back(rand());
}
KeyConst = ConstantDataArray::get(GV->getParent()->getContext(),
ArrayRef<uint8_t>(keys));
EncryptedConst = ConstantDataArray::get(GV->getParent()->getContext(),
ArrayRef<uint8_t>(encry));
DummyConst = ConstantDataArray::get(GV->getParent()->getContext(),
ArrayRef<uint8_t>(dummy));
} else if (intType == Type::getInt16Ty(GV->getParent()->getContext())) {
vector<uint16_t> keys;
vector<uint16_t> encry;
vector<uint16_t> dummy;
for (unsigned i = 0; i < CDS->getNumElements(); i++) {
uint16_t K = cryptoutils->get_uint16_t();
uint64_t V = CDS->getElementAsInteger(i);
keys.push_back(K);
encry.push_back(K ^ V);
dummy.push_back(rand());
}
KeyConst = ConstantDataArray::get(GV->getParent()->getContext(),
ArrayRef<uint16_t>(keys));
EncryptedConst = ConstantDataArray::get(GV->getParent()->getContext(),
ArrayRef<uint16_t>(encry));
DummyConst = ConstantDataArray::get(GV->getParent()->getContext(),
ArrayRef<uint16_t>(dummy));
} else if (intType == Type::getInt32Ty(GV->getParent()->getContext())) {
vector<uint32_t> keys;
vector<uint32_t> encry;
vector<uint32_t> dummy;
for (unsigned i = 0; i < CDS->getNumElements(); i++) {
uint32_t K = cryptoutils->get_uint32_t();
uint64_t V = CDS->getElementAsInteger(i);
keys.push_back(K);
encry.push_back(K ^ V);
dummy.push_back(rand());
}
KeyConst = ConstantDataArray::get(GV->getParent()->getContext(),
ArrayRef<uint32_t>(keys));
EncryptedConst = ConstantDataArray::get(GV->getParent()->getContext(),
ArrayRef<uint32_t>(encry));
DummyConst = ConstantDataArray::get(GV->getParent()->getContext(),
ArrayRef<uint32_t>(dummy));
} else if (intType == Type::getInt64Ty(GV->getParent()->getContext())) {
vector<uint64_t> keys;
vector<uint64_t> encry;
vector<uint64_t> dummy;
for (unsigned i = 0; i < CDS->getNumElements(); i++) {
uint64_t K = cryptoutils->get_uint64_t();
uint64_t V = CDS->getElementAsInteger(i);
keys.push_back(K);
encry.push_back(K ^ V);
dummy.push_back(rand());
}
KeyConst = ConstantDataArray::get(GV->getParent()->getContext(),
ArrayRef<uint64_t>(keys));
EncryptedConst = ConstantDataArray::get(GV->getParent()->getContext(),
ArrayRef<uint64_t>(encry));
DummyConst = ConstantDataArray::get(GV->getParent()->getContext(),
ArrayRef<uint64_t>(dummy));
} else {
errs() << "Unsupported CDS Type\n";
abort();
}
// Prepare new rawGV
GlobalVariable *EncryptedRawGV = new GlobalVariable(
*(GV->getParent()), EncryptedConst->getType(), false,
GV->getLinkage(), EncryptedConst, "EncryptedString", nullptr,
GV->getThreadLocalMode(), GV->getType()->getAddressSpace());
GlobalVariable *DecryptSpaceGV = new GlobalVariable(
*(GV->getParent()), DummyConst->getType(), false,
GV->getLinkage(), DummyConst, "DecryptSpace", nullptr,
GV->getThreadLocalMode(), GV->getType()->getAddressSpace());
old2new[GV] = make_pair(EncryptedRawGV, DecryptSpaceGV);
GV2Keys[DecryptSpaceGV] = make_pair(KeyConst, EncryptedRawGV);
}
// Now prepare ObjC new GV
for (GlobalVariable *GV : objCStrings) {
ConstantStruct *CS = cast<ConstantStruct>(GV->getInitializer());
GlobalVariable *oldrawString = cast<GlobalVariable>(CS->getOperand(2)->stripPointerCasts());
if (old2new.find(oldrawString) == old2new.end()) { // Filter out zero initializers
continue;
}
GlobalVariable *EncryptedOCGV = ObjectivCString(GV, "EncryptedStringObjC", oldrawString, old2new[oldrawString].first, CS);
GlobalVariable *DecryptSpaceOCGV = ObjectivCString(GV, "DecryptSpaceObjC", oldrawString, old2new[oldrawString].second, CS);
old2new[GV] = make_pair(EncryptedOCGV, DecryptSpaceOCGV);
} // End prepare ObjC new GV
if(old2new.empty() || GV2Keys.empty())
return;
// Replace Uses
for (User *U : Users) {
for (map<GlobalVariable *, pair<GlobalVariable *, GlobalVariable *>>::iterator iter =
old2new.begin();
iter != old2new.end(); ++iter) {
U->replaceUsesOfWith(iter->first, iter->second.second);
iter->first->removeDeadConstantUsers();
}
} // End Replace Uses
// CleanUp Old ObjC GVs
for (GlobalVariable *GV : objCStrings) {
if (GV->getNumUses() == 0) {
GV->dropAllReferences();
old2new.erase(GV);
GV->eraseFromParent();
}
}
// CleanUp Old Raw GVs
for (map<GlobalVariable *, pair<GlobalVariable *, GlobalVariable *>>::iterator iter =
old2new.begin();
iter != old2new.end(); ++iter) {
GlobalVariable *toDelete = iter->first;
toDelete->removeDeadConstantUsers();
if (toDelete->getNumUses() == 0) {
toDelete->dropAllReferences();
toDelete->eraseFromParent();
}
}
GlobalVariable *StatusGV = encstatus[Func];
/*
- Split Original EntryPoint BB into A and C.
- Create new BB as Decryption BB between A and C. Adjust the terminators
into: A (Alloca a new array containing all)
|
B(If not decrypted)
|
C
*/
BasicBlock *A = &(Func->getEntryBlock());
BasicBlock *C = A->splitBasicBlock(A->getFirstNonPHIOrDbgOrLifetime());
C->setName("PrecedingBlock");
BasicBlock *B =
BasicBlock::Create(Func->getContext(), "StringDecryptionBB", Func, C);
// Change A's terminator to jump to B
// We'll add new terminator to jump C later
BranchInst *newBr = BranchInst::Create(B);
ReplaceInstWithInst(A->getTerminator(), newBr);
IRBuilder<> IRB(A->getFirstNonPHIOrDbgOrLifetime());
// Insert DecryptionCode
HandleDecryptionBlock(B, C, GV2Keys);
// Add atomic load checking status in A
LoadInst *LI = IRB.CreateLoad(StatusGV, "LoadEncryptionStatus");
LI->setAtomic(AtomicOrdering::Acquire); // Will be released at the start of
// C
LI->setAlignment(4);
Value *condition = IRB.CreateICmpEQ(
LI, ConstantInt::get(Type::getInt32Ty(Func->getContext()), 0));
A->getTerminator()->eraseFromParent();
BranchInst::Create(B, C, condition, A);
// Add StoreInst atomically in C start
// No matter control flow is coming from A or B, the GVs must be decrypted
IRBuilder<> IRBC(C->getFirstNonPHIOrDbgOrLifetime());
StoreInst *SI = IRBC.CreateStore(
ConstantInt::get(Type::getInt32Ty(Func->getContext()), 1), StatusGV);
SI->setAlignment(4);
SI->setAtomic(AtomicOrdering::Release); // Release the lock acquired in LI
} // End of HandleFunction
GlobalVariable *ObjectivCString(GlobalVariable *GV, string name, GlobalVariable *oldrawString, GlobalVariable *newString, ConstantStruct *CS) {
Value *zero = ConstantInt::get(Type::getInt32Ty(GV->getContext()), 0);
vector<Constant *> vals;
vals.push_back(CS->getOperand(0));
vals.push_back(CS->getOperand(1));
Constant *GEPed = ConstantExpr::getInBoundsGetElementPtr(nullptr, newString, {zero, zero});
if (GEPed->getType() == CS->getOperand(2)->getType()) {
vals.push_back(GEPed);
} else {
Constant *BitCasted = ConstantExpr::getBitCast(newString, CS->getOperand(2)->getType());
vals.push_back(BitCasted);
}
vals.push_back(CS->getOperand(3));
Constant *newCS = ConstantStruct::get(CS->getType(), ArrayRef<Constant *>(vals));
return new GlobalVariable(
*(GV->getParent()), newCS->getType(), false, GV->getLinkage(), newCS,
name.c_str(), nullptr, GV->getThreadLocalMode(),
GV->getType()->getAddressSpace());
}
void HandleDecryptionBlock(BasicBlock *B, BasicBlock *C,
map<GlobalVariable *, pair<Constant *, GlobalVariable *>> &GV2Keys) {
IRBuilder<> IRB(B);
Value *zero = ConstantInt::get(Type::getInt32Ty(B->getContext()), 0);
for (map<GlobalVariable *, pair<Constant *, GlobalVariable *>>::iterator iter = GV2Keys.begin();
iter != GV2Keys.end(); ++iter) {
ConstantDataArray *CastedCDA = cast<ConstantDataArray>(iter->second.first);
// Prevent optimization of encrypted data
appendToCompilerUsed(*iter->second.second->getParent(),
{iter->second.second});
// Element-By-Element XOR so the fucking verifier won't complain
// Also, this hides keys
for (unsigned i = 0; i < CastedCDA->getType()->getNumElements(); i++) {
Value *offset = ConstantInt::get(Type::getInt32Ty(B->getContext()), i);
Value *EncryptedGEP = IRB.CreateGEP(iter->second.second, {zero, offset});
Value *DecryptedGEP = IRB.CreateGEP(iter->first, {zero, offset});
LoadInst *LI = IRB.CreateLoad(EncryptedGEP, "EncryptedChar");
Value *XORed = IRB.CreateXor(LI, CastedCDA->getElementAsConstant(i));
IRB.CreateStore(XORed, DecryptedGEP);
}
}
IRB.CreateBr(C);
} // End of HandleDecryptionBlock
bool doFinalization(Module &M) override {
encstatus.clear();
return false;
}
};
ModulePass *createStringEncryptionPass() { return new StringEncryption(); }
ModulePass *createStringEncryptionPass(bool flag) {
return new StringEncryption(flag);
}
} // namespace llvm
char StringEncryption::ID = 0;
INITIALIZE_PASS(StringEncryption, "strcry", "Enable String Encryption", true,
true)