From 55787aefa3c4fdbff328e53ca65c8daf76d31725 Mon Sep 17 00:00:00 2001 From: Federico Sandri Date: Sun, 19 Jun 2016 17:19:18 -0300 Subject: [PATCH] Simplified logic * simjectGenerateDylibList returns formatted string for insertion. * Search results are filtered before enumeration. * Proper Foundation methods for the uses. --- respring_simulator.mm | 2 +- simjectCore.h | 2 +- simjectCore.mm | 46 +++++++++++++++++++++++-------------------- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/respring_simulator.mm b/respring_simulator.mm index 1cf2f41..d43c0ed 100644 --- a/respring_simulator.mm +++ b/respring_simulator.mm @@ -4,7 +4,7 @@ int main(int argc, const char * argv[]) { printf("respring_simulator (C) 2016 Karen Tsai (angelXwind)\n"); printf("Injecting appropriate dynamic libraries from /opt/simject...\n"); NSString *envVars; - system([[NSString stringWithFormat:@"xcrun simctl spawn booted launchctl debug system/com.apple.SpringBoard%@", (envVars = [simjectGenerateDylibList(nil) componentsJoinedByString:@":"]) ? [NSString stringWithFormat:@" --environment DYLD_INSERT_LIBRARIES=%@", envVars] : nil] UTF8String]); + system([[NSString stringWithFormat:@"xcrun simctl spawn booted launchctl debug system/com.apple.SpringBoard%@", (envVars = simjectGenerateDylibList(nil)) ? [NSString stringWithFormat:@" --environment DYLD_INSERT_LIBRARIES=%@", envVars] : nil] UTF8String]); printf("Respringing...\n"); system("xcrun simctl spawn booted launchctl stop com.apple.SpringBoard"); return 0; diff --git a/simjectCore.h b/simjectCore.h index 4c37285..e921bed 100644 --- a/simjectCore.h +++ b/simjectCore.h @@ -10,5 +10,5 @@ -(NSString *) bundleIdentifier; @end -NSArray *simjectGenerateDylibList(SBApplicationInfo *appInfo); +NSString *simjectGenerateDylibList(SBApplicationInfo *appInfo); NSDictionary *simjectEnvironmentVariables(NSDictionary *origVars, SBApplicationInfo *appInfo); diff --git a/simjectCore.mm b/simjectCore.mm index 7100d5e..cab7da3 100644 --- a/simjectCore.mm +++ b/simjectCore.mm @@ -1,41 +1,45 @@ #import "simjectCore.h" -NSArray *simjectGenerateDylibList(SBApplicationInfo *appInfo) { +NSString *simjectGenerateDylibList(SBApplicationInfo *appInfo) { // Store the selected app's CFBundleID in an NSString just for easy access // If appInfo is nil, then set bundleIdentifier to com.apple.springboard // Why? Because that means this function's probably being called by respring_simulator, which is targeting SpringBoard NSString *bundleIdentifier = (appInfo) ? [appInfo bundleIdentifier] : @"com.apple.springboard"; + // Create an array containing all the filenames in dylibDir (/opt/simject) + NSError *e = nil; + NSArray *dylibDirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dylibDir error:&e]; + if (e) { + return nil; + } + // We're only interested in the plist files + NSArray *plists = [dylibDirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF endswith %@", @"plist"]]; // Create an empty mutable array that will contain a list of dylib paths to be injected into the target process NSMutableArray *dylibsToInject = [NSMutableArray array]; - // Create an array containing all the filenames in dylibDir (/opt/simject) - NSArray *dylibDirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dylibDir error:nil]; - // Loop through the entirety of dylibDir - for (NSString *plist in dylibDirContents) { + // Loop through the list of plists + for (NSString *plist in plists) { // We'll want to deal with absolute paths, so append the filename to dylibDir - plist = [dylibDir stringByAppendingString:[NSString stringWithFormat:@"/%@", plist]]; - // Now, we're only interested in the file if it's a plist - if ([[plist pathExtension] compare:@"plist" options:NSCaseInsensitiveSearch] == NSOrderedSame) { - for (NSString *entry in [NSDictionary dictionaryWithContentsOfFile:plist][@"Filter"][@"Bundles"]) { - // Now, check if the selected app's bundle ID matches anything in the plist - // Also check if any of the bundle IDs in the plist start with com.apple.* - if ([entry isEqualToString:bundleIdentifier] || [entry hasPrefix:@"com.apple."]) { - // If either of those conditions are met, inject the dylib - // Why inject com.apple.*? If a dylib is targeting that, it's likely a framework (com.apple.UIKit, etc.) - // An improvement can be made here by checking if the bundle ID is an installed system app or not... - // Such a check could be possible by using the MobileInstallationLookup function from the MobileInstallationInstall private framework - // I had considered doing that, but for the sake of releasing this in a timely manner, chose not to - [dylibsToInject addObject:[[plist stringByDeletingPathExtension] stringByAppendingString:@".dylib"]]; - } + NSString *plistPath = [dylibDir stringByAppendingPathComponent:plist]; + NSDictionary *filter = [NSDictionary dictionaryWithContentsOfFile:plist]; + for (NSString *entry in filter[@"Filter"][@"Bundles"]) { + // Now, check if the selected app's bundle ID matches anything in the plist + // Also check if any of the bundle IDs in the plist start with com.apple.* + if ([entry isEqualToString:bundleIdentifier] || [entry hasPrefix:@"com.apple."]) { + // If either of those conditions are met, inject the dylib + // Why inject com.apple.*? If a dylib is targeting that, it's likely a framework (com.apple.UIKit, etc.) + // An improvement can be made here by checking if the bundle ID is an installed system app or not... + // Such a check could be possible by using the MobileInstallationLookup function from the MobileInstallationInstall private framework + // I had considered doing that, but for the sake of releasing this in a timely manner, chose not to + [dylibsToInject addObject:[[plist stringByDeletingPathExtension] stringByAppendingString:@".dylib"]]; } } } - return dylibsToInject; + return [dylibsToInject componentsJoinedByString:@":"]; } NSDictionary *simjectEnvironmentVariables(NSDictionary *origVars, SBApplicationInfo *appInfo) { // Create a mutable dictionary containing the original environment variables NSMutableDictionary *envVars = (origVars) ? [origVars mutableCopy] : [NSMutableDictionary dictionary]; // Add/replace DYLD_INSERT_LIBRARIES with our own - envVars[@"DYLD_INSERT_LIBRARIES"] = [simjectGenerateDylibList(appInfo) componentsJoinedByString:@":"]; + envVars[@"DYLD_INSERT_LIBRARIES"] = simjectGenerateDylibList(appInfo); return envVars; }