bootstrap.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // bootstrap.m
  3. // g0blin
  4. //
  5. // Created by Sticktron on 2017-12-27.
  6. // Copyright © 2017 xerub. All rights reserved.
  7. // Copyright © 2017 qwertyoruiop. All rights reserved.
  8. //
  9. #include "common.h"
  10. #include <sys/spawn.h>
  11. #include <sys/stat.h>
  12. #include <copyfile.h>
  13. #include <mach-o/dyld.h>
  14. #include <stdint.h>
  15. #include <spawn.h>
  16. #include <sys/wait.h>
  17. extern char **environ;
  18. int RunCmd(const char *cmd)
  19. {
  20. pid_t pid;
  21. char *argv[] = {"sh", "-c", (char*)cmd, NULL};
  22. int status;
  23. fprintf(stderr, "Run command: %s\n", cmd);
  24. status = posix_spawn(&pid, "/usr/bin/bash", NULL, NULL, argv, environ);
  25. if (status == 0) {
  26. printf("Child pid: %i\n", pid);
  27. if (waitpid(pid, &status, 0) != -1) {
  28. printf("Child exited with status %i\n", status);
  29. } else {
  30. perror("waitpid");
  31. }
  32. } else {
  33. // printf("posix_spawn: %s\n", strerror(status));
  34. }
  35. return status;
  36. }
  37. kern_return_t do_bootstrap(bool force) {
  38. char path[256];
  39. uint32_t size = sizeof(path);
  40. _NSGetExecutablePath(path, &size);
  41. char *pt = realpath(path, 0);
  42. pid_t pd = 0;
  43. NSString* execpath = [[NSString stringWithUTF8String:pt] stringByDeletingLastPathComponent];
  44. int f = open("/.installed_g0blin_rc0", O_RDONLY);
  45. if (f == -1 || force) {
  46. LOG("installing bootstrap...");
  47. NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
  48. NSLog(@"bundlePath: %@", bundlePath);
  49. NSString *tarPath = [[NSBundle mainBundle] pathForResource:@"tar" ofType:nil];
  50. NSLog(@"tar path: %@", tarPath);
  51. NSError *theError = nil;
  52. NSArray *pathArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/bin" error:&theError];
  53. NSString* bootstrap = [execpath stringByAppendingPathComponent:@"bootstrap.tar"];
  54. NSString* bintools = [execpath stringByAppendingPathComponent:@"binpack64-256.tar"];
  55. NSString* tar = [execpath stringByAppendingPathComponent:@"tar"];
  56. NSString* launchctl = [execpath stringByAppendingPathComponent:@"launchctl"];
  57. NSString* motd = [execpath stringByAppendingPathComponent:@"motd"];
  58. unlink("/bin/tar");
  59. unlink("/bin/launchctl");
  60. unlink("/private/etc/motd");
  61. copyfile([tar UTF8String], "/bin/tar", 0, COPYFILE_ALL);
  62. chmod("/bin/tar", 0755);
  63. pathArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/bin" error:&theError];
  64. //mkdir("/jb", 0755);
  65. chdir("/");
  66. // posix_spawn(&pd, "/bin/tar", 0, 0, (char**)&(const char*[]){"/bin/tar", "--preserve-permissions", "-k", "-xvf", [bootstrap UTF8String], NULL}, NULL);
  67. posix_spawn(&pd, "/bin/tar", 0, 0, (char**)&(const char*[]){"/bin/tar", "--preserve-permissions","-xvf", [bootstrap UTF8String], NULL}, NULL);
  68. waitpid(pd, 0, 0);
  69. LOG("bootstrap unpacked");
  70. copyfile([motd UTF8String], "/private/etc/motd", 0, COPYFILE_ALL);
  71. chmod("/private/etc/motd", 0755);
  72. /*
  73. mkdir("/jb", 0755);
  74. NSString *theCommand = [NSString stringWithFormat:@"/usr/bin/tar fxp %@ -C /jb", bintools];
  75. */
  76. LOG("extracting jons bootstrap to root... caution!");
  77. //testing
  78. NSString *theCommand = [NSString stringWithFormat:@"/usr/bin/tar fxpk %@ -C /", bintools];
  79. RunCmd([theCommand UTF8String]);
  80. copyfile([launchctl UTF8String], "/bin/launchctl", 0, COPYFILE_ALL);
  81. chmod("/bin/launchctl", 0755);
  82. //chown("-R", <#uid_t#>, <#gid_t#>)
  83. unlink(".installed_g0blin");
  84. open("/.installed_g0blin_rc0", O_RDWR|O_CREAT);
  85. //open("/.cydia_no_stash", O_RDWR|O_CREAT);
  86. // run nito install scripts
  87. {
  88. char *name = "/var/lib/dpkg/info/mobilesubstrate.postinst";
  89. RunCmd(name);
  90. RunCmd("/usr/libexec/substrate");
  91. RunCmd("/usr/libexec/nito/firmware.sh");
  92. //posix_spawn(&pd, name, 0, 0, (char**)&(const char*[]){name, NULL}, NULL);
  93. //waitpid(pd, 0, 0);
  94. //RunCmd(@")
  95. }
  96. LOG("ran nitoTV extrainst scripts");
  97. // block some Apple IPs
  98. posix_spawn(&pd, "/bin/bash", 0, 0, (char**)&(const char*[]){"/bin/bash", "-c", """echo '127.0.0.1 iphonesubmissions.apple.com' >> /etc/hosts""", NULL}, NULL);
  99. posix_spawn(&pd, "/bin/bash", 0, 0, (char**)&(const char*[]){"/bin/bash", "-c", """echo '127.0.0.1 radarsubmissions.apple.com' >> /etc/hosts""", NULL}, NULL);
  100. posix_spawn(&pd, "/bin/bash", 0, 0, (char**)&(const char*[]){"/bin/bash", "-c", """echo '127.0.0.1 mesu.apple.com' >> /etc/hosts""", NULL}, NULL);
  101. posix_spawn(&pd, "/bin/bash", 0, 0, (char**)&(const char*[]){"/bin/bash", "-c", """echo '127.0.0.1 appldnld.apple.com' >> /etc/hosts""", NULL}, NULL);
  102. LOG("modified hosts file");
  103. /*
  104. // set SBShowNonDefaultSystemApps = YES so we can see Cydia (TODO: set via cfprefsd instead?)
  105. posix_spawn(&pd, "killall", 0, 0, (char**)&(const char*[]){"killall", "-SIGSTOP", "cfprefsd", NULL}, NULL);
  106. NSMutableDictionary *plist = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.apple.springboard.plist"];
  107. [plist setObject:[NSNumber numberWithBool:YES] forKey:@"SBShowNonDefaultSystemApps"];
  108. [plist writeToFile:@"/var/mobile/Library/Preferences/com.apple.springboard.plist" atomically:YES];
  109. posix_spawn(&pd, "killall", 0, 0, (char**)&(const char*[]){"killall", "-9", "cfprefsd", NULL}, NULL);
  110. LOG("modified com.apple.springboard.plist");
  111. */
  112. // rebuild icon cache
  113. // LOG("uicache...");
  114. //posix_spawn(&pd, "/usr/bin/uicache", 0, 0, (char**)&(const char*[]){"/usr/bin/uicache", NULL}, NULL);
  115. //waitpid(pd, 0, 0);
  116. LOG("finished installing bootstrap");
  117. }
  118. LOG("bootstrap ready");
  119. // copy reload
  120. NSString *reload = [execpath stringByAppendingPathComponent:@"reload"];
  121. unlink("/usr/libexec/reload");
  122. copyfile([reload UTF8String], "/usr/libexec/reload", 0, COPYFILE_ALL);
  123. chmod("/usr/libexec/reload", 0755);
  124. chown("/usr/libexec/reload", 0, 0);
  125. // copy 0.reload.plist
  126. NSString *reloadPlist = [execpath stringByAppendingPathComponent:@"0.reload.plist"];
  127. unlink("/Library/LaunchDaemons/0.reload.plist");
  128. copyfile([reloadPlist UTF8String], "/Library/LaunchDaemons/0.reload.plist", 0, COPYFILE_ALL);
  129. chmod("/Library/LaunchDaemons/0.reload.plist", 0644);
  130. chown("/Library/LaunchDaemons/0.reload.plist", 0, 0);
  131. // copy dropbear.plist
  132. NSString *dropbearPlist = [execpath stringByAppendingPathComponent:@"dropbear.plist"];
  133. unlink("/Library/LaunchDaemons/dropbear.plist");
  134. copyfile([dropbearPlist UTF8String], "/Library/LaunchDaemons/dropbear.plist", 0, COPYFILE_ALL);
  135. chmod("/Library/LaunchDaemons/dropbear.plist", 0644);
  136. chown("/Library/LaunchDaemons/dropbear.plist", 0, 0);
  137. chmod("/etc/dropbear", 0755);
  138. //RunCmd("/usr/bin/chown -R root:wheel /usr/local/bin");
  139. //RunCmd("/usr/bin/chown -R root:wheel /usr/local/bin");
  140. //RunCmd("/usr/bin/chown -R root:wheel /private/etc/dropbear");
  141. //RunCmd("/usr/bin/chown -R root:wheel /private/etc/profile.d");
  142. //RunCmd("/usr/bin/chown -R root:wheel /private/etc/profile");
  143. // stop SU daemon
  144. unlink("/System/Library/LaunchDaemons/com.apple.mobile.softwareupdated.plist");
  145. // update permissions
  146. chmod("/private", 0777);
  147. chmod("/private/var", 0777);
  148. chmod("/private/var/mobile", 0777);
  149. chmod("/private/var/mobile/Library", 0777);
  150. chmod("/private/var/mobile/Library/Preferences", 0777);
  151. // kill OTA updater
  152. pid_t pid;
  153. unlink("/var/MobileAsset/Assets/com_apple_MobileAsset_SoftwareUpdate");
  154. posix_spawn(&pid, "touch", 0, 0, (char**)&(const char*[]){"touch", "/var/MobileAsset/Assets/com_apple_MobileAsset_SoftwareUpdate", NULL}, NULL);
  155. chmod("/var/MobileAsset/Assets/com_apple_MobileAsset_SoftwareUpdate", 000);
  156. chown("/var/MobileAsset/Assets/com_apple_MobileAsset_SoftwareUpdate", 0, 0);
  157. LOG("killed OTA updater");
  158. //RunCmd("/usr/local/bin/dropbear -F -R -p 22 &");
  159. //RunCmd("/jb/usr/local/bin/dropbear -R --shell /jb/bin/bash");
  160. LOG("bootstrapped");
  161. return KERN_SUCCESS; // TODO: handle errors?
  162. }