-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_engine.py
146 lines (127 loc) · 5.68 KB
/
app_engine.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
import os
import time
import dependencies_builder
import constants
import mdcontracts
import docker
import exceptions
import logging
docker_client = docker.APIClient(base_url='unix://var/run/docker.sock')
logger = logging.getLogger(constants.LOGGER_NAME)
# TODO: continuar desarrollando la funcion intermedia que devuelve todas las ejecuciones que hay que realizar
def intermediate_exec(tool, cmd, version):
if version != '':
version = int(version.replace(".", ""))
if tool.get("name") == 'securify' and version <= 510:
return '', 0
if tool.get("name") == 'slither' and version <= 430:
if version <= 430:
cmd.format(version)
else:
return 'Unable to run analysis because solidity pragma code version.', 0
executions = [docker_client.exec_create(tool.get('container'), cmd=cmd, workdir=tool.get('workdir'),
privileged=True)]
if tool.get("name") == 'solmet':
executions.append(docker_client.exec_create(tool.get('container'), cmd='cat output.csv',
workdir=tool.get('workdir'),
privileged=True))
if tool.get("name") == 'ethir':
executions.append(docker_client.exec_create(tool.get('container'), cmd='cat rbr.rbr',
workdir='/tmp/costabs/',
privileged=True))
if tool.get("name") == 'madmax':
executions.append(docker_client.exec_create(tool.get('container'), cmd='cat graph.html',
workdir='/home/MadMax/',
privileged=True))
result = ''
for exe in executions:
time_start = time.perf_counter()
res_iter = docker_client.exec_start(exe, stream=True, demux=True)
time_end = time.perf_counter()
for line in res_iter:
if line[0]:
result += line[0].decode('utf-8')
if line[1]:
result += line[1].decode('utf-8')
result += '\n'
return result, (time_end - time_start)
def get_tool():
for tool in constants.TOOLS_PROPERTIES:
if tool.get('name') == constants.DEFAULT_TOOL:
ret = tool
return ret
def get_version(tool, path):
if tool.get('ext') == '.sol':
return dependencies_builder.get_version(path)
else:
return ''
def exec_start_gui(files, tools):
results = ""
for tool in tools:
if tool.get('ext') == '.sol':
file = files[0]
else:
file = files[1]
if file == 'nofile':
break
path = os.path.join(constants.DEFAULT_DIRECTORY + constants.DEFAULT_INPUT, file)
if os.path.isfile(path):
version = get_version(tool, path)
if tool.get('name') == 'mythril':
cmd = tool.get("cmd").format(version, file)
elif tool.get("name") == 'slither':
cmd = tool.get("cmd").format(constants.SOLC_SLITHER.format(version), file)
else:
cmd = tool.get("cmd").format(file)
result, time_elapsed = intermediate_exec(tool, cmd, version)
else:
result = "{} doesn't allow {} files format".format(tool.get('name'))
time_elapsed = 0
results += "Herramienta: {} Resultado: {} Tiempo ejecución: {}\n{}\n\n\n\n".format(tool.get('name'), result,
time_elapsed, constants.EOF_STRING)
return results
# Se corren todas las herramientas diferenciando por analisis
def exec_start(files, contract, tools):
for tool in tools:
if tool.get('ext') == '.sol':
file = files[0]
else:
file = files[1]
path = os.path.join(constants.DEFAULT_DIRECTORY + constants.DEFAULT_INPUT, file)
if os.path.isfile(path):
version = get_version(tool, path)
if tool.get('name') == 'mythril' :
cmd = tool.get("cmd").format(version, file)
elif tool.get("name") == 'slither':
cmd = tool.get("cmd").format(constants.SOLC_SLITHER.format(version), file)
else:
cmd = tool.get("cmd").format(file)
logger.debug("Running {} ...".format(tool.get('name')))
result, time_elapsed = intermediate_exec(tool, cmd, version)
logger.debug("Completed. Saving results...")
mdcontracts.insert_result(contract.get_id(), contract.get_address(), tool.get('name'), result, time_elapsed)
logger.debug("Completed.")
else:
mdcontracts.insert_result(contract.get_id(), contract.get_address(), tool.get('name'),
"Contract extension doesn't allow this analysis", '0')
def execute_command(files, contract):
if constants.DEFAULT_TOOL == 'all':
exec_start(files, contract, constants.TOOLS_PROPERTIES)
else:
custom_tools_properties = []
for tool in constants.TOOLS_PROPERTIES:
if tool in constants.DEFAULT_TOOL:
custom_tools_properties.append(tool)
exec_start(files, contract, custom_tools_properties)
def init():
containers_running = docker_client.containers(all=True)
final_name_list = list()
for c in containers_running:
name_list = c['Names']
if len(name_list) > 0:
final_name_list += name_list
for container in constants.CONTAINERS:
try:
docker_client.start(container)
except:
logger.WARNING("The awaiten docker {} doesn't exits".format(container))