bootstrap.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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("[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. /*
  86. file = fopen("/etc/apt/sources.list.d/electra-shim.list","w");
  87. fprintf(file,"%s","deb https://electrarepo64.coolstar.org/substrate-shim/ ./\n"); //writes
  88. fprintf(file,"%s","\n"); //writes
  89. fclose(file);
  90. cp_electra("/usr/libexec/cydia/move.sh", progname("move.sh"));
  91. */
  92. int rv = open("/.bootstrapped_electra", O_RDWR|O_CREAT);
  93. close(rv);
  94. rv = open("/.cydia_no_stash",O_RDWR|O_CREAT);
  95. close(rv);
  96. rv = open("/var/mobile/Library/Preferences/.kickstart",O_RDWR|O_CREAT);
  97. close(rv);
  98. //printf("[bootstrapper] extracted bootstrap to / \n");
  99. writeMessage("[bootstrapper] extracted bootstrap to / \n");
  100. post_bootstrap(true);
  101. }
  102. void post_bootstrap(const bool runUICache) {
  103. pid_t pd;
  104. if (runUICache){
  105. posix_spawn(&pd, "/usr/bin/uicache", NULL, NULL, (char **)&(const char*[]){ "uicache", NULL }, NULL);
  106. waitpid(pd, NULL, 0);
  107. //posix_spawn(&pd, "/usr/bin/killall", NULL, NULL, (char **)&(const char*[]){ "-9", "backboardd", NULL }, NULL);
  108. //waitpid(pd, NULL, 0);
  109. }
  110. unlink(tar);
  111. /*
  112. FILE *file;
  113. file = fopen("/etc/apt/sources.list.d/electra.list","w");
  114. fprintf(file,"%s","deb https://electrarepo64.coolstar.org/ ./\n"); //writes
  115. fprintf(file,"%s","\n"); //writes
  116. fclose(file);
  117. */
  118. unlink("/usr/lib/libjailbreak.dylib");
  119. cp_electra("/usr/lib/libjailbreak.dylib","/electra/libjailbreak.dylib");
  120. inject_trusts(1, (const char **)&(const char*[]){"/bin/launchctl"});
  121. /*
  122. int rv = open("/var/lib/dpkg/available", O_RDWR|O_CREAT);
  123. close(rv);
  124. */
  125. posix_spawn(&pd, "/bin/bash", NULL, NULL, (char **)&(const char*[]){ "bash", "/Library/dpkg/info/openssh.postinst", NULL }, NULL);
  126. waitpid(pd, NULL, 0);
  127. posix_spawn(&pd, "/bin/launchctl", NULL, NULL, (char **)&(const char*[]){ "launchctl", "load", "/Library/LaunchDaemons/com.openssh.sshd.plist", NULL }, NULL);
  128. waitpid(pd, NULL, 0);
  129. chmod("/Library/LaunchDaemons/dropbear.plist", 0600);
  130. chown("/Library/LaunchDaemons/dropbear.plist", 0, 0);
  131. chmod("/etc/dropbear", 0700);
  132. //chmod 700 /etc/dropbear
  133. posix_spawn(&pd, "/bin/launchctl", NULL, NULL, (char **)&(const char*[]){ "launchctl", "load", "/Library/LaunchDaemons/dropbear.plist", NULL }, NULL);
  134. waitpid(pd, NULL, 0);
  135. /*
  136. run("rm /var/lib/apt/lists/apt.saurik.com*");
  137. blockSaurikRepo();
  138. char *myenviron[] = {
  139. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games",
  140. "PS1=\\h:\\w \\u\\$ ",
  141. NULL
  142. };
  143. posix_spawn(&pd, "/usr/bin/dpkg", NULL, NULL, (char **)&(const char*[]){ "dpkg", "-i", "--refuse-downgrade", progname("nitotv.deb"), NULL }, (char **)&myenviron);
  144. waitpid(pd, NULL, 0);
  145. */
  146. printf("[bootstrapper] device has been bootstrapped!\n");
  147. if (runUICache){
  148. nitoTVDone();
  149. }
  150. }