forked from HexRaysSA/goomba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goomba.cpp
260 lines (229 loc) · 7.41 KB
/
goomba.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
/*
* Copyright (c) 2023 by Hex-Rays, [email protected]
* ALL RIGHTS RESERVED.
*
* gooMBA plugin for Hex-Rays Decompiler.
* It deobfuscates the MBA (mixed boolean arithmetic) epxressions.
*
*/
#include <chrono>
#include "z3++_no_warn.h"
#include "consts.hpp"
#include "optimizer.hpp"
#include "equiv_class.hpp"
#include "file.hpp"
#include <hexrays.hpp>
#include <err.h>
struct plugin_ctx_t;
//--------------------------------------------------------------------------
// returns true if the environment variables indicate the plugin should
// always be enabled (i.e. in testing environments)
inline bool always_on(void)
{
return qgetenv("VD_MBA_AUTO");
}
//--------------------------------------------------------------------------
struct action_handler : public action_handler_t
{
plugin_ctx_t *plugmod;
action_handler(plugin_ctx_t *_plugmod) : plugmod(_plugmod) {}
virtual int idaapi activate(action_activation_ctx_t *ctx) override;
virtual action_state_t idaapi update(action_update_ctx_t *) override
{
return AST_ENABLE;
};
};
//--------------------------------------------------------------------------
//lint -e{958} padding of 7 bytes needed to align member on a 8 byte boundary
struct plugin_ctx_t : public plugmod_t
{
bool run_automatically = false;
qstring oracle_path;
action_handler ah;
optimizer_t optimizer;
bool plugmod_active = false;
plugin_ctx_t();
~plugin_ctx_t() { term_hexrays_plugin(); }
virtual bool idaapi run(size_t) override;
};
//--------------------------------------------------------------------------
static plugmod_t *idaapi init()
{
if ( !init_hexrays_plugin() )
return nullptr; // no decompiler
const char *hxver = get_hexrays_version();
msg("Hex-rays version %s has been detected, %s ready to use\n",
hxver, PLUGIN.wanted_name);
plugin_ctx_t *plugmod = new plugin_ctx_t;
const cfgopt_t cfgopts[] =
{
cfgopt_t("MBA_RUN_AUTOMATICALLY", &plugmod->run_automatically, 1),
cfgopt_t("MBA_Z3_TIMEOUT", &plugmod->optimizer.z3_timeout),
cfgopt_t("MBA_ORACLE_PATH", &plugmod->oracle_path),
cfgopt_t("MBA_Z3_ASSUME_TIMEOUTS_CORRECT", &plugmod->optimizer.z3_assume_timeouts_correct, 1)
};
read_config_file("goomba", cfgopts, qnumber(cfgopts), nullptr);
if ( plugmod->oracle_path.empty() )
qgetenv("VD_MBA_ORACLE_PATH", &plugmod->oracle_path);
if ( !plugmod->oracle_path.empty() )
{
const char *path = plugmod->oracle_path.c_str();
FILE *fin = qfopen(path, "rb");
if ( fin != nullptr )
{
plugmod->optimizer.equiv_classes = new equiv_class_finder_lazy_t(fin);
msg("%s: loaded MBA oracle\n", path);
}
else
{
msg("%s: %s\n", path, qstrerror(-1));
}
}
qstring ifpath;
if ( qgetenv("VD_MSYNTH_PATH", &ifpath) )
{
qstring ofpath = ifpath + ".b";
FILE *fin = qfopen(ifpath.c_str(), "r");
if ( fin == nullptr )
error("%s: failed to open for reading", ifpath.c_str());
FILE *fout = qfopen(ofpath.c_str(), "wb");
if ( fout == nullptr )
error("%s: failed to open for writing", ofpath.c_str());
create_minsns_file(fin, fout);
qfclose(fin);
qfclose(fout);
// do not save the IDB
set_database_flag(DBFL_KILL);
qexit(0);
}
if ( qgetenv("VD_MBA_MINSNS_PATH", &ifpath) )
{
qstring ofpath = ifpath + ".c";
FILE *fin = qfopen(ifpath.c_str(), "rb");
if ( fin == nullptr )
error("%s: failed to open for reading", ifpath.c_str());
FILE *fout = qfopen(ofpath.c_str(), "wb");
if ( fout == nullptr )
error("%s: failed to open for writing", ofpath.c_str());
bool ok = create_oracle_file(fin, fout);
qfclose(fin);
qfclose(fout);
if ( !ok )
error("%s: failed to process", ifpath.c_str());
// do not save the IDB
set_database_flag(DBFL_KILL);
qexit(0);
}
return plugmod;
}
//--------------------------------------------------------------------------
int idaapi action_handler::activate(action_activation_ctx_t *ctx)
{
vdui_t *vu = get_widget_vdui(ctx->widget);
if ( vu != nullptr )
{
plugmod->plugmod_active = true;
vu->refresh_view(true);
return 1;
}
return 0;
}
//--------------------------------------------------------------------------
// This callback handles various hexrays events.
static ssize_t idaapi callback(void *ud, hexrays_event_t event, va_list va)
{
plugin_ctx_t *plugmod = (plugin_ctx_t *) ud;
switch ( event )
{
case hxe_microcode: // microcode has been generated
{
mba_t *mba = va_arg(va, mba_t *);
if ( always_on() || plugmod->run_automatically )
plugmod->plugmod_active = true;
if ( plugmod->plugmod_active )
mba->set_mba_flags2(MBA2_PROP_COMPLEX); // increase acceptable complexity
}
break;
case hxe_populating_popup:
{
TWidget *widget = va_arg(va, TWidget *);
TPopupMenu *popup = va_arg(va, TPopupMenu *);
attach_action_to_popup(widget, popup, ACTION_NAME);
}
break;
case hxe_glbopt:
if ( plugmod->plugmod_active )
{
mba_t *mba = va_arg(va, mba_t *);
struct ida_local insn_optimize_t : public minsn_visitor_t
{
optimizer_t &optimizer;
int cnt = 0;
insn_optimize_t ( optimizer_t &o ) : optimizer(o) {}
int idaapi visit_minsn() override
{
// msg("Optimizing %s\n", curins->dstr());
if ( optimizer.optimize_insn_recurse(curins) )
{
cnt++;
blk->mark_lists_dirty();
mba->dump_mba(true, "vd_mba success %a", curins->ea);
}
return 0;
}
};
insn_optimize_t visitor(plugmod->optimizer);
mba->for_all_topinsns(visitor);
if ( visitor.cnt != 0 )
{
mba->verify(true);
msg("Completed mba optimization pass, improved %d expressions\n", visitor.cnt);
}
plugmod->plugmod_active = false;
mba->clr_mba_flags2(MBA2_PROP_COMPLEX);
return MERR_LOOP; // restart optimization
}
break;
default:
break;
}
return 0;
}
//--------------------------------------------------------------------------
plugin_ctx_t::plugin_ctx_t() : ah(this)
{
install_hexrays_callback(callback, this);
register_action(ACTION_DESC_LITERAL_PLUGMOD(
ACTION_NAME,
"Run gooMBA Optimizer",
&ah,
this,
nullptr,
nullptr,
-1));
}
//--------------------------------------------------------------------------
bool idaapi plugin_ctx_t::run(size_t)
{
return true;
}
//--------------------------------------------------------------------------
static char comment[] = "gooMBA plugin for Hex-Rays decompiler";
//--------------------------------------------------------------------------
//
// PLUGIN DESCRIPTION BLOCK
//
//--------------------------------------------------------------------------
plugin_t PLUGIN =
{
IDP_INTERFACE_VERSION,
PLUGIN_MULTI // The plugin can work with multiple idbs in parallel
| PLUGIN_HIDE, // no menu items in Edit, Plugins
init, // initialize
nullptr,
nullptr,
comment, // long comment about the plugin
nullptr, // multiline help about the plugin
"gooMBA plugin", // the preferred short name of the plugin
nullptr, // the preferred hotkey to run the plugin
};