-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanalyze.py
377 lines (341 loc) · 16.1 KB
/
analyze.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
import functools
import os
import sys
import traceback
import uuid
from threading import Thread
from typing import List, Tuple, Dict
from cprinter import CPrinter
from fprinter import FPrinter
from smsymer import utils, Printer
from smsymer.analyzer import Analyzer
from smsymer.analyzer.exception import AnalyzerException
from smsymer.cfg import CFG
from smsymer.evm import ByteCode
class TimeoutException(Exception):
pass
def timeout(timeout):
def deco(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
res = [TimeoutException('function [%s] timeout [%s seconds] exceeded!' % (func.__name__, timeout))]
def newFunc():
try:
res[0] = func(*args, **kwargs)
except Exception as e:
res[0] = e
t = Thread(target=newFunc)
t.daemon = True
try:
t.start()
t.join(timeout)
except Exception as je:
print('error starting thread')
raise je
ret = res[0]
if isinstance(ret, BaseException):
raise ret
return ret
return wrapper
return deco
class CfgReport(object):
def __init__(self):
self.n_timestamp_dependency = 0
self.n_unchecked_call = 0
self.n_reentrancy = 0
class ContractReport(object):
def __init__(self):
self.cfg_reports: List[CfgReport] = []
self.success = False
def add(self, cfg_r: CfgReport):
self.cfg_reports.append(cfg_r)
self.success = True
class FileReport(object):
def __init__(self):
self.c_reports: List[ContractReport] = []
self.success = False
def add(self, c_r: ContractReport):
self.c_reports.append(c_r)
self.success = True
class Report(object):
def __init__(self):
self.f_reports: List[FileReport] = []
def add(self, f_r: FileReport):
self.f_reports.append(f_r)
def process(args):
c_printer = CPrinter()
result = {}
if args.result is not None:
if not os.path.exists(args.result):
c_printer.error("Result output directory '{0}' does not exist".format(args.result))
sys.exit(-1)
if not os.path.isdir(args.result):
c_printer.error("'{0}' is not a directory".format(args.result))
sys.exit(-1)
if args.file:
if args.source and args.extension is None:
args.extension = 'sol'
elif args.bytecode and args.extension is None:
args.extension = 'hex'
c_printer.info("processing files with extension '{0}'".format(args.extension))
for s in args.input:
f_r = FileReport()
if os.path.splitext(s)[-1][1:] != args.extension:
c_printer.warn("file '{0}' extension mismatch".format(s))
c_printer.warn("skipping '{0}'".format(s))
continue
if not os.path.exists(s):
c_printer.error("file '{0}' does not exist".format(s))
c_printer.warn("skipping '{0}'".format(s))
elif not os.path.isfile(s):
c_printer.error("'{0}' is not a file".format(s))
c_printer.warn("skipping '{0}'".format(s))
else:
with open(s) as file:
c_printer.info("start analyzing {0}".format(s))
try:
if args.source:
bytecodes = utils.compile_sol(s, args.t_runtime)
else:
bytecodes = ''.join(file.readlines())
for contract_name, bytecode in bytecodes:
if args.result is not None:
filename = os.path.splitext(s)[0] + ".txt"
f_printer = FPrinter(filename=os.path.join(args.result, filename))
c_r = analyze(bytecode, contract_name, f_printer)
else:
c_r = analyze(bytecode, contract_name, c_printer)
f_r.add(c_r)
except AttributeError as e:
c_printer.error(str(e))
c_printer.warn("fail to analyze {0}".format(s))
except AnalyzerException as e:
c_printer.warn("Unsupported feature: {}".format(e))
c_printer.warn("fail to analyze {0}".format(s))
except TimeoutException:
c_printer.warn("Analyze timeout")
c_printer.warn("fail to analyze {0}".format(s))
except Exception as e:
c_printer.error("SmSymer internal Error: {}".format(e))
c_printer.warn("fail to analyze {0}".format(s))
else:
c_printer.info("finish analyzing {0}".format(s))
result[s] = f_r
elif args.dir:
if args.source and args.extension is None:
args.extension = 'sol'
elif args.bytecode and args.extension is None:
args.extension = 'hex'
c_printer.info("processing files with extension '{0}'".format(args.extension))
for s in args.input:
if not os.path.exists(s):
c_printer.error("directory '{0}' does not exist")
c_printer.warn("skipping '{0}'")
elif not os.path.isdir(s):
c_printer.error("'{0}' is not a directory")
c_printer.warn("skipping '{0}'")
else:
f_r = process_dir(s, args)
result.update(f_r)
else:
for i, s in enumerate(args.input):
bytecode = s
c_printer.info("start analyzing {0}".format(s))
f_r = FileReport()
try:
if args.result is not None:
filename = str(i) + ".txt"
f_printer = FPrinter(filename=os.path.join(args.result, filename))
c_r = analyze(bytecode, '', f_printer)
else:
c_r = analyze(bytecode, '', c_printer)
f_r.add(c_r)
except AttributeError as e:
c_printer.error(str(e))
c_printer.warn("fail to analyze {0}".format(s))
except AnalyzerException as e:
c_printer.warn("Unsupported feature: {}".format(e))
c_printer.warn("fail to analyze {0}".format(s))
except TimeoutException:
c_printer.warn("Analyze timeout")
c_printer.warn("fail to analyze {0}".format(s))
except Exception as e:
c_printer.error("SmSymer internal Error: {}".format(e))
c_printer.warn("fail to analyze {0}".format(s))
else:
c_printer.info("finish analyzing {0}".format(s))
result[i] = f_r
# t_result_file = "/home/troublor/Desktop/result/t_contracts"
# t_r_printer = FPrinter(t_result_file)
# u_result_file = "/home/troublor/Desktop/result/u_contracts"
# u_r_printer = FPrinter(u_result_file)
# r_result_file = "/home/troublor/Desktop/result/r_contracts"
# r_r_printer = FPrinter(r_result_file)
c_printer.info("***********************************")
f_total = 0
f_success = 0
c_total = 0
c_success = 0
n_td = 0
n_td_f = 0
n_uc = 0
n_r = 0
for filename, f_r in result.items():
f_total += 1
if f_r.success:
f_success += 1
has_timestamp = False
for c_r in f_r.c_reports:
c_total += 1
if c_r.success:
c_success += 1
for cfg_r in c_r.cfg_reports:
if cfg_r.n_timestamp_dependency > 0:
n_td += 1
has_timestamp = True
# t_r_printer.print(filename)
break
for cfg_r in c_r.cfg_reports:
if cfg_r.n_unchecked_call > 0:
n_uc += 1
# u_r_printer.print(filename)
break
for cfg_r in c_r.cfg_reports:
if cfg_r.n_reentrancy > 0:
n_r += 1
# r_r_printer.print(filename)
break
if has_timestamp:
n_td_f += 1
c_printer.info("SmSymer analyzed {0} files".format(f_total))
c_printer.info("{0} success files, containing {1} contracts".format(f_success, c_total))
c_printer.info("{0} success analyzed contracts".format(c_success))
c_printer.info("{0} contracts contains Timestamp Dependency Vulnerability".format(n_td))
c_printer.info("{0} contracts contains Unchecked Call Vulnerability".format(n_uc))
c_printer.info("{0} contracts contains Reentrancy Vulnerability".format(n_r))
c_printer.info("{0} files contains Timestamp Dependency Vulnerability".format(n_td_f))
# result_file = "C:\\Users\\troub\\Desktop\\result\\" + os.path.split(args.input[0])[-1]
#
# result_printer = FPrinter(result_file)
# result_printer.info("{0} files".format(f_total))
# result_printer.info("{0} success analyzed files".format(f_success))
# result_printer.info("{0} contracts".format(c_total))
# result_printer.info("{0} success analyzed contracts".format(c_success))
# result_printer.info("{0} contracts contains Timestamp Dependency Vulnerability".format(n_td))
# result_printer.info("{0} contracts contains Unchecked Call Vulnerability".format(n_uc))
# result_printer.info("{0} contracts contains Reentrancy Vulnerability".format(n_r))
def process_dir(directory: str, args) -> Dict[str, FileReport]:
result = {}
c_printer = CPrinter()
for item in os.listdir(directory):
f_r: FileReport = FileReport()
if os.path.isdir(item):
if args.recursively:
process_dir(item, args)
else:
c_printer.warn("not specify -R option, skipping subdirectory '{}'".format(item))
else:
if os.path.splitext(item)[-1][1:] != args.extension:
c_printer.warn("file '{0}' extension mismatch".format(item))
c_printer.warn("skipping '{0}'".format(item))
continue
with open(os.path.join(directory, item)) as file:
c_printer.info("start analyzing {0}".format(os.path.join(directory, item)))
try:
if args.source:
bytecodes = utils.compile_sol(os.path.join(directory, item), args.t_runtime)
else:
bytecodes = ''.join(file.readlines())
for contract_name, bytecode in bytecodes:
if args.result is not None:
filename = os.path.join(args.result, os.path.split(directory)[0], item + ".txt")
f_printer = FPrinter(filename=filename)
c_r = analyze(bytecode, contract_name, f_printer)
f_r.add(c_r)
else:
c_r = analyze(bytecode, contract_name, c_printer)
f_r.add(c_r)
except AttributeError as e:
c_printer.error(str(e))
traceback.print_exc()
c_printer.warn("fail to analyze {0}".format(os.path.join(directory, item)))
except AnalyzerException as e:
c_printer.warn("Unsupported feature: {}".format(e))
c_printer.warn("fail to analyze {0}".format(os.path.join(directory, item)))
except TimeoutException:
c_printer.warn("Analyze timeout")
c_printer.warn("fail to analyze {0}".format(os.path.join(directory, item)))
except Exception as e:
c_printer.error("SmSymer internal Error: {}".format(e))
traceback.print_exc()
c_printer.warn("fail to analyze {0}".format(os.path.join(directory, item)))
else:
c_printer.info("finish analyzing {0}".format(os.path.join(directory, item)))
result[os.path.join(directory, item)] = f_r
return result
# @timeout(300)
def analyze(bytecode: str, contract_name, result_printer: Printer, verbose=False) -> ContractReport:
result_printer.info("***************************************************")
result_printer.info("Analyzing contract: {0}".format(contract_name))
result = ContractReport()
c_printer = CPrinter()
instructions = ByteCode.disasm(bytecode, c_printer)
result_printer.info("Symbolically executing")
analyzer = Analyzer(instructions, result_printer, verbose)
# analyze construction code
result_printer.info("Checking construction assemble code")
cfg_r = analyze_cfg(analyzer.construct_cfg, result_printer)
result.add(cfg_r)
result_printer.info("Checking construction assemble code...done")
result_printer.info("- - - - - - - - - - - - - - - - - - - - - - - - - -")
# analyze body code
result_printer.info("Checking runtime assemble code")
cfg_r = analyze_cfg(analyzer.body_cfg, result_printer, verbose)
result.add(cfg_r)
result_printer.info("Checking runtime assemble code...done")
result_printer.info("***************************************************")
return result
def analyze_cfg(cfg: CFG, result_printer: Printer, verbose=False) -> CfgReport:
result = CfgReport()
if verbose:
result_printer.info("assemble code begin")
for ins in cfg.instructions:
result_printer.print(str(ins))
result_printer.info("assemble code end")
timestamp_dependency_report = cfg.check_timestamp_dependency()
if timestamp_dependency_report["vulnerable"]:
result_printer.warn("found timestamp dependency")
result_printer.warn("---------------------------------------------------")
result.n_timestamp_dependency = len(timestamp_dependency_report["spots"])
for index, report in enumerate(timestamp_dependency_report["spots"]):
result_printer.warn("\t TIMESTAMP DEPENDENCY {0}".format(index))
result_printer.warn("\t \t timestamp introduced at {0}".format(
cfg.get_instruction(report["timestamp_address"])))
result_printer.warn("\t \t used in path condition at {0}".format(
cfg.get_instruction(report["dependency_address"])))
result_printer.warn("---------------------------------------------------")
uncheck_call_report = cfg.check_unchecked_call()
if uncheck_call_report["vulnerable"]:
result_printer.warn("found unchecked call")
result_printer.warn("---------------------------------------------------")
result.n_unchecked_call = len(uncheck_call_report["spots"])
for index, report in enumerate(uncheck_call_report["spots"]):
result_printer.warn("\t UNCHECKED CALL {0}".format(index))
result_printer.warn("\t \t unchecked call at {0}".format(
cfg.get_instruction(report["call_address"])))
result_printer.warn("---------------------------------------------------")
reentrancy_report = cfg.check_reentrancy()
if reentrancy_report["vulnerable"]:
result_printer.warn("found reentrancy vulnerability")
result_printer.warn("---------------------------------------------------")
result.n_reentrancy = len(reentrancy_report["spots"])
for index, report in enumerate(reentrancy_report["spots"]):
result_printer.warn("\t REENTRANCY {0}".format(index))
result_printer.warn("\t \t reentrancy call at {0}".format(
cfg.get_instruction(report["call_address"])))
if len(report["storage_addresses"]) > 0:
result_printer.warn("\t \t possible guard storage variable")
for addr in report["storage_addresses"]:
result_printer.warn("\t \t \tstorage variable loaded at {0}".format(addr))
result_printer.warn("---------------------------------------------------")
return result