forked from wdmchaft/iSpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiSpy.rpc.xm
516 lines (447 loc) · 12.4 KB
/
iSpy.rpc.xm
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#include <stack>
#include <fcntl.h>
#include <stdio.h>
#include <dlfcn.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <stdbool.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/mman.h>
#include <sys/uio.h>
#include <objc/objc.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <mach-o/dyld.h>
#include <netinet/in.h>
#import <Security/Security.h>
#import <Security/SecCertificate.h>
#include <CFNetwork/CFNetwork.h>
#include <CFNetwork/CFProxySupport.h>
#include <CoreFoundation/CFString.h>
#include <CoreFoundation/CFStream.h>
#import <Foundation/NSJSONSerialization.h>
#include <CommonCrypto/CommonDigest.h>
#include <CommonCrypto/CommonHMAC.h>
#include "iSpy.common.h"
#include "iSpy.instance.h"
#include "iSpy.class.h"
#include "hooks_C_system_calls.h"
#include "hooks_CoreFoundation.h"
#include <dlfcn.h>
#include <mach-o/nlist.h>
#include <semaphore.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <QuartzCore/QuartzCore.h>
#import "typestring.h"
#import "iSpy.rpc.h"
/*
*
* RPC handlers take exactly one argument: an NSDictionary of parameter/value pairs.
*
* RPC handlers return an NSDictionary that will be sent to the RPC caller as JSON,
* either via a websocket (if initiated by websocket) or as a response to an HTTP POST.
*
* You can also return nil, that's fine too. For websockets nothing will happen; for POST
* requests it'll cause a blank response to be sent back to the RPC caller.
*
*/
@implementation RPCHandler
-(NSDictionary *) setMsgSendLoggingState:(NSDictionary *) args {
NSString *state = [args objectForKey:@"state"];
if( ! state || ( ! [state isEqualToString:@"true"] && ! [state isEqualToString:@"false"] )) {
ispy_log_debug(LOG_HTTP, "setMsgSendLoggingState: Invalid state");
return @{
@"status":@"error",
@"errorMessage":@"Invalid status"
};
}
if([state isEqualToString:@"true"]) {
[[iSpy sharedInstance] msgSend_enableLogging];
}
else if([state isEqualToString:@"false"]) {
[[iSpy sharedInstance] msgSend_disableLogging];
}
return @{
@"status":@"OK",
@"JSON": @{
@"state": state,
},
};
}
-(NSDictionary *) testJSONRPC:(NSDictionary *)args {
return @{
@"status":@"OK",
@"JSON": @{
@"args": args,
},
};
}
-(NSDictionary *) ASLR:(NSDictionary *)args {
return @{
@"status":@"OK",
@"JSON": @{
@"ASLROffset": [NSString stringWithFormat:@"%d", [[iSpy sharedInstance] ASLR]]
},
};
}
/*
args = NSDictionary containing an object ("classes"), which is is an NSArray of NSDictionaries, like so:
{
"classes": [
{
"class": "ClassName1",
"methods": [ @"Method1", @"Method2", ... ]
},
{
"class": "ClassName2",
"methods": [ @"MethodX", @"MethodY", ... ]
},
...
]
}
If "methods" is nil, assume all methods in class.
*/
-(NSDictionary *) addMethodsToWhitelist:(NSDictionary *)args {
int i, numClasses, m, numMethods;
static std::tr1::unordered_map<std::string, std::tr1::unordered_map<std::string, int> > WhitelistClassMap;
NSArray *classes = [args objectForKey:@"classes"];
if(classes == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Empty class list"
};
}
numClasses = [classes count];
// Iterate through all the class names, adding each one to our lookup table
for(i = 0; i < numClasses; i++) {
NSDictionary *itemToAdd = [classes objectAtIndex:i];
NSString *name = [itemToAdd objectForKey:@"class"];
if(!name) {
continue;
}
NSArray *methods = [itemToAdd objectForKey:@"methods"];
if(!methods) {
continue;
}
numMethods = [methods count];
if(!numMethods) {
continue;
}
for(m = 0; m < numMethods; m++) {
NSString *methodName = [methods objectAtIndex:m];
if(!methodName) {
continue;
}
std::string *classNameString = new std::string([name UTF8String]);
std::string *methodNameString = new std::string([methodName UTF8String]);
if(!classNameString || !methodNameString) {
if(methodNameString)
delete methodNameString;
if(classNameString)
delete classNameString;
continue;
}
ispy_log_debug(LOG_GENERAL, "[Whitelist] Adding [%s %s]", classNameString->c_str(), methodNameString->c_str());
whitelist_add_method(classNameString, methodNameString, (unsigned int)WHITELIST_PRESENT);
delete methodNameString;
delete classNameString;
}
}
return @{
@"status": @"OK",
@"JSON": @{},
};
}
/*
* Classes and internals
*/
-(NSDictionary *) classList:(NSDictionary *)args {
NSArray *classes = [[iSpy sharedInstance] classes];
return @{
@"status": @"OK",
@"JSON": @{
@"classes": classes,
},
};
}
-(NSDictionary *) classListWithProtocolInfo:(NSDictionary *)args {
NSArray *classes = [[iSpy sharedInstance] classesWithSuperClassAndProtocolInfo];
return @{
@"status": @"OK",
@"JSON": @{
@"classes": classes,
},
};
}
-(NSDictionary *) methodsForClass:(NSDictionary *)args {
NSString *className = [args objectForKey:@"class"];
if(className == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Empty class name",
};
}
NSArray *methods = [[iSpy sharedInstance] methodListForClass:className];
if(methods == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Empty methods list",
};
}
return @{
@"status": @"OK",
@"JSON": @{
@"name": className,
@"methods": methods,
},
};
}
-(NSDictionary *) propertiesForClass:(NSDictionary *)args {
NSString *className = [args objectForKey:@"class"];
if(className == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Empty class name"
};
}
NSArray *properties = [[iSpy sharedInstance] propertiesForClass:className];
if(properties == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Empty properties list"
};
}
return @{
@"status": @"OK",
@"JSON": @{
@"name": className,
@"properties": properties,
},
};
}
-(NSDictionary *) protocolsForClass:(NSDictionary *)args {
NSString *className = [args objectForKey:@"class"];
if(className == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Empty class name"
};
}
NSArray *protocols = [[iSpy sharedInstance] protocolsForClass:className];
if(protocols == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Empty protocols list"
};
}
return @{
@"status": @"OK",
@"JSON": @{
@"name": className,
@"protocols": protocols,
},
};
}
-(NSDictionary *) iVarsForClass:(NSDictionary *)args {
NSString *className = [args objectForKey:@"class"];
if(className == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Empty class name"
};
}
NSArray *iVars = [[iSpy sharedInstance] iVarsForClass:className];
if(iVars == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Empty iVars list"
};
}
return @{
@"status": @"OK",
@"JSON": @{
@"name": className,
@"iVars": iVars,
},
};
}
-(NSDictionary *) infoForMethod:(NSDictionary *)args {
NSString *className = [args objectForKey:@"class"];
NSString *methodName = [args objectForKey:@"method"];
if(className == nil || methodName == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Empty class and/or name"
};
}
Class cls = objc_getClass([className UTF8String]);
if(cls == nil) {
return @{
@"status": @"error",
@"errorMessage": @"That class doesn't exist"
};
}
SEL selector = sel_registerName([methodName UTF8String]);
if(selector == nil) {
return @{
@"status": @"error",
@"errorMessage": @"That selector name was bad"
};
}
NSLog(@"class: %@ // method: %s", cls, [methodName UTF8String]);
NSDictionary *infoDict = [[iSpy sharedInstance] infoForMethod:selector inClass:cls];
if(infoDict == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Error fetching information for that class/method"
};
}
return @{
@"status": @"OK",
@"JSON": @{
@"methodInfo": infoDict
},
};
}
/*
* Protocol RPC
*/
-(NSDictionary *) methodsForProtocol:(NSDictionary *)args {
NSString *protocolName = [args objectForKey:@"protocol"];
if(protocolName == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Empty protocol name"
};
}
NSArray *methods = [[iSpy sharedInstance] methodsForProtocol:objc_getProtocol([protocolName UTF8String])];
if(methods == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Empty methods list"
};
}
return @{
@"status": @"OK",
@"JSON": methods
};
}
-(NSDictionary *) propertiesForProtocol:(NSDictionary *)args {
NSString *protocolName = [args objectForKey:@"protocol"];
if(protocolName == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Empty protocol name"
};
}
NSArray *properties = [[iSpy sharedInstance] propertiesForProtocol:objc_getProtocol([protocolName UTF8String])];
if(properties == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Empty properties list"
};
}
return @{
@"status": @"OK",
@"JSON": properties
};
}
/*
* Instance RPC
*/
-(NSDictionary *) instanceAtAddress:(NSDictionary *)args {
NSString *addr = [args objectForKey:@"address"];
if(addr == nil) {
return @{
@"status": @"error",
@"errorMessage": @"Empty address value"
};
}
return @{
@"status":@"OK",
@"JSON": [[InstanceTracker sharedInstance] instanceAtAddress:addr]
};
}
-(NSDictionary *) instancesOfAppClasses:(NSDictionary *)args {
return @{
@"status":@"OK",
@"JSON": @{
@"classInstances": [[InstanceTracker sharedInstance] instancesOfAppClasses],
},
};
}
/*
* App info RPC
*/
-(NSDictionary *) applicationIcon:(NSDictionary *)args {
UIImage *appIcon = [UIImage imageNamed:[[NSBundle mainBundle].infoDictionary[@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconFiles"] firstObject]];
if(!appIcon) {
appIcon = [UIImage imageNamed: [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIconFiles"] objectAtIndex:0]];
if(!appIcon) {
appIcon = [UIImage imageNamed:@"[email protected]"];
if(!appIcon) {
appIcon = [UIImage imageNamed:@"Icon-72.png"];
if(!appIcon) {
appIcon = [UIImage imageNamed:@"/var/www/iSpy/img/bf-orange-alpha.png"];
if(!appIcon) {
return @{
@"status":@"error",
@"error": @"WTF, no app icon"
};
}
}
}
}
}
//NSLog(@"Icon files: %@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIconFiles"]);
NSLog(@"appIcon: %@", appIcon);
NSData *PNG = UIImagePNGRepresentation(appIcon);
NSLog(@"PNG: %@", appIcon);
NSString *base64PNG = [PNG base64EncodedStringWithOptions:0];
return @{
@"status":@"OK",
@"JSON": @{
@"imageURI": [NSString stringWithFormat:@"data:image/png;base64,%@", base64PNG]
}
};
}
-(NSDictionary *) appInfo:(NSDictionary *)args {
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSArray *keys = [infoDict allKeys];
NSMutableDictionary *interestingProperties = [[NSMutableDictionary alloc] init];
for(int i=0; i < [keys count]; i++) {
id obj = [keys objectAtIndex:i];
if([[infoDict objectForKey:obj] class] == objc_getClass("__NSCFString")) {
[interestingProperties setObject:[NSString stringWithString:[infoDict objectForKey:obj]] forKey:obj];
}
}
return @{
@"status":@"OK",
@"JSON": interestingProperties
};
}
-(NSDictionary *) keyChainItems:(NSDictionary *)args {
return @{
@"status":@"OK",
@"JSON": [[iSpy sharedInstance] keyChainItems]
};
}
-(NSDictionary *) releaseBreakpoint:(NSDictionary *)args {
const char *className = [[args objectForKey:@"className"] UTF8String];
const char *methodName = [[args objectForKey:@"methodName"] UTF8String];
breakpoint_release_breakpoint(className, methodName);
return @{
@"status":@"OK",
@"JSON": @"Released."
};
}
@end