-
Notifications
You must be signed in to change notification settings - Fork 8
/
hookify.py
226 lines (202 loc) · 10.8 KB
/
hookify.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
import os
import re
import sys
import glob
import getopt
from regexp import *
#Should remove
KNOWN_TYPES = [
'id', 'NSObject', 'void', 'char', 'int', 'unsigned', 'double', 'float', 'long', 'bool', 'BOOL', '_Bool',
'NSAffineTransform','NSAppleEventDescriptor','NSAppleEventManager','NSAppleScript',
'NSArchiver','NSArray','NSAssertionHandler','NSAttributedString','NSAutoreleasePool',
'NSBlockOperation','NSBundle','NSCache','NSCachedURLResponse','NSCalendar','NSCharacterSet',
'NSClassDescription','NSCloneCommand','NSCloseCommand','NSCoder','NSComparisonPredicate',
'NSCompoundPredicate','NSCondition','NSConditionLock','NSConnection','NSCountCommand',
'NSCountedSet','NSCreateCommand','NSData','NSDataDetector','NSDate','NSDateComponents',
'NSDateFormatter','NSDecimalNumber','NSDecimalNumberHandler','NSDeleteCommand','NSDictionary',
'NSDirectoryEnumerator','NSDistantObject','NSDistantObjectRequest','NSDistributedLock',
'NSDistributedNotificationCenter','NSEnumerator','NSError','NSException','NSExistsCommand',
'NSExpression','NSFileCoordinator','NSFileHandle','NSFileManager','NSFileVersion','NSFileWrapper',
'NSFormatter','NSGarbageCollector','NSGetCommand','NSHashTable','NSHost','NSHTTPCookie',
'NSHTTPCookieStorage','NSHTTPURLResponse','NSIndexPath','NSIndexSet','NSIndexSpecifier','NSInputStream',
'NSInvocation','NSInvocationOperation','NSKeyedArchiver','NSKeyedUnarchiver','NSLinguisticTagger',
'NSLocale','NSLock','NSLogicalTest','NSMachBootstrapServer','NSMachPort','NSMapTable','NSMessagePort',
'NSMessagePortNameServer','NSMetadataItem','NSMetadataQuery','NSMetadataQueryAttributeValueTuple',
'NSMetadataQueryResultGroup','NSMethodSignature','NSMiddleSpecifier','NSMoveCommand','NSMutableArray',
'NSMutableAttributedString','NSMutableCharacterSet','NSMutableData','NSMutableDictionary',
'NSMutableIndexSet','NSMutableOrderedSet','NSMutableSet','NSMutableString','NSMutableURLRequest',
'NSNameSpecifier','NSNetService','NSNetServiceBrowser','NSNotification','NSNotificationCenter',
'NSNotificationQueue','NSNull','NSNumber','NSNumberFormatter','NSObject','NSOperation','NSOperationQueue',
'NSOrderedSet','NSOrthography','NSOutputStream','NSPipe','NSPointerArray','NSPointerFunctions','NSPort',
'NSPortCoder','NSPortMessage','NSPortNameServer','NSPositionalSpecifier','NSPredicate','NSProcessInfo',
'NSPropertyListSerialization','NSPropertySpecifier','NSProtocolChecker','NSProxy','NSQuitCommand',
'NSRandomSpecifier','NSRangeSpecifier','NSRecursiveLock','NSRegularExpression','NSRelativeSpecifier',
'NSRunLoop','NSScanner','NSScriptClassDescription','NSScriptCoercionHandler','NSScriptCommand',
'NSScriptCommandDescription','NSScriptExecutionContext','NSScriptObjectSpecifier','NSScriptSuiteRegistry',
'NSScriptWhoseTest','NSSet','NSSetCommand','NSSocketPort','NSSocketPortNameServer','NSSortDescriptor',
'NSSpecifierTest','NSSpellServer','NSStream','NSString','NSTask','NSTextCheckingResult','NSThread',
'NSTimer','NSTimeZone','NSUbiquitousKeyValueStore','NSUnarchiver','NSUndoManager','NSUniqueIDSpecifier',
'NSURL','NSURLAuthenticationChallenge','NSURLCache','NSURLConnection','NSURLCredential','NSURLCredentialStorage',
'NSURLDownload','NSURLHandle','NSURLProtectionSpace','NSURLProtocol','NSURLRequest','NSURLResponse',
'NSUserAppleScriptTask','NSUserAutomatorTask','NSUserDefaults','NSUserNotification','NSUserNotificationCenter',
'NSUserScriptTask','NSUserUnixTask','NSUUID','NSValue','NSValueTransformer','NSWhoseSpecifier','NSXMLDocument',
'NSXMLDTD','NSXMLDTDNode','NSXMLElement','NSXMLNode','NSXMLParser','NSXPCConnection','NSXPCInterface',
'NSXPCListener','NSXPCListenerEndpoint','NSCoding','NSComparisonMethods','NSConnectionDelegate','NSCopying',
'NSDecimalNumberBehaviors','NSErrorRecoveryAttempting','NSFastEnumeration','NSFileManagerDelegate',
'NSFilePresenter','NSKeyedArchiverDelegate','NSKeyedUnarchiverDelegate','NSKeyValueCoding','NSKeyValueObserving',
'NSLocking','NSMachPortDelegate','NSMetadataQueryDelegate','NSMutableCopying','NSNetServiceBrowserDelegate',
'NSNetServiceDelegate', 'NSPortDelegate','NSScriptingComparisonMethods','NSScriptKeyValueCoding',
'NSScriptObjectSpecifiers','NSSecureCoding','NSSpellServerDelegate','NSStreamDelegate',
'NSURLAuthenticationChallengeSender','NSURLConnectionDataDelegate','NSURLConnectionDelegate',
'NSURLConnectionDelegate','NSURLHandleClient','NSURLProtocolClient','NSUserNotificationCenterDelegate',
'NSXMLParserDelegate','NSXPCListenerDelegate','NSXPCProxyCreating', 'CBCentralManager', 'CBPeripheral',
]
def map_format_specifier(type):
map = {
"int": "%d",
"BOOL": "%u",
"_Bool": "%u",
"bool": "%u",
"unsigned int": "%u",
"long long": "%lld",
"float": "%f",
"double": "%f",
"unsigned char": "%c",
"unichar": "%C",
"unsigned long long": "%llu",
"short": "%hd",
"unsigned short": "%hu",
}
specifier = map.get(type)
if specifier:
return specifier
if type_pointer_regex().search(type):
return "%p"
else:
return "%@"
class ObjcMethod():
def __init__(self, line, interface):
# TODO: do replacement in one single line...
self.skip = False
self.line = type_unknown_regex().sub("id", line);
self.line = struct_regex().sub("id", self.line)
self.ret_type = self.get_ret_type()
self.interface = interface
self.name = self.get_name()
self.full_name = "[%s %s]" %(interface, self.name)
if avoided_pointer_regex().search(self.line):
self.skip = True
return
def get_name(self):
splits = self.line.split(')')
splits = splits[1].split(':')
splits = splits[0].split(';')
name = splits[0]
return name
def get_ret_type(self):
splits = self.line[3:].split(')', 1)
ret_type = splits[0]
# we replace UNKNOWN_TYPE and struct by type id
if ret_type is not type_unknown_regex().search(ret_type) and not struct_regex().search(ret_type) and not avoided_pointer_regex().search(ret_type):
return ret_type
return "id"
def hook(self):
if self.ret_type == "void":
print '%s {[LogTool logDataFromNSString:@">>>> - %s"];%%log; %%orig;[LogTool logDataFromNSString:@"<<<< - %s"]; }' %(self.line[:-2], self.full_name, self.full_name)
else:
print '%s {[LogTool logDataFromNSString:@">>>> - %s"];%%log; %s ret = %%orig;[LogTool logDataFromNSString:[NSString stringWithFormat: @"<<<< - %s ==> ret value: %s", ret]];return ret; }' %(self.line[:-2], self.full_name, self.ret_type, self.full_name, map_format_specifier(self.ret_type))
class HeaderParser():
def __init__(self, filepath):
self.file = open(filepath)
self.lines = self.file.readlines()
self.interface = self.get_interface_name()
self.methods = self.get_methods()
def get_interface_name(self):
for line in self.lines:
if "@interface" in line:
splits = line.split(' ')
return splits[1]
def get_methods(self):
methods = []
for line in self.lines:
if "- (" == line[:3] or "+ (" == line[:3]:
method = ObjcMethod(line, self.interface)
methods.append(method)
return methods
def hook_file(filepath):
parser = HeaderParser(filepath)
if (parser.interface == None):
return
print "%hook", parser.interface
for method in parser.methods:
if method.name not in ".cxx_destruct" and method.name not in ".cxx_construct" and not method.skip:
method.hook()
print "%end"
def hook_many_files(filepaths):
for file in filepaths:
hook_file(file)
def get_all_header_files(basepath, pattern=""):
files = glob.glob(basepath + "*.h")
print("// %d files found" %(len(files)))
return files
def filter_files(files, regex_pattern):
try:
regex = re.compile(regex_pattern, re.IGNORECASE)
selected_files = filter(regex.search, files)
print("// %d filtered files" %(len(selected_files)))
return selected_files
except re.error:
print("invalid regex pattern, exiting...")
sys.exit()
def print_hookify_header():
print("///////////////////////////////////////////////////////////////////////////\n///////////////////////////////////////////////////////////////////////////\n//////// Following hooked methods has been generated by Hookify\n//////// \n//////// Happy ~Hacking~ Security Research ;)\n///////////////////////////////////////////////////////////////////////////\n///////////////////////////////////////////////////////////////////////////\n")
def usage():
print("usage: python hookify.py [d:p:r:f:] [dir= pattern= regex= file=]")
print("\nto hook an entire directory based on class name pre-made regex pattern use:")
print("\tpython hookify.py -d <your_dir_path> -p [network|crypto]")
print("\nto hook an entire directory based on class name custom regex pattern use:")
print("\tpython hookify.py -d <your_dir_path> -r <your_regex_string>")
print("\nto hook a simple header file use:")
print("\tpython hookify.py -f <your_file_path>")
def main(argv):
try:
opts, args = getopt.getopt(argv[1:], 'd:p:r:f:', ['dir=', 'pattern=', 'regex=', 'file='])
except getopt.GetoptError:
print('unknown error with getopt')
sys.exit()
dumped_class_filter_pattern = None
headers_directory_path = None
header_file_path = None
missing_required_option = True
for opt, arg in opts:
if opt in ('-p', '--pattern'):
if arg not in ['crypto', 'network']:
print('unkown pattern %s, did you mean: "crypto" or "network" ?' %(arg))
usage()
sys.exit()
elif arg == 'network':
dumped_class_filter_pattern = NETWORKING_CLASS_PATTERN
elif arg == 'crypto':
dumped_class_filter_pattern = CRYPTO_CLASS_PATTERN
elif opt in ('-r', '--regex'):
dumped_class_filter_pattern = arg
elif opt in ('-d', '--dir'):
headers_directory_path = arg
elif opt in ('-f', '--file'):
header_file_path = arg
if headers_directory_path:
print_hookify_header()
if not dumped_class_filter_pattern:
print("no filter pattern set for directory dump, please choose a pattern")
usage()
sys.exit()
files = get_all_header_files(headers_directory_path)
filtered_files = filter_files(files, dumped_class_filter_pattern)
hook_many_files(filtered_files)
elif header_file_path:
print_hookify_header()
hook_file(header_file_path)
else:
usage()
main(sys.argv)