-
Notifications
You must be signed in to change notification settings - Fork 13
/
climacros.cpp
433 lines (365 loc) · 12.5 KB
/
climacros.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
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
/*
CLI Macros: a plugin that allows you to define and use macros in IDA's command line interfaces
When a CLI is registered, this plugin augments its functionality so it supports user defined macros. The macros expand to hardcoded strings
or to dynamic expressions evaluated in Python.
To expand Python expressions dynamically, encapsulate the string in ${expression}$.
All expressions should resolve to a string (i.e. have a __str__ magic method).
(c) Elias Bachaalany <[email protected]>
*/
#include <type_traits>
#include <string>
#include <algorithm>
#include <regex>
#include <functional>
#include "idasdk.h"
#include "utils_impl.cpp"
constexpr char IDAREG_CLI_MACROS[] = "CLI_Macros";
constexpr int MAX_CLI_MACROS = 200;
constexpr char SER_SEPARATOR[] = "\x1";
#include "macros.hpp"
//-------------------------------------------------------------------------
// Macro replace and expand via Python expression evaluation
macro_replacer_t macro_replacer(
[](std::string expr)->std::string
{
if (auto py = pylang())
{
qstring errbuf;
idc_value_t rv;
if (py->eval_expr(&rv, BADADDR, expr.c_str(), &errbuf) && rv.vtype == VT_STR)
return rv.qstr().c_str();
}
return std::move(expr);
}
);
//-------------------------------------------------------------------------
// Context structure to allow hooking CLIs
struct cli_ctx_t
{
const cli_t *old_cli;
cli_t new_cli;
};
#define MAX_CTX 18
static cli_ctx_t g_cli_ctx[MAX_CTX] = {};
//-------------------------------------------------------------------------
// Mechanism to create cli->execute_line() callback with user data
#define DEF_HOOK(n) execute_line_with_ctx_##n
#define IMPL_HOOK(n) \
static bool idaapi execute_line_with_ctx_##n(const char *line) \
{ \
std::string repl = macro_replacer(line); \
return g_cli_ctx[n].old_cli->execute_line(repl.c_str()); \
}
IMPL_HOOK(0); IMPL_HOOK(1); IMPL_HOOK(2); IMPL_HOOK(3); IMPL_HOOK(4); IMPL_HOOK(5);
IMPL_HOOK(6); IMPL_HOOK(7); IMPL_HOOK(8); IMPL_HOOK(9); IMPL_HOOK(10); IMPL_HOOK(11);
IMPL_HOOK(12); IMPL_HOOK(13); IMPL_HOOK(14); IMPL_HOOK(15); IMPL_HOOK(16); IMPL_HOOK(17);
static bool (idaapi *g_cli_execute_line_with_ctx[MAX_CTX])(const char *) =
{
DEF_HOOK(0), DEF_HOOK(1), DEF_HOOK(2), DEF_HOOK(3), DEF_HOOK(4), DEF_HOOK(5),
DEF_HOOK(6), DEF_HOOK(7), DEF_HOOK(8), DEF_HOOK(9), DEF_HOOK(10), DEF_HOOK(11),
DEF_HOOK(12), DEF_HOOK(13), DEF_HOOK(14), DEF_HOOK(15), DEF_HOOK(16), DEF_HOOK(17)
};
#undef DEF_HOOK
#undef IMPL_HOOK
// Ignore UI hooks when set
bool g_b_ignore_ui_notification = false;
//-------------------------------------------------------------------------
const cli_t *hook_cli(const cli_t *cli)
{
for (int i=0; i < qnumber(g_cli_ctx); ++i)
{
auto &ctx = g_cli_ctx[i];
if (ctx.old_cli != nullptr)
continue;
ctx.old_cli = cli;
ctx.new_cli = *cli;
ctx.new_cli.execute_line = g_cli_execute_line_with_ctx[i];
return &ctx.new_cli;
}
return nullptr;
}
//-------------------------------------------------------------------------
const cli_t *unhook_cli(const cli_t *cli)
{
for (auto &ctx: g_cli_ctx)
{
if (ctx.old_cli != cli)
continue;
ctx.old_cli = nullptr;
return &ctx.new_cli;
}
return nullptr;
}
//-------------------------------------------------------------------------
// Modal macro editor
struct macro_editor_t: public chooser_t
{
protected:
static const uint32 flags_;
static const int widths_[];
static const char *const header_[];
macros_t m_macros;
static bool edit_macro_def(macro_def_t &def, bool as_new)
{
static const char form_fmt[] =
"%s\n"
"\n"
"%s"
"<~E~xpression :q2:0:60::>\n"
"<~D~escription:q3:0:60::>\n"
"\n";
// A new macro can edit all 3 fields. An existing one cannot change its name.
int r;
qstring form;
form.sprnt(
form_fmt,
as_new ? "New macro" : "Edit macro",
as_new ? "<~M~acro :q1:0:60::>\n" : "");
qstring macro = def.macro.c_str(), expr = def.expr.c_str(), desc = def.desc.c_str();
if (as_new)
r = ask_form(form.c_str(), ¯o, &expr, &desc);
else
r = ask_form(form.c_str(), &expr, &desc);
if (r > 0)
{
if (as_new)
def.macro = macro.c_str();
def.expr = expr.c_str();
def.desc = desc.c_str();
return true;
}
return false;
}
void reg_del_macro(const macro_def_t ¯o)
{
std::string ser;
macro.to_string(ser);
reg_update_strlist(IDAREG_CLI_MACROS, nullptr, MAX_CLI_MACROS, ser.c_str());
}
void reg_save_macro(const macro_def_t ¯o, std::string *ser_out = nullptr)
{
std::string ser;
macro.to_string(ser);
reg_update_strlist(IDAREG_CLI_MACROS, ser.c_str(), MAX_CLI_MACROS);
if (ser_out != nullptr)
*ser_out = std::move(ser);
}
// Add a new macro
macro_def_t *add_macro(macro_def_t macro)
{
auto &new_macro = m_macros.push_back();
new_macro = std::move(macro);
return &new_macro;
}
bool init() override
{
build_macros_list();
return true;
}
size_t idaapi get_count() const override
{
return m_macros.size();
}
void idaapi get_row(
qstrvec_t *cols,
int *icon,
chooser_item_attrs_t *attrs,
size_t n) const override
{
auto ¯o = m_macros[n];
cols->at(0) = macro.macro.c_str();
cols->at(1) = macro.expr.c_str();
cols->at(2) = macro.desc.c_str();
}
// Add a new script
cbret_t idaapi ins(ssize_t n) override
{
macro_def_t new_macro;
while (true)
{
if (!edit_macro_def(new_macro, true))
return {};
auto p = m_macros.find({ new_macro.macro });
if (p == m_macros.end())
break;
warning("A macro with the name '%s' already exists. Please choose another name!", new_macro.macro.c_str());
}
reg_save_macro(*add_macro(std::move(new_macro)));
build_macros_list();
return cbret_t(0, chooser_base_t::ALL_CHANGED);
}
// Remove a script from the list
cbret_t idaapi del(size_t n) override
{
reg_del_macro(m_macros[n]);
build_macros_list();
return adjust_last_item(n);
}
// Edit the macro
cbret_t idaapi edit(size_t n) override
{
// Take a copy of the old macro
auto old_macro = m_macros[n];
// In place edit the macro
auto ¯o = m_macros[n];
if (!edit_macro_def(macro, false))
return cbret_t(n, chooser_base_t::NOTHING_CHANGED);
// Delete the old macro
reg_del_macro(old_macro);
// Re-insert the macro with different fields (same macro name)
reg_save_macro(macro);
build_macros_list();
return cbret_t(n, chooser_base_t::ALL_CHANGED);
}
public:
macro_editor_t(const char *title_);
// Rebuilds the macros list
void build_macros_list()
{
// Read all the serialized macro definitions
qstrvec_t ser_macros;
reg_read_strlist(&ser_macros, IDAREG_CLI_MACROS);
m_macros.qclear();
// Empty macros?
if (ser_macros.empty())
{
// If this is not the first run, then keep the macros list empty
qstring first_run;
first_run.sprnt("%s/firstrun.climacros", get_user_idadir());
if (!qfileexist(first_run.c_str()))
{
// Populate with the default macros (once)
FILE *fp = qfopen(first_run.c_str(), "w"); qfclose(fp);
for (auto ¯o: DEFAULT_MACROS)
{
std::string ser_macro;
reg_save_macro(*add_macro(macro), &ser_macro);
ser_macros.push_back(ser_macro.c_str());
}
}
}
else
{
for (auto &ser_macro: ser_macros)
{
char *macro_str = ser_macro.extract();
char *sptr;
int icol = 0;
macro_def_t macro;
for (auto tok = qstrtok(macro_str, SER_SEPARATOR, &sptr);
tok != nullptr;
tok = qstrtok(nullptr, SER_SEPARATOR, &sptr), ++icol)
{
if (icol == 0) macro.macro = tok;
else if (icol == 1) macro.expr = tok;
else if (icol == 2) macro.desc = tok;
}
add_macro(std::move(macro));
qfree(macro_str);
}
}
// Re-create the pattern replacement
macro_replacer.begin_update();
for (auto &m: m_macros)
macro_replacer.update(m.macro, m.expr);
macro_replacer.end_update();
}
};
const uint32 macro_editor_t::flags_ = CH_MODAL | CH_KEEP | CH_CAN_DEL | CH_CAN_EDIT | CH_CAN_INS | CH_CAN_REFRESH;
const int macro_editor_t::widths_[3] = { 10, 30, 70 };
const char *const macro_editor_t::header_[3] = { "Macro", "Expression", "Description" };
inline macro_editor_t::macro_editor_t(const char *title_ = "CLI macros editor")
: chooser_t(flags_, qnumber(widths_), widths_, header_, title_)
{
}
class climacros_plg_t : public plugmod_t, public event_listener_t
{
macro_editor_t macro_editor;
public:
climacros_plg_t() : plugmod_t()
{
msg("IDA Command Line Interface macros initialized\n");
macro_editor.build_macros_list();
hook_event_listener(HT_UI, this, HKCB_GLOBAL);
}
bool idaapi run(size_t) override
{
macro_editor.choose();
return true;
}
ssize_t idaapi on_event(ssize_t code, va_list va) override
{
//if (code != ui_msg)
//{
// msg("got event: %d\n", int(code));
// return 0;
//}
switch (code)
{
case ui_install_cli:
{
// Only capture CLIs requests not originating internally
if (g_b_ignore_ui_notification)
break;
auto cli = va_arg(va, const cli_t*);
auto install = bool(va_arg(va, int));
auto hooked_cli = install ? hook_cli(cli) : unhook_cli(cli);
if (hooked_cli != nullptr)
{
// [Un]install the replacement CLI
request_install_cli(hooked_cli, install);
// Do not accept this CLI [un]registration
return 1;
}
}
}
// Pass-through...
return 0;
}
~climacros_plg_t()
{
unhook_event_listener(HT_UI, this);
}
};
//--------------------------------------------------------------------------
plugmod_t *idaapi init(void)
{
if (!is_idaq())
return nullptr;
return new climacros_plg_t;
}
#ifdef _DEBUG
static const char wanted_hotkey[] = "Ctrl-Shift-A";
#else
// No hotkey, just run from the Ctrl+3 dialog
static const char wanted_hotkey[] = "";
#endif
//--------------------------------------------------------------------------
static const char comment[] = "Use macros in CLIs";
static const char help[] =
"Define your own macros and use then in the CLIs.\n"
"Comes in handy with the WinDbg or other debuggers' CLIs\n"
"\n"
"climacros is developed by Elias Bachaalany. Please see https://github.com/0xeb/ida-climacros for more information\n"
"\0"
__DATE__ " " __TIME__ "\n"
"\n";
//--------------------------------------------------------------------------
//
// PLUGIN DESCRIPTION BLOCK
//
//--------------------------------------------------------------------------
plugin_t PLUGIN =
{
IDP_INTERFACE_VERSION,
PLUGIN_FIX | PLUGIN_MULTI,
init, // initialize
nullptr, // terminate. this pointer may be NULL.
nullptr, // invoke plugin
comment, // long comment about the plugin
// it could appear in the status line
// or as a hint
help, // multiline help about the plugin
"CLI Macros", // the preferred short name of the plugin
wanted_hotkey // the preferred hotkey to run the plugin
};