-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathidarest.py
446 lines (385 loc) · 12.8 KB
/
idarest.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from SocketServer import ThreadingMixIn
import re
import threading
import cgi
import urlparse
import json
try:
import idaapi
import idautils
import idc
except:
pass
API_PREFIX = '/ida/api/v1.0'
class HTTPRequestError(BaseException):
def __init__(self, msg, code):
self.msg = msg
self.code = code
class UnknownApiError(HTTPRequestError):
pass
class HTTPRequestHandler(BaseHTTPRequestHandler):
routes = {}
prefns = {}
postfns = {}
@staticmethod
def build_route_pattern(route):
return re.compile("^{0}$".format(route))
@staticmethod
def route(route_str):
def decorator(f):
route_path = API_PREFIX + '/' + route_str + '/?'
route_pattern = HTTPRequestHandler.build_route_pattern(route_path)
HTTPRequestHandler.routes[route_str] = (route_pattern, f)
return f
return decorator
@staticmethod
def prefn(route_str):
def decorator(f):
HTTPRequestHandler.prefns.setdefault(route_str, []).append(f)
return f
return decorator
@staticmethod
def postfn(route_str):
def decorator(f):
HTTPRequestHandler.postfns.setdefault(route_str, []).append(f)
return f
return decorator
def _get_route_match(self, path):
for (key, (route_pattern,view_function)) in self.routes.items():
m = route_pattern.match(path)
if m:
return key,view_function
return None
def _get_route_prefn(self, key):
try:
return self.prefns[key]
except:
return []
def _get_route_postfn(self, key):
try:
return self.postfns[key]
except:
return []
def _serve_route(self, args):
path = urlparse.urlparse(self.path).path
route_match = self._get_route_match(path)
if route_match:
key,view_function = route_match
for prefn in self._get_route_prefn(key):
args = prefn(self, args)
results = view_function(self, args)
for postfn in self._get_route_postfn(key):
results = postfn(self, results)
return results
else:
raise UnknownApiError('Route "{0}" has not been registered'.format(path), 404)
def _serve(self, args):
try:
response = {
'code' : 200,
'msg' : 'OK',
'data' : self._serve_route(args)
}
except UnknownApiError as e:
self.send_error(e.code, e.msg)
return
except HTTPRequestError as e:
response = {'code': e.code, 'msg' : e.msg}
except ValueError as e:
response = {'code': 400, 'msg': 'ValueError : ' + str(e)}
except KeyError as e:
response = {'code': 400, 'msg': 'KeyError : ' + str(e)}
jsonp_callback = self._extract_callback()
if jsonp_callback:
content_type = 'application/javascript'
response_fmt = jsonp_callback + '({0});'
else:
content_type = 'application/json'
response_fmt = '{0}'
self.send_response(200)
self.send_header('Content-Type', content_type)
self.end_headers()
response = json.dumps(response)
self.wfile.write(response_fmt.format(response))
def _extract_post_map(self):
content_type,_t = cgi.parse_header(self.headers.getheader('content-type'))
if content_type != 'application/json':
raise HTTPRequestError(
'Bad content-type, use application/json',
400)
length = int(self.headers.getheader('content-length'))
try:
return json.loads(self.rfile.read(length))
except ValueError as e:
raise HTTPRequestError(
'Bad or malformed json content',
400)
def _extract_query_map(self):
query = urlparse.urlparse(self.path).query
qd = urlparse.parse_qs(query)
args = {}
for k,v in qd.iteritems():
if len(v) != 1:
raise HTTPRequestError(
"Query param specified multiple times : " + k,
400)
args[k.lower()] = v[0]
return args
def _extract_callback(self):
try:
args = self._extract_query_map()
return args['callback']
except:
return ''
def do_POST(self):
try:
args = self._extract_post_map()
except TypeError as e:
# thrown on no content, just continue on
args = '{}'
except HTTPRequestError as e:
self.send_error(e.code, e.msg)
return
self._serve(args)
def do_GET(self):
try:
args = self._extract_query_map()
except HTTPRequestError as e:
self.send_error(e.code, e.msg)
return
self._serve(args)
"""
API handlers for IDA
"""
def check_ea(f):
def wrapper(self, args):
if 'ea' in args:
try:
ea = int(args['ea'], 16)
except ValueError:
raise IDARequestError(
'ea parameter malformed - must be 0xABCD', 400)
if ea > idc.MaxEA():
raise IDARequestError(
'ea out of range - MaxEA is 0x%x' % idc.MaxEA(), 400)
args['ea'] = ea
return f(self, args)
return wrapper
def check_color(f):
def wrapper(self, args):
if 'color' in args:
color = args['color']
try:
color = color.lower().lstrip('#').rstrip('h')
if color.startswith('0x'):
color = color[2:]
# IDA Color is BBGGRR, we need to convert from RRGGBB
color = color[-2:] + color[2:4] + color[:2]
color = int(color, 16)
except:
raise IDARequestError(
'color parameter malformed - must be RRGGBB form', 400)
args['color'] = color
return f(self, args)
return wrapper
def require_params(*params):
def decorator(f):
def wrapped(self, args):
for x in params:
if x not in args:
raise IDARequestError('missing parameter {0}'.format(x), 400)
return f(self, args)
return wrapped
return decorator
class IDARequestError(HTTPRequestError):
pass
class IDARequestHandler(HTTPRequestHandler):
@staticmethod
def _hex(v):
return hex(v).rstrip('L')
@HTTPRequestHandler.route('info')
def info(self, args):
# No args, Return everything we can meta-wise about the ida session
# file crcs
result = {
'md5' : idc.GetInputMD5(),
'idb_path' : idc.GetIdbPath(),
'file_path' : idc.GetInputFilePath(),
'ida_dir' : idc.GetIdaDirectory(),
'min_ea' : self._hex(idc.MinEA()),
'max_ea' : self._hexidc.MaxEA()),
'segments' : self.segments({})['segments'],
# idaapi.cvar.inf
'procname' : idc.GetLongPrm(idc.INF_PROCNAME),
}
return result
@HTTPRequestHandler.route('query')
@check_ea
def query(self, args):
# multiple modes
# with address return everything about that address
# with name, return everything about that name
return {}
@HTTPRequestHandler.route('cursor')
@check_ea
def cursor(self, args):
# XXX - Doesn't work
#if 'window' in args:
# tform = idaapi.find_tform(args['window'])
# if tform:
# idaapi.switchto_tform(tform, 1)
# else:
# raise IDARequestError(
# 'invalid window - {0}'.format(args['window']), 400)
result = {}
if 'ea' in args:
ea = args['ea']
success = idaapi.jumpto(ea)
result['moved'] = success
result['ea'] = self._hex(idaapi.get_screen_ea())
return result
def _get_segment_info(self, s):
return {
'name' : idaapi.get_true_segm_name(s),
'ida_name' : idaapi.get_segm_name(s),
'start' : self._hex(s.startEA),
'end' : self._hex(s.endEA),
'size' : self._hex(s.size())
}
@HTTPRequestHandler.route('segments')
@check_ea
def segments(self, args):
if 'ea' in args:
s = idaapi.getseg(args['ea'])
if not s:
raise IDARequestError('Invalid address', 400)
return {'segment': self._get_segment_info(s)}
else:
m = {'segments': []}
for i in range(idaapi.get_segm_qty()):
s = idaapi.getnseg(i)
m['segments'].append(self._get_segment_info(s))
return m
@HTTPRequestHandler.route('names')
def names(self, args):
m = {'names' : []}
for n in idautils.Names():
m['names'].append([self._hex(n[0]), n[1]])
return m
@HTTPRequestHandler.route('color')
@check_color
@check_ea
@require_params('ea')
def color(self, args):
ea = args['ea']
if 'color' in args:
color = args['color']
def f():
idc.SetColor(ea, idc.CIC_ITEM, color)
idaapi.execute_sync(f, idaapi.MFF_WRITE)
idc.Refresh()
return {}
else:
return {'color' : str(GetColor(ea, idc.CIC_ITEM))}
# Figure out when this is really needed
#def f():
# idaapi.jumpto(ea) # DO STUFF
#idaapi.execute_sync(f, idaapi.MFF_FAST)
"""
Threaded HTTP Server and Worker
Use a worker thread to manage the server so that we can run inside of
IDA Pro without blocking execution.
"""
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
allow_reuse_address = True
class Worker(threading.Thread):
def __init__(self, host='127.0.0.1', port=8899):
threading.Thread.__init__(self)
self.httpd = ThreadedHTTPServer((host, port), IDARequestHandler)
def run(self):
self.httpd.serve_forever()
def stop(self):
self.httpd.shutdown()
"""
IDA Pro Plugin Interface
Define an IDA Python plugin required class and function.
"""
MENU_PATH = 'Edit/Other'
class idarest_plugin_t(idaapi.plugin_t):
flags = idaapi.PLUGIN_KEEP
comment = ""
help = "IDA Rest API for basic RE tool interoperability"
wanted_name = "IDA Rest API"
wanted_hotkey = "Alt-7"
def _add_menu(self, *args):
idaapi.msg("Adding menu item\n")
ctx = idaapi.add_menu_item(*args)
if ctx is None:
idaapi.msg("Add failed!\n")
return False
else:
self.ctxs.append(ctx)
return True
def _add_menus(self):
ret = []
ret.append(
self._add_menu(MENU_PATH, 'Stop IDARest', '', 1, self.stop, tuple()))
ret.append(
self._add_menu(MENU_PATH, 'Start IDARest', '', 1, self.start, tuple()))
if False in ret:
return idaapi.PLUGIN_SKIP
else:
return idaapi.PLUGIN_KEEP
def init(self):
idaapi.msg("Initializing %s\n" % self.wanted_name)
self.ctxs = []
self.worker = None
self.port = 8899
self.host = '127.0.0.1'
ret = self._add_menus()
idaapi.msg("Init done\n")
return ret
def _get_netinfo(self):
info = idaapi.askstr(0,
"{0}:{1}".format(self.host, self.port),
"Enter IDA Rest Connection Info")
if not info:
raise ValueError("User canceled")
host,port = info.split(':')
port = int(port)
return host,port
def start(self, *args):
idaapi.msg("Starting IDARest\n")
if self.worker:
idaapi.msg("Already running\n")
return
try:
self.host,self.port = self._get_netinfo()
except:
pass
try:
self.worker = Worker(self.host,self.port)
except Exception as e:
idaapi.msg("Error starting worker : " + str(e) + "\n")
return
self.worker.start()
idaapi.msg("Worker running\n")
def stop(self, *args):
idaapi.msg("Stopping IDARest\n")
if self.worker:
self.worker.stop()
del self.worker
self.worker = None
def run(self, arg):
pass
def term(self):
idaapi.msg("Terminating %s\n" % self.wanted_name)
try:
self.stop()
except:
pass
for ctx in self.ctxs:
idaapi.del_menu_item(ctx)
def PLUGIN_ENTRY():
return idarest_plugin_t()