bootstrap.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // bootstrap.c
  3. // electra
  4. //
  5. // Created by Jamie Bishop on 11/02/2018.
  6. // Copyright © 2018 Electra Team. All rights reserved.
  7. //
  8. #include "bootstrap.h"
  9. #include "file_utils.h"
  10. #include "fun_objc.h"
  11. #include "amfi_utils.h"
  12. #include "utils.h"
  13. #include <sys/stat.h>
  14. #include <sys/wait.h>
  15. #include <sys/fcntl.h>
  16. #include <unistd.h>
  17. #include <spawn.h>
  18. #define tar "/electra/tar"
  19. pid_t pd;
  20. void copy_tar() {
  21. extractGz("tar", "/electra/tar");
  22. chmod(tar, 0755);
  23. inject_trusts(1, (const char **)&(const char*[]){tar});
  24. }
  25. void copy_basebinaries() {
  26. mkdir("/electra", 0755);
  27. copy_tar();
  28. // Remove old base binaries
  29. unlink("/electra/inject_amfid");
  30. unlink("/electra/inject_launchd");
  31. unlink("/electra/launchd_payload.dylib");
  32. unlink("/electra/xpcproxy_payload.dylib");
  33. unlink("/electra/inject_ctriticald");
  34. unlink("/electra/pspawn_payload.dylib");
  35. unlink("/electra/amfid_payload.dylib");
  36. unlink("/electra/launchjailbreak");
  37. unlink("/electra/jailbreakd");
  38. extractGz("rm","/electra/rm");
  39. chmod("/electra/rm", 0755);
  40. posix_spawn(&pd, tar, NULL, NULL, (char **)&(const char*[]){ tar, "-xpvf", progname("basebinaries.tar"), "-C", "/electra", NULL }, NULL);
  41. waitpid(pd, NULL, 0);
  42. writeMessage("\n[bootstrapper] copied the required binaries into the right places\n");
  43. inject_trusts(4, (const char **)&(const char*[]){
  44. "/electra/inject_criticald",
  45. "/electra/amfid_payload.dylib",
  46. "/electra/pspawn_payload.dylib",
  47. "/electra/libjailbreak.dylib"
  48. });
  49. }
  50. void extract_bootstrap() {
  51. unlink("/bin/launchctl");
  52. /*
  53. unlink("/usr/lib/TweakInject.dylib");
  54. unlink("/usr/lib/libsubstitute.dylib");
  55. unlink("/usr/lib/libsubstrate.dylib");
  56. unlink("/usr/lib/libsubstitute.0.dylib");
  57. unlink("/usr/lib/librespring.dylib");
  58. */
  59. extractGz("launchctl", "/electra/launchctl");
  60. cp_electra("/bin/launchctl", "/electra/launchctl");
  61. chmod("/bin/launchctl", 0755);
  62. unlink("/electra/launchctl");
  63. int bootstrapped = open("/.bootstrapped_electra", O_RDONLY);
  64. if (bootstrapped != -1) {
  65. close(bootstrapped);
  66. return post_bootstrap(false);
  67. }
  68. close(bootstrapped);
  69. installingNitoTV();
  70. extractGz("bootstrap.tar", "/electra/bootstrap.tar");
  71. posix_spawn(&pd, tar, NULL, NULL, (char **)&(const char*[]){ tar, "--preserve-permissions", "-xvkf", "/electra/bootstrap.tar", "-C", "/", NULL }, NULL);
  72. waitpid(pd, NULL, 0);
  73. unlink("/electra/bootstrap.tar");
  74. //unlink("/usr/libexec/cydia/move.sh");
  75. FILE *file = fopen("/etc/hosts","w"); /* write file (create a file if it does not exist and if it does treat as empty.*/
  76. fprintf(file,"%s","##\n"); //writes
  77. fprintf(file,"%s","# Host Database\n"); //writes
  78. fprintf(file,"%s","# localhost is used to configure the loopback interface\n"); //writes
  79. fprintf(file,"%s","# when the system is booting. Do not change this entry.\n"); //writes
  80. fprintf(file,"%s","##\n"); //writes
  81. fprintf(file,"%s","127.0.0.1 localhost\n"); //writes
  82. fprintf(file,"%s","255.255.255.255 broadcasthost\n"); //writes
  83. fprintf(file,"%s","::1 localhost\n"); //writes
  84. fclose(file); /*done!*/
  85. //dont need
  86. /*
  87. file = fopen("/etc/apt/sources.list.d/electra-shim.list","w");
  88. fprintf(file,"%s","deb https://electrarepo64.coolstar.org/substrate-shim/ ./\n"); //writes
  89. fprintf(file,"%s","\n"); //writes
  90. fclose(file);
  91. cp_electra("/usr/libexec/cydia/move.sh", progname("move.sh"));
  92. */
  93. int rv = open("/.bootstrapped_electra", O_RDWR|O_CREAT);
  94. close(rv);
  95. rv = open("/.cydia_no_stash",O_RDWR|O_CREAT);
  96. close(rv);
  97. rv = open("/var/mobile/Library/Preferences/.kickstart",O_RDWR|O_CREAT);
  98. close(rv);
  99. //printf("[bootstrapper] extracted bootstrap to / \n");
  100. writeMessage("[bootstrapper] extracted bootstrap to / \n");
  101. post_bootstrap(true);
  102. }
  103. void post_bootstrap(const bool runUICache) {
  104. pid_t pd;
  105. //cant run uicache yet on iOS because we dont have code injection yet.
  106. if (runUICache){
  107. //posix_spawn(&pd, "/usr/bin/uicache", NULL, NULL, (char **)&(const char*[]){ "uicache", NULL }, NULL);
  108. //waitpid(pd, NULL, 0);
  109. }
  110. unlink(tar);
  111. //dont need the electra list
  112. /*
  113. FILE *file;
  114. file = fopen("/etc/apt/sources.list.d/electra.list","w");
  115. fprintf(file,"%s","deb https://electrarepo64.coolstar.org/ ./\n"); //writes
  116. fprintf(file,"%s","\n"); //writes
  117. fclose(file);
  118. */
  119. unlink("/usr/lib/libjailbreak.dylib");
  120. cp_electra("/usr/lib/libjailbreak.dylib","/electra/libjailbreak.dylib");
  121. inject_trusts(1, (const char **)&(const char*[]){"/bin/launchctl"});
  122. //we already have an available file
  123. /*
  124. int rv = open("/var/lib/dpkg/available", O_RDWR|O_CREAT);
  125. close(rv);
  126. */
  127. //nitoTV script specific, this is different in other versions
  128. posix_spawn(&pd, "/bin/bash", NULL, NULL, (char **)&(const char*[]){ "bash", "/usr/libexec/nito/firmware.sh", NULL }, NULL);
  129. waitpid(pd, NULL, 0);
  130. /*
  131. Next three can remain the same, i still have dropbear AND openssh in the bootstrap for the beta / testing phase in case one of them fails to fire. no way your arent SSHing in!!
  132. opensshd is on port 22 and db is on 2223 on localhost, so will need to do usb-ssh method (i think)
  133. */
  134. posix_spawn(&pd, "/bin/bash", NULL, NULL, (char **)&(const char*[]){ "bash", "/Library/dpkg/info/openssh.postinst", NULL }, NULL);
  135. waitpid(pd, NULL, 0);
  136. posix_spawn(&pd, "/bin/launchctl", NULL, NULL, (char **)&(const char*[]){ "launchctl", "load", "/Library/LaunchDaemons/com.openssh.sshd.plist", NULL }, NULL);
  137. waitpid(pd, NULL, 0);
  138. chmod("/Library/LaunchDaemons/dropbear.plist", 0600);
  139. chown("/Library/LaunchDaemons/dropbear.plist", 0, 0);
  140. chmod("/etc/dropbear", 0700);
  141. //chmod 700 /etc/dropbear
  142. posix_spawn(&pd, "/bin/launchctl", NULL, NULL, (char **)&(const char*[]){ "launchctl", "load", "/Library/LaunchDaemons/dropbear.plist", NULL }, NULL);
  143. waitpid(pd, NULL, 0);
  144. //we wont have his lists... on the meridian side in 'needed' to install nitoTV like this, but dont need to here
  145. //maybe the dpkg after step is no longer necessary.
  146. run("rm /var/lib/apt/lists/apt.saurik.com*");
  147. blockSaurikRepo();
  148. char *myenviron[] = {
  149. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games",
  150. "PS1=\\h:\\w \\u\\$ ",
  151. NULL
  152. };
  153. posix_spawn(&pd, "/usr/bin/dpkg", NULL, NULL, (char **)&(const char*[]){ "dpkg", "-i", "--refuse-downgrade", progname("safetynet.deb"), NULL }, (char **)&myenviron);
  154. waitpid(pd, NULL, 0);
  155. printf("[bootstrapper] device has been bootstrapped!\n");
  156. if (runUICache){
  157. nitoTVDone();
  158. }
  159. }