/
InjectionClient.mm
517 lines (467 loc) Β· 19.6 KB
/
InjectionClient.mm
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
515
516
517
//
// InjectionClient.mm
// InjectionBundle
//
// Created by John Holdsworth on 06/11/2017.
// Copyright Β© 2017 John Holdsworth. All rights reserved.
//
// $Id: //depot/ResidentEval/InjectionBundle/InjectionClient.mm#133 $
//
#import "InjectionClient.h"
#import "SwiftTrace.h"
#import <mach-o/dyld.h>
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
#import <UIKit/UIKit.h>
#if __has_include("tvOSInjection10-Swift.h")
#import "tvOSInjection10-Swift.h"
#elif __has_include("tvOSInjection-Swift.h")
#import "tvOSInjection-Swift.h"
#elif __has_include("iOSInjection10-Swift.h")
#import "iOSInjection10-Swift.h"
#else
#import "iOSInjection-Swift.h"
#endif
#import <objc/runtime.h>
@interface NSObject (Remapped)
+ (void)addMappingFromIdentifier:(NSString *)identifier toObject:(id)object forCoder:(id)coder;
+ (id)mappedObjectForCoder:(id)decoder withIdentifier:(NSString *)identifier;
@end
@implementation NSObject (Remapper)
static struct {
NSMutableDictionary *inputIndexes;
NSMutableArray *output, *order;
int orderIndex;
} remapper;
+ (void)my_addMappingFromIdentifier:(NSString *)identifier toObject:(id)object forCoder:(id)coder {
//NSLog(@"Map %@ = %@", identifier, object);
if(remapper.output && [identifier hasPrefix:@"UpstreamPlaceholder-"]) {
if (remapper.inputIndexes)
remapper.inputIndexes[identifier] = @([remapper.inputIndexes count]);
else
[remapper.output addObject:object];
}
[self my_addMappingFromIdentifier:identifier toObject:object forCoder:coder];
}
+ (id)my_mappedObjectForCoder:(id)decoder withIdentifier:(NSString *)identifier {
//NSLog(@"Mapped? %@", identifier);
if(remapper.output && [identifier hasPrefix:@"UpstreamPlaceholder-"]) {
if (remapper.inputIndexes)
[remapper.order addObject:remapper.inputIndexes[identifier] ?: @""];
else
return remapper.output[[remapper.order[remapper.orderIndex++] intValue]];
}
return [self my_mappedObjectForCoder:decoder withIdentifier:identifier];
}
@end
@interface UIViewController (StoryboardInjection)
- (void)_loadViewFromNibNamed:(NSString *)a0 bundle:(NSBundle *)a1;
@end
@implementation UIViewController (iOS14StoryboardInjection)
- (void)iOS14LoadViewFromNibNamed:(NSString *)nibName bundle:(NSBundle *)bundle {
if ([self respondsToSelector:@selector(_loadViewFromNibNamed:bundle:)])
[self _loadViewFromNibNamed:nibName bundle:bundle];
else {
size_t vcSize = class_getInstanceSize([UIViewController class]);
size_t mySize = class_getInstanceSize([self class]);
char *extra = (char *)(__bridge void *)self + vcSize;
NSData *ivars = [NSData dataWithBytes:extra length:mySize-vcSize];
(void)[self initWithNibName:nibName bundle:bundle];
memcpy(extra, ivars.bytes, ivars.length);
[self loadView];
}
}
@end
#else
#if __has_include("macOSInjection10-Swift.h")
#import "macOSInjection10-Swift.h"
#else
#import "macOSInjection-Swift.h"
#endif
#endif
#ifdef XPROBE_PORT
#import "../XprobePlugin/Classes/Xtrace.mm"
#import "../XprobePlugin/Classes/Xprobe.mm"
#import "../XprobePlugin/Classes/Xprobe+Service.mm"
@interface BundleInjection: NSObject
@end
@implementation BundleInjection
+ (const char *)connectedAddress {
return "127.0.0.1";
}
@end
@implementation Xprobe(Seeding)
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
+ (NSArray *)xprobeSeeds {
UIApplication *app = [UIApplication sharedApplication];
NSMutableArray *seeds = [[app windows] mutableCopy];
[seeds insertObject:app atIndex:0];
return seeds;
}
#else
+ (NSArray *)xprobeSeeds {
NSApplication *app = [NSApplication sharedApplication];
NSMutableArray *seeds = [[app windows] mutableCopy];
if ( app.delegate )
[seeds insertObject:app.delegate atIndex:0];
return seeds;
}
#endif
@end
#endif
#import <XCTest/XCTest.h>
@implementation NSObject(RunXCTestCase)
+ (void)runXCTestCase:(Class)aTestCase {
Class _XCTestSuite = objc_getClass("XCTestSuite");
XCTestSuite *suite0 = [_XCTestSuite testSuiteWithName: @"InjectedTest"];
XCTestSuite *suite = [_XCTestSuite testSuiteForTestCaseClass: aTestCase];
Class _XCTestSuiteRun = objc_getClass("XCTestSuiteRun");
XCTestSuiteRun *tr = [_XCTestSuiteRun testRunWithTest: suite];
[suite0 addTest:suite];
[suite0 performTest:tr];
}
@end
@interface SwiftUISupport
+ (void)setupWithPointer:(void *)ptr;
@end
@interface SwiftTrace : NSObject
@end
@implementation InjectionClient
+ (void)load {
// connect to InjectionIII.app using socket
if (InjectionClient *client = [self connectTo:INJECTION_ADDRESS])
[client run];
else {
printf("π Injection loaded but could not connect. Is InjectionIII.app running?\n");
#ifndef __IPHONE_OS_VERSION_MIN_REQUIRED
printf("β οΈ For a macOS app you need to turn off the sandbox to connect. β οΈ\n");
#endif
}
}
- (void)runInBackground {
SwiftEval *builder = [SwiftEval sharedInstance];
NSString *tmpDir = [self readString];
BOOL notPlugin = ![@"/tmp" isEqualToString:tmpDir];
builder.tmpDir = tmpDir;
if (notPlugin)
[self writeInt:INJECTION_SALT];
[self writeString:INJECTION_KEY];
NSString *frameworksPath = [NSBundle mainBundle].privateFrameworksPath;
[self writeString:frameworksPath];
[self writeString:builder.arch];
[self writeString:[NSBundle mainBundle].executablePath];
int codesignStatusPipe[2];
pipe(codesignStatusPipe);
SimpleSocket *reader = [[SimpleSocket alloc] initSocket:codesignStatusPipe[0]];
SimpleSocket *writer = [[SimpleSocket alloc] initSocket:codesignStatusPipe[1]];
// make available implementation of signing delegated to macOS app
builder.signer = ^BOOL(NSString *_Nonnull dylib) {
[self writeCommand:InjectionSign withString:dylib];
return [reader readString].boolValue;
};
NSDictionary<NSString *,NSString *> *frameworkPaths;
if (notPlugin) {
NSMutableArray *frameworks = [NSMutableArray new];
NSMutableArray *sysFrameworks = [NSMutableArray new];
NSMutableDictionary *imageMap = [NSMutableDictionary new];
const char *bundleFrameworks = frameworksPath.UTF8String;
for (int32_t i = _dyld_image_count()-1; i >= 0 ; i--) {
const char *imageName = _dyld_get_image_name(i);
if (!strstr(imageName, ".framework/")) continue;
NSString *imagePath = [NSString stringWithUTF8String:imageName];
NSString *frameworkName = imagePath.lastPathComponent;
[imageMap setValue:imagePath forKey:frameworkName];
[strstr(imageName, bundleFrameworks) ?
frameworks : sysFrameworks addObject:frameworkName];
}
[self writeCommand:InjectionFrameworkList withString:
[frameworks componentsJoinedByString:FRAMEWORK_DELIMITER]];
[self writeString:
[sysFrameworks componentsJoinedByString:FRAMEWORK_DELIMITER]];
frameworkPaths = imageMap;
}
// As tmp file names come in, inject them
InjectionCommand command;
while ((command = (InjectionCommand)[self readInt]) != InjectionEOF) {
switch (command) {
case InjectionVaccineSettingChanged: {
NSString *string = [self readString];
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSDictionary *dictionary = (NSDictionary *)json;
if (dictionary != nil) {
NSNumber *vaccineEnabled = [dictionary valueForKey:@"Enabled Vaccine"];
builder.vaccineEnabled = [vaccineEnabled boolValue];
}
break;
}
case InjectionConnected: {
NSString *projectFile = [self readString];
builder.projectFile = projectFile;
builder.derivedLogs = nil;
printf("π Injection connected π\n");
NSString *pbxFile = [projectFile
stringByAppendingPathComponent:@"project.pbxproj"];
NSString *pbxContents = [NSString
stringWithContentsOfFile:pbxFile
encoding:NSUTF8StringEncoding error:NULL];
if (![pbxContents containsString:@"-interposable"])
printf("π Have you remembered to add \"-Xlinker -interposable\" to your project's \"Other Linker Flags\"?\n");
break;
}
case InjectionWatching: {
NSString *directory = [self readString];
printf("π Watching %s/**\n", directory.UTF8String);
break;
}
case InjectionLog:
printf("%s\n", [self readString].UTF8String);
break;
case InjectionSigned:
[writer writeString:[self readString]];
break;
case InjectionTrace:
[SwiftTrace swiftTraceMainBundle];
printf("π Added trace to non-final methods of classes in app bundle\n");
[self filteringChanged];
break;
case InjectionUntrace:
[SwiftTrace swiftTraceRemoveAllTraces];
break;
case InjectionTraceUI:
[self loadSwuftUISupprt];
[SwiftTrace swiftTraceMainBundleMethods];
[SwiftTrace swiftTraceMainBundle];
printf("π Added trace to methods in main bundle\n");
[self filteringChanged];
break;
case InjectionTraceUIKit:
dispatch_sync(dispatch_get_main_queue(), ^{
Class OSView = objc_getClass("UIView") ?: objc_getClass("NSView");
printf("π Adding trace to the framework containg %s, this will take a while...\n", class_getName(OSView));
[OSView swiftTraceBundle];
printf("π Completed adding trace.\n");
});
[self filteringChanged];
break;
case InjectionTraceSwiftUI:
if (Class AnyText = [self loadSwuftUISupprt]) {
printf("π Adding trace to SwiftUI calls.\n");
[SwiftTrace swiftTraceMethodsInFrameworkContaining:AnyText];
[self filteringChanged];
}
break;
case InjectionTraceFramework:
if (NSString *frameworkName = [self readString]) {
const int8_t *frameworkPath =
(const int8_t *)frameworkPaths[frameworkName].UTF8String;
printf("π Tracing %s\n", frameworkPath);
[SwiftTrace swiftTraceMethodsInBundle:frameworkPath];
[SwiftTrace swiftTraceBundlePath:frameworkPath];
[self filteringChanged];
}
break;
case InjectionQuietInclude:
[SwiftTrace setSwiftTraceFilterInclude:[self readString]];
break;
case InjectionInclude:
[SwiftTrace setSwiftTraceFilterInclude:[self readString]];
[self filteringChanged];
break;
case InjectionExclude:
[SwiftTrace setSwiftTraceFilterExclude:[self readString]];
[self filteringChanged];
break;
case InjectionStats:
[SwiftInjection dumpStats];
[self needsTracing];
break;
case InjectionCallOrder:
printf("\nπ Function names in the order they were first called:\n"
"π ===================================================\n");
for (NSString *signature : [SwiftInjection callOrder])
printf("%s\n", signature.UTF8String);
[self needsTracing];
break;
case InjectionFileOrder:
printf("\nπ Source files in the order they were first referenced:\n"
"π =====================================================\n"
"π (Order the source files should be compiled in target)\n");
[self writeCommand:InjectionCallOrderList
withString:[[SwiftInjection callOrder]
componentsJoinedByString:CALLORDER_DELIMITER]];
[self needsTracing];
break;
case InjectionFileReorder:
[self writeCommand:InjectionCallReorderList
withString:[[SwiftInjection callOrder]
componentsJoinedByString:CALLORDER_DELIMITER]];
[self needsTracing];
break;
case InjectionInvalid:
printf("π β οΈ Connection rejected. Are you running the correct version of InjectionIII.app from /Applications? β οΈ\n");
break;
case InjectionIdeProcPath: {
builder.lastIdeProcPath = [self readString];
break;
}
default: {
NSString *changed = [self readString];
dispatch_async(dispatch_get_main_queue(), ^{
NSError *err = nil;
switch (command) {
case InjectionLoad:
[SwiftInjection injectWithTmpfile:changed error:&err];
break;
case InjectionInject: {
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
if ([changed hasSuffix:@"storyboard"] || [changed hasSuffix:@"xib"]) {
if (![self injectUI:changed])
return;
}
else
#endif
[SwiftInjection injectWithOldClass:nil classNameOrFile:changed];
break;
}
#ifdef XPROBE_PORT
case InjectionXprobe:
[Xprobe connectTo:NULL retainObjects:YES];
[Xprobe search:@""];
break;
case InjectionEval: {
NSArray<NSString *> *parts = [changed componentsSeparatedByString:@"^"];
int pathID = parts[0].intValue;
[self writeCommand:InjectionPause withString:@"5"];
if ([xprobePaths[pathID].object respondsToSelector:@selector(swiftEvalWithCode:)])
(void)[xprobePaths[pathID].object swiftEvalWithCode:parts[3].stringByRemovingPercentEncoding];
else
printf("π Xprobe: Eval only works on NSObject subclasses\n");
[Xprobe writeString:[NSString stringWithFormat:@"$('BUSY%d').hidden = true; ", pathID]];
break;
}
#endif
default:
[self writeCommand:InjectionError withString:[NSString
stringWithFormat:@"Invalid command #%d", command]];
break;
}
[self writeCommand:err ? InjectionError : InjectionComplete
withString:err ? err.localizedDescription : nil];
});
}
}
}
}
- (Class)loadSwuftUISupprt {
static char classInSwiftUIMangled[] = "$s7SwiftUI14AnyTextStorageCN";
if (Class AnyText = (__bridge Class)
dlsym(RTLD_DEFAULT, classInSwiftUIMangled)) {
NSString *swiftUIBundlePath = [[[NSBundle
bundleForClass:[self class]] bundlePath]
stringByReplacingOccurrencesOfString:@"Injection.bundle"
withString:@"SwiftUISupport.bundle"];
if (Class swiftUISupport = [[NSBundle
bundleWithPath:swiftUIBundlePath]
classNamed:@"SwiftUISupport"])
[swiftUISupport setupWithPointer:NULL];
else
printf("π Could not find SwiftUISupport at path: %s\n",
swiftUIBundlePath.UTF8String);
return AnyText;
}
return nil;
}
- (void)needsTracing {
if (![SwiftTrace swiftTracing])
printf("π β οΈ You need to have traced something to gather stats.\n");
}
- (void)filteringChanged {
if ([SwiftTrace swiftTracing]) {
NSString *exclude = SwiftTrace.swiftTraceFilterExclude;
if (NSString *include = SwiftTrace.swiftTraceFilterInclude)
printf(exclude ?
"π Filtering trace to include methods matching '%s' but not '%s'.\n" :
"π Filtering trace to include methods matching '%s'.\n",
include.UTF8String, exclude.UTF8String);
else
printf(exclude ?
"π Filtering trace to exclude methods matching '%s'.\n" :
"π Not filtering trace (Menu Item: 'Set Filters')\n",
exclude.UTF8String);
}
}
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
- (BOOL)injectUI:(NSString *)changed {
static NSMutableDictionary *allOrder;
static dispatch_once_t once;
dispatch_once(&once, ^{
Class proxyClass = objc_getClass("UIProxyObject");
method_exchangeImplementations(
class_getClassMethod(proxyClass,
@selector(my_addMappingFromIdentifier:toObject:forCoder:)),
class_getClassMethod(proxyClass,
@selector(addMappingFromIdentifier:toObject:forCoder:)));
method_exchangeImplementations(
class_getClassMethod(proxyClass,
@selector(my_mappedObjectForCoder:withIdentifier:)),
class_getClassMethod(proxyClass,
@selector(mappedObjectForCoder:withIdentifier:)));
allOrder = [NSMutableDictionary new];
});
@try {
UIViewController *rootViewController = [UIApplication sharedApplication].windows.firstObject.rootViewController;
UINavigationController *navigationController = (UINavigationController*)rootViewController;
UIViewController *visibleVC = rootViewController;
if (UIViewController *child =
visibleVC.childViewControllers.firstObject)
visibleVC = child;
if ([visibleVC respondsToSelector:@selector(viewControllers)])
visibleVC = [(UISplitViewController *)visibleVC
viewControllers].lastObject;
if ([visibleVC respondsToSelector:@selector(visibleViewController)])
visibleVC = [(UINavigationController *)visibleVC
visibleViewController];
if (!visibleVC.nibName && [navigationController respondsToSelector:@selector(topViewController)]) {
visibleVC = [navigationController topViewController];
}
NSString *nibName = visibleVC.nibName;
if (!(remapper.order = allOrder[nibName])) {
remapper.inputIndexes = [NSMutableDictionary new];
remapper.output = [NSMutableArray new];
allOrder[nibName] = remapper.order = [NSMutableArray new];
[visibleVC iOS14LoadViewFromNibNamed:visibleVC.nibName
bundle:visibleVC.nibBundle];
remapper.inputIndexes = nil;
remapper.output = nil;
}
NSError *err = nil;
[[SwiftEval sharedInstance] rebuildWithStoryboard:changed error:&err];
if (err)
return FALSE;
void (^resetRemapper)(void) = ^{
remapper.output = [NSMutableArray new];
remapper.orderIndex = 0;
};
resetRemapper();
[visibleVC iOS14LoadViewFromNibNamed:visibleVC.nibName
bundle:visibleVC.nibBundle];
if ([SwiftEval sharedInstance].vaccineEnabled == YES) {
resetRemapper();
[SwiftInjection vaccine:visibleVC];
} else {
[visibleVC viewDidLoad];
[visibleVC viewWillAppear:NO];
[visibleVC viewDidAppear:NO];
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
[SwiftInjection flash:visibleVC];
#endif
}
}
@catch(NSException *e) {
printf("Problem reloading nib: %s\n", e.reason.UTF8String);
}
remapper.output = nil;
return true;
}
#endif
@end