-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_disasm.py
More file actions
260 lines (238 loc) · 10.9 KB
/
python_disasm.py
File metadata and controls
260 lines (238 loc) · 10.9 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
249
250
251
252
253
254
255
256
257
258
259
260
from binaryninja import InstructionTextToken, InstructionTextTokenType, InstructionInfo, BranchType
from binaryninjaui import UIContext
from dis import dis
from io import StringIO
class Disassembler:
def __init__(self):
self.bv = None
self.functions = {
"JUMP_FORWARD" : self.jump_forward,
"LOAD_GLOBAL": self.load_global,
"LOAD_CONST": self.load_const,
"LOAD_NAME": self.load_name,
"STORE_CONST": self.store_const,
"STORE_NAME": self.store_name,
"IMPORT_NAME": self.import_name,
"IMPORT_FROM": self.import_from,
"RETURN_VALUE": self.return_value,
"JUMP_IF_FALSE_OR_POP":self.jump_ifop,
"POP_JUMP_IF_FALSE":self.jump_pif,
"EXTENDED_ARG":self.extended_arg
}
def update_bv(self):
if self.bv == None:
ac = UIContext.activeContext()
cv = ac.getCurrentViewFrame()
if cv != None:
self.bv = cv.getCurrentBinaryView()
if self.bv != None:
return True
return False
return False
return True
def disassemble(self,data,addr):
# stupid hack to get arch to talk to bv
while not self.update_bv():
pass
disas = ""
disas = StringIO()
dis(data,file=disas)
disas.seek(0)
disas_string = disas.read()
if disas_string != "":
data_split = [x for x in disas_string.strip().split(" ") if x !=''][1:]
if len(data_split) > 1:
instr = data_split[0].strip()
arg = data_split[1]
if instr in self.functions.keys():
return self.functions[instr](arg,addr)
else:
return self.default_function(2,instr,arg,addr)
else:
instr = data_split[0]
if instr in self.functions.keys():
return self.functions[instr](addr)
else:
return self.default_function_one(instr,addr)
result = InstructionInfo()
result.length = len(data)
return None,None,result
def default_function(self,length,instr,arg,addr):
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,instr)]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken,arg))
result = InstructionInfo()
result.length = length
return tokens,length,result
def default_function_one(self,instr,addr):
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,instr)]
result = InstructionInfo()
result.length = 2
return tokens,2,result
def jump_forward(self,arg,addr):
instr = "JUMP FORWARD"
jump = addr + int(arg)
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,instr)]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
tokens.append(InstructionTextToken(InstructionTextTokenType.CodeRelativeAddressToken,hex(jump),jump,width=len(hex(jump))))
result = InstructionInfo()
result.length = 2
result.add_branch(BranchType.UnconditionalBranch,jump)
return tokens,2,result
def load_global(self,arg,addr):
instr = "LOAD GLOBAL"
global_reg = "global_"+arg
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,instr)]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
tokens.append(InstructionTextToken(InstructionTextTokenType.RegisterToken,global_reg))
result = InstructionInfo()
result.length = 2
return tokens,2,result
def load_const(self,arg,addr):
instr = "LOAD CONST"
const = int(arg)
const_value = self.bv.session_data['co_consts'][const][0]
if type(const_value) == int:
num = const_value
const_value = hex(const_value)
elif type(const_value) == bytes:
const_value = repr(const_value)
num = self.bv.session_data['co_consts'][const][1]
else:
const_value = str(const_value)
num = self.bv.session_data['co_consts'][const][1]
addr = self.bv.session_data['co_consts'][const][1]
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,instr)]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
tokens.append(InstructionTextToken(InstructionTextTokenType.IntegerToken,const_value,num))
tokens.append(InstructionTextToken(InstructionTextTokenType.DataSymbolToken," {"+hex(addr)+"}",addr,addr))
result = InstructionInfo()
result.length = 2
return tokens,2,result
def load_name(self,arg,addr):
instr = "LOAD NAME"
const = int(arg)
const_value = self.bv.session_data['co_names'][const][0]
if type(const_value) == int:
const_value = hex(const_value)
elif type(const_value) == bytes:
const_value == const_value.decode()
else:
const_value = str(const_value)
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,instr)]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
tokens.append(InstructionTextToken(InstructionTextTokenType.CharacterConstantToken,const_value,const))
result = InstructionInfo()
result.length = 2
return tokens,2,result
def store_const(self,arg,addr):
instr = "STORE CONST"
const = int(arg)
const_value = self.bv.session_data['co_consts'][const][0]
if type(const_value) == int:
const_value = hex(const_value)
elif type(const_value) == bytes:
const_value == const_value.decode()
else:
const_value = str(const_value)
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,instr)]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
tokens.append(InstructionTextToken(InstructionTextTokenType.IntegerToken,const_value,const))
result = InstructionInfo()
result.length = 2
return tokens,2,result
def store_name(self,arg,addr):
instr = "STORE NAME"
const = int(arg)
const_value = self.bv.session_data['co_names'][const][0]
if type(const_value) == int:
const_value = hex(const_value)
elif type(const_value) == bytes:
const_value == const_value.decode()
else:
const_value = str(const_value)
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,instr)]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
tokens.append(InstructionTextToken(InstructionTextTokenType.CharacterConstantToken,const_value,const))
result = InstructionInfo()
result.length = 2
return tokens,2,result
def import_name(self,arg,addr):
instr = "IMPORT NAME"
const = int(arg)
const_value = self.bv.session_data['co_names'][const][0]
if type(const_value) == int:
const_value = hex(const_value)
elif type(const_value) == bytes:
const_value == const_value.decode()
else:
const_value = str(const_value)
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,instr)]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
tokens.append(InstructionTextToken(InstructionTextTokenType.CharacterConstantToken,const_value,const))
result = InstructionInfo()
result.length = 2
return tokens,2,result
def import_from(self,arg,addr):
instr = "IMPORT FROM"
const = int(arg)
const_value = self.bv.session_data['co_names'][const][0]
if type(const_value) == int:
const_value = hex(const_value)
elif type(const_value) == bytes:
const_value == const_value.decode()
else:
const_value = str(const_value)
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,instr)]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
tokens.append(InstructionTextToken(InstructionTextTokenType.CharacterConstantToken,const_value,const))
result = InstructionInfo()
result.length = 2
return tokens,2,result
def return_value(self,addr):
instr = "RETURN VALUE"
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,instr)]
result = InstructionInfo()
result.length = 2
result.add_branch(BranchType.FunctionReturn)
return tokens,2,result
def jump_ifop(self,arg,addr):
instr = "JUMP IF FALSE OR POP"
# check if extended_arg is before it
extended = self.check_extended(addr)
jump = int(arg) + (extended<<8)+self.bv.entry_function.start
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,instr)]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
tokens.append(InstructionTextToken(InstructionTextTokenType.CodeRelativeAddressToken,hex(jump),jump,width=len(hex(jump))))
result = InstructionInfo()
result.length = 2
result.add_branch(BranchType.TrueBranch,addr+2)
result.add_branch(BranchType.FalseBranch,jump)
return tokens,2,result
def jump_pif(self,arg,addr):
instr = "POP JUMP IF FALSE"
extended = self.check_extended(addr)
jump = int(arg) + (extended<<8)+self.bv.entry_function.start
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,instr)]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
tokens.append(InstructionTextToken(InstructionTextTokenType.CodeRelativeAddressToken,hex(jump),jump,width=len(hex(jump))))
result = InstructionInfo()
result.length = 2
result.add_branch(BranchType.TrueBranch,addr+2)
result.add_branch(BranchType.FalseBranch,jump)
return tokens,2,result
def extended_arg(self,arg,addr):
instr = "EXTENDED ARG"
arg = int(arg)
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,instr)]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
tokens.append(InstructionTextToken(InstructionTextTokenType.IntegerToken,hex(arg),arg))
result = InstructionInfo()
result.length = 2
return tokens,2,result
def check_extended(self,addr):
prev_instr_bytes = self.bv.read(addr-2,2)
prev_instr = self.disassemble(prev_instr_bytes,addr-2)
if prev_instr[0][0].text == "EXTENDED ARG":
return prev_instr[0][-1].value
return 0