-
Notifications
You must be signed in to change notification settings - Fork 365
/
ida_search_block.py
199 lines (144 loc) · 5.65 KB
/
ida_search_block.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
# -*- coding: utf-8 -*-
import idautils
import idc
from idaapi import PluginForm
import operator
import csv
import sys
import json
IS32BIT = not idaapi.get_inf_structure().is_64bit()
IS_MAC = 'X86_64' in idaapi.get_file_type_name()
print "Start analyze binary for " + ("Mac" if IS_MAC else "iOS")
def isInText(x):
return SegName(x) == '__text'
GlobalBlockAddr = LocByName("__NSConcreteGlobalBlock")
class GlobalBlockInfo:
pass
AllGlobalBlockMap = {}
for struct in list(DataRefsTo(GlobalBlockAddr)):
func = 0L
FUNC_OFFSET_IN_BLOCK = 12 if IS32BIT else 16
if IS32BIT:
func = Dword(struct + FUNC_OFFSET_IN_BLOCK)
else:
func = Qword(struct + FUNC_OFFSET_IN_BLOCK)
info = GlobalBlockInfo()
info.func = func
info.struct = struct
if len(list(DataRefsTo(struct))) == 0:
continue
refTo = list(DataRefsTo(struct))[0]
info.superFuncName = GetFunctionName(refTo)
info.superFunc = LocByName(info.superFuncName)
AllGlobalBlockMap[func] = info
def funcIsGlobalBlockFunc(block_func):
return block_func in AllGlobalBlockMap
def isPossibleStackBlockForFunc(block_func):
# def superFuncForStackBlock(block_func):
if not isInText(block_func):
return False
if GetFunctionAttr(block_func,FUNCATTR_START) != (block_func & ~ 1):
return False
#block addr cannot be called directly
if len(list(CodeRefsTo(block_func, 0))) !=0 :
# print '%x is not block because be call by %x' % (block_func ,list(CodeRefsTo(block_func, 0))[0])
return False
# ref to block should be in text section
refsTo = list(DataRefsTo(block_func))
for addr in refsTo:
if not isInText(addr):
# print '%x is not block because be ref from %x' % (block_func, addr)
return False
# block func should be ref in only 1 function
superFuncs = [GetFunctionAttr(x,FUNCATTR_START) for x in refsTo]
superFuncs = list (set (superFuncs))
if len(superFuncs) != 1:
# print '%x is not block because be not ref from 1 function' % block_func
return False
return True
def superFuncForStackBlock(block_func):
refsTo = list(DataRefsTo(block_func))
superFuncs = [GetFunctionAttr(x,FUNCATTR_START) for x in refsTo]
superFuncs = list (set (superFuncs))
if len(superFuncs) != 1:
return None
super_func_addr = superFuncs[0]
if IS_MAC:
return super_func_addr
else:
return super_func_addr | GetReg(super_func_addr, "T") # thumb
def superFuncForBlockFunc(block_func):
if funcIsGlobalBlockFunc(block_func):
return AllGlobalBlockMap[block_func].superFunc
superStackFunc = superFuncForStackBlock(block_func)
return superStackFunc # maybe None
resultDict = {}
def findBlockName(block_func):
# print "find block name %X" % block_func
funcName = GetFunctionName(block_func)
if len(funcName) != 0 and funcName[0] in ('-', '+'):
return funcName
# maybe nested block
superBlockFuncAddr = superFuncForBlockFunc(block_func)
if superBlockFuncAddr == None:
return "";
if not IS_MAC:
superBlockFuncAddr = superBlockFuncAddr | GetReg(superBlockFuncAddr, "T") # thumb
superBlockName = findBlockName(superBlockFuncAddr)
if len(superBlockName) == 0:
return ""
else:
return superBlockName + "_block"
#find all possible Stack Block
allPossibleStackBlockFunc = []
allRefToBlock=[]
if IS32BIT:
allRefToBlock = list(DataRefsTo(LocByName("__NSConcreteStackBlock")))
else:
allRefToBlock = list(DataRefsTo(LocByName("__NSConcreteStackBlock_ptr")))
allRefToBlock.sort()
'''
2 ref (@PAGE , @PAGEOFF) to __NSConcreteStackBlock_ptr ,
but once actual
filter the list
__text:0000000102D9979C ADRP X8, #__NSConcreteStackBlock_ptr@PAGE
__text:0000000102D997A0 LDR X8, [X8,#__NSConcreteStackBlock_ptr@PAGEOFF]
'''
tmp_array = allRefToBlock[:1]
for i in range(1, len(allRefToBlock)):
if allRefToBlock[i] - allRefToBlock[i - 1] <= 8:
pass
else:
tmp_array.append(allRefToBlock[i])
allRefToBlock = tmp_array
allRefToBlock = filter(lambda x:isInText(x), allRefToBlock)
for addr in allRefToBlock:
LineNumAround = 30 #Around 30 arm instruction
scan_addr_min= max (addr - LineNumAround * 4, GetFunctionAttr(addr,FUNCATTR_START))
scan_addr_max= min (addr + LineNumAround * 4, GetFunctionAttr(addr,FUNCATTR_END))
for scan_addr in range(scan_addr_min, scan_addr_max):
allPossibleStackBlockFunc += list(DataRefsFrom(scan_addr)) # all function pointer used around __NSConcreteStackBlock
allPossibleStackBlockFunc = list (set (allPossibleStackBlockFunc))
allPossibleStackBlockFunc = filter(lambda x:isPossibleStackBlockForFunc(x) , allPossibleStackBlockFunc )
#process all Global Block
for block_func in AllGlobalBlockMap:
block_name = findBlockName(block_func)
resultDict[block_func] = block_name
for block_func in allPossibleStackBlockFunc:
block_name = findBlockName(block_func)
resultDict[block_func] = block_name
output_file = './block_symbol.json'
list_output = []
error_num = 0
for addr in resultDict:
name = resultDict[addr]
if len(name) == 0 or name[0] not in ('-', '+'):
error_num += 1
continue
list_output += [{"address":("0x%X" % addr), "name":name}]
encodeJson = json.dumps(list_output, indent=1)
f = open(output_file, "w")
f.write(encodeJson)
f.close()
print 'restore block num %d ' % len(list_output)
print 'origin block num: %d(GlobalBlock: %d, StackBlock: %d)' % (len(allRefToBlock) + len(AllGlobalBlockMap), len(AllGlobalBlockMap), len(allRefToBlock))