-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.cpp
More file actions
152 lines (129 loc) · 4.54 KB
/
plugin.cpp
File metadata and controls
152 lines (129 loc) · 4.54 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
// Python Extension Plugin
// By Elias Bachaalany / AllThingsIDA
#include "idasdk.h"
#include "extension.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
class python_ext_plg_t : public plugmod_t, public event_listener_t
{
public:
python_ext_plg_t()
{
hook_event_listener(HT_UI, this);
}
ssize_t idaapi on_event(ssize_t code, va_list va) override
{
if (code == ui_initing_database)
{
// To prevent IDA from unloading this plugin and invalidating our Python references, we must keep ourselves in memory.
#ifdef _WIN32
LoadLibraryA("python_ext.dll");
#elif defined(__APPLE__)
dlopen("python_ext.dylib", RTLD_NOLOAD | RTLD_LAZY);
#else
dlopen("python_ext.so", RTLD_NOLOAD | RTLD_LAZY);
#endif
register_extensions();
// We don't need to be notified no longer
remove_event_listener(this);
}
return 0;
}
bool idaapi run(size_t /*arg*/) override
{
namespace py = pybind11;
py::gil_scoped_acquire acquire;
//
// 1. Call the Python sum function
//
py::object sum = py::eval("sum([1, 2, 3, 4])");
int sum_result = sum.cast<int>();
msg("sum([1, 2, 3, 4])=%d\n", sum_result);
//
// 2. Call `os.getcwd()`
//
py::module_ os = py::module_::import("os");
py::object getcwd = os.attr("getcwd");
std::string cwd = getcwd().cast<std::string>();
msg("os.getcwd()=%s\n", cwd.c_str());
// 3. Call `idaapi.warning(m: str)`
py::module_ idaapi_module = py::module_::import("idaapi");
py::object py_warning = idaapi_module.attr("warning");
py_warning("Hello from Python!\n");
//
// 4. Load my module
//
// Get ALLTHINGSIDA environment variable:
qstring allthingsida_env;
if (!qgetenv("ALLTHINGSIDA", &allthingsida_env))
{
msg("ALLTHINGSIDA environment variable is not set!\n");
return false;
}
allthingsida_env.append(SDIRCHAR "sdk" SDIRCHAR "python_ext");
// Add your module's directory to sys.path
py::module_ sys = py::module_::import("sys");
py::list sys_path = sys.attr("path");
sys_path.append(allthingsida_env.c_str());
do
{
// Import mymodule
py::module_ my_module = py::module_::import("mymodule");
if (!my_module)
{
msg("Failed to import mymodule!\n");
break;
}
//
// Get class object and create an instance
//
py::object MyObjectClass = my_module.attr("MyObject");
if (!MyObjectClass)
{
msg("Failed to import MyObject!\n");
break;
}
py::object obj = MyObjectClass(1, 2); // Create an instance of MyObjectClass
//
// Call the dothis method
//
py::object result = obj.attr("dothis")("example string", 10, 20);
std::string result_str = result.cast<std::string>();
msg("dothis result: %s\n", result_str.c_str());
//
// Call the get_dict method
//
py::dict result_dict = obj.attr("get_dict")().cast<py::dict>();
// Enumerate and print the dictionary
for (auto item : result_dict)
{
std::string key = item.first.cast<std::string>();
std::string value = item.second.cast<std::string>();
msg("key=%s, value=%s\n", key.c_str(), value.c_str());
}
} while (false);
// Remove the module path from sys.path
sys_path.attr("remove")(allthingsida_env.c_str());
return true;
}
~python_ext_plg_t() override
{
unhook_event_listener(HT_UI, this);
}
};
//--------------------------------------------------------------------------
plugin_t PLUGIN =
{
IDP_INTERFACE_VERSION,
PLUGIN_MULTI | PLUGIN_FIX /* | PLUGIN_HIDE*/,// Plugin flags
[]()->plugmod_t* { return new python_ext_plg_t(); },
nullptr, // Terminate function
nullptr, // Run function
"Python extension plugin", // Long comment about the plugin
"", // Multiline help about the plugin
"python_ext", // The preferred short name of the plugin
"Ctrl-4" // The preferred hotkey to run the plugin
};