main.x 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * AppEnabler for nitoTV
  3. *
  4. * Makes it possible to load Applications from /var/mobile/Applications
  5. *
  6. * By Luca Todesco and Kevin Bradley
  7. *
  8. */
  9. #import <CoreFoundation/CoreFoundation.h>
  10. #import <Foundation/Foundation.h>
  11. #include <mach-o/dyld.h>
  12. #import <objc/runtime.h>
  13. //#import "rocketbootstrap.h"
  14. //#import "AppSupport/CPDistributedMessagingCenter.h"
  15. #import "../PBReloadHelper.h"
  16. #import <Foundation/Foundation.h>
  17. #import "AppSupport/CPDistributedMessagingCenter.h"
  18. @interface NSDistributedNotificationCenter : NSNotificationCenter
  19. + (id)defaultCenter;
  20. - (void)addObserver:(id)arg1 selector:(SEL)arg2 name:(id)arg3 object:(id)arg4;
  21. - (void)postNotificationName:(id)arg1 object:(id)arg2 userInfo:(id)arg3;
  22. @end
  23. @interface MessageHandler : NSObject
  24. -(void)runAppEnabler:(id)a;
  25. @end
  26. @implementation MessageHandler
  27. -(void)runAppEnabler:(id)a
  28. {
  29. //NSLog(@"#### for luck!");
  30. [PBReloadHelper reloadApplications];
  31. }
  32. @end
  33. %class LSContext
  34. %class FSNode
  35. /*
  36. Some fun magic happens in here. We hook _MobileInstallationEnumerateAllInstalledItemDictionaries
  37. in tvOS 10 and _MobileInstallationCopyInstalledAppsForLaunchServices in tvOS 9 to inject
  38. our "whitelist" of applications that is created during uicache process.
  39. */
  40. //tvOS 10 function for _MobileInstallationEnumerateAllInstalledItemDictionaries
  41. id (*original_func)(uint64_t a, id b);
  42. //tvOS 9 function for _MobileInstallationCopyInstalledAppsForLaunchServices
  43. id (*original_func2)(uint64_t a);
  44. //hook for _MobileInstallationCopyInstalledAppsForLaunchServices
  45. id we_really_out_here_nine(uint64_t a)
  46. {
  47. /*
  48. in tvOS 9 this list is just one huge plist that we need to inject ourselves into the
  49. System key of.
  50. */
  51. id retv = original_func2(a);
  52. //read the list created in uicache of apps to inject
  53. NSString* pth = @"/var/mobile/Library/Preferences/kjc.appenabler.state.plist";
  54. NSArray* arr = [NSArray arrayWithContentsOfFile: pth];
  55. if(!arr) return retv;
  56. NSLog(@"got apps to inject!!");
  57. [retv autorelease];
  58. retv = [retv mutableCopy];
  59. NSLog(@"retv allKeys:%@", [(NSDictionary*)retv allKeys]);
  60. //get the System key of the installed plist
  61. id sysdb = retv[@"System"];
  62. id pkp = retv[@"PluginKitPlugin"];
  63. if(sysdb){
  64. NSLog(@"injecting to system apps");
  65. sysdb=[sysdb mutableCopy];
  66. pkp=[pkp mutableCopy];
  67. retv[@"System"] = sysdb;
  68. retv[@"PluginKitPlugin"] = pkp;
  69. for (NSDictionary* appb in arr)
  70. {
  71. NSString *appType = appb[@"ApplicationType"];
  72. if (![appType isEqualToString:@"PluginKitPlugin"])
  73. {
  74. NSLog(@"injecting %@", appb[@"CFBundleIdentifier"]);
  75. if (sysdb[appb[@"CFBundleIdentifier"]])
  76. {
  77. NSLog(@"already present!! WTF??? overwriting..");
  78. }
  79. //add current CFBundleIdentifier to System bundle
  80. sysdb[appb[@"CFBundleIdentifier"]] = appb;
  81. } else { //its a plugin
  82. NSLog(@"injecting %@", appb[@"CFBundleIdentifier"]);
  83. if (pkp[appb[@"CFBundleIdentifier"]])
  84. {
  85. NSLog(@"already present!! WTF??? overwriting..");
  86. }
  87. //add current CFBundleIdentifier to System bundle
  88. pkp[appb[@"CFBundleIdentifier"]] = appb;
  89. }
  90. }
  91. }
  92. //NSLog(@"return: %@",retv);
  93. //at this point we've injected our bundle ids into the System list/
  94. //from here, some other process adds the entry to PBAppDepot for us! :)
  95. return retv;
  96. }
  97. //hook for _MobileInstallationEnumerateAllInstalledItemDictionaries
  98. id we_really_out_here(uint64_t a, uint64_t (^block)(id b1, uint64_t b2, uint64_t b3))
  99. {
  100. //NSLog(@"Stack trace : %@",[NSThread callStackSymbols]);
  101. //define the return block
  102. uint64_t (^blockz)(id b1, uint64_t b2, uint64_t b3) = ^(id b1, uint64_t b2, uint64_t b3){
  103. //NSLog(@"called!!! %@ %llx %llx", b1, b2, b3);
  104. return block(b1, b2, b3);
  105. };
  106. //load our whitelist that is created in uicache
  107. NSString* pth = @"/var/mobile/Library/Preferences/kjc.appenabler.state.plist";
  108. NSArray* arr = [NSArray arrayWithContentsOfFile: pth];
  109. //get original return value
  110. id retv = original_func(a, blockz);
  111. //NSLog(@"crack out of coke");
  112. //loop through our apps an call the block to add the application to the list,
  113. //from here it will be added to PBAppDepot automatically, but in a disabled state.
  114. for (NSDictionary* appb in arr)
  115. {
  116. //blockz(@{@"ApplicationType": @"System", @"CFBundleIdentifier": appb[0], @"CodeInfoIdentifier": appb[0], @"Path": appb[1]},0,0);
  117. blockz(appb,0,0);
  118. }
  119. return retv;
  120. }
  121. %ctor
  122. {
  123. dlopen("/System/Library/PrivateFrameworks/MobileInstallation.framework/MobileInstallation", RTLD_LAZY);
  124. MSImageRef gangshit = MSGetImageByName("/System/Library/PrivateFrameworks/MobileInstallation.framework/MobileInstallation");
  125. //tvos 10 symbol
  126. void* weouthere = MSFindSymbol(gangshit, "_MobileInstallationEnumerateAllInstalledItemDictionaries");
  127. NSLog(@"tvOS 10 hooking %p", weouthere);
  128. if (weouthere)
  129. {
  130. MSHookFunction((void*)weouthere, (void*)we_really_out_here, (void**)&original_func);
  131. //void (*lolz)() = weouthere;
  132. //lolz();
  133. }
  134. //tvos 9 symbol
  135. void* weouthere2 = MSFindSymbol(gangshit, "_MobileInstallationCopyInstalledAppsForLaunchServices");
  136. NSLog(@"ios 9 hooking %p", weouthere2);
  137. if (weouthere2)
  138. {
  139. MSHookFunction((void*)weouthere2, (void*)we_really_out_here_nine, (void**)&original_func2);
  140. //void (*lolz)() = weouthere;
  141. //lolz();
  142. }
  143. //NSLog(@"before note center");
  144. NSDistributedNotificationCenter* notificationCenter = [NSDistributedNotificationCenter defaultCenter];
  145. //NSLog(@"after note center");
  146. [notificationCenter addObserver:[MessageHandler new] selector:@selector(runAppEnabler:) name:@"kjc.AppEnabler.recache" object:nil];
  147. // NSLog(@"after observe");
  148. }