-
Notifications
You must be signed in to change notification settings - Fork 96
/
plugin_loader.py
104 lines (83 loc) · 3.35 KB
/
plugin_loader.py
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
import os
import itertools
import idaapi
import idc
PLUGINS_LIST = "plugins.list"
USER_PLUGIN_LIST_PATH = os.path.join(idaapi.get_user_idadir(), PLUGINS_LIST)
SYS_PLUGIN_LIST_PATH = os.path.join(idaapi.idadir(idaapi.CFG_SUBDIR), PLUGINS_LIST)
if idc.get_idb_path():
PROJECT_PLUGIN_LIST_PATH = os.path.join(os.path.dirname(idc.get_idb_path()), PLUGINS_LIST)
else:
PROJECT_PLUGIN_LIST_PATH = None
def message(*messages):
for msg in messages:
for line in msg.splitlines():
idaapi.msg("[PluginLoader] {}\n".format(line))
def iter_without_duplicates(*iterables):
visited = set()
chained_iterables = itertools.chain(*iterables)
for item in chained_iterables:
if item in visited:
continue
yield item
visited.add(item)
def iter_paths(filepath):
if not filepath:
return
try:
with open(filepath) as f:
for line in f:
# Use `#` for comments
if line.startswith("#"):
continue
# Remove trailing spaces and newlines, then normalize to avoid duplicates.
path = os.path.normpath(line.strip())
if path:
yield path
except IOError:
pass
def iter_plugin_paths():
return iter_without_duplicates(iter_paths(SYS_PLUGIN_LIST_PATH),
iter_paths(USER_PLUGIN_LIST_PATH),
iter_paths(PROJECT_PLUGIN_LIST_PATH))
class PluginLoader(idaapi.plugin_t):
flags = idaapi.PLUGIN_FIX
comment = "Plugin Loader"
help = "Plugin Loader"
wanted_name = "PluginLoader"
wanted_hotkey = ""
def init(self):
# Show usage message.
usage_message = ["Loading plugins from system-wide and user-specific lists:",
" System-wide List: {}".format(SYS_PLUGIN_LIST_PATH),
" User-specific List: {}".format(USER_PLUGIN_LIST_PATH)]
if PROJECT_PLUGIN_LIST_PATH:
usage_message.append(" Project-specific List: {}".format(PROJECT_PLUGIN_LIST_PATH))
message(*usage_message)
# Make sure the files exist. If not - create them.
if not os.path.isfile(SYS_PLUGIN_LIST_PATH):
try:
with open(SYS_PLUGIN_LIST_PATH, "wb"):
message("Created system plugin list at {}".format(SYS_PLUGIN_LIST_PATH))
except IOError:
message("Failed creating system plugin list at {}".format(SYS_PLUGIN_LIST_PATH))
if not os.path.isfile(USER_PLUGIN_LIST_PATH):
try:
with open(USER_PLUGIN_LIST_PATH, "wb"):
message("Created user plugin list at {}".format(USER_PLUGIN_LIST_PATH))
except IOError:
message("Failed creating user plugin list at {}".format(USER_PLUGIN_LIST_PATH))
for path in iter_plugin_paths():
# This check is not needed, but saves us from the dreaded error message-box
# that pops when a python plugin is not found.
if not os.path.isfile(path):
message("Plugin file not found: {}".format(path))
continue
idaapi.load_plugin(path)
return idaapi.PLUGIN_SKIP
def term(self):
pass
def run(self, arg):
pass
def PLUGIN_ENTRY():
return PluginLoader()