-
Notifications
You must be signed in to change notification settings - Fork 43
/
ida-rellic.py
186 lines (135 loc) · 6.42 KB
/
ida-rellic.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
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
#
# Copyright (c) 2021-present, Trail of Bits, Inc.
# All rights reserved.
#
# This source code is licensed in accordance with the terms specified in
# the LICENSE file found in the root directory of this source tree.
#
import ida_funcs
import ida_kernwin
import idautils
import anvill
import os
import json
import tempfile
import subprocess
from shutil import which
class rellic_decompile_function_t(ida_kernwin.action_handler_t):
_anvill_decompile_json_path = None
_rellic_decomp_path = None
_opt_path = None
def __init__(self):
self.locate_external_programs()
print("rellic: Found `opt` executable:", self._opt_path)
print("rellic: Found `anvill-decompile-json` executable:",
self._anvill_decompile_json_path)
print("rellic: Found `rellic-decomp` executable:",
self._rellic_decomp_path)
print("rellic: Place the cursor inside a function and use the right-click menu, or press CTRL+Y")
def locate_external_programs(self):
opt_path = which("opt")
if opt_path is None:
return False
llvm_version_list = ["14"]
for llvm_version in llvm_version_list:
anvill_decompile_json_path = which(
"anvill-decompile-json-" + llvm_version)
rellic_decomp_path = which("rellic-decomp")
if anvill_decompile_json_path is not None and rellic_decomp_path is not None:
remill_semantics_path = "/usr/local/share/remill/" + \
llvm_version + "/semantics"
if not os.path.isdir(remill_semantics_path):
raise RuntimeError(
"rellic: anvill and rellic have been compiled for llvm {}, but the matching remill semantics were not found".format(llvm_version))
self._opt_path = opt_path
self._anvill_decompile_json_path = anvill_decompile_json_path
self._rellic_decomp_path = rellic_decomp_path
return True
raise RuntimeError("rellic: Failed to locate rellic/anvill/remill/opt")
def display_output(self, window_name, decompiled_function):
# Make sure we have a newline at the end
if not decompiled_function.endswith("\n"):
decompiled_function.append("\n")
# Create a window and display the output
output_window = ida_kernwin.simplecustviewer_t()
if output_window.Create(window_name):
for line in decompiled_function.splitlines():
output_window.AddLine(line)
output_window.Show()
else:
print("rellic: Failed to create the display window")
def activate(self, ctx):
# Identify the function at the cursor
screen_cursor = ida_kernwin.get_screen_ea()
function_name = ida_funcs.get_func_name(screen_cursor)
if function_name is None:
print("rellic: The cursor is not located inside a function")
return
# Generate the anvill spec data
anvill_program = anvill.get_program()
try:
anvill_program.add_function_definition(screen_cursor)
except Exception as e:
print("rellic: Failed to process the function at address {:x}. Error details follow:\n{}".format(
screen_cursor, e))
return
spec_data = json.dumps(anvill_program.proto(),
sort_keys=False, indent=2)
# Create a temporary directory and keep it around, since we may have to
# debug issues in anvill/rellic
working_directory = tempfile.mkdtemp()
print("rellic: The working directory is located here: {}".format(
working_directory))
# Dump the anvill spec file to disk
spec_file_path = os.path.join(working_directory, 'spec.json')
with open(spec_file_path, "w+") as spec_file:
spec_file.write(spec_data)
spec_file.close()
# Now ask anvill to generate the bc from the spec data
bc_file_path = os.path.join(working_directory, "out.bc")
ir_file_path = os.path.join(working_directory, "out.ir")
try:
subprocess.check_output([self._anvill_decompile_json_path, "-spec", spec_file_path,
"-bc_out", bc_file_path, "-ir_out", ir_file_path], text=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print(
"rellic: Failed to start the anvill-decompile-json process. Error details follow:\n{}".format(e.output))
return
# Run the reg2mem pass on the generated bitcode
processed_bc_file_path = os.path.join(working_directory, "reg2mem.bc")
try:
subprocess.check_output([self._opt_path, "-reg2mem", bc_file_path,
"-o=" + processed_bc_file_path], text=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print(
"rellic: Failed to start the opt process. Error details follow:\n{}".format(e.output))
return
# Finally, ask rellic to decompile the bitcode
c_file_path = os.path.join(working_directory, 'out.c')
try:
subprocess.check_output([self._rellic_decomp_path, "--input", processed_bc_file_path,
"--output", c_file_path], text=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print(
"rellic: Failed to start the rellic-decomp process. Error details follow:\n{}".format(e.output))
return
with open(c_file_path, "r") as rellic_output_file:
decompiled_function = rellic_output_file.read()
self.display_output(function_name, decompiled_function)
def update(self, ctx):
if ctx.widget_type == ida_kernwin.BWN_DISASM:
return ida_kernwin.AST_ENABLE_FOR_WIDGET
return ida_kernwin.AST_DISABLE_FOR_WIDGET
ACTION_NAME = "decompile-with-rellic"
ida_kernwin.register_action(
ida_kernwin.action_desc_t(
ACTION_NAME,
"Decompile with rellic",
rellic_decompile_function_t(),
"Ctrl+Y"))
class popup_hooks_t(ida_kernwin.UI_Hooks):
def finish_populating_widget_popup(self, w, popup):
if ida_kernwin.get_widget_type(w) == ida_kernwin.BWN_DISASM:
ida_kernwin.attach_action_to_popup(w, popup, ACTION_NAME, None)
hooks = popup_hooks_t()
hooks.hook()