-
Notifications
You must be signed in to change notification settings - Fork 1
/
idarest_client.py
297 lines (266 loc) · 12.9 KB
/
idarest_client.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
import urllib.request, urllib.error, urllib.parse
import requests
import json
import idaapi
from superglobals import *
from underscoretest import _
# execfile('underscoretest')
try:
from .idarest_mixins import IdaRestConfiguration
except:
from idarest_mixins import IdaRestConfiguration
# IdaRestClient.config['master_port'] = 28612 # hash('idarest75') & 0xffff
# IdaRestClient.config['master_host'] = '127.0.0.1'
# IdaRestClient.config['api_prefix'] = '/ida/api/v1.0'
# IdaRestClient.config['api_debug'] = True
# IdaRestClient.config['api_info'] = True
class _MyChoose(idaapi.Choose):
def __init__(self, items, title, cols, icon=-1):
idaapi.Choose.__init__(self, title, cols, flags=idaapi.Choose.CH_MODAL, icon=icon)
self.items = items
def OnClose(self):
pass
def OnGetLine(self, n):
return self.items[n]
def OnGetSize(self):
return len(self.items)
class HttpResponseError(Exception):
pass
class IdaRestClient(IdaRestConfiguration, object):
def __init__(self):
self.hosts = {}
self.master_host = IdaRestClient.config['master_host']
self.master_port = IdaRestClient.config['master_port']
self.connect_timeout = IdaRestClient.config['client_connect_timeout']
self.read_timeout = IdaRestClient.config['client_read_timeout']
self.update_hosts_timeout = IdaRestClient.config['client_update_hosts_timeout']
def host_failed(self, url):
# http://127.0.0.1:2012/ida/api/v1.0/get_type
idb = _.findKey(self.hosts, lambda x, *a: x.startswith(url))
if idb:
request_url = 'http://{}:{}{}/fail?idb={}'.format(self.master_host, self.master_port, IdaRestClient.config['api_prefix'], urllib.parse.quote(idb))
try:
request = requests.get(request_url, timeout=self.update_hosts_timeout)
except requests.exceptions.ReadTimeout:
print("MasterReadTimeout: {}".format(url + route))
except requests.exceptions.ConnectTimeout:
print("MasterConnectTimeout: {}".format(url + route))
if request.status_code != 200:
print("MasterHttpResponseError attempting to inform host about slow client: {}".format(r.status_code))
def terminate_master(self):
# http://127.0.0.1:2012/ida/api/v1.0/get_type
request_url = 'http://{}:{}{}/term'.format(self.master_host, self.master_port, IdaRestClient.config['api_prefix'])
r = requests.get(request_url, timeout=self.update_hosts_timeout)
if r.status_code != 200:
print("HttpResponseError attempting to inform host about slow client: {}".format(r.status_code))
def update_hosts(self):
request_url = 'http://{}:{}{}/show'.format(self.master_host, self.master_port, IdaRestClient.config['api_prefix'])
r = requests.get(request_url, timeout=self.update_hosts_timeout)
if r.status_code != 200:
raise HttpResponseError(r.status_code)
# dprint("[debug] request_url, r.content")
# print("[debug] request_url:{}, r.content:{}".format(request_url, r.content))
if not r.content:
if self.config['client_debug']: print("[IdaRestClient::update_hosts] master returned no data")
return
j = r.json()
# we need to remove ourself from the list of available hosts, else we
# will deadlock when trying to self-query
# check if idarest is loaded as a plugin
ir = getglobal('sys.modules.__plugins__idarest.instance', None)
# check if idarest is loaded as a module
ir = ir or getglobal('sys.modules.idarest.instance', None)
# check if idarest is loaded in global context
ir = ir or getglobal('idarest_main.instance', None)
if ir and hasattr(ir, 'host'):
skip = "http://{}:{}/".format(ir.host, ir.port)
else:
skip = None
self.hosts.clear()
if isinstance(j, dict):
for idb, url in j.items():
if self.config['client_debug']: print("idb: {} url: {}".format(idb, url))
if not skip or not url.startswith(skip):
self.hosts[idb] = url
return self.hosts
else:
if self.config['client_debug']: print("[IdaRestClient::update_hosts] master returned invalid data: {}".format(j))
def get_json(self, route, host=None, **kwargs):
"""Get the result of an eval query from every active host (except ourselves)"""
self.update_hosts()
results = dict()
if host is not None:
if not host.startswith('http'):
url = 'http://{}/ida/api/v1.0/'.format(host)
else:
url = host
try:
request = requests.get(url + route, params=kwargs, timeout=(self.connect_timeout, self.read_timeout))
if request.status_code != 200:
raise HttpResponseError(request.status_code)
return request
except requests.exceptions.ReadTimeout:
print("ReadTimeout: {}".format(url + route))
self.host_failed(url)
except requests.exceptions.ConnectTimeout:
print("ConnectTimeout: {}".format(url + route))
return
for idb, url in self.hosts.items():
try:
request = requests.get(url + route, params=kwargs, timeout=(self.connect_timeout, self.read_timeout))
if request.status_code != 200:
raise HttpResponseError(request.status_code)
results[idb] = request.json()
except requests.exceptions.ReadTimeout:
print("ReadTimeout: {}".format(url + route))
self.host_failed(url)
continue
except requests.exceptions.ConnectTimeout:
print("ConnectTimeout: {}".format(url + route))
continue
except requests.exceptions.ConnectionError:
print("ConnectError: {}".format(url + route))
continue
# print("get_json: results: {}".format(results))
return results
def GetDecls(self, memcmd='mem("8d 04 11 c3")'):
results = self.get_json('eval', cmd=memcmd + ".type()")
print("results: {}".format(results))
f = _.filterObject(results, lambda v, k, *a: v['msg'] == 'OK' and v['data'] and not v['data'].startswith('False'))
# o = _.mapObject(f, lambda v, k, *a: (string_between('/', '', k, rightmost=1), _.pick(v, 'data')))
o = _.mapObject(f, lambda v, k, *a: (
# key
string_between('/', '', k, rightmost=1),
# value
string_between("'", "'", v['data'], greedy=1)
))
p = _.pairs(o)
variable_chooser = _MyChoose(
p,
"Select Function",
[["Database", 25], ["Function", 16]]
)
row = variable_chooser.Show(modal=True)
if row != -1:
print("Chose {}: {}".format(row, ": ".join(p[row])))
SetType(EA(), p[row][1].replace('(', ' fn('))
def eval(self, cmd):
""" eval(cmd) eval a command on all clients """
results = self.get_json('eval', cmd=cmd)
f = _.filterObject(results, lambda v, k, *a: v['msg'] == 'OK' and v['data'] and not v['data'].startswith('False'))
# o = _.mapObject(f, lambda v, k, *a: (string_between('/', '', k, rightmost=1), _.pick(v, 'data')))
o = _.mapObject(f, lambda v, k, *a: (
# key
string_between('/', '', k, rightmost=1),
# value
string_between("'", "'", v['data'], greedy=1)
))
p = _.pairs(o)
return p
def GetNames(self, memcmd='mem("8d 04 11 c3")'):
results = self.get_json('eval', cmd=memcmd + ".name()")
f = _.filterObject(results, lambda v, k, *a: v['msg'] == 'OK' and v['data'] and not v['data'].startswith('False'))
# o = _.mapObject(f, lambda v, k, *a: (string_between('/', '', k, rightmost=1), _.pick(v, 'data')))
o = _.mapObject(f, lambda v, k, *a: (
# key
string_between('/', '', k, rightmost=1),
# value
string_between("'", "'", v['data'], greedy=1)
))
p = _.pairs(o)
variable_chooser = _MyChoose(
p,
"Select Function",
[["Database", 25], ["Function", 16]]
)
row = variable_chooser.Show(modal=True)
if row != -1:
print("Chose {}: {}".format(row, ": ".join(p[row])))
LabelAddressPlus(EA(), p[row][1])
def GetTypes2(self, types='Hash', ask=False, flags=None):
# names = _.flatten(irc.get_json('names', 'http://192.168.1.123:2007/ida/api/v1.0/').json()['data'], 1)
# [LabelAddressPlus(x, y) for x, y in names if ean(x) != y and not re_match_array(r'^(NATIVE|The|Arxan|Balance|\w+Bunny|.*stack_align|.*Concurrency)', [ean(x), y])];
# pph([(x, ean(x), y) for x, y in names if ean(x) != y and not re_match_array(r'^(NATIVE|The|Arxan|Balance|\w+Bunny|.*stack_align|.*Concurrency)', [ean(x), y])])
#
# patches = irc.get_json('patches.picle', 'http://192.168.1.123:2021/ida/api/v1.0/')
# p = pickle.loads(bytearray.fromhex(patches.json()['data']))
# for ea, b in p.items(): PatchBytes(ea, b)
if flags is not None:
results = self.get_json('get_type', type=types, flags=flags)
else:
results = self.get_json('get_type', type=types)
f = _.filterObject(results, lambda v, k, *a: v['msg'] == 'OK' and 'data' in v and v['data'] and 'data' in v['data'][0])
# o = _.mapObject(f, lambda v, k, *a: (string_between('/', '', k, rightmost=1), _.pick(v, 'data')))
o = _.mapObject(f, lambda v, k, *a: (
# key
string_between('/', '', k, rightmost=1),
# value
"/*({})*/ {}".format(len(v['data'][0]['data']), v['data'][0]['data'])
))
p = _.pairs(o)
variable_chooser = _MyChoose(
p,
"Select Function",
[["Database", 25], ["Function", 128]]
)
row = variable_chooser.Show(modal=True)
if row != -1:
cdecl_typedef = p[row][1]
if ask:
cdecl_typedef = idaapi.ask_text(0x10000, cdecl_typedef, "The following new type will be created")
if not cdecl_typedef:
return
print("Parsing decls {}: {}".format(row, cdecl_typedef))
idc.parse_decls(cdecl_typedef)
@staticmethod
def GetTypes(types, decls=None):
"""
An example of how to request type decls from all hosts and then
select only one
:param types: a typename or list of types
:param decls: dict which will contain {typename: decl} items
:returns: number of hosts which returned decls, or 0 if none
"""
def asList(l):
if not isinstance(l, list):
return [l]
return l
if decls is None:
decls = {}
count = 0
q = IdaRestClient()
response = q.get_json('get_type', type=','.join(asList(types)))
if isinstance(response, dict):
for idb, r in response.items():
if r['msg'] == 'OK':
if isinstance(r['data'], list):
for t in r['data']:
if t['msg'] == 'OK':
name = t['name']
data = t['data']
if q.config['client_debug']: print("received definition for type '{}': {}".format(name, data))
if name not in decls:
decls[name] = data
else:
if len(data.split('\n')) >= len(decls[name].split('\n')):
decls[name] = data
if q.config['client_debug']: print("using second definition for type '{}'".format(name))
else:
if q.config['client_debug']: print("using first definition for type '{}'".format(name))
count += 1
else:
print("Unexpected return type: {}".format(type(response)))
# if r['msg'] == 'OK':
# if isinstance(r['data'], list):
# for t in r['data']:
# if t['msg'] == 'OK':
# name = t['name']
# data = t['data']
# if q.config['client_debug']: print("received definition for type '{}': {}".format(name, data))
# decls[name] = data
# count += 1
return count
IdaRestClient.load_configuration()
irc = IdaRestClient()
# irc.read_timeout = 2
# irc.update_hosts_timeout = 2