-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
167 lines (117 loc) · 3.78 KB
/
utils.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
from binaryninja import BackgroundTaskThread
from binaryninja.enums import Endianness
import struct
from .pefile import *
class RunInBackground(BackgroundTaskThread):
def __init__(self, msg, func, *args, **kwargs):
BackgroundTaskThread.__init__(self, msg, True)
self.func = func
self.args = args
self.kwargs = kwargs
def run(self):
self.func(self, *self.args, **self.kwargs)
def read_cstring(view, address):
buf = bytearray()
while True:
data = view.read(address, 1)
address += 1
if not data:
break
value = ord(data)
if not value:
break
buf.append(value)
return buf, address
def align_up(value, align):
remainder = value % align
if remainder:
value += (align - remainder)
return value
def check_address(view, offset):
return view.start <= offset < view.end
def read_pe_header(view):
seg = view.get_segment_at(view.start)
return pefile.PE(data = view.read(seg.start, seg.data_length))
def get_endian_format(endian):
if endian == Endianness.LittleEndian:
return '<'
elif endian == Endianness.BigEndian:
return '>'
raise NotImplementedError()
def get_bool_format(width):
if width == 1:
return '?'
return get_int_format(width, False)
_int_formats = {
1: ['B', 'b'],
2: ['H', 'h'],
4: ['I', 'i'],
8: ['Q', 'q']
}
def get_int_format(width, signed):
if width in _int_formats:
return _int_formats[width][bool(signed)]
raise NotImplementedError()
_float_formats = {
4: 'f',
8: 'd'
}
def get_float_format(width):
if width in _float_formats:
return _float_formats[width]
raise NotImplementedError()
def get_pointer_format(width):
return get_int_format(width, False)
class BinjaStruct(object):
def __init__(self, fmt, names = None, single = False):
self._struct = struct.Struct(fmt)
self._names = names
self._single = single
@classmethod
def Pointer(cls, view):
return BinjaStruct('{0}{1}'.format(get_endian_format(view.endianness), get_pointer_format(view.address_size)), single = True)
@property
def struct(self):
return self._struct
@property
def names(self):
return self._names
@property
def single(self):
return self._single
@property
def size(self):
return self.struct.size
def read(self, view, address, align = 1):
address = align_up(address, align)
data = view.read(address, self.size)
result = None
if len(data) == self.size:
result = self.struct.unpack(data)
if self.names is not None:
if len(self.names) == len(result):
result = dict(zip(self.names, result))
else:
result = None
elif self.single:
if len(result) == 1:
result = result[0]
else:
result = None
return result, address + self.size
def read_array(self, view, address, count, align = 1):
results = [ ]
for i in range(count):
value, address = self.read(view, address, align)
results.append(value)
return results, address
def get_bits(value, offset, count):
return (value >> offset) & ((1 << count) - 1)
def split_bits(container, name, fields):
value = container[name]
for sub_name, offset, count in fields:
container[sub_name] = get_bits(value, offset, count)
del container[name]
def update_percentage(thread, start, end, current, message):
percentage = int(100.0 * (current - start) / (end -start))
thread.progress = '{0} - {1}%'.format(message, percentage)