This repository was archived by the owner on Jan 27, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy paththoth.py
More file actions
248 lines (224 loc) · 8.4 KB
/
Copy paththoth.py
File metadata and controls
248 lines (224 loc) · 8.4 KB
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
import os
import sys
import tempfile
from thoth.app.decompiler.decompiler import Decompiler
from thoth.app.dfg.dfg import DFG
from thoth.app.symbex.symbex import SymbolicExecution
from thoth.app.arguments import parse_args
from thoth.app.analyzer import all_analyzers
from thoth.app.analyzer.abstract_analyzer import category_classification_text
from thoth.app.disassembler.disassembler import Disassembler
from thoth.app.utils import load_symbex_yaml_config
from thoth.app.starknet.starknet import StarkNet
def main() -> int:
"""Main function of Thoth
Returns:
Int: Return 0
"""
args = parse_args()
# Show analyzers help
if args.analyzers_help is not None:
if args.analyzers_help:
for analyzer_name in args.analyzers_help:
analyzer = [
analyzer for analyzer in all_analyzers if analyzer.ARGUMENT == analyzer_name
][0]
analyzer._print_help()
return 0
else:
for analyzer in all_analyzers:
analyzer._print_help()
return 0
# Handle the --scarb flag
if args.scarb:
target_dir = "./target/dev"
file_pattern = "compiled_contract_class.json"
file = None
for root, _, files in os.walk(target_dir):
for f in files:
if f.endswith(file_pattern):
file = os.path.join(root, f)
break
if file:
break
if not file:
print('You need to run "scarb build" before using the --scarb flag')
return 1
filename = os.path.basename(file).split(".")[0]
else:
# Load compiled contract from a file
if args.contract == "local":
file = args.path.name
filename = os.path.basename(args.path.name).split(".")[0]
# Load compiled contract from starknet API
else:
try:
contract = StarkNet(args.network).get_full_contract(args.address)
except Exception as e:
print(e)
exit()
file = tempfile.NamedTemporaryFile().name
with open(file, "w") as f:
f.write(contract)
filename = args.address
disassembler = Disassembler(file, color=args.color)
if args.verbose:
disassembler.dump_json()
# Decompiler
if args.decompile and args.analyzers is None:
if disassembler.cairo1:
print(
"Decompiler isn't supported yet on Cairo bytecode generated with compiler >= 1.0.0"
)
else:
print(disassembler.decompiler())
if args.output:
output = Disassembler(file, color=False).decompiler()
with args.output as output_file:
output_file.write(output)
# Disassembler
elif args.disassembly and args.analyzers is None:
print(disassembler.print_disassembly())
if args.output:
output = Disassembler(file, color=False).print_disassembly()
with args.output as output_file:
output_file.write(output)
format = "pdf" if args.format is None else str(args.format)
# Print the call flow graph
if args.call:
disassembler.print_call_flow_graph(
folder=args.output_callgraph_folder,
filename=filename,
format=format,
view=args.view,
)
# Print the CFG
if args.cfg:
if args.color:
disassembler = Disassembler(file, color=False)
disassembler.print_cfg(
folder=args.output_cfg_folder,
filename=filename,
format=format,
function_name=args.function,
view=args.view,
)
# Print the DFG
if args.dfg:
contract_functions = disassembler.functions
decompiler = Decompiler(functions=contract_functions)
decompiler.decompile_code(first_pass_only=True, imported_functions=False)
dfg = DFG(decompiler.ssa.memory)
dfg._create_dfg()
if args.taint:
dfg._taint_functions_arguments()
dfg._create_graph_representation()
dfg._print_dfg(view=args.view, folder=args.output_dfg_folder, format=args.format)
# Symbolic execution
if args.symbolic:
# Load the symbolic execution parameters from a config file
if args.config:
try:
(
symbex_function,
symbex_constraints,
symbex_variables,
symbex_solves,
) = load_symbex_yaml_config(args.config).values()
except Exception:
return 1
# Load the symbolic execution parameters from the command line
else:
# Mandatory arguments (function, solves, constraints)
if args.function is None:
print(
"Symbolic execution: You need to set the -function flag e.g. -function __main__.main"
)
functions_list = [f.name for f in disassembler.functions]
print("\nPossible values:\n\t%s" % ("\n\t".join(functions_list)))
return 1
if not args.solves:
print("Symbolic execution: You need to set the -solve flag, e.g. -solves v1 v2 v3")
return 1
if not args.constraints and not args.assertions:
print(
"Symbolic execution: You need to set the -constraints flag e.g. - constraints v1==0 v2==0"
)
return 1
symbex_function = args.function
symbex_constraints = args.constraints
symbex_variables = args.variables
symbex_solves = args.solves
contract_functions = disassembler.functions
decompiler = Decompiler(functions=contract_functions)
decompiler.decompile_code(first_pass_only=True)
assertions = decompiler.assertions if args.assertions else []
symbex = SymbolicExecution(variables=decompiler.ssa.memory, assertions=assertions)
# Function parameter
try:
function = [f for f in disassembler.functions if f.name == symbex_function][0]
except:
print("Symbolic execution: Function %s doesn't exist" % symbex_function)
return 1
solve = symbex._solve(
function=function,
constraints=symbex_constraints,
variables_values=symbex_variables,
solves=symbex_solves,
)
if solve:
for variable in solve:
print("%s: %s" % (variable[0], variable[1]))
else:
print("No solution")
return 0
if args.analyzers is None:
return 0
# Find selected analyzers
analyzers_names = [analyzer.ARGUMENT for analyzer in all_analyzers]
selected_analyzers = []
if args.analyzers:
selected_analyzers = []
for analyzer_name in args.analyzers:
# Select a single analyzer
if analyzer_name in analyzers_names:
selected_analyzers.append(
[analyzer for analyzer in all_analyzers if analyzer.ARGUMENT == analyzer_name][
0
]
)
# Select a whole category
else:
selected_category = [
k
for k, v in category_classification_text.items()
if v == analyzer_name.capitalize()
][0]
selected_analyzers += [
analyzer for analyzer in all_analyzers if analyzer.CATEGORY == selected_category
]
# Select all analyzers by default
else:
selected_analyzers = all_analyzers
# Run analyzers
detected_analyzers_count = 0
for analyzer in selected_analyzers:
detected = False
a = analyzer(disassembler, color=args.color)
if a._detect():
detected = True
detected_analyzers_count += 1
a._print()
if detected:
print()
selected_analyzers_count = len(selected_analyzers)
print(
"[+] %s analyzer%s %s run (%s detected)"
% (
selected_analyzers_count,
"s" if selected_analyzers_count > 1 else "",
"were" if selected_analyzers_count > 1 else "was",
detected_analyzers_count,
)
)
return 0