forked from 61bcdefg/Hikari-LLVM15-Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Obfuscation.cpp
229 lines (220 loc) · 8.2 KB
/
Obfuscation.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
// For open-source license, please refer to
// [License](https://github.com/HikariObfuscator/Hikari/wiki/License).
//===----------------------------------------------------------------------===//
/*
Hikari 's own "Pass Scheduler".
Because currently there is no way to add dependency to transform passes
Ref : http://lists.llvm.org/pipermail/llvm-dev/2011-February/038109.html
*/
#include "llvm/Transforms/Obfuscation/Obfuscation.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Transforms/Obfuscation/Utils.h"
using namespace llvm;
// Begin Obfuscator Options
static cl::opt<uint64_t> AesSeed("aesSeed", cl::init(0x1337),
cl::desc("seed for the PRNG"));
static cl::opt<bool> EnableAntiClassDump("enable-acdobf", cl::init(false),
cl::NotHidden,
cl::desc("Enable AntiClassDump."));
static cl::opt<bool> EnableAntiHooking("enable-antihook", cl::init(false),
cl::NotHidden,
cl::desc("Enable AntiHooking."));
static cl::opt<bool> EnableAntiDebugging("enable-adb", cl::init(false),
cl::NotHidden,
cl::desc("Enable AntiDebugging."));
static cl::opt<bool>
EnableBogusControlFlow("enable-bcfobf", cl::init(false), cl::NotHidden,
cl::desc("Enable BogusControlFlow."));
static cl::opt<bool> EnableFlattening("enable-cffobf", cl::init(false),
cl::NotHidden,
cl::desc("Enable Flattening."));
static cl::opt<bool>
EnableBasicBlockSplit("enable-splitobf", cl::init(false), cl::NotHidden,
cl::desc("Enable BasicBlockSpliting."));
static cl::opt<bool>
EnableSubstitution("enable-subobf", cl::init(false), cl::NotHidden,
cl::desc("Enable Instruction Substitution."));
static cl::opt<bool> EnableAllObfuscation("enable-allobf", cl::init(false),
cl::NotHidden,
cl::desc("Enable All Obfuscation."));
static cl::opt<bool> EnableFunctionCallObfuscate(
"enable-fco", cl::init(false), cl::NotHidden,
cl::desc("Enable Function CallSite Obfuscation."));
static cl::opt<bool>
EnableStringEncryption("enable-strcry", cl::init(false), cl::NotHidden,
cl::desc("Enable String Encryption."));
static cl::opt<bool>
EnableConstantEncryption("enable-constenc", cl::init(false), cl::NotHidden,
cl::desc("Enable Constant Encryption."));
static cl::opt<bool>
EnableIndirectBranching("enable-indibran", cl::init(false), cl::NotHidden,
cl::desc("Enable Indirect Branching."));
static cl::opt<bool>
EnableFunctionWrapper("enable-funcwra", cl::init(false), cl::NotHidden,
cl::desc("Enable Function Wrapper."));
// End Obfuscator Options
static void LoadEnv(void) {
if (getenv("SPLITOBF")) {
EnableBasicBlockSplit = true;
}
if (getenv("SUBOBF")) {
EnableSubstitution = true;
}
if (getenv("ALLOBF")) {
EnableAllObfuscation = true;
}
if (getenv("FCO")) {
EnableFunctionCallObfuscate = true;
}
if (getenv("STRCRY")) {
EnableStringEncryption = true;
}
if (getenv("INDIBRAN")) {
EnableIndirectBranching = true;
}
if (getenv("FUNCWRA")) {
EnableFunctionWrapper = true; // Broken
}
if (getenv("BCFOBF")) {
EnableBogusControlFlow = true;
}
if (getenv("ACDOBF")) {
EnableAntiClassDump = true;
}
if (getenv("CFFOBF")) {
EnableFlattening = true;
}
if (getenv("CONSTENC")) {
EnableConstantEncryption = true;
}
if (getenv("ANTIHOOK")) {
EnableAntiHooking = true;
}
if (getenv("ADB")) {
EnableAntiDebugging = true;
}
}
namespace llvm {
struct Obfuscation : public ModulePass {
static char ID;
Obfuscation() : ModulePass(ID) {
initializeObfuscationPass(*PassRegistry::getPassRegistry());
}
StringRef getPassName() const override {
return "HikariObfuscationScheduler";
}
bool runOnModule(Module &M) override {
TimerGroup *tg =
new TimerGroup("Obfuscation Timer Group", "Obfuscation Timer Group");
Timer *timer = new Timer("Obfuscation Timer", "Obfuscation Timer", *tg);
timer->startTimer();
errs() << "Running Hikari On " << M.getSourceFileName() << "\n";
annotation2Metadata(M);
ModulePass *MP = createAntiHookPass(EnableAntiHooking);
MP->doInitialization(M);
MP->runOnModule(M);
delete MP;
// Initial ACD Pass
if (EnableAllObfuscation || EnableAntiClassDump) {
ModulePass *P = createAntiClassDumpPass();
P->doInitialization(M);
P->runOnModule(M);
delete P;
}
// Now do FCO
FunctionPass *FP = createFunctionCallObfuscatePass(
EnableAllObfuscation || EnableFunctionCallObfuscate);
for (Function &F : M)
if (!F.isDeclaration())
FP->runOnFunction(F);
delete FP;
MP = createAntiDebuggingPass(EnableAntiDebugging);
MP->runOnModule(M);
delete MP;
// Now Encrypt Strings
MP = createStringEncryptionPass(EnableAllObfuscation ||
EnableStringEncryption);
MP->runOnModule(M);
delete MP;
// Now perform Function-Level Obfuscation
for (Function &F : M)
if (!F.isDeclaration()) {
FunctionPass *P = nullptr;
P = createSplitBasicBlockPass(EnableAllObfuscation ||
EnableBasicBlockSplit);
P->runOnFunction(F);
delete P;
P = createBogusControlFlowPass(EnableAllObfuscation ||
EnableBogusControlFlow);
P->runOnFunction(F);
delete P;
P = createFlatteningPass(EnableAllObfuscation || EnableFlattening);
P->runOnFunction(F);
delete P;
P = createSubstitutionPass(EnableAllObfuscation || EnableSubstitution);
P->runOnFunction(F);
delete P;
}
MP = createConstantEncryptionPass(EnableConstantEncryption);
MP->runOnModule(M);
delete MP;
errs() << "Doing Post-Run Cleanup\n";
FunctionPass *P = createIndirectBranchPass(EnableAllObfuscation ||
EnableIndirectBranching);
for (Function &F : M)
if (!F.isDeclaration())
P->runOnFunction(F);
delete P;
MP = createFunctionWrapperPass(EnableAllObfuscation ||
EnableFunctionWrapper);
MP->runOnModule(M);
delete MP;
// Cleanup Flags
SmallVector<Function *, 4> toDelete;
for (Function &F : M)
if (F.isDeclaration() && F.hasName() && F.getName().contains("hikari_")) {
for (User *U : F.users())
if (Instruction *Inst = dyn_cast<Instruction>(U))
Inst->eraseFromParent();
toDelete.emplace_back(&F);
}
for (Function *F : toDelete)
F->eraseFromParent();
timer->stopTimer();
errs() << "Hikari Out\n";
errs() << "Spend Time: "
<< format("%.7f", timer->getTotalTime().getWallTime()) << "s"
<< "\n";
tg->clearAll();
return true;
} // End runOnModule
};
ModulePass *createObfuscationLegacyPass() {
LoadEnv();
if (AesSeed != 0x1337) {
cryptoutils->prng_seed(AesSeed);
} else {
cryptoutils->prng_seed();
}
errs() << "Initializing Hikari Core with Revision ID:" << GIT_COMMIT_HASH
<< "\n";
return new Obfuscation();
}
PreservedAnalyses ObfuscationPass::run(Module &M, ModuleAnalysisManager &MAM) {
if (createObfuscationLegacyPass()->runOnModule(M)) {
return PreservedAnalyses::none();
}
return PreservedAnalyses::all();
}
} // namespace llvm
char Obfuscation::ID = 0;
INITIALIZE_PASS_BEGIN(Obfuscation, "obfus", "Enable Obfuscation", false, false)
INITIALIZE_PASS_DEPENDENCY(AntiClassDump);
INITIALIZE_PASS_DEPENDENCY(BogusControlFlow);
INITIALIZE_PASS_DEPENDENCY(Flattening);
INITIALIZE_PASS_DEPENDENCY(FunctionCallObfuscate);
INITIALIZE_PASS_DEPENDENCY(IndirectBranch);
INITIALIZE_PASS_DEPENDENCY(SplitBasicBlock);
INITIALIZE_PASS_DEPENDENCY(StringEncryption);
INITIALIZE_PASS_DEPENDENCY(Substitution);
INITIALIZE_PASS_END(Obfuscation, "obfus", "Enable Obfuscation", false, false)