bootstrap.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. kern_return_t do_bootstrap() {
  15. char path[256];
  16. uint32_t size = sizeof(path);
  17. _NSGetExecutablePath(path, &size);
  18. char *pt = realpath(path, 0);
  19. pid_t pd = 0;
  20. NSString* execpath = [[NSString stringWithUTF8String:pt] stringByDeletingLastPathComponent];
  21. int f = open("/.installed_g0blin", O_RDONLY);
  22. if (f == -1) {
  23. LOG("bootstrap not yet installed");
  24. NSString* bootstrap = [execpath stringByAppendingPathComponent:@"bootstrap.tar"];
  25. NSString* tar = [execpath stringByAppendingPathComponent:@"tar"];
  26. NSString* launchctl = [execpath stringByAppendingPathComponent:@"launchctl"];
  27. unlink("/bin/tar");
  28. unlink("/bin/launchctl");
  29. // copy over launchctl
  30. copyfile([launchctl UTF8String], "/bin/launchctl", 0, COPYFILE_ALL);
  31. chmod("/bin/launchctl", 0755);
  32. // copy over tar
  33. copyfile([tar UTF8String], "/bin/tar", 0, COPYFILE_ALL);
  34. chmod("/bin/tar", 0777);
  35. // unpack bootstrap tarball
  36. chdir("/");
  37. posix_spawn(&pd, "/bin/tar", 0, 0, (char**)&(const char*[]){"/bin/tar", "--preserve-permissions", "--no-overwrite-dir", "-xvf", [bootstrap UTF8String], NULL}, NULL);
  38. NSLog(@"pid = %x", pd);
  39. waitpid(pd, 0, 0);
  40. LOG("bootstrap unpacked");
  41. // leave a reminder that we did this already
  42. open("/.installed_g0blin", O_RDWR|O_CREAT);
  43. // disable Cydia filesystem stashing
  44. open("/.cydia_no_stash", O_RDWR|O_CREAT);
  45. // block some Apple IPs
  46. 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);
  47. 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);
  48. 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);
  49. 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);
  50. LOG("modified hosts file");
  51. // update icons
  52. LOG("running uicache");
  53. posix_spawn(&pd, "/usr/bin/uicache", 0, 0, (char**)&(const char*[]){"/usr/bin/uicache", NULL}, NULL);
  54. // set SBShowNonDefaultSystemApps
  55. posix_spawn(&pd, "killall", 0, 0, (char**)&(const char*[]){"killall", "-SIGSTOP", "cfprefsd", NULL}, NULL);
  56. NSMutableDictionary *plist = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.apple.springboard.plist"];
  57. [plist setObject:[NSNumber numberWithBool:YES] forKey:@"SBShowNonDefaultSystemApps"];
  58. [plist writeToFile:@"/var/mobile/Library/Preferences/com.apple.springboard.plist" atomically:YES];
  59. posix_spawn(&pd, "killall", 0, 0, (char**)&(const char*[]){"killall", "-9", "cfprefsd", NULL}, NULL);
  60. }
  61. LOG("bootstrapped");
  62. // copy reload
  63. NSString *reload = [execpath stringByAppendingPathComponent:@"reload"];
  64. unlink("/usr/libexec/reload");
  65. copyfile([reload UTF8String], "/usr/libexec/reload", 0, COPYFILE_ALL);
  66. chmod("/usr/libexec/reload", 0755);
  67. chown("/usr/libexec/reload", 0, 0);
  68. // copy 0.reload.plist
  69. NSString *reloadPlist = [execpath stringByAppendingPathComponent:@"0.reload.plist"];
  70. unlink("/Library/LaunchDaemons/0.reload.plist");
  71. copyfile([reloadPlist UTF8String], "/Library/LaunchDaemons/0.reload.plist", 0, COPYFILE_ALL);
  72. chmod("/Library/LaunchDaemons/0.reload.plist", 0644);
  73. chown("/Library/LaunchDaemons/0.reload.plist", 0, 0);
  74. // copy dropbear.plist
  75. NSString *dropbearPlist = [execpath stringByAppendingPathComponent:@"dropbear.plist"];
  76. unlink("/Library/LaunchDaemons/dropbear.plist");
  77. copyfile([dropbearPlist UTF8String], "/Library/LaunchDaemons/dropbear.plist", 0, COPYFILE_ALL);
  78. chmod("/Library/LaunchDaemons/dropbear.plist", 0644);
  79. chown("/Library/LaunchDaemons/dropbear.plist", 0, 0);
  80. // stop SU daemon
  81. unlink("/System/Library/LaunchDaemons/com.apple.mobile.softwareupdated.plist");
  82. // update permissions
  83. chmod("/private", 0777);
  84. chmod("/private/var", 0777);
  85. chmod("/private/var/mobile", 0777);
  86. chmod("/private/var/mobile/Library", 0777);
  87. chmod("/private/var/mobile/Library/Preferences", 0777);
  88. LOG("updated permissions");
  89. // kill OTA updater
  90. pid_t pid;
  91. unlink("/var/MobileAsset/Assets/com_apple_MobileAsset_SoftwareUpdate");
  92. posix_spawn(&pid, "touch", 0, 0, (char**)&(const char*[]){"touch", "/var/MobileAsset/Assets/com_apple_MobileAsset_SoftwareUpdate", NULL}, NULL);
  93. chmod("/var/MobileAsset/Assets/com_apple_MobileAsset_SoftwareUpdate", 000);
  94. chown("/var/MobileAsset/Assets/com_apple_MobileAsset_SoftwareUpdate", 0, 0);
  95. LOG("killed OTA updater");
  96. // WriteAnywhere64(bsd_task+0x100, orig_cred);
  97. // reload
  98. LOG("reloading");
  99. posix_spawn(&pid, "/bin/launchctl", 0, 0, (char**)&(const char*[]){"/bin/launchctl", "load", "/Library/LaunchDaemons/0.reload.plist", NULL}, NULL);
  100. sleep(2);
  101. // done.
  102. return KERN_SUCCESS;
  103. }