-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathplugin.cpp
More file actions
182 lines (155 loc) · 5.6 KB
/
plugin.cpp
File metadata and controls
182 lines (155 loc) · 5.6 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
/*
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 <elias.bachaalany@gmail.com>
*/
#include "idasdk.h"
#include <idacpp/callbacks/callbacks.hpp>
#include "cli_utils.h"
#include "macro_editor.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
using namespace idacpp::callbacks;
//-------------------------------------------------------------------------
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);
// Hook pre-existing CLIs (like Python) that were loaded before our plugin
hook_preexisting_clis();
// Pin ourselves in memory to prevent crashes from dangling CLI callback pointers
#ifdef _WIN32
LoadLibraryA("climacros.dll");
#elif defined(__APPLE__)
dlopen("climacros.dylib", RTLD_NOLOAD | RTLD_LAZY);
#else
dlopen("climacros.so", RTLD_NOLOAD | RTLD_LAZY);
#endif
}
void hook_preexisting_clis()
{
// Tabular approach: define CLI finders to try
struct cli_finder_t
{
const char* name;
cli_t* (*finder)();
};
static const cli_finder_t cli_finders[] =
{
{"Python", find_python_cli},
{"IDC", find_idc_cli}
};
// Try to find and hook each CLI
for (const auto& finder : cli_finders)
{
cli_t* cli = finder.finder();
if (cli != nullptr)
{
// Hook the CLI
auto new_cli = hook_cli(cli);
if (new_cli != nullptr)
{
// Uninstall previous CLI and replace with new hooked CLI
request_install_cli(cli, false);
request_install_cli(new_cli, true);
msg("climacros: successfully hooked pre-existing CLI '%s'\n", cli->sname);
}
}
}
}
bool idaapi run(size_t) override
{
macro_editor.choose();
return true;
}
ssize_t idaapi on_event(ssize_t code, va_list va) override
{
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));
if (install)
{
// Create a copy of the CLI with our execute_line hook
auto new_cli = hook_cli(cli);
// Remove the old CLI and install the new one
request_install_cli(cli, false);
request_install_cli(new_cli, true);
msg("climacros: hooked CLI '%s'\n", cli->sname);
}
else
{
// Find the new CLI using the prevously registered CLI
// Note: from the original plugin perspective, the old CLI was never uninstalled
auto new_cli = unhook_cli(cli);
// Remove the new CLI (which we installed when we uninstalled the old CLI)
request_install_cli(new_cli, false);
msg("climacros: unhooked CLI '%s'\n", cli->sname);
}
}
}
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/allthingsida/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
};