-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathXcodeAnyTroll.xm
More file actions
377 lines (290 loc) · 13.8 KB
/
XcodeAnyTroll.xm
File metadata and controls
377 lines (290 loc) · 13.8 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
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
#import <Foundation/Foundation.h>
#import <SSZipArchive/SSZipArchive.h>
#import <HBLog.h>
#import <dlfcn.h>
#import <libSandy.h>
#import <libSandyXpc.h>
#import "LSApplicationProxy.h"
#import "LSRecordPromise.h"
#import "MCMContainer.h"
#define TAG "[XcodeAnyTroll] "
static NSString *gPackageIdentifier = nil;
static NSString *gPackagePath = nil;
static LSRecordPromise *gRecordPromise = nil;
static SandyXpcMessagingCenter *GetXpcMessagingCenter(void) {
static SandyXpcMessagingCenter *messagingCenter = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
messagingCenter = [SandyXpcMessagingCenter centerNamed:@"com.82flex.xcodeanytroll"];
});
return messagingCenter;
}
#import <Foundation/Foundation.h>
struct BlockDescriptor {
unsigned long reserved;
unsigned long size;
void *rest[1];
};
struct Block {
void *isa;
int flags;
int reserved;
void *invoke;
struct BlockDescriptor *descriptor;
};
__used
static const char *BlockSig(id blockObj)
{
struct Block *block = (__bridge struct Block *)blockObj;
struct BlockDescriptor *descriptor = block->descriptor;
int copyDisposeFlag = 1 << 25;
int signatureFlag = 1 << 30;
assert(block->flags & signatureFlag);
int index = 0;
if (block->flags & copyDisposeFlag)
index += 2;
return (const char *)descriptor->rest[index];
}
@interface MIInstaller : NSObject
@property (readonly, nonatomic) LSRecordPromise *recordPromise;
@end
@interface MIInstallOptions : NSObject
@property (getter=isDeveloperInstall, nonatomic) bool developerInstall;
@end
%hook MICodeSigningVerifier
+ (id)_validateSignatureAndCopyInfoForURL:(NSURL *)url withOptions:(id)options error:(NSError **)errorPtr {
id result = %orig(url, options, errorPtr);
if (errorPtr && *errorPtr) {
NSError *error = *errorPtr;
if (![[error description] containsString:@"0xe800801c"] && ![[error description] containsString:@"0xe8008001"]) {
return result;
}
// Extract source directory from error description instead of using url.path
NSString *errorDesc = [error description];
NSString *sourceDirectory = nil;
// Find path that starts with /var/installd/ and ends with colon
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/var/installd/[^:]+" options:0 error:nil];
NSTextCheckingResult *match = [regex firstMatchInString:errorDesc options:0 range:NSMakeRange(0, errorDesc.length)];
if (!match) {
HBLogDebug(@TAG "Failed to extract source directory from error: %@", errorDesc);
return result;
}
sourceDirectory = [[errorDesc substringWithRange:match.range] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
HBLogDebug(@TAG "Extracted source directory: %@", sourceDirectory);
// Ensure sourceDirectory is valid
NSString *infoPlistPath = [sourceDirectory stringByAppendingPathComponent:@"Info.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:infoPlistPath]) {
HBLogDebug(@TAG "Info.plist not found at path: %@", infoPlistPath);
return result;
}
// Read Info.plist to get package identifier
NSDictionary *infoPlist = [NSDictionary dictionaryWithContentsOfFile:infoPlistPath];
if (!infoPlist) {
HBLogDebug(@TAG "Failed to read Info.plist at path: %@", infoPlistPath);
return result;
}
gPackageIdentifier = infoPlist[@"CFBundleIdentifier"];
if (!gPackageIdentifier) {
HBLogDebug(@TAG "CFBundleIdentifier not found in Info.plist");
return result;
}
HBLogDebug(@TAG "Package identifier extracted: %@", gPackageIdentifier);
NSString *tempDir = NSTemporaryDirectory();
NSString *workDir = [tempDir stringByAppendingPathComponent:[[NSUUID UUID] UUIDString]];
NSString *payloadDir = [workDir stringByAppendingPathComponent:@"Payload"];
NSString *ipaFileName = [NSString stringWithFormat:@"XcodeAnyTroll_%@.ipa", [[NSUUID UUID] UUIDString]];
NSString *ipaFilePath = [tempDir stringByAppendingPathComponent:ipaFileName];
// Create directory structure
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createDirectoryAtPath:payloadDir withIntermediateDirectories:YES attributes:nil error:nil];
// Get app name (last path component)
NSString *appName = [sourceDirectory lastPathComponent];
NSString *destAppPath = [payloadDir stringByAppendingPathComponent:appName];
HBLogDebug(@TAG "Starting to copy app to Payload directory: %@", sourceDirectory);
// Copy the app to Payload directory
NSError *copyError;
BOOL copySuccess = [fileManager copyItemAtPath:sourceDirectory toPath:destAppPath error:©Error];
if (!copySuccess) {
HBLogDebug(@TAG "Failed to copy app to Payload directory: %@", copyError);
} else {
HBLogDebug(@TAG "Successfully copied app to Payload directory");
// Create zip file (IPA)
HBLogDebug(@TAG "Creating IPA file from Payload directory");
BOOL success = [SSZipArchive createZipFileAtPath:ipaFilePath withContentsOfDirectory:workDir keepParentDirectory:NO compressionLevel:0 password:nil AES:NO progressHandler:nil];
if (success) {
HBLogDebug(@TAG "Successfully created IPA file: %@", ipaFilePath);
} else {
HBLogDebug(@TAG "Failed to create IPA file");
}
// Clean up temporary directory
[fileManager removeItemAtPath:workDir error:nil];
// Assign the IPA file path to the global variable
gPackagePath = ipaFilePath;
}
}
return result;
}
%end
%hook MIInstaller
- (BOOL)performInstallationWithError:(NSError **)errorPtr {
BOOL result = %orig;
if ([self respondsToSelector:@selector(recordPromise)] && self.recordPromise) {
gRecordPromise = self.recordPromise;
}
return result;
}
%end
%hook MIClientConnection
/* iOS 18.2 */
- (void)_installURL:(NSURL *)url identity:(id)identity targetingDomain:(NSUInteger)domain options:(MIInstallOptions *)options operationType:(NSUInteger)operationType completion:(void (^)(BOOL, NSArray *, id, NSError *))completion {
HBLogDebug(@TAG "installURL:%@ withOptions:%@", url, options);
if (![options isKindOfClass:%c(MIInstallOptions)] || ![options respondsToSelector:@selector(isDeveloperInstall)] || ![options isDeveloperInstall]) {
HBLogDebug(@TAG "Not a developer install, skipping custom handling");
%orig;
return;
}
void (^replCompletion)(BOOL, NSArray *, id, NSError *) = ^(BOOL succeed, NSArray *appList, id recordPromise, NSError *error) {
HBLogDebug(@TAG "completion called with recordPromise:%@ error:%@", recordPromise, error);
if (!completion) {
return;
}
if (gPackagePath && gPackageIdentifier && ([[error description] containsString:@"0xe800801c"] || [[error description] containsString:@"0xe8008001"])) {
NSError *error = nil;
NSDictionary *retVal = nil;
retVal = [GetXpcMessagingCenter() sendMessageAndReceiveReplyName:@"InstallPackage" userInfo:@{
@"PackagePath": gPackagePath,
@"PackageIdentifier": gPackageIdentifier,
} error:&error];
if (error) {
HBLogDebug(@TAG "XPC error occurred: %@", error);
completion(succeed, appList, recordPromise, error);
return;
}
HBLogDebug(@TAG "XPC reply received: %@", retVal);
LSApplicationProxy *appProxy = [LSApplicationProxy applicationProxyForIdentifier:gPackageIdentifier];
LSRecordPromise *recordPromise = [[%c(LSRecordPromise) alloc] initWithRecord:appProxy.correspondingApplicationRecord error:nil];
/* LSRecordPromise is not properly constructed in some cases, still need some work here. */
completion(YES, retVal[@"InstalledAppInfoArray"], gRecordPromise ?: recordPromise, nil);
return;
}
completion(succeed, appList, recordPromise, error);
};
%orig(url, identity, domain, options, operationType, replCompletion);
}
/* iOS 16.2 */
- (void)_installURL:(NSURL *)url identity:(id)identity targetingDomain:(NSUInteger)domain options:(MIInstallOptions *)options completion:(void (^)(BOOL, NSArray *, id, NSError *))completion {
HBLogDebug(@TAG "installURL:%@ withOptions:%@", url, options);
if (![options isKindOfClass:%c(MIInstallOptions)] || ![options respondsToSelector:@selector(isDeveloperInstall)] || ![options isDeveloperInstall]) {
HBLogDebug(@TAG "Not a developer install, skipping custom handling");
%orig;
return;
}
void (^replCompletion)(BOOL, NSArray *, id, NSError *) = ^(BOOL succeed, NSArray *appList, id recordPromise, NSError *error) {
HBLogDebug(@TAG "completion called with recordPromise:%@ error:%@", recordPromise, error);
if (!completion) {
return;
}
if (gPackagePath && gPackageIdentifier && ([[error description] containsString:@"0xe800801c"] || [[error description] containsString:@"0xe8008001"])) {
NSError *error = nil;
NSDictionary *retVal = nil;
retVal = [GetXpcMessagingCenter() sendMessageAndReceiveReplyName:@"InstallPackage" userInfo:@{
@"PackagePath": gPackagePath,
@"PackageIdentifier": gPackageIdentifier,
} error:&error];
if (error) {
HBLogDebug(@TAG "XPC error occurred: %@", error);
completion(succeed, appList, recordPromise, error);
return;
}
HBLogDebug(@TAG "XPC reply received: %@", retVal);
LSApplicationProxy *appProxy = [LSApplicationProxy applicationProxyForIdentifier:gPackageIdentifier];
LSRecordPromise *recordPromise = [[%c(LSRecordPromise) alloc] initWithRecord:appProxy.correspondingApplicationRecord error:nil];
/* LSRecordPromise is not properly constructed in some cases, still need some work here. */
completion(YES, retVal[@"InstalledAppInfoArray"], gRecordPromise ?: recordPromise, nil);
return;
}
completion(succeed, appList, recordPromise, error);
};
%orig(url, identity, domain, options, replCompletion);
}
/* iOS 15 */
- (void)installURL:(NSURL *)url withOptions:(NSDictionary *)options completion:(void (^)(id, NSError *))completion {
HBLogDebug(@TAG "installURL:%@ withOptions:%@", url, options);
if (![options[@"PackageType"] isEqualToString:@"Developer"]) {
HBLogDebug(@TAG "Not a developer install, skipping custom handling");
%orig;
return;
}
void (^replCompletion)(NSDictionary *, NSError *) = ^(NSDictionary *userInfo, NSError *error) {
HBLogDebug(@TAG "completion called with userInfo:%@ error:%@", [userInfo[@"InstalledAppInfoArray"] firstObject], error);
if (!completion) {
return;
}
if (gPackagePath && gPackageIdentifier && ([[error description] containsString:@"0xe800801c"] || [[error description] containsString:@"0xe8008001"])) {
NSError *error = nil;
NSDictionary *retVal = nil;
retVal = [GetXpcMessagingCenter() sendMessageAndReceiveReplyName:@"InstallPackage" userInfo:@{
@"PackagePath": gPackagePath,
@"PackageIdentifier": gPackageIdentifier,
} error:&error];
if (error) {
HBLogDebug(@TAG "XPC error occurred: %@", error);
completion(userInfo, error);
return;
}
HBLogDebug(@TAG "XPC reply received: %@", retVal);
completion(retVal, nil);
return;
}
completion(userInfo, error);
};
%orig(url, options, replCompletion);
}
%end
#if DEBUG
static void TestConnection(void) {
SandyXpcMessagingCenter *messagingCenter = GetXpcMessagingCenter();
NSDictionary *msgBody = @{@"name" : [[NSBundle mainBundle] bundleIdentifier] ?: @"Sandy"};
[messagingCenter sendMessageName:@"OneWayMessage" userInfo:msgBody];
NSError *error = nil;
NSDictionary *retVal = nil;
retVal = [messagingCenter sendMessageAndReceiveReplyName:@"TwoWayMessage"
userInfo:msgBody
error:&error];
if (error) {
HBLogDebug(@TAG "Error occurred: %@", error);
return;
}
if (!retVal[@"reply"]) {
HBLogDebug(@TAG "No reply received");
return;
}
HBLogDebug(@TAG "Received reply: %@", retVal[@"reply"]);
}
#endif
%ctor {
@autoreleasepool {
void *sandyHandle = dlopen("@rpath/libsandy.dylib", RTLD_LAZY);
if (!sandyHandle) {
sandyHandle = dlopen("/usr/lib/libsandy.dylib", RTLD_LAZY);
}
if (!sandyHandle) {
sandyHandle = dlopen("@loader_path/.jbroot/usr/lib/libsandy.dylib", RTLD_LAZY);
}
if (sandyHandle) {
int (*__dyn_libSandy_applyProfile)(const char *profileName) =
(int (*)(const char *))dlsym(sandyHandle, "libSandy_applyProfile");
if (__dyn_libSandy_applyProfile) {
int sandyStatus = __dyn_libSandy_applyProfile("XcodeAnyTroll");
if (sandyStatus == kLibSandyErrorXPCFailure) {
HBLogDebug(@TAG "Failed to apply profile");
} else {
HBLogDebug(@TAG "Profile applied");
}
}
}
#if DEBUG
TestConnection();
#endif
}
}