This repository was archived by the owner on Feb 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathobjc_class.py
More file actions
270 lines (213 loc) · 8.39 KB
/
objc_class.py
File metadata and controls
270 lines (213 loc) · 8.39 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
261
262
263
264
265
266
267
268
269
270
import idaapi, idautils, idc
import re
encoded_types = {
'c':'char',
'i':'int',
's':'short',
'l':'long',
'q':'long long',
'C':'unsigned char',
'I':'unsigned int',
'S':'unsigned short',
'L':'unsigned long',
'Q':'unsigned long long',
'f':'float',
'd':'double',
'B':'bool',
'v':'void',
'*':'char *',
'@':'id',
'#':'class',
'?':'unknown',
':':'SEL',
'^':'ptr'}
class ObjcProperties:
def __init__(self, ea):
self.property_list = list()
# print '%08x: Parsing properties' % ea
entry_size = idc.Dword(ea)
num_entries = idc.Dword(ea + 4)
ea = ea + 8
for i in range(num_entries):
var_name = idc.GetString(idc.Dword(ea), -1, idc.ASCSTR_C)
var_type = idc.GetString(idc.Dword(ea + 4), -1, idc.ASCSTR_C)
self.property_list.append({'name':var_name, 'type':var_type})
ea = ea + entry_size
return
def __len__(self):
return len(self.property_list)
def __repr__(self):
dump = ''
for entry in self.property_list:
dump += '%s: %s\n' % (entry['name'], entry['type'])
return dump
class ObjcIvars:
def __init__(self, ea):
self.ivar_list = list()
# print '%08x: Parsing ivars' % ea
entry_size = idc.Dword(ea)
num_entries = idc.Dword(ea + 4)
ea = ea + 8
for i in range(num_entries):
ivar_offset = idc.Dword(idc.Dword(ea))
ivar_name = idc.GetString(idc.Dword(ea + 4), -1, idc.ASCSTR_C)
ivar_type = idc.GetString(idc.Dword(ea + 8), -1, idc.ASCSTR_C)
self.ivar_list.append({'name':ivar_name, 'type':ivar_type, 'offset':ivar_offset})
ea = ea + entry_size
return
def __len__(self):
return len(self.ivar_list)
def __repr__(self):
dump = ''
for entry in self.ivar_list:
dump += '%s (%d): %s\n' % (entry['name'], entry['offset'], encoded_types.get(entry['type'], entry['type']))
return dump
class ObjcMethods:
def __init__(self, ea):
self.method_list = list()
# print '%08x: Parsing methods' % ea
entry_size = idc.Dword(ea)
num_entries = idc.Dword(ea + 4)
ea = ea + 8
for i in range(num_entries):
method_name = idc.GetString(idc.Dword(ea), -1, idc.ASCSTR_C)
method_type = idc.GetString(idc.Dword(ea + 4), -1, idc.ASCSTR_C)
method_ea = idc.Dword(ea + 8)
self.method_list.append({'name': method_name, 'type':method_type, 'addr':method_ea})
ea = ea + entry_size
return
def __len__(self):
return len(self.method_list)
def __repr__(self):
dump = ''
for entry in self.method_list:
dump += '%08x: %s\n' % (entry['addr'], decode_type(entry['name'], entry['type']))
return dump
class ObjcProtocols:
def __init__(self, ea):
self.protocol_list = list()
# print '%08x: Parsing protocols' % ea
num_main_structs = idc.Dword(ea)
ea = ea + 4
for i in range(num_main_structs):
struct_off = idc.Dword(ea)
protocol_name = idc.GetString(idc.Dword(struct_off + 4), -1, idc.ASCSTR_C)
protocol_list = idc.Dword(struct_off + 8)
instance_methods = idc.Dword(struct_off + 0xC)
class_methods = idc.Dword(struct_off + 0x14)
if instance_methods:
_inst = ObjcMethods(instance_methods)
else:
_inst = None
if class_methods:
_class = ObjcMethods(class_methods)
else:
_class = None
if protocol_list:
_meta = ObjcProtocols(protocol_list)
else:
_meta = None
self.protocol_list.append({
'name': protocol_name,
'instance_methods': _inst,
'class_methods': _class,
'meta_protocols': _meta})
ea = ea + 4
return
def __len__(self):
return len(self.protocol_list)
def __repr__(self):
dump = ''
for entry in self.protocol_list:
dump += 'Protocol %s:\n' % entry['name']
if entry['instance_methods'] and len(entry['instance_methods']):
dump += ' Instance Methods:\n '
dump += repr(entry['instance_methods']).replace('\n', '\n ')
if entry['class_methods'] and len(entry['class_methods']):
dump += ' Class Methods:\n '
dump += repr(entry['class_methods']).replace('\n', '\n ')
if entry['meta_protocols'] and len(entry['meta_protocols']):
dump += ' Protocol list:\n '
dump += repr(entry['meta_protocols']).replace('\n', '\n ')
return dump
class ObjcClass:
def __init__(self, ea):
self.class_info = dict()
objc_const_seg = idc.SegByBase(idc.SegByName('__objc_const'))
objc_const_end = idc.SegEnd(objc_const_seg)
self.class_info['meta_class'] = idc.Dword(ea)
self.class_info['super_class'] = idc.Dword(ea + 4)
self.class_info['cache'] = idc.Dword(ea + 8)
self.class_info['vtable'] = idc.Dword(ea + 0xC)
_class_def = idc.Dword(ea + 0x10)
if _class_def < objc_const_seg or _class_def > objc_const_end:
return
self.class_info['top_class'] = idc.Dword(_class_def)
self.class_info['instance_size'] = idc.Dword(_class_def + 8)
name_off = idc.Dword(_class_def + 0x10)
class_name = idc.GetString(name_off, -1, idc.ASCSTR_C)
if not class_name:
class_name = '[UNKNOWN]'
self.class_info['name'] = class_name
self.class_info['methods'] = list()
self.class_info['protocols'] = list()
self.class_info['ivars'] = list()
self.class_info['properties'] = list()
if idc.Dword(_class_def + 0x14):
self.class_info['methods'] = ObjcMethods(idc.Dword(_class_def + 0x14))
if idc.Dword(_class_def + 0x18):
self.class_info['protocols'] = ObjcProtocols(idc.Dword(_class_def + 0x18))
if idc.Dword(_class_def + 0x1C):
self.class_info['ivars'] = ObjcIvars(idc.Dword(_class_def + 0x1C))
if idc.Dword(_class_def + 0x24):
self.class_info['properties'] = ObjcProperties(idc.Dword(_class_def + 0x24))
return
def dump(self):
if not self.class_info.has_key('name'):
return
print 'Class: %s' % self.class_info['name']
print 'Attributes:'
print ' IsTopClass: %d' % self.class_info['top_class']
print ' Instance Size: %d' % self.class_info['instance_size']
print ' Methods: %d' % len(self.class_info['methods'])
print ' Protocols: %d' % len(self.class_info['protocols'])
print ' Instance Vars: %d' % len(self.class_info['ivars'])
print ' properties: %d' % len(self.class_info['properties'])
if len(self.class_info['methods']):
print 'Method list:\n %s' % repr(self.class_info['methods']).replace('\n', '\n ')
if len(self.class_info['protocols']):
print 'Protocols:\n %s' % repr(self.class_info['protocols']).replace('\n', '\n ')
if len(self.class_info['ivars']):
print 'Instance variables:\n %s' % repr(self.class_info['ivars']).replace('\n', '\n ')
if len(self.class_info['properties']):
print 'properties:\n %s' % repr(self.class_info['properties']).replace('\n', '\n ')
def main():
objc_data_seg = idc.SegByBase(idc.SegByName('__objc_data'))
if objc_data_seg == idc.BADADDR:
print 'Cannot locate objc_data segment'
return
ea = objc_data_seg
while ea < idc.SegEnd(objc_data_seg):
objc_class = ObjcClass(ea)
objc_class.dump()
ea = ea + 0x14
def decode_type(name, type):
global encoded_types
list_types = re.split('\d+', type)
if list_types[0][0] in '{[(':
proto = list_types[0]
else:
proto = encoded_types.get(list_types[0], 'unknown ' + list_types[0])
proto += ' ' + name + '('
for t in list_types[3:]:
if not t:
continue
if len(t) > 1 and t[0] in '{[(':
proto += t + ', '
else:
proto += encoded_types.get(t, 'unknown ' + t) + ', '
proto = proto.rstrip(' ,') + ')'
return proto
if __name__ == '__main__':
main()
print 'Done.'