-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.m
More file actions
132 lines (108 loc) · 4.74 KB
/
Copy pathmain.m
File metadata and controls
132 lines (108 loc) · 4.74 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
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#include <mach/mach.h>
#include <mach-o/dyld.h>
#include <mach-o/loader.h>
#include <objc/runtime.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <unistd.h>
@interface NSUserDefaults(private)
+ (void)setStandardUserDefaults:(id)defaults;
@end
const char **_CFGetProgname(void);
const char **_CFGetProcessPath(void);
static NSBundle *overwrittenBundle;
@implementation NSBundle(LC_iOS12)
+ (id)hooked_mainBundle {
if (overwrittenBundle) {
return overwrittenBundle;
}
return self.hooked_mainBundle;
}
@end
static int (*appMain)(int, char**);
static void overwriteExecPath(NSString *bundlePath) {
char *path = (char *)_dyld_get_image_name(0);
const char *newPath = [bundlePath stringByAppendingPathComponent:@"YuriGame"].UTF8String;
size_t maxLen = strlen(path);
size_t newLen = strlen(newPath);
assert(maxLen >= newLen);
close(open(newPath, O_CREAT | S_IRUSR | S_IWUSR));
vm_protect(mach_task_self(), (vm_address_t)path, maxLen, false, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY);
bzero(path, maxLen);
strncpy(path, newPath, newLen);
}
static void *getAppEntryPoint(void *handle, uint32_t imageIndex) {
uint32_t entryoff = 0;
const struct mach_header_64 *header = (struct mach_header_64 *)_dyld_get_image_header(imageIndex);
uint8_t *imageHeaderPtr = (uint8_t*)header + sizeof(struct mach_header_64);
struct load_command *command = (struct load_command *)imageHeaderPtr;
for(int i = 0; i < header->ncmds; ++i) {
if(command->cmd == LC_MAIN) {
struct entry_point_command ucmd = *(struct entry_point_command *)imageHeaderPtr;
entryoff = ucmd.entryoff;
break;
}
imageHeaderPtr += command->cmdsize;
command = (struct load_command *)imageHeaderPtr;
}
assert(entryoff > 0);
return (void *)header + entryoff;
}
static void invokeAppMain(NSString *selectedApp, int argc, char *argv[]) {
[NSUserDefaults.standardUserDefaults removeObjectForKey:@"selected"];
NSString *docPath = [NSFileManager.defaultManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]
.lastObject.path;
NSString *bundlePath = [NSString stringWithFormat:@"%@/Applications/%@", docPath, selectedApp];
NSBundle *appBundle = [[NSBundle alloc] initWithPath:bundlePath];
NSString *newHomePath = [NSString stringWithFormat:@"%@/Data/%@", docPath, appBundle.infoDictionary[@"CFBundleIdentifier"]];
setenv("CFFIXED_USER_HOME", newHomePath.UTF8String, 1);
setenv("HOME", newHomePath.UTF8String, 1);
NSString *cachePath = [NSString stringWithFormat:@"%@/Library/Caches", newHomePath];
[NSFileManager.defaultManager createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:nil];
[NSUserDefaults setStandardUserDefaults:[[NSUserDefaults alloc] initWithSuiteName:appBundle.bundleIdentifier]];
const char **path = _CFGetProcessPath();
const char *oldPath = *path;
*path = appBundle.executablePath.UTF8String;
overwriteExecPath(appBundle.bundlePath);
uint32_t appIndex = _dyld_image_count();
void *appHandle = dlopen(appBundle.executablePath.UTF8String, RTLD_LAZY|RTLD_LOCAL|RTLD_FIRST);
if (!appHandle || (uint64_t)appHandle > 0xf00000000000) {
*path = oldPath;
return;
}
appMain = getAppEntryPoint(appHandle, appIndex);
if (!appMain) {
*path = oldPath;
return;
}
if (![appBundle loadAndReturnError:nil]) {
*path = oldPath;
return;
}
method_exchangeImplementations(class_getClassMethod(NSBundle.class, @selector(mainBundle)), class_getClassMethod(NSBundle.class, @selector(hooked_mainBundle)));
overwrittenBundle = appBundle;
NSMutableArray<NSString *> *objcArgv = NSProcessInfo.processInfo.arguments.mutableCopy;
objcArgv[0] = appBundle.executablePath;
[NSProcessInfo.processInfo performSelector:@selector(setArguments:) withObject:objcArgv];
NSProcessInfo.processInfo.processName = appBundle.infoDictionary[@"CFBundleExecutable"];
*_CFGetProgname() = NSProcessInfo.processInfo.processName.UTF8String;
argv[0] = (char *)NSBundle.mainBundle.executablePath.UTF8String;
appMain(argc, argv);
}
int YuriGameMain(int argc, char *argv[]) {
NSString *selectedApp = [NSUserDefaults.standardUserDefaults stringForKey:@"selected"];
if (selectedApp) {
invokeAppMain(selectedApp, argc, argv);
}
void *YuriGameUIHandle = dlopen("@executable_path/Frameworks/YuriGameUI.dylib", RTLD_LAZY);
assert(YuriGameUIHandle);
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, @"LCAppDelegate");
}
}
int main(int argc, char *argv[]) {
assert(appMain != NULL);
return appMain(argc, argv);
}