-
Notifications
You must be signed in to change notification settings - Fork 12
/
intezer_analyze_gh_community.py
323 lines (252 loc) · 12 KB
/
intezer_analyze_gh_community.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
# Plugin for Intezer Analyze in Ghidra (python 2.7 - jython)
# @author
# @category Detection
# @keybinding
# @menupath
# @toolbar
from distutils.version import StrictVersion
import os
import sys
if (os.name == "Posix" or os.name.getshadow() == "posix") and (("Linux") in os.uname()):
sys.path.append('/usr/lib/python2.7/dist-packages')
sys.path.append('/usr/local/lib/python2.7/dist-packages')
sys.path.append(os.path.expanduser('~') + '/.local/lib/python2.7/site-packages')
elif ("Darwin") in os.uname():
sys.path.append('/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages')
sys.path.append('/System/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python')
sys.path.append('/Library/Python/2.7/site-packages')
sys.path.append(os.path.expanduser('~') + '/Library/Python/2.7/lib/python/site-packages')
elif os.name == "nt" or ("windows") in java.lang.System.getProperty("os.name").lower():
sys.path.append('C:\\Python27\\lib\\site-packages')
elif os.name == "java":
sys.path.append('/usr/lib/python2.7/dist-packages')
sys.path.append('/usr/local/lib/python2.7/dist-packages')
sys.path.append('/usr/local/lib/python2.7/site-packages')
else:
print('Whelp, something went wrong.')
import hashlib
import traceback
import requests
import time
from xml.etree import ElementTree
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import SubElement
from xml.dom import minidom
req_ver = StrictVersion(requests.__version__)
if req_ver < StrictVersion("2.27.1") or req_ver >= StrictVersion("2.28.0"):
print('Dependency not met: requests 2.27.1 or newer, but not 2.28 which does not support Python 2.')
sys.exit(1)
VERSION = '0.1'
INTEZER_API_KEY = os.environ.get('INTEZER_API_KEY')
BASE_URL = os.environ.get('INTEZER_BASE_URL', 'https://analyze.intezer.com')
API_URL = '{}/api'.format(BASE_URL)
DIR = os.getenv('intezer_analyze_ghidra_export_file_path', os.path.dirname(os.path.abspath("__file__")))
PATH_TO_XML = os.path.join(DIR, "items.xml")
URLS = {
'get_access_token': '{}/v2-0/get-access-token'.format(API_URL),
'create_ghidra_plugin_report': '{}/v1-2/files/{{}}/community-ida-plugin-report'.format(API_URL)
}
MESSAGES = {
'missing_api_key': 'Please set INTEZER_API_KEY in your environment variables',
'file_not_open': 'Please open a file to analyze',
'file_not_exists': 'Problem occurred while opening file',
'file_not_searched': 'Please analyze the file first on Intezer Analyze. The sha256 is: {}',
'not_supported_file': 'File type not supported for creating Intezer Analyze Ghidra report',
'authentication_failure': 'Failed to authenticate Intezer service',
'connection_error': 'Failed to connect to the Intezer cloud platform',
'no_genes': 'No genes where extracted from the file',
}
FUNCTIONS_LIMIT = 10000
FUNCTIONS_FALLBACK_LIMIT = 1000
class PluginException(Exception):
pass
class Proxy:
def __init__(self, api_key):
self._api_key = api_key
self._session = None
@property
def session(self):
if not self._session:
session = requests.session()
session.mount('https://', requests.adapters.HTTPAdapter(max_retries=3))
session.mount('http://', requests.adapters.HTTPAdapter(max_retries=3))
session.headers = {'User-Agent': 'ghidra_plugin/{}'.format(VERSION)}
self._session = session
return self._session
def init_access_token(self):
if 'Authorization' not in self.session.headers:
response = requests.post(URLS['get_access_token'], json={'api_key': self._api_key})
response.raise_for_status()
token = 'Bearer {}'.format(response.json()['result'])
self.session.headers['Authorization'] = token
def _post(self, url_path, **kwargs):
self.init_access_token()
retries = 5
retries_counter = 0
while retries_counter <= retries:
response = self.session.post(url_path, **kwargs)
if 299 >= response.status_code >= 200 or 499 >= response.status_code >= 400:
return response
else:
time.sleep(2)
retries_counter += 1
return None
def _get(self, url_path, **kwargs):
self.init_access_token()
return self.session.get(url_path, **kwargs)
def create_plugin_report(self, sha256, functions_data):
response = self._post(URLS['create_ghidra_plugin_report'].format(sha256),
json={'functions_data': functions_data[:FUNCTIONS_LIMIT]})
if response is None:
raise Exception('Failed creating plugin report')
if response.status_code == 404:
raise PluginException(MESSAGES['file_not_searched'].format(sha256))
if response.status_code == 409:
raise PluginException(MESSAGES['not_supported_file'])
if response.status_code != 201:
raise Exception(response.reason)
result_url = response.json()['result_url']
return result_url
def get_plugin_report(self, result_url):
retries = 5
retries_counter = 0
while retries_counter <= retries:
response = self._get(API_URL + result_url)
if response.status_code == 202:
time.sleep(2)
retries_counter += 1
else:
response.raise_for_status()
return response.json()['result']
class CodeIntelligenceHelper:
def __init__(self):
self._proxy = Proxy(INTEZER_API_KEY)
self._imagebase = None
self._entrypoint = None
@property
def entrypoint(self):
if not self._entrypoint:
self._entrypoint = None
return self._entrypoint
@property
def imagebase(self):
if not self._imagebase:
self._imagebase = (currentProgram.getImageBase().offset)
return self._imagebase
def _get_function_map(self, sha256):
functions_data = []
function_manager = currentProgram.getFunctionManager()
functions = function_manager.getFunctions(1)
image_base = int("0x{}".format(str(currentProgram.imageBase)), 16)
for f in functions:
function_start_address = f.getEntryPoint()
function_end_address = f.getBody().getMaxAddress()
start_address_as_int = int("0x{}".format(str(function_start_address)), 16)
end_address_as_int = int("0x{}".format(str(function_end_address)), 16)
functions_data.append({'start_address': long(start_address_as_int - image_base),
'end_address': long(end_address_as_int - image_base + 1)})
is_partial_result = len(functions_data) >= FUNCTIONS_LIMIT
try:
result_url = self._proxy.create_plugin_report(sha256, functions_data)
except requests.ConnectionError:
# We got connection error when sending a large payload of functions.
# The fallback is to send a limited amount of functions
result_url = self._proxy.create_plugin_report(sha256, functions_data[:FUNCTIONS_FALLBACK_LIMIT])
is_partial_result = True
ghidra_plugin_report = self._proxy.get_plugin_report(result_url)
if not ghidra_plugin_report['functions']:
raise PluginException(MESSAGES['no_genes'])
functions_map = {}
for function_address, record in ghidra_plugin_report['functions'].iteritems():
absolute_address = self._get_absolute_address(int(function_address))
functions_map[absolute_address] = {'function_address': absolute_address}
functions_map[absolute_address].update(record)
return functions_map, is_partial_result
def _get_absolute_address(self, function_address):
return hex(self.imagebase + function_address)
def _enrich_function_map(self, function_map):
fm = currentProgram.getFunctionManager()
for function_absolute_address in function_map:
n = "" # needed for the cast from string to Address
n = function_absolute_address.replace('L', '')
address = currentProgram.getAddressFactory().getAddress(n)
function_object = fm.getFunctionContaining(address)
try:
function_start_address = function_object.getEntryPoint()
function_map[function_absolute_address]['function_address'] = "0x{}".format(str(function_start_address))
function_map[function_absolute_address]['function_name'] = function_object.getName()
except:
function_map[function_absolute_address]['function_address'] = function_absolute_address
function_map[function_absolute_address]['function_name'] = "" # Failed resolve function name
return function_map
def write_xml_file(self, functions_map, is_partial_result):
def prettify(elem):
"""Return a pretty-printed XML string for the Element."""
rough_string = ElementTree.tostring(elem)
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ").encode('utf-8')
root = Element('Data')
for key in functions_map.keys():
entry = SubElement(root, 'gene')
function_address = SubElement(entry, "function_address")
function_name = SubElement(entry, "function_name")
software_type = SubElement(entry, "software_type")
code_reuse = SubElement(entry, "code_reuse")
try:
function_address.text = functions_map[key]["function_address"]
except KeyError as ex:
print("Error in key = {0} when getting function_address. meta = ({1})".format(key, functions_map[key]))
try:
function_name.text = functions_map[key]["function_name"]
except KeyError as ex:
print("Error in key = {0} when getting function_name. meta = ({1})".format(key, functions_map[key]))
software_type.text = ','.join(map(str, functions_map[key]["software_type"]))
for e in functions_map[key]["code_reuse"]:
code_reuse.text = e
print(">>>Done building xml. Writing xml...")
if is_partial_result:
print(">>>The result is partial due to the large amount of functions")
output_file = open(PATH_TO_XML, 'w')
output_file.write(prettify(root))
output_file.close()
def create_function_map(self, sha256):
function_map, is_partial_result = self._get_function_map(sha256)
function_map = self._enrich_function_map(function_map)
self.write_xml_file(function_map, is_partial_result)
class IntezerAnalyzePlugin():
def run(self):
if not INTEZER_API_KEY:
print(MESSAGES['missing_api_key'])
return
path = currentProgram.getExecutablePath()
program_name = currentProgram.getName()
creation_date = currentProgram.getCreationDate()
language_id = currentProgram.getLanguageID()
compiler_spec_id = currentProgram.getCompilerSpec().getCompilerSpecID()
if not path:
print(MESSAGES['file_not_exists'])
return
print(">>> Program Info:\n"
">>>\t%s:\n"
"\t%s_%s\n"
"\t(%s)\n"
"\t%s" % (
program_name, language_id, compiler_spec_id, creation_date, path))
try:
with open(path, 'rb') as fh:
sha256 = hashlib.sha256(fh.read()).hexdigest()
except Exception:
print(MESSAGES['file_not_exists'])
return
print(">>> file SHA : " + sha256)
print('>>> Start analyzing file...')
try:
helper = CodeIntelligenceHelper()
helper.create_function_map(sha256)
print(">>> Calling java script")
runScript("XMLParser.java")
print('>>> Done analyzing, loading data')
except Exception:
traceback.print_exc()
runner = IntezerAnalyzePlugin()
runner.run()