-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStringEncryptionPass.cpp
More file actions
434 lines (390 loc) · 15.3 KB
/
Copy pathStringEncryptionPass.cpp
File metadata and controls
434 lines (390 loc) · 15.3 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include "StringEncryptionPass.h"
#include "ChakravyuhaReport.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include <algorithm>
#include <numeric>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace llvm;
// Define the different encryption schemes.
enum class EncryptionScheme {
XOR_WITH_INDEX = 0,
ADD_WITH_INDEX = 1,
SUB_FROM_CONSTANT = 2,
SBOX = 3,
};
const int NUM_ENCRYPTION_SCHEMES = 4;
const unsigned int XOR_KEY_LENGTH = 16;
const unsigned int SBOX_SIZE = 256;
// Helper to generate a random S-Box and its inverse for decryption.
void generateSBox(std::vector<uint8_t> &sbox, std::vector<uint8_t> &invSbox,
std::mt19937 &gen) {
sbox.resize(SBOX_SIZE);
invSbox.resize(SBOX_SIZE);
std::iota(sbox.begin(), sbox.end(), 0);
std::shuffle(sbox.begin(), sbox.end(), gen);
for (unsigned i = 0; i < SBOX_SIZE; ++i) {
invSbox[sbox[i]] = i;
}
}
static std::vector<char> encryptStringWithXOR(StringRef S,
const std::vector<uint8_t> &key) {
std::vector<char> Encrypted(S.begin(), S.end());
for (size_t i = 0; i < Encrypted.size(); ++i) {
Encrypted[i] ^= key[i % XOR_KEY_LENGTH];
}
return Encrypted;
}
// An encryption function for the ADD_WITH_INDEX scheme.
static std::vector<char> encryptStringWithAdd(StringRef S,
const std::vector<uint8_t> &key) {
std::vector<char> Encrypted(S.begin(), S.end());
for (size_t i = 0; i < Encrypted.size(); ++i) {
Encrypted[i] += key[i % XOR_KEY_LENGTH];
}
return Encrypted;
}
static std::vector<char>
encryptStringWithSBox(StringRef S, const std::vector<uint8_t> &sbox) {
std::vector<char> Encrypted;
Encrypted.reserve(S.size());
for (unsigned char C : S) {
Encrypted.push_back(sbox[C]);
}
return Encrypted;
}
// Injects a polymorphic decryption stub for a chosen cipher scheme.
static Function *
injectCipherStub(Module &M, EncryptionScheme scheme, int cloneId,
const std::vector<uint8_t> &obfuscatedKey = {},
GlobalVariable *invSboxGV = nullptr) {
LLVMContext &Ctx = M.getContext();
Type *Int8PtrTy = PointerType::get(Ctx, 0);
Type *Int32Ty = Type::getInt32Ty(Ctx);
FunctionType *DecryptFTy =
FunctionType::get(Type::getVoidTy(Ctx), {Int8PtrTy, Int32Ty}, false);
std::string funcName = "chakravyuha_decrypt_" + std::to_string(cloneId);
Function *DecryptF = M.getFunction(funcName);
if (DecryptF)
return DecryptF;
DecryptF =
Function::Create(DecryptFTy, GlobalValue::PrivateLinkage, funcName, M);
DecryptF->addFnAttr(Attribute::NoInline);
DecryptF->addFnAttr(Attribute::OptimizeNone);
DecryptF->setCallingConv(CallingConv::C);
auto ArgIt = DecryptF->arg_begin();
Argument *EncryptedStringPtr = &*ArgIt++;
Argument *Length = &*ArgIt++;
BasicBlock *EntryBB = BasicBlock::Create(Ctx, "entry", DecryptF);
IRBuilder<> Builder(EntryBB);
BasicBlock *LoopHeader = BasicBlock::Create(Ctx, "loop_header", DecryptF);
BasicBlock *LoopBody = BasicBlock::Create(Ctx, "loop_body", DecryptF);
BasicBlock *LoopExit = BasicBlock::Create(Ctx, "loop_exit", DecryptF);
Value *DeobfuscatedKeyAlloca = nullptr;
if (scheme != EncryptionScheme::SBOX) {
ArrayType *KeyArrayTy =
ArrayType::get(Type::getInt8Ty(Ctx), XOR_KEY_LENGTH);
DeobfuscatedKeyAlloca =
Builder.CreateAlloca(KeyArrayTy, nullptr, "dec_key");
for (unsigned i = 0; i < XOR_KEY_LENGTH; ++i) {
Value *ObfuscatedByte = Builder.getInt8(obfuscatedKey[i]);
Value *DeobfuscatedByte;
switch (scheme) {
case EncryptionScheme::XOR_WITH_INDEX:
DeobfuscatedByte =
Builder.CreateXor(ObfuscatedByte, Builder.getInt8(i));
break;
case EncryptionScheme::ADD_WITH_INDEX:
DeobfuscatedByte =
Builder.CreateSub(ObfuscatedByte, Builder.getInt8(i));
break;
case EncryptionScheme::SUB_FROM_CONSTANT:
DeobfuscatedByte =
Builder.CreateSub(Builder.getInt8(0xFF), ObfuscatedByte);
break;
default:
llvm_unreachable("Invalid scheme for key-based crypto");
}
Value *DestGEP =
Builder.CreateInBoundsGEP(KeyArrayTy, DeobfuscatedKeyAlloca,
{Builder.getInt32(0), Builder.getInt32(i)});
Builder.CreateStore(DeobfuscatedByte, DestGEP);
}
}
Builder.CreateBr(LoopHeader);
Builder.SetInsertPoint(LoopHeader);
PHINode *IndexPhi = Builder.CreatePHI(Int32Ty, 2, "index");
IndexPhi->addIncoming(Builder.getInt32(0), EntryBB);
Value *LoopCond = Builder.CreateICmpSLT(IndexPhi, Length, "loop_cond");
Builder.CreateCondBr(LoopCond, LoopBody, LoopExit);
Builder.SetInsertPoint(LoopBody);
Value *SrcCharPtr = Builder.CreateGEP(
Type::getInt8Ty(Ctx), EncryptedStringPtr, IndexPhi, "src_char_ptr");
if (scheme == EncryptionScheme::SBOX) {
Value *EncryptedByte = Builder.CreateLoad(Type::getInt8Ty(Ctx), SrcCharPtr);
Value *Index = Builder.CreateZExt(EncryptedByte, Type::getInt64Ty(Ctx));
Value *SboxLookupPtr = Builder.CreateInBoundsGEP(
invSboxGV->getValueType(), invSboxGV, {Builder.getInt64(0), Index});
Value *DecryptedByte =
Builder.CreateLoad(Type::getInt8Ty(Ctx), SboxLookupPtr);
Builder.CreateStore(DecryptedByte, SrcCharPtr);
} else {
Value *KeyIndex =
Builder.CreateURem(IndexPhi, Builder.getInt32(XOR_KEY_LENGTH));
Value *KeyByteGEP = Builder.CreateInBoundsGEP(
cast<AllocaInst>(DeobfuscatedKeyAlloca)->getAllocatedType(),
DeobfuscatedKeyAlloca, {Builder.getInt32(0), KeyIndex});
Value *KeyByte = Builder.CreateLoad(Type::getInt8Ty(Ctx), KeyByteGEP);
Value *LoadedByte = Builder.CreateLoad(Type::getInt8Ty(Ctx), SrcCharPtr);
Value *DecryptedByte;
switch (scheme) {
case EncryptionScheme::ADD_WITH_INDEX:
DecryptedByte = Builder.CreateSub(LoadedByte, KeyByte);
break;
case EncryptionScheme::XOR_WITH_INDEX:
case EncryptionScheme::SUB_FROM_CONSTANT:
DecryptedByte = Builder.CreateXor(LoadedByte, KeyByte);
break;
default:
llvm_unreachable("Invalid key-based scheme in decryption loop");
}
Builder.CreateStore(DecryptedByte, SrcCharPtr);
}
Value *NextIndex =
Builder.CreateAdd(IndexPhi, Builder.getInt32(1), "next_index");
IndexPhi->addIncoming(NextIndex, LoopBody);
Builder.CreateBr(LoopHeader);
Builder.SetInsertPoint(LoopExit);
Builder.CreateRetVoid();
return DecryptF;
}
// Creates the dispatch functions for the self-modifying check.
static Function *createDispatchFunctions(Module &M, GlobalVariable *EncryptedGV,
Function *DecryptFunc,
GlobalVariable *DispatchPtrGV,
int stringId) {
LLVMContext &Ctx = M.getContext();
Type *Int8PtrTy = PointerType::get(Ctx, 0);
FunctionType *DispatchFTy = FunctionType::get(Int8PtrTy, false);
Function *JustDispatchF =
Function::Create(DispatchFTy, GlobalValue::PrivateLinkage,
"dispatch_fast_" + std::to_string(stringId), M);
BasicBlock *JDEntry = BasicBlock::Create(Ctx, "entry", JustDispatchF);
IRBuilder<> JDBuilder(JDEntry);
Value *JD_GEP = JDBuilder.CreateInBoundsGEP(
EncryptedGV->getValueType(), EncryptedGV,
{JDBuilder.getInt32(0), JDBuilder.getInt32(0)});
JDBuilder.CreateRet(JD_GEP);
Function *DecryptAndDispatchF =
Function::Create(DispatchFTy, GlobalValue::PrivateLinkage,
"dispatch_slow_" + std::to_string(stringId), M);
BasicBlock *DADEntry = BasicBlock::Create(Ctx, "entry", DecryptAndDispatchF);
IRBuilder<> DADBuilder(DADEntry);
uint64_t stringSize =
cast<ArrayType>(EncryptedGV->getValueType())->getNumElements();
Value *DAD_GEP = DADBuilder.CreateInBoundsGEP(
EncryptedGV->getValueType(), EncryptedGV,
{DADBuilder.getInt32(0), DADBuilder.getInt32(0)});
DADBuilder.CreateCall(DecryptFunc,
{DAD_GEP, DADBuilder.getInt32(stringSize)});
DADBuilder.CreateStore(JustDispatchF, DispatchPtrGV)
->setOrdering(AtomicOrdering::Monotonic);
DADBuilder.CreateRet(DAD_GEP);
return DecryptAndDispatchF;
}
PreservedAnalyses StringEncryptionPass::run(Module &M,
ModuleAnalysisManager &) {
bool Changed = false;
auto &R = chakravyuha::ReportData::get();
R.enableStringEncryption = true;
R.passesRun.push_back("StringEncrypt");
std::vector<GlobalVariable *> StringGlobalsToEncrypt;
for (GlobalVariable &GV : M.globals()) {
if (!GV.isConstant() || !GV.hasInitializer())
continue;
if (auto *CDA = dyn_cast<ConstantDataArray>(GV.getInitializer())) {
if (CDA->isString()) {
StringGlobalsToEncrypt.push_back(&GV);
}
}
}
if (StringGlobalsToEncrypt.empty())
return PreservedAnalyses::all();
std::set<Function *> unsafeFunctions;
for (Function &F : M) {
for (Instruction &I : instructions(F)) {
if (auto *CI = dyn_cast<CallInst>(&I)) {
if (CI->isInlineAsm()) {
unsafeFunctions.insert(&F);
break;
}
Function *CalledFunc = CI->getCalledFunction();
if (CalledFunc && (CalledFunc->getName() == "setjmp" ||
CalledFunc->getName() == "_setjmp" ||
CalledFunc->getName() == "longjmp")) {
unsafeFunctions.insert(&F);
break;
}
}
}
}
size_t lastSize;
do {
lastSize = unsafeFunctions.size();
for (Function &F : M) {
if (unsafeFunctions.count(&F))
continue;
for (Instruction &I : instructions(F)) {
if (auto *CI = dyn_cast<CallInst>(&I)) {
Function *CalledFunc = CI->getCalledFunction();
if (CalledFunc && unsafeFunctions.count(CalledFunc)) {
unsafeFunctions.insert(&F);
break;
}
}
}
}
} while (unsafeFunctions.size() > lastSize);
LLVMContext &Ctx = M.getContext();
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distrib(0, 255);
int stringId = 0;
for (GlobalVariable *GV : StringGlobalsToEncrypt) {
StringRef OriginalString =
cast<ConstantDataArray>(GV->getInitializer())->getAsString();
if (OriginalString.empty())
continue;
bool usedInUnsafe = false;
for (const Use &U : GV->uses()) {
if (const auto *I = dyn_cast<Instruction>(U.getUser())) {
if (unsafeFunctions.count(const_cast<Function *>(I->getFunction()))) {
usedInUnsafe = true;
break;
}
}
}
if (usedInUnsafe)
continue;
R.stringsEncrypted++;
R.originalIRStringDataSize += OriginalString.size();
int schemeIdx = distrib(gen) % NUM_ENCRYPTION_SCHEMES;
EncryptionScheme scheme = static_cast<EncryptionScheme>(schemeIdx);
std::vector<char> EncryptedBytes;
Function *chosenDecryptFunc;
if (scheme == EncryptionScheme::SBOX) {
std::vector<uint8_t> sbox, invSbox;
generateSBox(sbox, invSbox, gen);
EncryptedBytes = encryptStringWithSBox(OriginalString, sbox);
ArrayType *SboxArrayTy = ArrayType::get(Type::getInt8Ty(Ctx), SBOX_SIZE);
Constant *InvSboxConst = ConstantDataArray::get(Ctx, invSbox);
GlobalVariable *InvSboxGV = new GlobalVariable(
M, SboxArrayTy, true, GlobalValue::PrivateLinkage, InvSboxConst,
"inv_sbox_" + std::to_string(stringId));
chosenDecryptFunc = injectCipherStub(M, scheme, stringId, {}, InvSboxGV);
} else {
std::vector<uint8_t> moduleKey;
for (unsigned j = 0; j < XOR_KEY_LENGTH; ++j) {
moduleKey.push_back((uint8_t)distrib(gen));
}
std::vector<uint8_t> obfuscatedModuleKey;
for (unsigned j = 0; j < XOR_KEY_LENGTH; ++j) {
uint8_t obfuscatedByte;
switch (scheme) {
case EncryptionScheme::XOR_WITH_INDEX:
obfuscatedByte = moduleKey[j] ^ j;
break;
case EncryptionScheme::ADD_WITH_INDEX:
obfuscatedByte = moduleKey[j] + j;
break;
case EncryptionScheme::SUB_FROM_CONSTANT:
obfuscatedByte = 0xFF - moduleKey[j];
break;
default:
llvm_unreachable("Invalid arithmetic scheme");
}
obfuscatedModuleKey.push_back(obfuscatedByte);
}
// Call the correct encryption function based on the chosen scheme.
switch (scheme) {
case EncryptionScheme::ADD_WITH_INDEX:
EncryptedBytes = encryptStringWithAdd(OriginalString, moduleKey);
break;
case EncryptionScheme::XOR_WITH_INDEX:
case EncryptionScheme::SUB_FROM_CONSTANT:
EncryptedBytes = encryptStringWithXOR(OriginalString, moduleKey);
break;
default:
llvm_unreachable("Invalid key-based scheme for encryption");
}
chosenDecryptFunc =
injectCipherStub(M, scheme, stringId, obfuscatedModuleKey);
}
R.obfuscatedIRStringDataSize += EncryptedBytes.size();
ArrayType *ArrTy =
ArrayType::get(Type::getInt8Ty(Ctx), EncryptedBytes.size());
Constant *EncryptedConst =
ConstantDataArray::get(Ctx, ArrayRef<char>(EncryptedBytes));
GlobalVariable *EncryptedGV =
new GlobalVariable(M, ArrTy, false, GV->getLinkage(), EncryptedConst,
GV->getName() + ".enc");
EncryptedGV->setAlignment(GV->getAlign());
Type *Int8PtrTy = PointerType::get(Ctx, 0);
FunctionType *DispatchFTy = FunctionType::get(Int8PtrTy, false);
//
// THIS IS THE FINAL FIX USING THE OPAQUE POINTER API
// The type of a function pointer is now just a generic "ptr".
//
Type *DispatchPtrTy = PointerType::get(Ctx, 0);
//
//
//
std::string ptrName = "dispatch_ptr_" + std::to_string(stringId);
GlobalVariable *DispatchPtrGV = new GlobalVariable(
M, DispatchPtrTy, false, GlobalValue::PrivateLinkage, nullptr, ptrName);
Function *InitialDispatchFunc = createDispatchFunctions(
M, EncryptedGV, chosenDecryptFunc, DispatchPtrGV, stringId);
// Since types are opaque, we must cast the initializer to the generic ptr
// type.
Constant *Initializer =
ConstantExpr::getBitCast(InitialDispatchFunc, DispatchPtrTy);
DispatchPtrGV->setInitializer(Initializer);
std::vector<Use *> uses;
for (auto &U : GV->uses()) {
uses.push_back(&U);
}
for (Use *U : uses) {
Instruction *UserInst = dyn_cast<Instruction>(U->getUser());
if (!UserInst)
continue;
IRBuilder<> Builder(UserInst);
Value *LoadedFuncPtr = Builder.CreateLoad(DispatchPtrTy, DispatchPtrGV);
// The CreateCall instruction is smart enough to handle a generic pointer
// as long as we provide the correct FunctionType.
Value *DecryptedStringPtr =
Builder.CreateCall(DispatchFTy, LoadedFuncPtr, {});
Value *CastedPtr =
Builder.CreateBitCast(DecryptedStringPtr, U->get()->getType());
U->set(CastedPtr);
}
if (GV->use_empty()) {
GV->eraseFromParent();
Changed = true;
}
stringId++;
}
R.stringMethod = "Fully Polymorphic On-Demand Decryption via Self-Modifying "
"Pointers and Data-in-Code Stubs";
if (Changed)
return PreservedAnalyses::none();
return PreservedAnalyses::all();
}