forked from R3dFruitRollUp/gef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinja_gef.py
296 lines (234 loc) · 9.23 KB
/
binja_gef.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""
This script is the server-side of the XML-RPC defined for gef for
BinaryNinja.
It will spawn a threaded XMLRPC server from your current BN session
making it possible for gef to interact with Binary Ninja.
To install this script as a plugin:
$ ln -sf /path/to/gef/binja_gef.py ~/.binaryninja/plugins/binaryninja_gef.py
Then run it from Binary Ninja:
- open a disassembly session
- click "Tools" -> "gef : start/stop server"
If all went well, you will see something like
[+] Creating new thread for XMLRPC server: Thread-1
[+] Starting XMLRPC server: 0.0.0.0:1337
[+] Registered 10 functions.
@_hugsy_
"""
from binaryninja import *
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer, list_public_methods
import threading, string, inspect, xmlrpclib, copy
HOST, PORT = "0.0.0.0", 1337
DEBUG = True
HL_NO_COLOR = enums.HighlightStandardColor.NoHighlightColor
HL_BP_COLOR = enums.HighlightStandardColor.RedHighlightColor
HL_CUR_INSN_COLOR = enums.HighlightStandardColor.GreenHighlightColor
started = False
t = None
_breakpoints = set()
_current_instruction = 0
PAGE_SZ = 0x1000
def expose(f):
"Decorator to set exposed flag on a function."
f.exposed = True
return f
def is_exposed(f):
"Test whether another function should be publicly exposed."
return getattr(f, 'exposed', False)
def ishex(s):
return s.startswith("0x") or s.startswith("0X")
class Gef:
"""
Top level class where exposed methods are declared.
"""
def __init__(self, server, bv, *args, **kwargs):
self.server = server
self.view = bv
self.base = bv.entry_point & ~(PAGE_SZ-1)
self._version = ("Binary Ninja", core_version)
self.old_bps = set()
return
def _dispatch(self, method, params):
"""
Plugin dispatcher
"""
func = getattr(self, method)
if not is_exposed(func):
raise NotImplementedError('Method "%s" is not exposed' % method)
if DEBUG:
log_info("[+] Executing %s(%s)" % (method, params))
return func(*params)
def _listMethods(self):
"""
Class method listing (required for introspection API).
"""
m = []
for x in list_public_methods(self):
if x.startswith("_"): continue
if not is_exposed( getattr(self, x) ): continue
m.append(x)
return m
def _methodHelp(self, method):
"""
Method help (required for introspection API).
"""
f = getattr(self, method)
return inspect.getdoc(f)
@expose
def shutdown(self):
""" shutdown() => None
Cleanly shutdown the XML-RPC service.
Example: binaryninja shutdown
"""
self.server.server_close()
log_info("[+] XMLRPC server stopped")
setattr(self.server, "shutdown", True)
return 0
@expose
def version(self):
""" version() => None
Return a tuple containing the tool used and its version
Example: binaryninja version
"""
return self._version
@expose
def Jump(self, address):
""" Jump(int addr) => None
Move the EA pointer to the address pointed by `addr`.
Example: binaryninja Jump 0x4049de
"""
addr = long(address, 16) if ishex(address) else long(address)
return self.view.file.navigate(self.view.file.view, addr)
@expose
def MakeComm(self, address, comment):
""" MakeComm(int addr, string comment) => None
Add a comment at the location `address`.
Example: binaryninja MakeComm 0x40000 "Important call here!"
"""
addr = long(address, 16) if ishex(address) else long(address)
start_addr = self.view.get_previous_function_start_before(addr)
func = self.view.get_function_at(start_addr)
return func.set_comment(addr, comment)
@expose
def SetColor(self, address, color='0xff0000'):
""" SetColor(int addr [, int color]) => None
Set the location pointed by `address` with `color`.
Example: binaryninja SetColor 0x40000 0xff0000
"""
addr = long(address, 16) if ishex(address) else long(address)
color = long(color, 16) if ishex(color) else long(color)
R,G,B = (color >> 16)&0xff, (color >> 8)&0xff, (color&0xff)
color = highlight.HighlightColor(red=R, blue=G, green=B)
return hl(self.view, addr, color)
@expose
def Sync(self, off, added, removed):
""" Sync(off, added, removed) => None
Synchronize debug info with gef. This is an internal function. It is
not recommended using it from the command line.
"""
global _breakpoints, _current_instruction
# we use long() for pc because if using 64bits binaries might create
# OverflowError for XML-RPC service
off = long(off, 16) if ishex(off) else long(off)
pc = self.base + off
if DEBUG: log_info("[*] current_pc=%#x , old_pc=%#x" % (pc, _current_instruction))
# unhighlight the _current_instruction
if _current_instruction > 0:
hl(self.view, _current_instruction, HL_NO_COLOR)
hl(self.view, pc, HL_CUR_INSN_COLOR)
# update the _current_instruction
_current_instruction = pc
if DEBUG:
log_info("[*] pre-gdb-add-breakpoints: %s" % (added,))
log_info("[*] pre-gdb-del-breakpoints: %s" % (removed,))
log_info("[*] pre-binja-breakpoints: %s" % (_breakpoints))
bn_added = [ x-self.base for x in _breakpoints if x not in self.old_bps ]
bn_removed = [ x-self.base for x in self.old_bps if x not in _breakpoints ]
for bp in added:
gef_add_breakpoint_to_list(self.view, self.base + bp)
for bp in removed:
gef_del_breakpoint_from_list(self.view, self.base + bp)
self.old_bps = copy.deepcopy(_breakpoints)
if DEBUG:
log_info("[*] post-gdb-add-breakpoints: %s" % (bn_added,))
log_info("[*] post-gdb-del-breakpoints: %s" % (bn_removed,))
log_info("[*] post-binja-breakpoints: %s" % (_breakpoints,))
return [bn_added, bn_removed]
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ("/RPC2",)
def hl(bv, addr, color):
if DEBUG: log_info("[*] hl(%#x, %s)" % (addr, color))
start_addr = bv.get_previous_function_start_before(addr)
func = bv.get_function_at(start_addr)
if func is None: return
func.set_user_instr_highlight(addr, color)
return
def start_service(host, port, bv):
log_info("[+] Starting service on {}:{}".format(host, port))
server = SimpleXMLRPCServer((host, port),
requestHandler=RequestHandler,
logRequests=False,
allow_none=True)
server.register_introspection_functions()
server.register_instance(Gef(server, bv))
log_info("[+] Registered {} functions.".format( len(server.system_listMethods()) ))
while True:
if hasattr(server, "shutdown") and server.shutdown==True: break
server.handle_request()
return
def gef_start(bv):
global t, started
t = threading.Thread(target=start_service, args=(HOST, PORT, bv))
t.daemon = True
log_info("[+] Creating new thread {}".format(t.name))
t.start()
if not started:
create_binja_menu()
started = True
return
def gef_stop(bv):
global t
t.join()
t = None
log_info("[+] Server stopped")
return
def gef_start_stop(bv):
if t is None:
gef_start(bv)
show_message_box("GEF", "Service successfully started, you can now have gef connect to it",
MessageBoxButtonSet.OKButtonSet, MessageBoxIcon.InformationIcon)
else:
try:
cli = xmlrpclib.ServerProxy("http://{:s}:{:d}".format(HOST, PORT))
cli.shutdown()
except socket.error:
pass
gef_stop(bv)
show_message_box("GEF", "Service successfully stopped",
MessageBoxButtonSet.OKButtonSet, MessageBoxIcon.InformationIcon)
return
def gef_add_breakpoint_to_list(bv, addr):
global _breakpoints
if addr in _breakpoints: return False
_breakpoints.add(addr)
log_info("[+] Breakpoint %#x added" % addr)
hl(bv, addr, HL_BP_COLOR)
return True
def gef_del_breakpoint_from_list(bv, addr):
global _breakpoints
if addr not in _breakpoints: return False
_breakpoints.discard(addr)
log_info("[+] Breakpoint %#x removed" % addr)
hl(bv, addr, HL_NO_COLOR)
return True
def create_binja_menu():
# Binja does not really support menu in its GUI just yet
PluginCommand.register_for_address("gef : add breakpoint",
"Add a breakpoint in gef at the specified location.",
gef_add_breakpoint_to_list)
PluginCommand.register_for_address("gef : delete breakpoint",
"Remove a breakpoint in gef at the specified location.",
gef_del_breakpoint_from_list)
return
PluginCommand.register("Start/stop server GEF interaction",
"Start/stop the XMLRPC server for communicating with gef",
gef_start_stop)