-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
225 lines (181 loc) · 7.43 KB
/
main.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
#include "BuildInfo.h"
#include "binaryninjaapi.h"
#include "mediumlevelilinstruction.h"
#include <algorithm>
#include <map>
#include <chrono>
#include <thread>
using namespace BinaryNinja;
constexpr auto PluginLoggerName = "Plugin.De-PAC";
extern "C" {
BN_DECLARE_CORE_ABI_VERSION;
std::set<std::string> pac_instructions;
void DePacMLIL(Ref<AnalysisContext> analysisContext)
{
const auto log = BinaryNinja::LogRegistry::GetLogger(PluginLoggerName);
Ref<Function> func = analysisContext->GetFunction();
if (!func) {
log->LogError("Could not get function object.");
}
if(func->IsAnalysisSkipped()) {
const std::map<BNAnalysisSkipReason,const char*> skip_reasons {
{ NoSkipReason, "no reason" },
{ AlwaysSkipReason, "always skipped" },
{ ExceedFunctionSizeSkipReason, "exceeds 'analysis.limits.maxFunctionSize'" },
{ ExceedFunctionAnalysisTimeSkipReason, "exceeds 'analysis.limits.maxFunctionAnalysisTime'" },
{ NewAutoFunctionAnalysisSuppressedReason, "Auto Function Analysis Suppression is enabled (analysis.suppressNewAutoFunctionAnalysis)" },
{ BasicAnalysisSkipReason, "basic analysis skipped" },
{ IntermediateAnalysisSkipReason, "intermediate analysis skipped" },
};
auto it = skip_reasons.find(func->GetAnalysisSkipReason());
const char* reason = it == skip_reasons.end() ? "invalid reason" : it->second;
log->LogWarn("Analysis was skipped for function 0x%llx: %s", func->GetStart(), reason);
return;
}
Ref<MediumLevelILFunction> mlil = analysisContext->GetMediumLevelILFunction();
if (!mlil) {
log->LogError("Could not get mlil function.");
return;
}
Ref<Function> function = mlil->GetFunction();
if (!function) {
log->LogError("Could not get core function for mlil function at 0x%llx", mlil->GetCurrentAddress());
return;
}
Ref<BinaryView> bv = analysisContext->GetFunction()->GetView();
if (!bv) {
log->LogError("Could not get binary view for mlil function at 0x%llx", mlil->GetCurrentAddress());
return;
}
Ref<Architecture> arch = mlil->GetArchitecture();
if (!arch) {
log->LogError("Could not get arch for mlil function at 0x%llx", mlil->GetCurrentAddress());
return;
}
bool updated = false;
// Loop over each instruction.
for (auto& bb : mlil->GetBasicBlocks()) {
for (size_t instrIndex = bb->GetStart(); instrIndex < bb->GetEnd(); instrIndex++) {
auto insn = mlil->GetInstruction(instrIndex);
if (insn.operation != MLIL_INTRINSIC)
continue;
std::string intrinsic = arch->GetIntrinsicName(insn.GetIntrinsic());
if (pac_instructions.find(intrinsic) == pac_instructions.end())
continue;
auto outputs = insn.GetOutputVariables();
if (outputs.size() < 1) {
log->LogError("0x%llx: Intrinsic %s did not have enough outputs", insn.address, intrinsic.c_str());
continue;
}
Variable dest = outputs[0];
auto params = insn.GetParameterExprs();
if (params.size() < 1) {
log->LogError("0x%llx: Intrinsic %s did not have enough params", insn.address, intrinsic.c_str());
continue;
}
auto src = params[0];
log->LogDebug("0x%llx: Replacing intrinsic %s", insn.address, intrinsic.c_str());
insn.Replace(mlil->SetVar(8, dest, src.CopyTo(mlil)));
// Apply the type if possible.
try {
if (src.operation == MLIL_VAR) {
auto src_var = src.GetSourceVariable<MLIL_VAR>();
function->CreateAutoVariable(dest, function->GetVariableType(src_var), function->GetVariableName(src_var));
}
}
catch(const std::exception &e) {
log->LogError("0x%llx: Could not propagate type for instruction: %s", insn.address, e.what());
}
updated = true;
}
}
if (updated)
mlil->GenerateSSAForm();
}
bool WorkflowIsRegistered(const std::string& name) {
std::vector<Ref<Workflow>> workflows = Workflow::GetList();
auto it = std::find_if(workflows.begin(), workflows.end(), [name](Ref<Workflow> workflow) {
return workflow->GetName() == name;
});
if (it == workflows.end()) {
return false;
}
return true;
}
void RegisterWorkflow(const std::string& name, const std::string& parent, const std::string& before) {
const auto log = BinaryNinja::LogRegistry::GetLogger(PluginLoggerName);
long long backoff = 10;
while (!WorkflowIsRegistered(parent)) {
std::this_thread::sleep_for(std::chrono::milliseconds(backoff));
backoff = backoff * 2;
if (backoff > 10000) {
log->LogError("Parent workflow %s not found", parent.c_str());
return;
}
}
Ref<Workflow> myWorkflow = Workflow::Instance(parent)->Clone(name);
myWorkflow->RegisterActivity(
new Activity("extension.depac", DePacMLIL));
myWorkflow->Insert(before, "extension.depac");
Workflow::RegisterWorkflow(myWorkflow,
R"#({
"title": "De-PAC Workflow",
"description": "Removes PAC intrinsics",
"capabilities": []
})#");
log->LogInfo("Registered Workflow: %s", name.c_str());
}
BINARYNINJAPLUGIN bool CorePluginInit()
{
BinaryNinja::LogRegistry::CreateLogger(PluginLoggerName);
const auto log = BinaryNinja::LogRegistry::GetLogger(PluginLoggerName);
pac_instructions.insert("__pacia");
pac_instructions.insert("__paciza");
pac_instructions.insert("__pacia1718");
pac_instructions.insert("__paciasp");
pac_instructions.insert("__paciaz");
pac_instructions.insert("__pacda");
pac_instructions.insert("__pacdaz");
pac_instructions.insert("__pacib");
pac_instructions.insert("__pacizb");
pac_instructions.insert("__pacib1718");
pac_instructions.insert("__pacibsp");
pac_instructions.insert("__pacibz");
pac_instructions.insert("__pacdb");
pac_instructions.insert("__pacdbz");
pac_instructions.insert("__autia");
pac_instructions.insert("__autiza");
pac_instructions.insert("__autia1718");
pac_instructions.insert("__autiasp");
pac_instructions.insert("__autiaz");
pac_instructions.insert("__autda");
pac_instructions.insert("__autdza");
pac_instructions.insert("__autib");
pac_instructions.insert("__autizb");
pac_instructions.insert("__autib1718");
pac_instructions.insert("__autibsp");
pac_instructions.insert("__autibz");
pac_instructions.insert("__autdb");
pac_instructions.insert("__autdzb");
pac_instructions.insert("__xpaci");
pac_instructions.insert("__xpacd");
pac_instructions.insert("__xpaclri");
std::thread register_default([]() {
RegisterWorkflow(
"extension.depac.defaultAnalysis",
"core.function.defaultAnalysis",
"core.function.analyzeTailCalls");
});
register_default.detach();
std::thread register_objc([](){
RegisterWorkflow(
"extension.depac.objectiveC",
"core.function.objectiveC",
"core.function.analyzeTailCalls");
});
register_objc.detach();
log->LogInfo("DePac loaded successfully (%s-%s/%s)",
GitBranch, GitCommit, BuildType);
return true;
}
}