ViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. //
  2. // ViewController.m
  3. // g0blinTV
  4. //
  5. // Created by Kevin Bradley on 1/15/18.
  6. // Copyright © 2018 Sticktron. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. #include <string.h>
  10. #include "v0rtex2.h"
  11. #include "common2.h"
  12. #include "offsets2.h"
  13. #include "kernel.h"
  14. #include "kpp.h"
  15. #include "remount.h"
  16. #include "bootstrap.h"
  17. #include <sys/utsname.h>
  18. #import <AVFoundation/AVFoundation.h>
  19. #import <AVKit/AVKit.h>
  20. #import "SVProgressHUD.h"
  21. #import "UIColor+Additions.h"
  22. //#define GRAPE [UIColor colorWithRed:0.5 green:0 blue:1 alpha:1]
  23. #define GRAPE [UIColor colorFromHex:@"25b24a"]
  24. @implementation FocusedButton
  25. - (instancetype)initWithCoder:(NSCoder *)coder
  26. {
  27. self = [super initWithCoder:coder];
  28. if (self) {
  29. self.layer.cornerRadius = 5;
  30. self.backgroundColor = [UIColor lightGrayColor];
  31. self.focusColor = [UIColor redColor];
  32. self.unfocusedColor = [UIColor lightGrayColor];
  33. }
  34. return self;
  35. }
  36. - (void)buttonColors:(UIColor *)focusedColor andUnfocused:(UIColor *)unfocusedColor {
  37. self.focusColor = focusedColor;
  38. self.unfocusedColor = unfocusedColor;
  39. self.backgroundColor = unfocusedColor;
  40. }
  41. - (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator {
  42. [coordinator addCoordinatedAnimations:^{
  43. if (self.focused) {
  44. self.backgroundColor = self.focusColor;
  45. self.transform = CGAffineTransformMakeScale(1.1, 1.1);
  46. self.layer.shadowColor = [UIColor blackColor].CGColor;
  47. self.layer.shadowOffset = CGSizeMake(0, 27);
  48. self.layer.shadowOpacity = 0.25;
  49. self.layer.shadowRadius = 10;
  50. self.clipsToBounds = NO;
  51. }
  52. else {
  53. self.transform = CGAffineTransformIdentity;
  54. self.backgroundColor = self.unfocusedColor;
  55. self.clipsToBounds = YES;
  56. }
  57. } completion:nil];
  58. }
  59. @end
  60. @interface ViewController ()
  61. {
  62. BOOL _force;
  63. BOOL _disabled;
  64. }
  65. @property (weak, nonatomic) IBOutlet UIImageView *logoView;
  66. @property (weak, nonatomic) IBOutlet FocusedButton *goButton;
  67. @property (weak, nonatomic) IBOutlet FocusedButton *reinstallBootstrap;
  68. @property (weak, nonatomic) IBOutlet UITextView *consoleView;
  69. @property (weak, nonatomic) IBOutlet UIButton *settingsButton;
  70. @property (weak, nonatomic) IBOutlet UILabel *reinstallBootstrapLabel;
  71. @end
  72. static task_t tfp0;
  73. static uint64_t kslide;
  74. static uint64_t kbase;
  75. static uint64_t kcred;
  76. BOOL respringNeeded;
  77. BOOL fun;
  78. AVPlayer *player;
  79. AVPlayerViewController *cont;
  80. @implementation ViewController
  81. - (IBAction)toggleForce:(id)sender
  82. {
  83. _force = !_force;
  84. if (_force)
  85. {
  86. [self.reinstallBootstrap setTitle:@"Force Reinstall Bootstrap" forState:UIControlStateNormal];
  87. [self.reinstallBootstrap setTitle:@"Force Reinstall Bootstrap" forState:UIControlStateFocused];
  88. } else {
  89. [self.reinstallBootstrap setTitle:@"Dont Reinstall Bootstrap" forState:UIControlStateNormal];
  90. [self.reinstallBootstrap setTitle:@"Dont Reinstall Bootstrap" forState:UIControlStateFocused];
  91. }
  92. }
  93. - (void)viewDidLoad {
  94. [super viewDidLoad];
  95. // Do any additional setup after loading the view, typically from a nib.
  96. [self.goButton buttonColors:GRAPE
  97. andUnfocused:[UIColor darkTextColor]];
  98. [self.reinstallBootstrap buttonColors:GRAPE
  99. andUnfocused:[UIColor darkTextColor]];
  100. //self.consoleView.layer.cornerRadius = 6;
  101. self.consoleView.text = nil;
  102. self.consoleView.editable = false;
  103. self.consoleView.userInteractionEnabled = true;
  104. self.consoleView.layoutManager.allowsNonContiguousLayout = NO;
  105. self.goButton.layer.cornerRadius = 16;
  106. _force = NO;
  107. //self.reinstallBootstrapLabel.hidden = YES;
  108. // print kernel version
  109. struct utsname u;
  110. uname(&u);
  111. [self log:[NSString stringWithFormat:@"%s \n", u.version]];
  112. _disabled = NO;
  113. // abort if already jailbroken
  114. //if (strstr(u.version, "MarijuanARM")) {
  115. UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithUTF8String:u.version] message:nil preferredStyle:UIAlertControllerStyleAlert];
  116. [self presentViewController:alert animated:YES completion:nil];
  117. if ([[NSString stringWithUTF8String:u.version] containsString:@"MarijuanARM"]){
  118. self.goButton.enabled = NO;
  119. _disabled = YES;
  120. //self.goButton.backgroundColor = UIColor.darkGrayColor;
  121. [self.goButton setTitle:@"jailbroke yo!" forState:UIControlStateDisabled];
  122. [self.goButton buttonColors:UIColor.darkGrayColor
  123. andUnfocused:[UIColor darkGrayColor]];
  124. }
  125. // try to load offsets for device
  126. /*
  127. if (init_offsets() == KERN_SUCCESS) {
  128. [self log:@"Ready. \n"];
  129. } else {
  130. self.goButton.enabled = NO;
  131. self.goButton.backgroundColor = UIColor.darkGrayColor;
  132. [self.goButton setTitle:@"device not supported" forState:UIControlStateDisabled];
  133. }
  134. */
  135. // fun
  136. UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fun:)];
  137. doubleTap.delaysTouchesBegan = YES;
  138. doubleTap.numberOfTapsRequired = 3;
  139. [self.logoView addGestureRecognizer:doubleTap];
  140. self.logoView.userInteractionEnabled = YES;
  141. }
  142. - (void)didReceiveMemoryWarning {
  143. [super didReceiveMemoryWarning];
  144. // Dispose of any resources that can be recreated.
  145. }
  146. - (void)log:(NSString *)text {
  147. dispatch_async(dispatch_get_main_queue(), ^{
  148. [UIView setAnimationsEnabled:NO];
  149. self.consoleView.text = [NSString stringWithFormat:@"%@%@ \n", self.consoleView.text, text];
  150. [self.consoleView scrollRangeToVisible:NSMakeRange([self.consoleView.text length], 0)];
  151. [UIView setAnimationsEnabled:YES];
  152. });
  153. }
  154. - (IBAction)prepareForUnwind:(UIStoryboardSegue *)segue {
  155. //segue exit marker
  156. //SettingsController *settingsController = segue.sourceViewController;
  157. //self.reinstallBootstrapLabel.hidden = !settingsController.reinstallBootstrapSwitch.on;
  158. }
  159. - (IBAction)go:(UIButton *)sender {
  160. if (_disabled) {
  161. LOG("Already jailbroken bro!");
  162. return;
  163. }
  164. dispatch_async(dispatch_get_main_queue(), ^{
  165. [self.consoleView.layoutManager ensureLayoutForTextContainer:self.consoleView.textContainer];
  166. });
  167. if (respringNeeded == YES) {
  168. [self restart];
  169. return;
  170. }
  171. self.goButton.enabled = NO;
  172. self.goButton.backgroundColor = UIColor.darkGrayColor;
  173. [self.goButton setTitle:@"jailbreaking" forState:UIControlStateDisabled];
  174. [self log:@"exploiting kernel"];
  175. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  176. offsets_t *off = get_offsets();
  177. if (off)
  178. {
  179. kern_return_t ret = v0rtex(off, &tfp0, &kslide, &kcred);
  180. if (ret != KERN_SUCCESS) {
  181. self.goButton.enabled = YES;
  182. self.goButton.backgroundColor = GRAPE;
  183. [self.goButton setTitle:@"try again" forState:UIControlStateNormal];
  184. [self log:@"ERROR: exploit failed \n"];
  185. [self kludgeBoot];
  186. return;
  187. }
  188. LOG("v0rtex was successful");
  189. LOG("tfp0 -> %x", tfp0);
  190. LOG("slide -> 0x%llx", kslide);
  191. kbase = kslide + 0xFFFFFFF007004000;
  192. LOG("kern base -> 0x%llx", kbase);
  193. LOG("kern cred -> 0x%llx", kcred);
  194. [self bypassKPP];
  195. }
  196. });
  197. }
  198. - (void)bypassKPP {
  199. [self log:@"pwning kernel"];
  200. if (do_kpp(1, 0, kbase, kslide, tfp0) == KERN_SUCCESS) {
  201. LOG("you down with kpp? yeah you know me");
  202. [self remount];
  203. } else {
  204. [self log:@"ERROR: kpp bypass failed \n"];
  205. }
  206. }
  207. - (void)remount {
  208. [self log:@"remounting"];
  209. if (do_remount(kslide) == KERN_SUCCESS) {
  210. [self bootstrap];
  211. } else {
  212. [self log:@"ERROR: failed to remount system partition \n"];
  213. }
  214. }
  215. - (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
  216. {
  217. if (context.nextFocusedView == self.goButton)
  218. {
  219. // set background color
  220. self.goButton.backgroundColor = [UIColor redColor];
  221. }
  222. }
  223. //we know retrying with reboot, and if we detect faiure wait a few seconds and try again to force a reboot
  224. - (void)kludgeBoot
  225. {
  226. LOG("== Auto retrying in 3 seconds to trigger reboot ==");
  227. [self log:@"== Auto retrying in 3 seconds to trigger reboot =="];
  228. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  229. [self go:nil];
  230. });
  231. }
  232. - (void)bootstrap {
  233. [self log:@"bootstrapping"];
  234. // _force = NO;
  235. if (_force == YES) {
  236. //_force = YES;
  237. [self log:@"(forcing reinstall)"];
  238. }
  239. if (do_bootstrap(_force) == KERN_SUCCESS) {
  240. [self finish];
  241. } else {
  242. [self log:@"ERROR: failed to bootstrap \n"];
  243. }
  244. }
  245. + (NSString *)returnForProcess:(NSString *)call
  246. {
  247. if (call==nil)
  248. return 0;
  249. char line[200];
  250. NSLog(@"running process: %@", call);
  251. FILE* fp = popen([call UTF8String], "r");
  252. NSMutableArray *lines = [[NSMutableArray alloc]init];
  253. if (fp)
  254. {
  255. while (fgets(line, sizeof line, fp))
  256. {
  257. NSString *s = [NSString stringWithCString:line encoding:NSUTF8StringEncoding];
  258. s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  259. [lines addObject:s];
  260. }
  261. }
  262. pclose(fp);
  263. return [lines componentsJoinedByString:@"\n"];
  264. }
  265. - (void)finish {
  266. NSString *hostname = [ViewController returnForProcess:@"/bin/hostname"];
  267. [self log:@"device is now jailbroken!"];
  268. [self log:@""];
  269. [self log:[NSString stringWithFormat:@"SSH server is ready at %@.local", hostname]];
  270. [self log:@"change your root/mobile passwords"];
  271. [self log:@""];
  272. [self log:@"reloading daemons..."];
  273. sleep(2);
  274. RunCmd("/usr/libexec/substrate");
  275. LOG("reloading daemons...");
  276. pid_t pid;
  277. posix_spawn(&pid, "/bin/launchctl", 0, 0, (char**)&(const char*[]){"/bin/launchctl", "load", "/Library/LaunchDaemons/0.reload.plist", NULL}, NULL);
  278. waitpid(pid, 0, 0);
  279. sleep(2);
  280. dispatch_async(dispatch_get_main_queue(), ^{
  281. respringNeeded = YES;
  282. [self log:@""];
  283. [self log:@"respring to reload Applications and Tweaks"];
  284. [self.goButton setTitle:@"respring" forState:UIControlStateNormal];
  285. self.goButton.enabled = YES;
  286. });
  287. }
  288. - (void)showHUD
  289. {
  290. dispatch_async(dispatch_get_main_queue(), ^{
  291. [SVProgressHUD setBackgroundColor:[UIColor clearColor]];
  292. [SVProgressHUD show];
  293. });
  294. }
  295. - (void)restart {
  296. [self showHUD];
  297. LOG("Running uicache...");
  298. //pid_t pd;
  299. //const char* args[] = { "PineBoard", "HeadBoard", "lsd", NULL };
  300. //posix_spawn(&pid, "/jb/usr/bin/killall", NULL, NULL, (char* const*)args, NULL);
  301. //posix_spawn(&pd, "/usr/bin/uicache", 0, 0, (char**)&(const char*[]){"/usr/bin/uicache", NULL}, NULL);
  302. //waitpid(pd, 0, 0);
  303. RunCmd("/usr/libexec/substrate");
  304. RunCmd("/usr/bin/uicache");
  305. }
  306. @end