forked from firecore/Seas0nPass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnitoUtility.m
2123 lines (1594 loc) · 61.1 KB
/
nitoUtility.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// nitoUtility.m
// Seas0nPass
//
// Created by Kevin Bradley on 3/20/09.
// Copyright 2009 nito, LLC. All rights reserved.
//
/*
a lot of this class is adapted from atvPwn and atvDrive, in addition to various other nito projects.
the bulk of convenience methods for extraction, hdiutil interactions, and actual patching/pwning process take place here
*/
#import "nitoUtility.h"
#import "FWBundle.h"
#define NULLOUT [NSFileHandle fileHandleWithNullDevice]
@implementation nitoUtility
@synthesize delegate, enableScripting, currentBundle, sshKey, sigServer, debWhitelist, restoreMode;
- (id)init
{
self = [super init];
return self;
}
- (void)dealloc {
[sshKey release];
[super dealloc];
}
+ (float)sizeFreeOnMountedPath:(NSString *)theDevice
{
NSFileManager *man = [NSFileManager defaultManager];
float available = [[[man attributesOfFileSystemForPath:theDevice error:nil] objectForKey:NSFileSystemFreeSize] floatValue];
float avail2 = available / 1024 / 1024;
return avail2;
}
#pragma mark •• dmg classes
+ (int)mountImageSimple:(NSString *)irString
{
NSTask *irTask = [[NSTask alloc] init];
NSMutableArray *irArgs = [[NSMutableArray alloc] init];
[irArgs addObject:@"attach"];
[irArgs addObject:irString];
[irTask setLaunchPath:@"/usr/bin/hdiutil"];
[irTask setArguments:irArgs];
[irArgs release];
[irTask setStandardError:NULLOUT];
[irTask setStandardOutput:NULLOUT];
[irTask launch];
[irTask waitUntilExit];
int returnStatus = [irTask terminationStatus];
[irTask release];
irTask = nil;
return returnStatus;
}
+ (NSString *)mountImageWithoutOwners:(NSString *)irString
{
NSTask *irTask = [[NSTask alloc] init];
NSPipe *hdip = [[NSPipe alloc] init];
NSFileHandle *hdih = [hdip fileHandleForReading];
NSMutableArray *irArgs = [[NSMutableArray alloc] init];
[irArgs addObject:@"attach"];
[irArgs addObject:@"-plist"];
[irArgs addObject:irString];
[irTask setLaunchPath:@"/usr/bin/hdiutil"];
[irTask setArguments:irArgs];
[irArgs release];
[irTask setStandardError:hdip];
[irTask setStandardOutput:hdip];
//NSLog(@"hdiutil %@", [[irTask arguments] componentsJoinedByString:@" "]);
[irTask launch];
NSData *outData = [hdih readDataToEndOfFile];
[irTask waitUntilExit];
NSString *the_error;
NSPropertyListFormat format;
id plist;
plist = [NSPropertyListSerialization propertyListFromData:outData
mutabilityOption:NSPropertyListImmutable
format:&format
errorDescription:&the_error];
if(!plist)
{
NSLog(@"%@",the_error);
[the_error release];
}
//NSLog(@"plist: %@", plist);
NSArray *plistArray = [plist objectForKey:@"system-entities"];
//int theItem = ([plistArray count] - 1);
int i;
NSString *mountPath = nil;
for (i = 0; i < [plistArray count]; i++)
{
NSDictionary *mountDict = [plistArray objectAtIndex:i];
mountPath = [mountDict objectForKey:@"mount-point"];
if (mountPath != nil)
{
//NSLog(@"Mount Point: %@", mountPath);
int rValue = [irTask terminationStatus];
if (rValue == 0)
{ [irTask release];
irTask = nil;
[hdip release];
hdip = nil;
return mountPath;
}
}
}
[irTask release];
irTask = nil;
[hdip release];
hdip = nil;
return nil;
}
+ (NSString *)mountImage:(NSString *)irString
{
NSTask *irTask = [[NSTask alloc] init];
NSPipe *hdip = [[NSPipe alloc] init];
NSFileHandle *hdih = [hdip fileHandleForReading];
NSMutableArray *irArgs = [[NSMutableArray alloc] init];
[irArgs addObject:@"attach"];
[irArgs addObject:@"-plist"];
[irArgs addObject:irString];
[irArgs addObject:@"-owners"];
[irArgs addObject:@"on"];
[irTask setLaunchPath:@"/usr/bin/hdiutil"];
[irTask setArguments:irArgs];
[irArgs release];
[irTask setStandardError:hdip];
[irTask setStandardOutput:hdip];
//NSLog(@"hdiutil %@", [[irTask arguments] componentsJoinedByString:@" "]);
[irTask launch];
NSData *outData = [hdih readDataToEndOfFile]; //FIX_ME: DO THIS TO ALL READDATATOENDOFFILES
[irTask waitUntilExit];
NSString *the_error;
NSPropertyListFormat format;
id plist;
plist = [NSPropertyListSerialization propertyListFromData:outData
mutabilityOption:NSPropertyListImmutable
format:&format
errorDescription:&the_error];
if(!plist)
{
NSLog(@"%@", the_error);
[the_error release];
}
//NSLog(@"plist: %@", plist);
NSArray *plistArray = [plist objectForKey:@"system-entities"];
//int theItem = ([plistArray count] - 1);
int i;
NSString *mountPath = nil;
for (i = 0; i < [plistArray count]; i++)
{
NSDictionary *mountDict = [plistArray objectAtIndex:i];
mountPath = [mountDict objectForKey:@"mount-point"];
if (mountPath != nil)
{
//NSLog(@"Mount Point: %@", mountPath);
int rValue = [irTask terminationStatus];
if (rValue == 0)
{ [irTask release];
irTask = nil;
return mountPath;
}
}
}
[irTask release];
irTask = nil;
return nil;
}
+ (void)unmountVolume:(NSString *)theVolume
{
NSTask *umountTask = [[NSTask alloc] init];
[umountTask setLaunchPath:HDIUTIL];
[umountTask setArguments:[NSArray arrayWithObjects:@"detach", theVolume, nil]];
[umountTask setStandardError:NULLOUT];
[umountTask setStandardOutput:NULLOUT];
[umountTask launch];
[umountTask waitUntilExit];
[umountTask release];
}
+ (int)scanForRestore:(NSString *)drivepath
{
NSTask *asrTask = [[NSTask alloc] init];
[asrTask setLaunchPath:ASR];
[asrTask setArguments:[NSArray arrayWithObjects:@"-imagescan", drivepath, nil]];
[asrTask launch];
[asrTask waitUntilExit];
int termStatus = [asrTask terminationStatus];
return termStatus;
}
+ (BOOL)validateFile:(NSString *)inputFile withChecksum:(NSString *)checksum
{
NSTask *sslTask = [[NSTask alloc] init];
NSPipe *sspipe = [[NSPipe alloc] init];
NSFileHandle *ssh = [sspipe fileHandleForReading];
[sslTask setLaunchPath:@"/usr/bin/openssl"];
[sslTask setArguments:[NSArray arrayWithObjects:@"sha1", inputFile, nil]];
[sslTask setStandardOutput:sspipe];
[sslTask setStandardError:sspipe];
[sslTask launch];
NSData *outData = [ssh readDataToEndOfFile];
[sslTask waitUntilExit];
NSString *outputString = [[[NSString alloc] initWithData:outData
encoding:NSASCIIStringEncoding]
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//example outputString: SHA1(~/Documents/Tether/AppleTV2,1_4.3_8F455_Restore.ipsw)= b6a2b0baae79daf95f75044c12946839c662d01d
//b6a2b0baae79daf95f75044c12946839c662d01d cleaned up
NSString *outputSHA = [[[outputString componentsSeparatedByString:@"="] lastObject] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"sha1: %@ against: %@", outputSHA, checksum);
if ([outputSHA isEqualToString:checksum])
{
[sslTask release];
sslTask = nil;
[sspipe release];
sspipe = nil;
return YES;
}
[sslTask release];
sslTask = nil;
[sspipe release];
sspipe = nil;
return NO;
}
+ (BOOL)checkFile:(NSString *)inputFile againstMD5:(NSString *)properMD5
{
//NSLog(@"%@ %s", self, _cmd);
NSTask *mdTask = [[NSTask alloc] init];
NSPipe *mdip = [[NSPipe alloc] init];
//NSString *fullPath = [inputVolume stringByAppendingPathComponent:@"mach_kernel.prelink"];
NSFileHandle *mdih = [mdip fileHandleForReading];
[mdTask setLaunchPath:@"/sbin/md5"];
[mdTask setArguments:[NSArray arrayWithObjects:@"-q", inputFile, nil]];
[mdTask setStandardOutput:mdip];
[mdTask setStandardError:mdip];
[mdTask launch];
NSData *outData;
outData = [mdih readDataToEndOfFile];
[mdTask waitUntilExit];
NSString *temp = [[NSString alloc] initWithData:outData encoding:NSASCIIStringEncoding];
temp = [temp stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//int theTerm = [mdTask terminationStatus];
NSLog(@"md5: %@ against: %@ " , temp, properMD5);
if ([temp isEqualToString:properMD5])
{
[mdTask release];
mdTask = nil;
[mdip release];
mdip = nil;
return YES;
}
[mdTask release];
mdTask = nil;
[mdip release];
mdip = nil;
return NO;
}
#pragma mark •• owners / permissions
+ (void)changePermissions:(NSString *)perms onFile:(NSString *)theFile isRecursive:(BOOL)isR
{
NSTask *permTask = [[NSTask alloc] init];
NSMutableArray *permArgs = [[NSMutableArray alloc] init];
if (isR)
[permArgs addObject:@"-R"];
[permArgs addObject:perms];
[permArgs addObject:theFile];
[permTask setLaunchPath:@"/bin/chmod"];
[permTask setArguments:permArgs];
//NSLog(@"chmod %@", [[permTask arguments] componentsJoinedByString:@" "]);
[permTask launch];
[permTask waitUntilExit];
[permTask release];
permTask = nil;
}
+ (void)changeOwner:(NSString *)theOwner onFile:(NSString *)theFile isRecursive:(BOOL)isR
{
NSTask *ownTask = [[NSTask alloc] init];
NSMutableArray *ownArgs = [[NSMutableArray alloc] init];
[ownTask setLaunchPath:@"/usr/sbin/chown"];
if (isR)
[ownArgs addObject:@"-R"];
[ownArgs addObject:theOwner];
[ownArgs addObject:theFile];
[ownTask setArguments:ownArgs];
//NSLog(@"chown %@", [ownArgs componentsJoinedByString:@" "]);
[ownArgs release];
[ownTask launch];
[ownTask waitUntilExit];
[ownTask release];
ownTask = nil;
}
#pragma mark •• pwnage classes
+ (void)createTempSetup
{
if ([FM fileExistsAtPath:IPSW_TMP])
{
[FM removeItemAtPath:IPSW_TMP error:nil];
}
[FM createDirectoryAtPath:IPSW_TMP withIntermediateDirectories:YES attributes:nil error:nil];
}
- (void)editOptions:(NSString *)optionsFile withFSSize:(int)fsSize
{
NSLog(@"editing options: %@", optionsFile);
NSMutableDictionary *optionsDict = [[NSMutableDictionary alloc] initWithContentsOfFile:optionsFile];
[optionsDict setObject:[NSNumber numberWithBool:NO] forKey:@"UpdateBaseband"];
[optionsDict setObject:[NSNumber numberWithBool:YES] forKey:@"CreateFilesystemPartitions"];
[optionsDict setObject:[NSNumber numberWithInt:fsSize] forKey:@"SystemPartitionSize"];
[optionsDict setObject:[NSNumber numberWithInt:fsSize] forKey:@"MinimumSystemPartition"];
[optionsDict writeToFile:optionsFile atomically:YES];
[optionsDict release];
}
- (int)removeUselessFilesFromRamdisk:(NSString *)mountedRamdisk
{
NSString *filePath = [mountedRamdisk stringByAppendingPathComponent:@"/usr/local/share/restore/PASS.png"];
NSString *otherFilePath = [mountedRamdisk stringByAppendingPathComponent:@"/usr/share/progressui/images-1x"];
NSString *otherFilePath2 = [mountedRamdisk stringByAppendingPathComponent:@"/usr/share/progressui/images-2x"];
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
[[NSFileManager defaultManager] removeItemAtPath:otherFilePath error:nil];
[[NSFileManager defaultManager] removeItemAtPath:otherFilePath2 error:nil];
NSString *firmwarePath = [mountedRamdisk stringByAppendingPathComponent:@"/usr/local/standalone/firmware/"];
NSArray *firmwareArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:firmwarePath error:nil];
NSEnumerator *firmwareEnum = [firmwareArray objectEnumerator];
id currentFile = nil;
while (currentFile = [firmwareEnum nextObject]) {
NSString *fullPath = [firmwarePath stringByAppendingPathComponent:currentFile];
NSLog(@"removing file: %@", fullPath);
if ([[NSFileManager defaultManager] removeItemAtPath:fullPath error:nil])
{
NSLog(@"%@ removed successfully!", fullPath);
} else {
NSLog(@"%@ removal failed!!!!!", fullPath);
}
}
return 0;
}
- (int)removeUselessFirmwareFilesFromRamdisk:(NSString *)mountedRamdisk
{
NSString *firmwarePath = [mountedRamdisk stringByAppendingPathComponent:@"/usr/local/standalone/firmware/"];
NSArray *firmwareArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:firmwarePath error:nil];
NSEnumerator *firmwareEnum = [firmwareArray objectEnumerator];
id currentFile = nil;
while (currentFile = [firmwareEnum nextObject]) {
NSString *extension = [[currentFile pathExtension] lowercaseString];
if ([extension isEqualToString:@"zip"])
{
NSString *fullPath = [firmwarePath stringByAppendingPathComponent:currentFile];
NSLog(@"removing file: %@", fullPath);
if ([[NSFileManager defaultManager] removeItemAtPath:fullPath error:nil])
{
NSLog(@"%@ removed successfully!", fullPath);
return 0;
} else {
NSLog(@"%@ removal failed!!!!!", fullPath);
}
}
}
return -1;
}
- (int)performPatchesFromBundle:(FWBundle *)theBundle onRamdisk:(NSDictionary *)ramdiskDict
{
//NSString *ramdiskSize = @"16541920";
//NSString *ramdiskSize = [currentBundle ramdiskSize];
//NSLog(@"ramdisk size: %@", ramdiskSize);
NSString *ramdiskIV = [ramdiskDict valueForKey:@"IV"];
NSString *ramdiskKey = [ramdiskDict valueForKey:@"Key"];
NSDictionary *ramdiskPatches = [theBundle ramdiskPatches];
NSString *theRamdisk = [TMP_ROOT stringByAppendingPathComponent:[ramdiskDict valueForKey:@"File"]];
int status = 0;
NSString *finalRam = [IPSW_TMP stringByAppendingPathComponent:[theRamdisk lastPathComponent]];
NSString *decryptRam = [IPSW_TMP stringByAppendingPathComponent:@"decrypt.dmg"];
status = [nitoUtility decryptRamdisk:theRamdisk toPath:decryptRam withIV:ramdiskIV key:ramdiskKey]; //1
//change up here, smarterize resize ramdisk
if (status == 0)
{
NSLog(@"decrypted %@ successfully!", theRamdisk);
NSString *ramdiskSize = [self ramdiskResizeValue:decryptRam];
if (![theBundle is4point4])
{
status = [nitoUtility resizeVolume:decryptRam toSize:ramdiskSize]; //2
} else {
//dont resize ramdisk for 4.4, fucks up in beta 3+
status = 0; //hopefully the removeUselessFirmwareFiles function works!!
}
if (status == 0)
{
NSLog(@"resized %@ successfully!", decryptRam);
NSString *mountedImage = [nitoUtility mountImageWithoutOwners:decryptRam]; //3
if (mountedImage == nil) //an attempt to fix the Unexpected character 2 at line 1 bug
{
NSLog(@"dictionary parse may have failed, trying to unmount all /Volumes/ramdisk*");
NSLog(@"remounting and hardcoding /Volumes/ramdisk, cross your fingers!");
/*
my theory is that the volume is mounting BUT the plist return from hdiutil is borking for some reason (maybe the use of deprecated code?)
anyhow, going to try and "clean" any /Volume/ramdisk mounts (i know, not ideal, but what else can i do?)
from there, use the attach code that doesnt try to parse the output, do a ghetto check to see if /Volumes/ramdisk exists, if it does, continue
*/
[nitoUtility unmountVolume:@"/Volumes/ramdisk"];
[nitoUtility unmountVolume:@"/Volumes/ramdisk 1"];
[nitoUtility unmountVolume:@"/Volumes/ramdisk 2"];
int mountReturn = [nitoUtility mountImageSimple:decryptRam]; //now force this to be /Volumes/ramdisk
NSLog(@"mountImageSimple return: %i", mountReturn);
if ([FM fileExistsAtPath:@"/Volumes/ramdisk"])
{
NSLog(@"ramdisk 'volume' exists!");
mountedImage = @"/Volumes/ramdisk";
}
}
if (mountedImage != nil)
{
NSLog(@"mountedImage %@ successfully!", decryptRam);
/*
patches here
*/
//FIXME: for now we are just going to delete useless files to fix ramdisk issues in beta3
[self removeUselessFirmwareFilesFromRamdisk:mountedImage];
[self removeUselessFilesFromRamdisk:mountedImage];
NSEnumerator *patchEnum = [ramdiskPatches objectEnumerator];
id thePatch = nil;
while (thePatch = [patchEnum nextObject])
{
//NSLog(@"thePAtch: %@", thePatch);
NSString *file = [thePatch valueForKey:@"File"];
NSString *patchFile = [thePatch valueForKey:@"Patch"];
NSString *md5 = [thePatch valueForKey:@"MD5"];
NSString *patchPath = [[theBundle bundlePath] stringByAppendingPathComponent:patchFile];
//NSLog(@"patching: %@ withPatch: %@", file, patchFile);
status = [nitoUtility patchFile:[mountedImage stringByAppendingPathComponent:file] withPatch:patchPath endMD5:md5];
if (status == 0)
{
NSLog(@"patched %@ successfully!", file);
[nitoUtility changePermissions:@"+x" onFile:[mountedImage stringByAppendingPathComponent:file] isRecursive:YES];
if ([file isEqualToString:@"usr/sbin/asr"])
{
[nitoUtility changePermissions:@"100755" onFile:[mountedImage stringByAppendingPathComponent:file] isRecursive:YES];
}
} else {
NSLog(@"patch %@ failure!!, bail!", thePatch);
return -1;
}
}
NSString *optionPath = [mountedImage stringByAppendingPathComponent:@"usr/local/share/restore/options.plist"];
if ([[NSFileManager defaultManager] fileExistsAtPath:optionPath])
{
int fsSize = [[theBundle filesystemSize] intValue];
fsSize += 100;
[self editOptions:optionPath withFSSize:fsSize];
}
/*
//update appletv partition size stuff
NSString *optionPathATV = [mountedImage stringByAppendingPathComponent:@"usr/local/share/restore/options.k66.plist"];
if ([[NSFileManager defaultManager] fileExistsAtPath:optionPathATV])
{
int fsSize = [[theBundle filesystemSize] intValue];
[self editOptions:optionPathATV withFSSize:fsSize];
}
*/
if (status == 0)
{
NSLog(@"performed patches successfully!");
[nitoUtility unmountVolume:mountedImage]; //8
status = [nitoUtility repackRamdisk:decryptRam toPath:finalRam withIV:ramdiskIV key:ramdiskKey originalPath:theRamdisk]; //9
[FM removeItemAtPath:decryptRam error:nil]; //10 no need for 11?
if (status == 0)
{
NSLog(@"patched ramdisk successfully!");
return 0;
}
}
}
}
}
NSLog(@"patch ramdisk failed!");
return -1;
}
- (int)patchRamdisk:(NSString *)theRamdisk
{
NSString *ramdiskSize = @"16541920";
NSString *ramdiskIV = @"7c256102d0580b960213540965618b5b";
NSString *ramdiskKey = @"5d4e967158ab75ba27ec281bff4e714dacc88123ea4913ae2bee6a719c15496c";
NSString *asrPath = [[NSBundle mainBundle] pathForResource:@"asr" ofType:@"patch" inDirectory:@"patches"];
/*
TODO: add patch data dictionary
1. xpwntool 038-0318-001.dmg ipsw/038-0318-001.dmg -iv 7c256102d0580b960213540965618b5b -k 5d4e967158ab75ba27ec281bff4e714dacc88123ea4913ae2bee6a719c15496c
2. hdiutil resize -size 16541920 ipsw/038-0318-001.dmg
3. hdiutil attach ipsw/038-0318-001.dmg
4. bspatch /Volumes/ramdisk/usr/sbin/asr /Volumes/ramdisk/usr/sbin/asr.patched AppleTV2,1_4.2.1_8C154.bundle/asr.patch
5. rm /Volumes/ramdisk/usr/sbin/asr
6. mv /Volumes/ramdisk/usr/sbin/asr.patched /Volumes/ramdisk/usr/sbin/asr
7. chmod +x /Volumes/ramdisk/usr/sbin/asr
8. hdiutil detach /Volumes/ramdisk/
9. xpwntool ipsw/038-0318-001.dmg ipsw/038-0318-001-repack.dmg -iv 7c256102d0580b960213540965618b5b -k 5d4e967158ab75ba27ec281bff4e714dacc88123ea4913ae2bee6a719c15496c -t 038-0318-001.dmg
10. rm ipsw/038-0318-001.dmg
11. mv ipsw/038-0318-001-repack.dmg ipsw/038-0318-001.dmg
*/
int status = 0;
NSString *finalRam = [IPSW_TMP stringByAppendingPathComponent:[theRamdisk lastPathComponent]];
NSString *decryptRam = [IPSW_TMP stringByAppendingPathComponent:@"decrypt.dmg"];
status = [nitoUtility decryptRamdisk:theRamdisk toPath:decryptRam withIV:ramdiskIV key:ramdiskKey]; //1
if (status == 0)
{
NSLog(@"decrypted %@ successfully!", theRamdisk);
status = [nitoUtility resizeVolume:decryptRam toSize:ramdiskSize]; //2
if (status == 0)
{
NSLog(@"resized %@ successfully!", decryptRam);
NSString *mountedImage = [nitoUtility mountImageWithoutOwners:decryptRam]; //3
if (mountedImage != nil)
{
NSLog(@"mountedImage %@ successfully!", decryptRam);
status = [nitoUtility patchFile:[mountedImage stringByAppendingPathComponent:@"usr/sbin/asr"] withPatch:asrPath endMD5:@"072c70c08790a4d80f1683e60f4edb71"]; //4 5 6
[nitoUtility changePermissions:@"+x" onFile:[mountedImage stringByAppendingPathComponent:@"usr/sbin/asr"] isRecursive:YES];
if (status == 0)
{
NSLog(@"patched asr successfully!");
[nitoUtility changePermissions:@"+x" onFile:[mountedImage stringByAppendingPathComponent:@"usr/sbin/asr"] isRecursive:YES]; //7
[nitoUtility unmountVolume:mountedImage]; //8
status = [nitoUtility repackRamdisk:decryptRam toPath:finalRam withIV:ramdiskIV key:ramdiskKey originalPath:theRamdisk]; //9
if (status == 0)
{
[FM removeItemAtPath:decryptRam error:nil]; //10 no need for 11?
NSLog(@"patched ramdisk successfully!");
return 0;
}
}
}
}
}
NSLog(@"patch ramdisk failed!");
return -1;
}
+ (NSDictionary *)fsImageInfo:(NSString *)inputFilesystem
{
return nil;
NSTask *irTask = [[NSTask alloc] init];
NSPipe *hdip = [[NSPipe alloc] init];
NSFileHandle *hdih = [hdip fileHandleForReading];
NSMutableArray *irArgs = [[NSMutableArray alloc] init];
[irArgs addObject:@"imageinfo"];
[irArgs addObject:@"-plist"];
[irArgs addObject:inputFilesystem];
[irTask setLaunchPath:@"/usr/bin/hdiutil"];
[irTask setArguments:irArgs];
[irArgs release];
[irTask setStandardError:hdip];
[irTask setStandardOutput:hdip];
//NSLog(@"hdiutil %@", [[irTask arguments] componentsJoinedByString:@" "]);
[irTask launch];
NSData *outData;
outData = [hdih readDataToEndOfFile];
[irTask waitUntilExit];
NSString *the_error;
NSPropertyListFormat format;
id plist;
plist = [NSPropertyListSerialization propertyListFromData:outData
mutabilityOption:NSPropertyListImmutable
format:&format
errorDescription:&the_error];
if(!plist)
{
NSLog(@"%@", the_error);
[the_error release];
}
//NSLog(@"fsImageInfo: %@", plist);
[irTask release];
irTask = nil;
return plist;
}
- (NSString *)ramdiskResizeValue:(NSString *)inputRD //adds 3 megs to the ramdisk size
{
return [currentBundle ramdiskSize];
NSDictionary *fsImageInfo = [nitoUtility fsImageInfo:inputRD];
if (fsImageInfo == nil)
{
return [currentBundle ramdiskSize];
} else {
NSLog(@"fsImageInfo: %@", fsImageInfo);
}
NSDictionary *sizeInfo = [fsImageInfo valueForKey:@"Size Information"];
float totalBytes = [[sizeInfo valueForKey:@"Total Bytes"] floatValue];
float totalEmptyBytes = [[sizeInfo valueForKey:@"Total Empty Bytes"] floatValue];
NSLog(@"bytes free: %.0f", totalEmptyBytes);
int finalTotal = totalBytes + 3145728;
NSLog(@"finalTotal to resize rd: %i", finalTotal);
return [NSString stringWithFormat:@"%i", finalTotal];
}
//25747456
- (NSString *)filesystemResizeValue:(NSString *)inputFilesystem
{
if([self.currentBundle is4point4])
{
NSLog(@"is 4.4/5.0");
float resizeValue = [[[self currentBundle] filesystemSize] floatValue];
int ft = resizeValue * 1048576;
NSLog(@"resizeValue: %f MB", resizeValue);
NSLog(@"finalTotal to resize fs: %i", ft);
return [NSString stringWithFormat:@"%i", ft];
}
NSDictionary *fsImageInfo = [nitoUtility fsImageInfo:inputFilesystem];
if (fsImageInfo == nil)
{
return nil;
}//divide by 1048576 to get approx MB value
NSDictionary *sizeInfo = [fsImageInfo valueForKey:@"Size Information"];
NSLog(@"sizeInfo: %@", sizeInfo);
float totalBytes = [[sizeInfo valueForKey:@"Total Bytes"] floatValue];
float totalEmptyBytes = [[sizeInfo valueForKey:@"Total Empty Bytes"] floatValue];
NSLog(@"totalEmptyBytes: %f", totalEmptyBytes);
float freeMB = (totalEmptyBytes / 1048576);
NSLog(@"MB free: %f", freeMB);
if (freeMB < 100) //check to see if there is more than 100 megs free
{
//we need to resize the filesystem!!
//approx 100 megs in bytes 104857600
int finalTotal = totalBytes + 104857600;
NSLog(@"finalTotal to resize fs: %i", finalTotal);
return [NSString stringWithFormat:@"%i", finalTotal];
}
return nil;
}
#pragma mark this is where all the cleanup would need to occur.
- (void)patchFilesystem:(NSString *)inputFilesystem
{
NSString *fsKey = [self.currentBundle filesystemKey];
// NSString *fstabPatch = [[NSBundle mainBundle] pathForResource:@"fstab" ofType:@"patch" inDirectory:@"patches"];
/*
1. vfdecrypt -i 038-0316-001.dmg -k 5407d28e075f5a2e06fddb7ad00123aa5a528bd6c2850d5fa0908a4dcae7dd3e00a9cdb2 -o ipsw/038-0316-001.dmg
2. hdiutil convert ipsw/038-0316-001.dmg -format UDRW -o ipsw/converted.dmg
3. sudo hdiutil attach -owners on ipsw/converted.dmg
4. sudo bspatch /Volumes/Jasper8C154.K66OS/etc/fstab /Volumes/Jasper8C154.K66OS/etc/fstab.patched AppleTV2,1_4.2.1_8C154.bundle/fstab.patch
5. sudo rm /Volumes/Jasper8C154.K66OS/etc/fstab
6. sudo mv /Volumes/Jasper8C154.K66OS/etc/fstab.patched /Volumes/Jasper8C154.K66OS/etc/fstab
7. sudo tar fxpz Cydia.tgz -C /Volumes/Jasper8C154.K66OS/
8. sudo ./space.sh /Volumes/Jasper8C154.K66OS/
9. hdiutil detach /Volumes/Jasper8C154.K66OS/
10. hdiutil convert ipsw/converted.dmg -format UDZO -o ipsw/RO.dmg
11. asr --imagescan ipsw/RO.dmg
12. rm ipsw/converted.dmg
13. rm ipsw/038-0316-001.dmg
14. mv ipsw/RO.dmg ipsw/038-0316-001.dmg
*/
int status = 0;
NSLog(@"Decrypting Filesystem...");
// NSString *finalFS = [IPSW_TMP stringByAppendingPathComponent:[inputFilesystem lastPathComponent]];
NSString *decryptFS = [inputFilesystem stringByAppendingPathExtension:@"decrypt"];
NSString *rwFS = [TMP_ROOT stringByAppendingPathComponent:@"rw.dmg"];
status = [nitoUtility decryptFilesystem:inputFilesystem withKey:fsKey]; //1
if (status == 0)
{
NSLog(@"Decrypted Filesystem successfully!");
NSLog(@"Converting Filesystem to read-write...");
NSString *convertImage = [nitoUtility convertImage:decryptFS toFile:rwFS toMode:kDMGReadWrite]; //2
if (convertImage != nil)
{
NSLog(@"converted to read write successfully: %@", rwFS);
/*
attempt undocumented/unsupported compat with other iOS devices. this will require resizing the filesystem if it doesn't have enough space free
seems to be common.
og line here was JUST
[self permissionedPatch:rwFS withOriginal:inputFilesystem];
*/
NSString *resizeFSValue = [self filesystemResizeValue:decryptFS];
//[[NSApplication sharedApplication] terminate:self];
if (resizeFSValue != nil)
{
NSLog(@"resizeFSValue: %@", resizeFSValue);
[nitoUtility resizeVolume:rwFS toSize:resizeFSValue];
}
//need to take over with root access here for 3-8
[self permissionedPatch:rwFS withOriginal:inputFilesystem];
} else {
NSLog(@"converting to read-write failed!");
[self failedWithReason:@"Failed to convert filesystem to read-write!"];
}
} else {
NSLog(@"filesystem decryption failed!");
[self failedWithReason:@"Filesystem failed to decrypt!"];
}
}
- (void)failedWithReason:(NSString *)theReason
{
NSDictionary *failDict = [NSDictionary dictionaryWithObject:theReason forKey:@"AbortReason"];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"pwnFailed" object:nil userInfo:failDict deliverImmediately:YES];
}
- (void)oldpatchFilesystem:(NSString *)inputFilesystem
{
NSString *fsKey = @"5407d28e075f5a2e06fddb7ad00123aa5a528bd6c2850d5fa0908a4dcae7dd3e00a9cdb2";
// NSString *fstabPatch = [[NSBundle mainBundle] pathForResource:@"fstab" ofType:@"patch" inDirectory:@"patches"];
/*
1. vfdecrypt -i 038-0316-001.dmg -k 5407d28e075f5a2e06fddb7ad00123aa5a528bd6c2850d5fa0908a4dcae7dd3e00a9cdb2 -o ipsw/038-0316-001.dmg
2. hdiutil convert ipsw/038-0316-001.dmg -format UDRW -o ipsw/converted.dmg
3. sudo hdiutil attach -owners on ipsw/converted.dmg
4. sudo bspatch /Volumes/Jasper8C154.K66OS/etc/fstab /Volumes/Jasper8C154.K66OS/etc/fstab.patched AppleTV2,1_4.2.1_8C154.bundle/fstab.patch
5. sudo rm /Volumes/Jasper8C154.K66OS/etc/fstab
6. sudo mv /Volumes/Jasper8C154.K66OS/etc/fstab.patched /Volumes/Jasper8C154.K66OS/etc/fstab
7. sudo tar fxpz Cydia.tgz -C /Volumes/Jasper8C154.K66OS/
8. sudo ./space.sh /Volumes/Jasper8C154.K66OS/
9. hdiutil detach /Volumes/Jasper8C154.K66OS/
10. hdiutil convert ipsw/converted.dmg -format UDZO -o ipsw/RO.dmg
11. asr --imagescan ipsw/RO.dmg
12. rm ipsw/converted.dmg
13. rm ipsw/038-0316-001.dmg
14. mv ipsw/RO.dmg ipsw/038-0316-001.dmg