-
Notifications
You must be signed in to change notification settings - Fork 22
/
disassembler.py
162 lines (136 loc) · 4.48 KB
/
disassembler.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
from bytecodeblock import *
from ceptions import *
from opcodes import *
from bytecodes import *
from itertools import groupby
import sys, re
class Disassembler(object):
def __init__(self, binary):
if len(binary) == 0:
raise InputError("empty hex string")
binary += "00"
self.__detect_swarm_hash(binary)
self.raw_bytes = list()
for i in range(0, len(binary), 2):
try:
byte = int(binary[i:i + 2], 16)
except ValueError:
raise InputError("illegal hex character")
self.raw_bytes.append(byte)
self.bytecodes = dict()
# self.__call_addresses = set()
self.__decode_bytecodes()
self.__block_count = 0
self.__basic_blocks = dict()
self.__addresses = dict()
self.jump_dests = dict()
self.__create_basic_blocks()
self.__simplify_assertions()
def __detect_swarm_hash(self, binary):
binary_length = len(binary)
if binary_length % 2 != 0:
raise InputError("odd length binary")
# 0xa1 0x65 'b' 'z' 'z' 'r' '0'
swarm_pattern = re.compile("a165627a7a7230")
match = re.search(swarm_pattern, binary)
if not match:
self.swarm_hash_address = binary_length / 2
else:
self.swarm_hash_address = int(match.start() / 2)
def __decode_data(self, begin, end):
data = self.raw_bytes[begin: end]
data = [hex(d)[2:] for d in data]
data = [d.zfill(2) for d in data]
data = "".join(data)
try:
return int(data, 16)
except ValueError:
return 0
def __decode_bytecodes(self):
address = 0
while address < self.swarm_hash_address:
raw_byte = self.raw_bytes[address]
if raw_byte in opcodes:
opcode = opcodes[raw_byte]
else:
opcode = "GARBAGE"
bytecode = self.decode_bytecode(opcode, address, raw_byte)
self.bytecodes[address] = bytecode
if opcode in push_ops:
gamma = actions[opcode][-1]
data = self.__decode_data(address + 1, address + gamma + 1)
bytecode.set_dependency(0, data)
address += gamma
address += 1
@staticmethod
def decode_bytecode(opcode, address, raw_byte):
if opcode in push_ops:
bytecode = PushByteCode(opcode, raw_byte, address)
elif opcode in bin_ops:
bytecode = BinOpByteCode(opcode, raw_byte, address)
elif opcode in mono_ops:
bytecode = MonoOpByteCode(opcode, raw_byte, address)
else:
bytecode = ByteCode(opcode, raw_byte, address)
return bytecode
def __create_basic_blocks(self):
header_addresses, split = set(), False
for address in sorted(self.bytecodes):
bytecode = self.bytecodes[address]
if bytecode.is_jumpdest() or split:
header_addresses.add(address)
split = False
if bytecode.opcode in exit_ops \
or bytecode.opcode in jump_ops:
split = True
basic_block = BytecodeBlock(self.__block_count)
for address in sorted(self.bytecodes):
bytecode = self.bytecodes[address]
if address in header_addresses and address != 0:
self.__basic_blocks[basic_block.get_id()] = basic_block
self.__block_count += 1
basic_block = BytecodeBlock(self.__block_count)
if bytecode.is_jumpdest():
self.jump_dests[address] = basic_block.get_id()
basic_block.append(bytecode)
self.__addresses[address] = basic_block.get_id()
self.__basic_blocks[basic_block.get_id()] = basic_block
def __simplify_assertions(self):
block_ids = sorted(self.__basic_blocks.keys())
for i in range(len(block_ids) - 1):
id_0, id_1 = block_ids[i:i+2]
block_0 = self.__basic_blocks[id_0]
block_1 = self.__basic_blocks[id_1]
address = block_0.get_jumpi_address()
if address is not None and block_1.is_abort_block():
block_0.insert_assert()
def debug_bytecodes(self):
for block_id in self.__basic_blocks:
basic_block = self.__basic_blocks[block_id]
basic_block.debug_block()
def get_raw_bytes(self, b=0, e=-1):
if e == -1:
return self.raw_bytes[b::]
return self.raw_bytes[b:e]
def get_swarm_hash_bytes(self):
return self.raw_bytes[self.swarm_hash_address:]
def get_blocks(self):
return self.__basic_blocks
def get_opcode_bytes(self):
opcode_bytes = list()
for address in sorted(self.bytecodes):
instruction = self.bytecodes[address]
opcode_byte = instruction.raw_byte
opcode_byte = hex(opcode_byte)[2:].zfill(2)
opcode_bytes.append(opcode_byte)
opcode_bytes = "".join(opcode_bytes)
return opcode_bytes
def get_block_trace(self, program_counters):
block_trace = [self.__addresses[pc] for pc in program_counters]
block_trace = [x[0] for x in groupby(block_trace)]
return block_trace
if __name__ == "__main__":
with open(sys.argv[1]) as f:
line = f.readline().strip()
dis = Disassembler(line)
dis.debug_bytecodes()