-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
89 lines (78 loc) · 2.61 KB
/
main.py
File metadata and controls
89 lines (78 loc) · 2.61 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
import argparse
from src import (
Disassembler,
build_cfg_from_hex,
minimize_cfg,
AbstractSemantics,
AbstractState,
check_reentrancy,
WeakestPrecondition,
Predicate
)
def example_simple_loop():
"""
0x00: PUSH1 0x02
0x02: JUMPDEST
0x03: PUSH0
0x04: SWAP1
0x05: DUP1
0x06: JUMP
"""
print("=" * 60)
print("Example 1: Loop")
print("=" * 60)
bytecode = "60025b5f908056"
bytecode = bytecode.replace(" ", "")
print(f"\nBytecode: 0x{bytecode}")
print("\nDisassembly:")
print("-" * 40)
disasm = Disassembler.from_hex(bytecode)
disasm.disassemble()
disasm.print_disassembly()
cfg = build_cfg_from_hex(bytecode)
cfg.print_cfg()
print("Dot format CFG:")
print("-" * 40)
print(cfg.to_dot())
def analyze_bytecode(bytecode: str, include_reentrancy: bool = False):
bytecode = bytecode.replace(" ", "")
disasm = Disassembler.from_hex(bytecode)
disasm.disassemble()
disasm.print_disassembly()
cfg = build_cfg_from_hex(bytecode)
cfg.print_cfg()
minimizer = minimize_cfg(cfg)
minimizer.print_minimized_cfg()
print(minimizer.get_minimized_dot())
if include_reentrancy:
print("\n" + "-" * 60)
print("\nReentrancy Analysis:")
print("-" * 60)
result = check_reentrancy(bytecode)
result.print_calls()
result.print_state_changes()
result.print_summary()
return result
return None
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="EVM Bytecode Analyzer",
formatter_class=argparse.RawTextHelpFormatter,
epilog="""
Examples:
python main.py 0x60025b5f908056
python main.py -r 0x60015415601057600080fd5b600160015560006000600060006000335af16000600055600060015500
"""
)
parser.add_argument('bytecode', nargs='?', help='EVM bytecode in hex format (e.g., 0x60025b5f908056)')
parser.add_argument('-r', '--reentrancy', action='store_true', help='Perform reentrancy analysis')
args = parser.parse_args()
# with reentrancy guard: 60015415601057600080fd5b600160015560006000600060006000335af16000600055600060015500
# with delegatecall vuln: 600060006000600073deadbeefdeadbeefdeadbeefdeadbeefdeadbeef5af450600160005500
# branchinf vuln: 60006000600060006000335af160205760016000556025565b6000505b00
# safe withdraw: 6000546000600055600060006000600084335af100
# vuln wihdraw: 600054600060006000600082335af150600060005500
if args.bytecode:
analyze_bytecode(args.bytecode, include_reentrancy=args.reentrancy)
else:
example_simple_loop()