-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_view.py
More file actions
218 lines (200 loc) · 10.3 KB
/
python_view.py
File metadata and controls
218 lines (200 loc) · 10.3 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
from binaryninja import Architecture, BinaryView,SegmentFlag, \
Symbol,SymbolType,SectionSemantics,StructureBuilder, Type, \
DataRenderer, InstructionTextToken, InstructionTextTokenType,DisassemblyTextLine
from enum import Enum
import marshal
class TypeCode(Enum):
INT = 0xe9
SMALL_TUPLE = 0x29
CODE = 0xe3
INT2 = 0x69
STRING = 0xf3
SHORT_ASCII = 0x7a
SHORT_ASCII2 = 0xfa
NULL = 0x4e
SHORT_ASCII_INTERNED = 0x5a
REF = 0x72
SHORT_ASCII_INTERNED2 = 0xda
class PythonView(BinaryView):
name = "python bytecode"
long_name = "python bytecode loader"
def __init__(self,data):
BinaryView.__init__(self,file_metadata=data.file,parent_view=data)
self.data = data
self.br = data.reader()
self.m_data = marshal.loads(self.data.read(0x10,len(self.data)))
# self.session_data['co_names'] = self.m_data.co_names
# self.session_data['co_consts'] = self.m_data.co_consts
self.session_data['co_names'] = []
self.session_data['co_consts'] = []
@classmethod
def is_valid_for_data(cls,data):
magic_number = data.read(0,4)
if magic_number == b"U\r\r\n":
return True
return False
def init(self):
self.platform = Architecture['python_bytecode'].standalone_platform
self.arch = Architecture['python_bytecode']
self.entry_addr = 0x2e
self.add_auto_segment(0,0x10,0,0x10,SegmentFlag.SegmentReadable)
self.add_auto_section("header",0,0x10,SectionSemantics.ReadOnlyDataSectionSemantics)
self.add_auto_segment(0x10,len(self.data),0x10,len(self.data),SegmentFlag.SegmentContainsCode)
self.add_auto_section("code",0x10,len(self.data),SectionSemantics.ReadOnlyCodeSectionSemantics)
self._load_header()
self.add_entry_point(self.entry_addr)
# self.add_function(self.entry_addr)
# self.define_auto_symbol(Symbol(SymbolType.FunctionSymbol,self.entry_addr,"start"))
return True
def _load_header(self):
with StructureBuilder.builder(self,"header_t") as header_t:
header_t.packed = True
header_t.append(Type.array(Type.char(),4),"magic")
header_t.append(Type.array(Type.char(),4),"timestamp")
header_t.append(Type.array(Type.char(),8),"idk")
header_t_struct = Type.structure_type(header_t)
self.define_data_var(0,header_t_struct)
self.br.read(16)
with StructureBuilder.builder(self,"func_info") as func_info:
func_info.packed = True
func_info.append(Type.int(1),"type")
func_info.append(Type.int(4),"co_argcount")
func_info.append(Type.int(4),"co_kwonlyargcount")
func_info.append(Type.int(4),"co_nlocals")
func_info.append(Type.int(4),"co_stacksize")
func_info.append(Type.int(4),"idk2")
func_info.append(Type.int(4),"co_flags")
func_info.append(Type.int(1),"indetifier")
func_info.append(Type.int(4),"length")
func_info_struct = Type.structure_type(func_info)
with StructureBuilder.builder(self,"int_info") as int_info:
int_info.packed = True
int_info.append(Type.int(1),"type")
int_info.append(Type.int(4),"int")
int_info_struct = Type.structure_type(int_info)
while self.br.offset < len(self.data):
# for _ in range(1):
_type = self.br.read8()
if _type == TypeCode.CODE.value:
self.define_data_var(self.br.offset-1,func_info_struct)
self.br.seek(self.br.offset+25)
length = self.br.read32()
self.add_function(self.br.offset)
self.define_auto_symbol(Symbol(SymbolType.FunctionSymbol,self.br.offset,f"sub_{hex(self.br.offset)[2:]}"))
self.br.read(length+2)
elif _type == TypeCode.SMALL_TUPLE.value:
#small tuple
self.session_data['co_consts'].append(('?',self.br.offset))
self.br.read(1)
pass
elif _type == TypeCode.INT.value or _type == TypeCode.INT2.value:
# int
self.define_data_var(self.br.offset-1,"int_info")
val = self.br.read32()
self.session_data['co_consts'].append((val,self.br.offset-4))
# elif :
# # int
# self.define_data_var(self.br.offset-1,"int_info")
# self.br.read(4)
elif _type == TypeCode.REF.value:
# int
self.define_data_var(self.br.offset-1,int_info_struct)
self.br.read(4)
elif _type == TypeCode.SHORT_ASCII.value or _type == TypeCode.SHORT_ASCII2.value:
# strings basically
length = self.br.read8()
with StructureBuilder.builder(self,f"str_info_{hex(self.br.offset)[2:]}") as str_info:
str_info.packed = True
str_info.append(Type.int(1),"type")
str_info.append(Type.int(1),"length")
str_info.append(Type.array(Type.char(),length),"string")
str_info_struct = Type.structure_type(str_info)
self.define_data_var(self.br.offset-2,f"str_info_{hex(self.br.offset)[2:]}")
_string = self.br.read(length)
self.session_data['co_consts'].append((_string,self.br.offset-length))
elif _type == TypeCode.STRING.value:
# strings basically
length = self.br.read32()
with StructureBuilder.builder(self,f"string_info_{hex(self.br.offset)[2:]}") as str_info:
str_info.packed = True
str_info.append(Type.int(1),"type")
str_info.append(Type.int(4),"length")
str_info.append(Type.array(Type.char(),length),"string")
str_info_struct = Type.structure_type(str_info)
self.define_data_var(self.br.offset-5,f"string_info_{hex(self.br.offset)[2:]}")
_string = self.br.read(length)
self.session_data['co_consts'].append((_string,self.br.offset-length))
elif _type == TypeCode.SHORT_ASCII_INTERNED.value or _type == TypeCode.SHORT_ASCII_INTERNED2.value :
# strings basically
length = self.br.read8()
with StructureBuilder.builder(self,f"sa_info_{hex(self.br.offset)[2:]}") as str_info:
str_info.packed = True
str_info.append(Type.int(1),"type")
str_info.append(Type.int(1),"length")
str_info.append(Type.array(Type.char(),length),"string")
str_info_struct = Type.structure_type(str_info)
self.define_data_var(self.br.offset-2,f"sa_info_{hex(self.br.offset)[2:]}")
name = self.br.read(length)
self.session_data['co_names'].append((name,self.br.offset-length))
elif _type == TypeCode.NULL.value:
# self.define_data_var(self.br.offset-1,int_info_struct)
self.session_data['co_consts'].append(("None",self.br.offset-1))
# self.br.seek(0)
class ConstRenderer(DataRenderer):
def perform_is_valid_for_data(self,ctxt,view,addr,type,context):
return DataRenderer.is_type_of_struct_name(type, "int_info", context)
def perform_get_lines_for_data(self,ctxt,view,addr,type,prefix,width,context):
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,"CONST:")]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
_int = view.get_data_var_at(addr).value['int']
tokens.append(InstructionTextToken(InstructionTextTokenType.IntegerToken,hex(_int),_int))
return [DisassemblyTextLine(tokens,addr)]
ConstRenderer().register_type_specific()
class StrRenderer(DataRenderer):
def perform_is_valid_for_data(self,ctxt,view,addr,type,context):
try:
dv = view.get_data_var_at(addr)
if dv != None:
return str(dv.type.name).startswith("str_info")
except:
pass
return False
def perform_get_lines_for_data(self,ctxt,view,addr,type,prefix,width,context):
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,"STRING:")]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
_string = view.get_data_var_at(addr).value['string']
tokens.append(InstructionTextToken(InstructionTextTokenType.CharacterConstantToken,repr(_string),addr))
return [DisassemblyTextLine(tokens,addr)]
StrRenderer().register_type_specific()
class StringRenderer(DataRenderer):
def perform_is_valid_for_data(self,ctxt,view,addr,type,context):
try:
dv = view.get_data_var_at(addr)
if dv != None:
return str(dv.type.name).startswith("string_info")
except:
pass
return False
def perform_get_lines_for_data(self,ctxt,view,addr,type,prefix,width,context):
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,"L STRING:")]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
_string = view.get_data_var_at(addr).value['string']
tokens.append(InstructionTextToken(InstructionTextTokenType.CharacterConstantToken,repr(_string),addr))
return [DisassemblyTextLine(tokens,addr)]
StringRenderer().register_type_specific()
class SARenderer(DataRenderer):
def perform_is_valid_for_data(self,ctxt,view,addr,type,context):
try:
dv = view.get_data_var_at(addr)
if dv != None:
return str(dv.type.name).startswith("sa_info")
except:
pass
return False
def perform_get_lines_for_data(self,ctxt,view,addr,type,prefix,width,context):
tokens = [InstructionTextToken(InstructionTextTokenType.TextToken,"SHORT ASCII:")]
tokens.append(InstructionTextToken(InstructionTextTokenType.TextToken," "))
_string = view.get_data_var_at(addr).value['string']
tokens.append(InstructionTextToken(InstructionTextTokenType.CharacterConstantToken,repr(_string),addr))
return [DisassemblyTextLine(tokens,addr)]
SARenderer().register_type_specific()