-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcfstring.py
204 lines (158 loc) · 5.65 KB
/
cfstring.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
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
from functools import partial
from binaryninja import (BinaryView, DataRenderer, DisassemblyTextLine,
Endianness, InstructionTextToken,
InstructionTextTokenType, Type, log_debug)
def _get_from_bytes(view: BinaryView):
return partial(
int.from_bytes,
byteorder=(
"little" if view.endianness == Endianness.LittleEndian
else "big"
)
)
_cfstring_definition = '''
struct CFString
{
void* isa;
int32_t info;
void* buffer;
size_t length;
};
'''
_wchar_definition = 'typedef wchar16 wchar;'
def define_cfstrings_plugin(view: BinaryView):
log_debug("define_cfstrings_plugin")
from_bytes = _get_from_bytes(view)
cfstring_type = view.get_type_by_name('CFString')
if cfstring_type is None:
cfstring_type = view.platform.parse_types_from_source(
_cfstring_definition
).types['CFString']
view.define_user_type('CFString', cfstring_type)
wchar_type = view.platform.parse_types_from_source(
_wchar_definition
).types['wchar']
cfstring = Type.named_type_from_type('CFString', cfstring_type)
__cfstring = view.get_section_by_name('__cfstring')
if __cfstring is None:
return
buffer = cfstring_type.structure['buffer']
length = cfstring_type.structure['length']
for addr in range(__cfstring.start, __cfstring.end, cfstring_type.width):
view.define_user_data_var(addr, cfstring)
for xref in view.get_data_refs(addr):
view.define_user_data_var(xref, Type.pointer(view.arch, cfstring))
string_pointer = from_bytes(
view.read(addr + buffer.offset, buffer.type.width)
)
string_length = from_bytes(
view.read(addr + length.offset, length.type.width),
) + 1
string_section = view.get_sections_at(string_pointer)
if not string_section:
return
if string_section[0].name == '__ustring':
char_type = wchar_type
else:
char_type = Type.char()
view.define_user_data_var(
string_pointer,
Type.array(char_type, string_length)
)
_cfstring_allocator_properties = {
0: 'inline',
1: 'noinline,default',
2: "noinline,nofree",
3: 'noinline,custom'
}
class CFStringDataRenderer(DataRenderer):
def __init__(self):
DataRenderer.__init__(self)
def perform_is_valid_for_data(self, ctxt, view: BinaryView, addr: int, type_: Type, context):
return DataRenderer.is_type_of_struct_name(type_, "CFString", context)
def perform_get_lines_for_data(self, ctxt, view: BinaryView, addr: int, type_: Type, prefix: list, width: int, context):
from_bytes = _get_from_bytes(view)
symbol: Symbol = view.get_symbol_at(addr)
cfstring: Type = view.get_type_by_name('CFString')
if cfstring is None:
log_debug('CFString is not defined; how did we even get here?')
return [DisassemblyTextLine(prefix, addr)]
cfstring: Structure = cfstring.structure
buffer = from_bytes(
view.read(addr + cfstring['buffer'].offset, view.address_size)
)
info = from_bytes(
view.read(
addr + cfstring['info'].offset,
cfstring['info'].type.width)
)
length = from_bytes(
view.read(
addr + cfstring['length'].offset,
cfstring['length'].type.width
)
)
if info & 0xff == 0xc8:
info_string = 'noinline,default,nofree,NI'
elif info & 0xff == 0xd0:
info_string = 'noinline,default,nofree,EUI'
else:
info_string = (
f'{_cfstring_allocator_properties[(info >> 5) & 0x3]},'
f'{"U" if info & 16 else ""}'
f'{"N" if info & 8 else ""}'
f'{"L" if info & 4 else ""}'
f'{"I" if info & 1 else ""}'
)
if 'U' not in info_string:
string = view.get_ascii_string_at(buffer, 0)
if string is None:
log_debug('string returned None; how did we even get here?')
return [DisassemblyTextLine(prefix, addr)]
string = string.value
else:
string = view.read(buffer, length * 2)
if symbol is None:
name = f'data_{addr:x}'
else:
name = symbol.short_name
prefix = [
InstructionTextToken(
InstructionTextTokenType.TypeNameToken,
'CFString'
),
InstructionTextToken(
InstructionTextTokenType.TextToken,
' '
),
InstructionTextToken(
InstructionTextTokenType.AnnotationToken,
f'{{{info_string}}}'
),
InstructionTextToken(
InstructionTextTokenType.TextToken,
' '
),
InstructionTextToken(
InstructionTextTokenType.DataSymbolToken,
name,
addr
),
InstructionTextToken(
InstructionTextTokenType.TextToken,
' = '
),
InstructionTextToken(
InstructionTextTokenType.StringToken,
f'{string!r}',
buffer
),
InstructionTextToken(
InstructionTextTokenType.TextToken,
' '
)
]
return [DisassemblyTextLine(prefix, addr)]
def __del__(self):
pass
CFStringDataRenderer().register_type_specific()