forked from NeatMonster/MCExplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcexplorer.py
121 lines (100 loc) · 3.42 KB
/
mcexplorer.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import ctypes
import os
import sys
import ida_diskio
import ida_funcs
import ida_graph
import ida_hexrays
import ida_idaapi
import ida_kernwin
import ida_lines
import ida_pro
LEVELS = ["MMAT_GENERATED", "MMAT_PREOPTIMIZED", "MMAT_LOCOPT", "MMAT_CALLS",
"MMAT_GLBOPT1", "MMAT_GLBOPT2", "MMAT_GLBOPT3", "MMAT_LVARS"]
class MCGraphView(ida_graph.GraphViewer):
def __init__(self, mba, func, mmat):
title = "MCGraph View - %s at %s" % (func, mmat)
ida_graph.GraphViewer.__init__(self, title, True)
self._mba = mba
def OnRefresh(self):
self.Clear()
qty = self._mba.qty
for src in range(qty):
self.AddNode(src)
for src in range(qty):
mblock = self._mba.get_mblock(src)
for dest in mblock.succset:
self.AddEdge(src, dest)
return True
def OnGetText(self, node):
mblock = self._mba.get_mblock(node)
vp = ida_hexrays.qstring_printer_t(None, True)
mblock._print(vp)
return vp.s
class MCTextView(ida_kernwin.simplecustviewer_t):
def __init__(self, mba, func, mmat):
ida_kernwin.simplecustviewer_t.__init__(self)
self._mba = mba
self._func = func
self._mmat = mmat
title = "MCText View - %s at %s" % (func, mmat)
self.Create(title)
self.ClearLines()
vp = ida_hexrays.qstring_printer_t(None, True)
mba._print(vp)
for line in vp.s.split('\n'):
self.AddLine(line)
def OnKeydown(self, vkey, shift):
if shift == 0 and vkey == ord("G"):
MCGraphView(self._mba, self._func, self._mmat).Show()
return True
return False
class MCExplorer(ida_idaapi.plugin_t):
flags = 0
comment = "Microcode Explorer"
help = ""
wanted_name = "MCExplorer"
wanted_hotkey = "Ctrl+Shift+M"
@staticmethod
def ask_desired_maturity():
class MaturityForm(ida_kernwin.Form):
def __init__(self):
ctrl = ida_kernwin.Form.DropdownListControl(LEVELS[::-1])
form = """Select maturity level
<Select maturity level:{ctrl}>"""
ida_kernwin.Form.__init__(self, form, {"ctrl": ctrl})
form = MaturityForm()
form, args = form.Compile()
ok = form.Execute()
mmat = 0
if ok == 1:
mmat = len(LEVELS) - form.ctrl.value
form.Free()
return mmat
def init(self):
if not ida_hexrays.init_hexrays_plugin():
return ida_idaapi.PLUGIN_SKIP
print("[MCExplorer] Plugin initialized")
return ida_idaapi.PLUGIN_KEEP
def term(self):
ida_hexrays.term_hexrays_plugin()
print("[MCExplorer] Plugin terminated")
def run(self, _):
fn = ida_funcs.get_func(ida_kernwin.get_screen_ea())
if fn is None:
ida_kernwin.warning("Please position the cursor within a function")
return True
mmat = MCExplorer.ask_desired_maturity()
if mmat == 0:
return True
hf = ida_hexrays.hexrays_failure_t()
mbr = ida_hexrays.mba_ranges_t(fn)
mba = ida_hexrays.gen_microcode(mbr, hf, None, 0, mmat)
if not mba:
return True
fn_name = ida_funcs.get_func_name(fn.start_ea)
mmat_name = LEVELS[mmat - 1]
MCTextView(mba, fn_name, mmat_name).Show()
return True
def PLUGIN_ENTRY():
return MCExplorer()