-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockwatchdog.py
338 lines (302 loc) · 10.8 KB
/
blockwatchdog.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
import argparse
import json
import logging
import time
import global_params
from contract import Contract
from graph.call_graph import CallGraph
from identifier import AttackIdentifier
if __name__ == "__main__":
# Main Body
parser = argparse.ArgumentParser()
parser.add_argument(
"-bp",
"--blockchain_platform",
help="The blockchain platform where the test contract is deployed",
action="store",
dest="platform",
type=str,
default="ETH",
)
parser.add_argument(
"-la",
"--logic_address",
help="Contract address for storing business logic",
action="store",
dest="logic_addr",
type=str,
)
parser.add_argument(
"-sa",
"--storage_address",
help="Contract address for storing business data",
action="store",
dest="storage_addr",
type=str,
default="",
)
parser.add_argument(
"-bn",
"--block_number",
help="Blockchain snapshot",
action="store",
dest="block_number",
type=int,
default=16000000,
)
parser.add_argument(
"-fs",
"--function_signature",
help="The function signature to be analyzed",
action="store",
dest="func_sign",
type=str,
default="",
)
parser.add_argument(
"-v", "--verbose", help="Verbose output, print everything.", action="store_true"
)
parser.add_argument(
"-cc",
"--complete_callflow",
help="Fetch storage type call arg value for complete call flow recovery.",
action="store_true",
)
args = parser.parse_args()
logging.basicConfig(
format="[%(levelname)s][%(filename)s:%(lineno)d]: %(message)s",
datefmt="%Y.%m.%d. %H:%M:%S",
)
rootLogger = logging.getLogger(None)
if args.verbose:
rootLogger.setLevel(level=logging.INFO)
else:
rootLogger.setLevel(level=logging.WARNING)
if args.complete_callflow:
global_params.CALLARG_STORAGETYPE = True
contracts = {}
# default: storage_addr is the same as logic_addr
storage_addr = ""
if args.storage_addr == "":
storage_addr = args.logic_addr
else:
storage_addr = args.storage_addr
logging.info("testing function signature {}...".format(args.func_sign))
source = {
"platform": args.platform,
"logic_addr": args.logic_addr,
"storage_addr": storage_addr,
"func_sign": "",
"block_number": args.block_number,
"caller": "msg.sender", # assume the attacker EOA
"caller_func_sign": "", # default trace all functions (with external calls)
"call_site": "", # default blank
"level": 0, # trace depth
"callArgVals": {},
}
begin = time.perf_counter()
call_paths = []
original_contract = Contract(
source["platform"],
source["logic_addr"],
source["storage_addr"],
source["func_sign"],
source["block_number"],
source["caller"],
source["call_site"],
source["level"],
source["callArgVals"],
)
func_sign_list = original_contract.get_func_sign_list()
if args.func_sign == "":
external_call_in_func_sigature = (
original_contract.get_external_call_in_func_sigature()
)
else:
external_call_in_func_sigature = [args.func_sign]
# store the signatures of functions that contain external calls
store_external_call_in_func_sigature_list = []
for i in external_call_in_func_sigature:
store_external_call_in_func_sigature_list.append(i)
visited_contracts = []
visited_funcs = []
# the max call depths of a contract
m_call_depth = 0
# if no runtime bin, just run __function_selector__ for the createbin
is_createbin = original_contract.is_createbin()
call_graph_str = ""
if is_createbin:
max_call_depth = 0
source = {
"platform": args.platform,
"logic_addr": args.logic_addr,
"storage_addr": storage_addr,
"func_sign": "",
"block_number": args.block_number,
"caller": "msg.sender",
"caller_func_sign": "",
"call_site": "",
"level": 0,
}
source["func_sign"] = "__function_selector__"
cross_contract_call_graph = CallGraph(source, contracts, source["platform"])
cross_contract_call_graph.construct_cross_contract_call_graph()
visited_contracts = (
visited_contracts + cross_contract_call_graph.visited_contracts
)
visited_funcs = visited_funcs + cross_contract_call_graph.visited_funcs
m_call_depth = cross_contract_call_graph.max_level
call_graph_str = cross_contract_call_graph.get_output()
call_paths.append(call_graph_str)
else:
# run all pub funs and remove function selector
if "__function_selector__" in external_call_in_func_sigature:
external_call_in_func_sigature.remove("__function_selector__")
# external_call_in_func_sigature = ["0xa1d48336"]
max_call_depth = 0
# for every functions that contains external calls
while len(external_call_in_func_sigature) > 0:
source = {
"platform": args.platform,
"logic_addr": args.logic_addr,
"storage_addr": storage_addr,
"func_sign": "",
"block_number": args.block_number,
"caller": "msg.sender",
"caller_func_sign": "",
"call_site": "",
"level": 0,
"callArgVals": {},
}
source["func_sign"] = external_call_in_func_sigature.pop(0)
cross_contract_call_graph = CallGraph(source, contracts, source["platform"])
cross_contract_call_graph.construct_cross_contract_call_graph()
visited_contracts = (
visited_contracts + cross_contract_call_graph.visited_contracts
)
visited_funcs = visited_funcs + cross_contract_call_graph.visited_funcs
call_depth = cross_contract_call_graph.max_level
if call_depth > max_call_depth:
max_call_depth = call_depth
call_graph_str = cross_contract_call_graph.get_output()
call_paths.append(call_graph_str)
m_call_depth = max_call_depth
detector = AttackIdentifier(
source["logic_addr"],
contracts,
func_sign_list,
store_external_call_in_func_sigature_list,
visited_contracts,
visited_funcs,
)
result = {
"is_attack": False,
"warning": "medium",
"attack_matrix": {},
"analysis_loc": "",
"platform": args.platform,
"block_number": args.block_number,
"time": None,
"semantic_features": {
"op_creation": {
"op_multicreate": False,
"op_solecreate": False,
},
"op_selfdestruct": False,
"op_env": False,
},
"external_call": {
"externalcall_inhook": False,
"externalcall_infallback": False,
},
"call_paths": [],
"visited_contracts": [],
"visited_contracts_num": 0,
"visited_funcs": [],
"visited_funcs_num": 0,
"max_call_depth": 0,
"contract_funcsigs": [],
"contract_funcsigs_external_call": [],
"sensitive_callsigs": [],
"overlap": {"has_overlap": False, "overlap_external_call": []},
"reentrancy_path_info": {},
}
result["is_attack"], result["attack_matrix"] = detector.detect()
result["call_paths"] = call_paths
result["max_call_depth"] = m_call_depth
result["visited_contracts"] = list(set(visited_contracts))
result["visited_contracts_num"] = len(list(set(visited_contracts)))
result["visited_funcs"] = list(set(visited_funcs))
result["visited_funcs_num"] = len(list(set(visited_funcs)))
result["semantic_features"]["op_creation"][
"op_multicreate"
] = detector.semantic_analysis.op_multicreate_analysis()
result["semantic_features"]["op_creation"][
"op_solecreate"
] = detector.semantic_analysis.op_solecreate_analysis()
result["semantic_features"][
"op_selfdestruct"
] = detector.semantic_analysis.op_selfdestruct_analysis()
result["semantic_features"][
"op_env"
] = detector.flow_analysis.tainted_env_call_arg()
result["external_call"][
"externalcall_inhook"
] = detector.semantic_analysis.externalcall_inhook()
result["external_call"][
"externalcall_infallback"
] = detector.semantic_analysis.externalcall_infallback()
sensitive_callsigs = detector.get_sig_info()
victim_callback_info, attack_reenter_info = detector.get_reen_info()
res = {}
for i in victim_callback_info.keys():
res[i] = {
"victim_call": victim_callback_info[i],
"reenter_call": attack_reenter_info[i],
}
result["reentrancy_path_info"] = res
result["sensitive_callsigs"] = list(set(sensitive_callsigs))
result["contract_funcsigs"] = func_sign_list
result["contract_funcsigs_external_call"] = (
store_external_call_in_func_sigature_list
)
# find whether there is a callback to the function that contains external call
overlap = list(
set(sensitive_callsigs).intersection(
set(store_external_call_in_func_sigature_list)
)
)
if len(overlap) > 0:
result["overlap"]["has_overlap"] = True
for i in overlap:
result["overlap"]["overlap_external_call"].append(i)
if (
result["semantic_features"]["op_creation"]["op_multicreate"]
or result["semantic_features"]["op_creation"]["op_solecreate"]
or result["semantic_features"]["op_selfdestruct"]
or result["semantic_features"]["op_env"]
):
result["warning"] = "high"
# just has overlap, lift the warning
if result["overlap"]["has_overlap"]:
result["warning"] = "high"
if result["external_call"]["externalcall_inhook"]:
result["warning"] = "high"
if is_createbin:
result["analysis_loc"] = "createbin"
else:
result["analysis_loc"] = "runtimebin"
end = time.perf_counter()
res = {}
res[args.logic_addr] = result
result["time"] = end - begin
print(res)
store_path = global_params.JSON_PATH + args.logic_addr + ".json"
# uncomment if not need to rewrite
# if not os.path.exists(store_path):
with open(store_path, "w", encoding="utf-8") as f:
f.write(json.dumps(res, indent=2, ensure_ascii=False))
# uncomment if need remove temp files
# for contract in list(set(visited_contracts)):
# if os.path.exists(TEMP_WORKING_DIR + contract):
# shutil.rmtree(TEMP_WORKING_DIR + contract)