-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathACommon.m
939 lines (681 loc) · 28.7 KB
/
ACommon.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
#import "ACommon.h"
@implementation ACommon
/*
return [NSArray arrayWithObjects:@"AppleLogo", @"BatteryCharging", @"BatteryCharging0", @"BatteryCharging1", @"BatteryFull", @"BatteryLow", @"BatteryLow0", @"BatteryLow1", @"BatteryPlugin", @"DeviceTree", @"KernelCache", @"LLB", @"OS", @"RecoveryMode", @"RestoreDeviceTree", @"RestoreKernelCache", @"RestoreLogo", @"RestoreRamDisk", @"UpdateRamDisk", @"iBEC", @"iBSS", @"iBoot", nil];
*/
-(NSString *)grabKeybagForFile:(NSString *)theFile
{
NSString *kbagProcess = [NSString stringWithFormat:@"\"%@\" \"%@\"", GRABKBAG, theFile];
// DebugLog(@"grabKeybag: %@", kbagProcess);
NSString *returnString = [ACommon singleLineReturnForProcess:kbagProcess];
if (returnString != nil)
{
DebugLog(@"%@ has keybag: %@", [theFile lastPathComponent], returnString);
return returnString;
}
DebugLog(@"%@ not encrypted!", [theFile lastPathComponent]);
return nil;
}
+ (id)sharedCommon
{
static ACommon *shared = nil;
if(shared == nil)
shared = [[ACommon alloc] init];
return shared;
}
+ (NSString *)dyldcacheFileFromVolume:(NSString *)theVolume
{
NSString *dyldcacheFolder = [theVolume stringByAppendingPathComponent:@"System/Library/Caches/com.apple.dyld"];
DebugLog(@"dyldcacheFolder: %@",dyldcacheFolder );
NSArray *cacheFiles = [FM contentsOfDirectoryAtPath:dyldcacheFolder error:nil];
DebugLog(@"cacheFiles: %@", cacheFiles);
if ([cacheFiles count] > 1)
{
NSEnumerator *cacheEnum = [cacheFiles objectEnumerator];
id theObject = nil;
while (theObject = [cacheEnum nextObject]) {
if ([theObject length] > 17)
{
NSString *substrg = [theObject substringToIndex:17];
DebugLog(@"substrg: %@", substrg);
if ([substrg isEqualToString:@"dyld_shared_cache"])
{
return [dyldcacheFolder stringByAppendingPathComponent:theObject];
}
}
//dyld_shared_cache_armv7
}
} else {
return [dyldcacheFolder stringByAppendingPathComponent:[cacheFiles objectAtIndex:0]];
}
return nil;
}
//kernelcache.release_abdec.n18
+ (NSString *)IOLogOffsetFromKernel:(NSString *)kernel
{
//NSString *kernelPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/FWAmbrosia/Firmware/iPod3,1_5.0_9A334_Restore/kernelcache.release_abdec.n18"];
//kernel = kernelPath;
NSString *returnString = [ACommon stringReturnForTask:@"/usr/bin/nm" withArguments:[NSArray arrayWithObjects:@"-P", kernel, nil]];
NSRange stringRange = [returnString rangeOfString:@"_IOLog"];
NSString *rangeString = NSStringFromRange(stringRange);
//NSLog(@"range: %@", rangeString);
int location = stringRange.location;
stringRange.location = location + 9; //should HOPEFULLY isolate JUST the symbol location
stringRange.length = 8; //should isolate to the thing
NSString *finalStringFormatted = [NSString stringWithFormat:@"0x%@",[returnString substringWithRange:stringRange]];
return finalStringFormatted;
}
+ (NSArray *)extractSystemLibsFromVolume:(NSString *)theVolume toPath:(NSString *)outputPath
{
NSString *dyldcacheFile = [self dyldcacheFileFromVolume:theVolume];
NSArray *arguments = [NSArray arrayWithObjects:dyldcacheFile, @"/usr/lib/system/libsystem_c.dylib", @"/usr/lib/system/libsystem_kernel.dylib", nil];
NSArray *theArray = [ACommon returnForTask:DYLDCACHE withArguments:arguments fromPath:outputPath];
[FM moveItemAtPath:[outputPath stringByAppendingPathComponent:@"out/usr/lib/system/libsystem_c.dylib"] toPath:[outputPath stringByAppendingPathComponent:@"libsystem_c.dylib"] error:nil];
[FM moveItemAtPath:[outputPath stringByAppendingPathComponent:@"out/usr/lib/system/libsystem_kernel.dylib"] toPath:[outputPath stringByAppendingPathComponent:@"libsystem_kernel.dylib"] error:nil];
}
+ (NSArray *)dyldcacheContentsFromVolume:(NSString *)theVolume
{
NSString *dyldcacheFile = [self dyldcacheFileFromVolume:theVolume];
return [ACommon returnForTask:DYLDCACHE withArguments:[NSArray arrayWithObjects:@"-l", dyldcacheFile, nil]];
}
+ (ACommon *)sharedInstance
{
return [[self alloc] init];
}
+ (NSString *)applicationSupportFolder {
NSFileManager *man = [NSFileManager defaultManager];
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:
0] : NSTemporaryDirectory();
basePath = [basePath stringByAppendingPathComponent:@"FWAmbrosia"];
if (![man fileExistsAtPath:basePath])
[man createDirectoryAtPath:basePath withIntermediateDirectories:YES attributes:nil error:nil];
return basePath;
}
+ (NSString *)absintheFiles {
NSFileManager *man = [NSFileManager defaultManager];
NSString *basePath = [[ACommon applicationSupportFolder] stringByAppendingPathComponent:@"AbsintheFiles"];
if (![man fileExistsAtPath:basePath])
[man createDirectoryAtPath:basePath withIntermediateDirectories:YES attributes:nil error:nil];
return basePath;
}
+ (NSString *)firmwarePath {
NSFileManager *man = [NSFileManager defaultManager];
NSString *basePath = [[ACommon applicationSupportFolder] stringByAppendingPathComponent:@"Firmware"];
if (![man fileExistsAtPath:basePath])
[man createDirectoryAtPath:basePath withIntermediateDirectories:YES attributes:nil error:nil];
return basePath;
}
+ (NSDictionary *)decryptedKbag:(NSString *)theKbag
{
//NSArray *ivK = [ACommon runHelper:theKbag];
NSArray *ivK = [ACommon runHelper:theKbag];
if ([ivK count] == 0)
{
NSLog(@"game over man!!! so... start over man!!!");
return nil;
}
if ([[ivK objectAtIndex:0] isEqualToString:@"TRY_AGAIN"])
{
ivK = [ACommon runHelper:theKbag];
}
DebugLog(@"ivK: %@", ivK);
if ([ivK count] == 0)
return nil;
//DebugLog(@"ivk: %@", ivK);
NSString *k = [ivK objectAtIndex:3];
NSString *iv = [ivK objectAtIndex:1];
NSCharacterSet *theSet = [NSCharacterSet characterSetWithCharactersInString:@"\0\n "];
k = [k stringByTrimmingCharactersInSet:theSet];
k = [k stringByReplacingOccurrencesOfString:@"\0" withString:@""];
k = [k stringByReplacingOccurrencesOfString:@" " withString:@""];
k = [k stringByReplacingOccurrencesOfString:@"\n" withString:@""];
//NSString *kTrim = [k substringWithRange:NSMakeRange(0, 96)];
iv = [iv stringByReplacingOccurrencesOfString:@"\0" withString:@""];
iv = [iv stringByReplacingOccurrencesOfString:@" " withString:@""];
//DebugLog(@"iv: %@", iv);
NSDictionary *ivKDict = [NSDictionary dictionaryWithObjectsAndKeys:iv, @"iv", k, @"k", nil];
DebugLog(@"ivkDict: %@", ivKDict);
if ([k isEqualToString:@"fail!"])
return nil;
//NSString *dict = [[NSHomeDirectory() stringByAppendingPathComponent:theKbag] stringByAppendingPathExtension:@"plist"];
//[ivKDict writeToFile:dict atomically:YES];
return ivKDict;
}
+ (void)detachImage:(NSString *)irString
{
NSTask *irTask = [[NSTask alloc] init];
NSMutableArray *irArgs = [[NSMutableArray alloc] init];
[irArgs addObject:@"detach"];
[irArgs addObject:irString];
[irTask setLaunchPath:@"/usr/bin/hdiutil"];
[irTask setArguments:irArgs];
[irArgs release];
//DebugLog(@"hdiutil %@", [[irTask arguments] componentsJoinedByString:@" "]);
[irTask launch];
[irTask waitUntilExit];
[irTask release];
irTask = 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];
//DebugLog(@"hdiutil %@", [[irTask arguments] componentsJoinedByString:@" "]);
[irTask launch];
[irTask waitUntilExit];
NSData *outData;
outData = [hdih readDataToEndOfFile];
NSString *the_error;
NSPropertyListFormat format;
id plist;
plist = [NSPropertyListSerialization propertyListFromData:outData
mutabilityOption:NSPropertyListImmutable
format:&format
errorDescription:&the_error];
if(!plist)
{
DebugLog(@"%@", the_error);
[the_error release];
}
//DebugLog(@"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)
{
//DebugLog(@"Mount Point: %@", mountPath);
int rValue = [irTask terminationStatus];
if (rValue == 0)
{ [irTask release];
irTask = nil;
return mountPath;
}
}
}
[irTask release];
irTask = nil;
return nil;
}
+ (NSString *)genpassFromRamdisk:(NSString *)ramdisk platform:(NSString *)thePlatform andFilesystem:(NSString *)theFilesystem
{
NSString *command = [NSString stringWithFormat:@"\"%@\" %@ \"%@\" \"%@\"\n", GENPASS, thePlatform, ramdisk, theFilesystem];
DebugLog(@"%@", command);
return [ACommon singleLineReturnForProcess:command];
}
+ (NSString *)decryptFilesystem:(NSString *)fileSystem withKey:(NSString *)fileSystemKey
{
NSTask *vfTask = [[NSTask alloc] init];
[vfTask setLaunchPath:VFDECRYPT];
NSString *decrypted = [[[fileSystem stringByDeletingPathExtension] stringByAppendingString:@"_decrypt"] stringByAppendingPathExtension:@"dmg"];
[vfTask setArguments:[NSArray arrayWithObjects:@"-i", fileSystem, @"-k", fileSystemKey, @"-o", decrypted, nil]];
DebugLog(@"%@ %@\n", VFDECRYPT, [[vfTask arguments] componentsJoinedByString:@" "]);
[vfTask setStandardError:NULLOUT];
[vfTask setStandardOutput:NULLOUT];
[vfTask launch];
[vfTask waitUntilExit];
int returnStatus = [vfTask terminationStatus];
DebugLog(@"decryptFilesystem: %i", returnStatus);
[vfTask release];
vfTask = nil;
//return returnStatus;
return decrypted;
}
+(NSString *)singleLineReturnForProcess:(NSString *)call
{
if (call==nil)
return 0;
char line[200];
FILE* fp = popen([call UTF8String], "r");
NSString *s = nil;
if (fp)
{
while (fgets(line, sizeof line, fp))
{
s = [NSString stringWithCString:line encoding:NSUTF8StringEncoding];
}
}
pclose(fp);
return [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
+ (NSString *)stringReturnForTask:(NSString *)taskBinary withArguments:(NSArray *)taskArguments fromLocation:(NSString *)outputPath
{
DebugLog(@"%@ %@", taskBinary, [taskArguments componentsJoinedByString:@" "]);
NSTask *task = [[NSTask alloc] init];
NSPipe *pipe = [[NSPipe alloc] init];
NSFileHandle *handle = [pipe fileHandleForReading];
[task setCurrentDirectoryPath:outputPath];
[task setLaunchPath:taskBinary];
[task setArguments:taskArguments];
[task setStandardOutput:pipe];
[task setStandardError:NULLOUT];
[task launch];
NSData *outData = nil;
NSString *temp = nil;
//NSMutableArray *lineArray = [[NSMutableArray alloc] init];
while((outData = [handle readDataToEndOfFile]) && [outData length])
{
// temp = [[[NSString alloc] initWithData:outData encoding:NSASCIIStringEncoding] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
temp = [[NSString alloc] initWithData:outData encoding:NSASCIIStringEncoding];
DebugLog(@"temp length: %i", [temp length]);
//[lineArray addObject:temp];
//[temp release];
}
//DebugLog(@"lineArray: %@", lineArray);
[handle closeFile];
[task release];
task = nil;
return [temp autorelease];
}
+ (NSString *)stringReturnForTask:(NSString *)taskBinary withArguments:(NSArray *)taskArguments
{
DebugLog(@"%@ %@", taskBinary, [taskArguments componentsJoinedByString:@" "]);
NSTask *task = [[NSTask alloc] init];
NSPipe *pipe = [[NSPipe alloc] init];
NSFileHandle *handle = [pipe fileHandleForReading];
[task setLaunchPath:taskBinary];
[task setArguments:taskArguments];
[task setStandardOutput:pipe];
[task setStandardError:pipe];
[task launch];
NSData *outData = nil;
NSString *temp = nil;
//NSMutableArray *lineArray = [[NSMutableArray alloc] init];
while((outData = [handle readDataToEndOfFile]) && [outData length])
{
// temp = [[[NSString alloc] initWithData:outData encoding:NSASCIIStringEncoding] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
temp = [[NSString alloc] initWithData:outData encoding:NSASCIIStringEncoding];
DebugLog(@"temp length: %i", [temp length]);
//[lineArray addObject:temp];
//[temp release];
}
//DebugLog(@"lineArray: %@", lineArray);
[handle closeFile];
[task release];
task = nil;
return [temp autorelease];
}
+ (NSArray *)returnForTask:(NSString *)taskBinary withArguments:(NSArray *)taskArguments fromPath:(NSString *)runFolder
{
DebugLog(@"%@ %@", taskBinary, [taskArguments componentsJoinedByString:@" "]);
NSTask *task = [[NSTask alloc] init];
NSPipe *pipe = [[NSPipe alloc] init];
NSFileHandle *handle = [pipe fileHandleForReading];
[task setCurrentDirectoryPath:runFolder];
[task setLaunchPath:taskBinary];
[task setArguments:taskArguments];
[task setStandardOutput:pipe];
[task setStandardError:pipe];
[task launch];
NSData *outData = nil;
NSString *temp = nil;
NSMutableArray *lineArray = [[NSMutableArray alloc] init];
while((outData = [handle readDataToEndOfFile]) && [outData length])
{
// temp = [[[NSString alloc] initWithData:outData encoding:NSASCIIStringEncoding] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
temp = [[NSString alloc] initWithData:outData encoding:NSASCIIStringEncoding];
DebugLog(@"temp length: %i", [temp length]);
//[lineArray addObject:temp];
[lineArray addObjectsFromArray:[temp componentsSeparatedByString:@"\n"]];
[temp release];
}
DebugLog(@"lineArray: %@", lineArray);
[handle closeFile];
[task release];
task = nil;
return [lineArray autorelease];
}
+ (NSArray *)returnForTask:(NSString *)taskBinary withArguments:(NSArray *)taskArguments
{
DebugLog(@"%@ %@", taskBinary, [taskArguments componentsJoinedByString:@" "]);
NSTask *task = [[NSTask alloc] init];
NSPipe *pipe = [[NSPipe alloc] init];
NSFileHandle *handle = [pipe fileHandleForReading];
[task setLaunchPath:taskBinary];
[task setArguments:taskArguments];
[task setStandardOutput:pipe];
[task setStandardError:pipe];
[task launch];
NSData *outData = nil;
NSString *temp = nil;
NSMutableArray *lineArray = [[NSMutableArray alloc] init];
while((outData = [handle readDataToEndOfFile]) && [outData length])
{
// temp = [[[NSString alloc] initWithData:outData encoding:NSASCIIStringEncoding] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
temp = [[NSString alloc] initWithData:outData encoding:NSASCIIStringEncoding];
DebugLog(@"temp length: %i", [temp length]);
//[lineArray addObject:temp];
[lineArray addObjectsFromArray:[temp componentsSeparatedByString:@"\n"]];
[temp release];
}
DebugLog(@"lineArray: %@", lineArray);
[handle closeFile];
[task release];
task = nil;
return [lineArray autorelease];
}
+ (NSArray *)runHelper:(NSString *)theKbag
{
NSString *helpPath = DBHELPER;
NSTask *pwnHelper = [[NSTask alloc] init];
[pwnHelper setLaunchPath:helpPath];
NSPipe *swp = [[NSPipe alloc] init];
NSFileHandle *swh = [swp fileHandleForReading];
[pwnHelper setArguments:[NSArray arrayWithObjects:@"nil", theKbag, nil]];
[pwnHelper setStandardOutput:swp];
[pwnHelper setStandardError:swp];
[pwnHelper launch];
NSData *outData = nil;
//Variables needed for reading output
NSString *temp = nil;
NSMutableArray *lineArray = [[NSMutableArray alloc] init];
while((outData = [swh readDataToEndOfFile]) && [outData length])
{
temp = [[[NSString alloc] initWithData:outData encoding:NSASCIIStringEncoding] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//DebugLog(@"temp length: %i", [temp length]);
if ([temp length] > 800)
{
[swh closeFile];
[pwnHelper release];
pwnHelper = nil;
return [NSArray arrayWithObject:@"TRY_AGAIN"];
}
//DebugLog(@"temp: %@", [temp componentsSeparatedByString:@" "]);
//[lineArray addObject:[temp stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
//NSArray *arrayOne = [temp componentsSeparatedByString:@"\n"];
//DebugLog(@"arrayOneCount: %i", [arrayOne count]);
//NSArray *arrayTwo = [[arrayOne objectAtIndex:0] componentsSeparatedByString:@" "];
[lineArray addObjectsFromArray:[[temp stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsSeparatedByString:@" "]];
[temp release];
}
DebugLog(@"lineArray: %@", lineArray);
[swh closeFile];
[pwnHelper release];
pwnHelper = nil;
return [lineArray autorelease];
}
+ (NSString *)SHA1FromFile:(NSString *)inputFile
{
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];
[sslTask waitUntilExit];
NSData *outData = [ssh readDataToEndOfFile];
NSString *outputString = [[[NSString alloc] initWithData:outData
encoding:NSASCIIStringEncoding]
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *outputSHA = [[[outputString componentsSeparatedByString:@"="] lastObject] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[sslTask release];
sslTask = nil;
[sspipe release];
sspipe = nil;
return outputSHA;
}
+(NSArray *)returnForProcess:(NSString *)call
{
if (call==nil)
return 0;
char line[200];
FILE* fp = popen([call UTF8String], "r");
NSMutableArray *lines = [[NSMutableArray alloc]init];
if (fp)
{
while (fgets(line, sizeof line, fp))
{
NSString *s = [NSString stringWithCString:line encoding:NSUTF8StringEncoding];
[lines addObject:s];
}
}
pclose(fp);
return [lines autorelease];
}
+ (BOOL)unzipFile:(NSString *)theFile toPath:(NSString *)newPath
{
NSString *uzp = @"/usr/bin/unzip";
//NSFileManager *man = [NSFileManager defaultManager];
NSFileHandle *nullOut = [NSFileHandle fileHandleWithNullDevice];
//DebugLog(@"uzp2: %@", uzp2);
NSTask *unzipTask = [[NSTask alloc] init];
[unzipTask setLaunchPath:uzp];
[unzipTask setArguments:[NSArray arrayWithObjects:@"-o", theFile, @"-d", newPath, @"-x", @"*MACOSX*", nil]];
[unzipTask setStandardOutput:nullOut];
[unzipTask setStandardError:nullOut];
[unzipTask launch];
[unzipTask waitUntilExit];
int theTerm = [unzipTask terminationStatus];
//DebugLog(@"helperTask terminated with status: %i",theTerm);
if (theTerm != 0)
{
//DebugLog(@"failure unzip %@ to %@", theFile, newPath);
return (FALSE);
} else if (theTerm == 0){
//DebugLog(@"success unzip %@ to %@", theFile, newPath);
return (TRUE);
}
return (FALSE);
}
+ (int)decryptRamdisk:(NSString *)theRamdisk toPath:(NSString *)outputDisk withIV:(NSString *)iv key:(NSString *)key
{
NSTask *decryptTask = [[NSTask alloc] init];
[decryptTask setLaunchPath:XPWNTOOL];
NSMutableArray *decryptArgs = [[NSMutableArray alloc ]init];
[decryptArgs addObject:theRamdisk];
[decryptArgs addObject:outputDisk];
if (iv != nil)
{
if (key != nil)
{
[decryptArgs addObject:@"-iv"];
[decryptArgs addObject:iv];
[decryptArgs addObject:@"-k"];
[decryptArgs addObject:key];
}
}
//DebugLog(@"decryptArgs; %@", decryptArgs);
DebugLog(@"xpwntool %@\n", [decryptArgs componentsJoinedByString:@" "]);
[decryptTask setArguments:decryptArgs];
[decryptArgs release];
//[decryptTask setArguments:[NSArray arrayWithObjects:theRamdisk, outputDisk, @"-iv", iv, @"-k", key, nil]];
[decryptTask setStandardError:NULLOUT];
[decryptTask setStandardOutput:NULLOUT];
[decryptTask launch];
[decryptTask waitUntilExit];
int returnStatus = [decryptTask terminationStatus];
[decryptTask release];
decryptTask = nil;
return returnStatus;
}
- (void)unzipFile:(NSString *)theFile toPath:(NSString *)newPath
{
NSMutableDictionary *unzipDict = [[NSMutableDictionary alloc] init];
[unzipDict setObject:theFile forKey:@"theFile"];
[unzipDict setObject:newPath forKey:@"newPath"];
[NSThread detachNewThreadSelector:@selector(threadedUnzipFile:) toTarget:self withObject:[unzipDict autorelease]];
}
+ (void)changeStatus:(NSString *)theStatus
{
NSDictionary * userInfo = [NSDictionary dictionaryWithObjectsAndKeys: theStatus, @"Status", nil];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"statusChanged" object:nil userInfo:userInfo deliverImmediately:YES];
}
+ (void)updateMountVolume:(NSString *)updateMV
{
NSDictionary * userInfo = [NSDictionary dictionaryWithObjectsAndKeys: updateMV, @"mountVolume", nil];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"updateCF" object:nil userInfo:userInfo deliverImmediately:YES];
}
- (void)threadedUnzipFile:(NSDictionary *)theDict
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *theFile = [theDict valueForKey:@"theFile"];
NSString *newPath = [theDict valueForKey:@"newPath"];
NSString *uzp = @"/usr/bin/unzip";
//NSFileManager *man = [NSFileManager defaultManager];
NSFileHandle *nullOut = [NSFileHandle fileHandleWithNullDevice];
//DebugLog(@"uzp2: %@", uzp2);
NSTask *unzipTask = [[NSTask alloc] init];
[unzipTask setLaunchPath:uzp];
[unzipTask setArguments:[NSArray arrayWithObjects:@"-o", theFile, @"-d", newPath, @"-x", @"*MACOSX*", nil]];
[unzipTask setStandardOutput:nullOut];
[unzipTask setStandardError:nullOut];
[unzipTask launch];
[unzipTask waitUntilExit];
NSLog(@"unzip finished!");
[ACommon changeStatus:@"Unzip finished!"];
NSLog(@"passing dict: %@", theDict);
[[NSNotificationCenter defaultCenter] postNotificationName:@"unzipComplete" object:nil userInfo:theDict];
[pool release];
}
/*
NSMutableDictionary *envDict = [[NSMutableDictionary alloc] init];
[envDict setObject: @"/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate" forKey: @"CODESIGN_ALLOCATE"];
[mpTask setEnvironment:envDict];
[envDict release];
*/
+(NSDictionary *)environDict
{
return [NSDictionary dictionaryWithObject:@"/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate" forKey:@"CODESIGN_ALLOCATE"];
}
+ (void)writeCodesign:(NSString *)newCodesign toFile:(NSString *)newFile
{
NSTask *ldidTask = [[NSTask alloc] init];
[ldidTask setLaunchPath:LDID];
[ldidTask setEnvironment:[ACommon environDict]];
NSString *newString = [NSString stringWithFormat:@"-S%@", newCodesign];
[ldidTask setArguments:[NSArray arrayWithObjects:newString, newFile, nil]];
[ldidTask launch];
[ldidTask waitUntilExit];
[ldidTask release];
ldidTask = nil;
}
+ (void)writeCodesignDictionaryFromFile:(NSString *)inputFile toFile:(NSString *)outputFile
{
NSString *theOutput = [ACommon stringReturnForTask:LDID withArguments:[NSArray arrayWithObjects:@"-e", inputFile, nil]];
[theOutput writeToFile:outputFile atomically:YES];
}
+ (NSDictionary *)decryptFilesystemFromFirmware:(id)currentFirmware
{
NSString *outputFile = [[currentFirmware RestoreRamDisk] stringByDeletingPathExtension];
outputFile = [outputFile stringByAppendingString:@"_patched.dmg"];
NSDictionary *rdKeys = [currentFirmware ramdiskKey];
// NSLog(@"rdKeys: %@", rdKeys);
if (rdKeys != nil)
{
NSLog(@"decrypting: %@\n", [currentFirmware RestoreRamDisk]);
[ACommon changeStatus:[NSString stringWithFormat:@"decrypting: %@\n", [[currentFirmware RestoreRamDisk]lastPathComponent]]];
[ACommon decryptRamdisk:[currentFirmware RestoreRamDisk] toPath:outputFile withIV:[rdKeys valueForKey:@"iv"] key:[rdKeys valueForKey:@"k"]];
} else {
//DebugLog(@"ivkDict: %@", decryptedKbag);
DebugLog(@"no kbag! no encryption on ramdisk.\n");
[ACommon decryptRamdisk:[currentFirmware RestoreRamDisk] toPath:outputFile withIV:nil key:nil];
}
[ACommon changeStatus:@"generating vfdecrypt key..."];
NSLog(@"checking for vfdecrypt key...\n");
NSString *vfDecryptKey = [currentFirmware vfDecryptKey];
NSLog(@"vfdecrypt key: %@\n", vfDecryptKey);
if (vfDecryptKey == nil)
{
NSLog(@"generating vfdecrypt key...\n");
vfDecryptKey = [ACommon genpassFromRamdisk:outputFile platform:[currentFirmware platform] andFilesystem:[currentFirmware OS]];
vfDecryptKey = [vfDecryptKey stringByReplacingOccurrencesOfString:@"\n" withString:@""];
vfDecryptKey = [vfDecryptKey substringFromIndex:14];
vfDecryptKey = [vfDecryptKey cleanedString];
NSLog(@"vfdecrypt key: %@\n", vfDecryptKey);
}
if (vfDecryptKey != nil)
{
[ACommon changeStatus:[NSString stringWithFormat:@"decrypting filesystem: %@\n", [[currentFirmware OS] lastPathComponent]]];
NSLog(@"decrypting filesystem: %@\n", [currentFirmware OS]);
NSString *decrypted = [ACommon decryptFilesystem:[currentFirmware OS] withKey:vfDecryptKey];
NSString *mountedVolume = [ACommon mountImage:decrypted];
NSLog(@"mounted?: %@", mountedVolume);
if (mountedVolume == nil)
{
[ACommon changeStatus:@"invalid vfdecrypt key, trying to generate from update ramdisk!"];
NSLog(@"invalid vfdecrypt key, trying to generate from update ramdisk!");
[FM removeItemAtPath:decrypted error:nil];
NSString *updateRamdisk = [currentFirmware UpdateRamDisk];
if (updateRamdisk != nil)
{
//must've been a bad vfdecrypt key, lets try the other ramdisk (hoping there is one)
DebugLog(@"huzzah! there is an update ramdisk! %@ lets hope we have better luck with it!!", updateRamdisk);
AFirmwareFile *urd = [[AFirmwareFile alloc] initWithFile:updateRamdisk];
NSString *outputFile2 = [[currentFirmware UpdateRamDisk] stringByDeletingPathExtension];
outputFile2 = [outputFile2 stringByAppendingString:@"_patched.dmg"];
if ([urd encrypted] == TRUE)
{
[ACommon changeStatus:@"ramdisk is encrypted... finding kbag"];
NSLog(@"ramdisk is encrypted... finding kbag");
NSString *kbag2 = [urd keyBag];
NSLog(@"decrypting keybag....");
[ACommon changeStatus:@"decrypting keybag...."];
NSDictionary *decryptedKbag2 = [ACommon decryptedKbag:kbag2];
if (decryptedKbag2 == nil)
{
[ACommon changeStatus:@"bail!!!, gotz to repois0n!"];
NSLog(@"bail!!!, gotz to repois0n!");
return nil;
}
[ACommon changeStatus:[NSString stringWithFormat:@"decrypting: %@", [currentFirmware UpdateRamDisk]]];
NSLog(@"decrypting: %@", [currentFirmware UpdateRamDisk]);
[ACommon decryptRamdisk:[currentFirmware UpdateRamDisk] toPath:outputFile2 withIV:[decryptedKbag2 valueForKey:@"iv"] key:[decryptedKbag2 valueForKey:@"k"]];
} else {
//DebugLog(@"ivkDict: %@", decryptedKbag);
DebugLog(@"no kbag! no encryption on ramdisk.");
[ACommon decryptRamdisk:[currentFirmware UpdateRamDisk] toPath:outputFile2 withIV:nil key:nil];
}
[ACommon changeStatus:@"generating vfdecrypt key (take two)..."];
NSLog(@"generating vfdecrypt key (take two)...");
NSString *vfDecryptKey2 = [ACommon genpassFromRamdisk:outputFile2 platform:[currentFirmware platform] andFilesystem:[currentFirmware OS]];
vfDecryptKey2 = [vfDecryptKey2 stringByReplacingOccurrencesOfString:@"\n" withString:@""];
vfDecryptKey2 = [vfDecryptKey2 substringFromIndex:14];
vfDecryptKey2 = [vfDecryptKey2 cleanedString];
[ACommon changeStatus:[NSString stringWithFormat:@"vfdecrypt key take 2: %@", vfDecryptKey2]];
DebugLog(@"vfdecrypt key take 2: %@", vfDecryptKey2);
if (vfDecryptKey2 != nil)
{
NSString *decrypted2 = [ACommon decryptFilesystem:[currentFirmware OS] withKey:vfDecryptKey2];
NSString *mountedVolume2 = [ACommon mountImage:decrypted2];
NSLog(@"mounted?: %@", mountedVolume2);
if (mountedVolume2 != nil)
{
DebugLog(@"valid vfdecrypt found!: %@", vfDecryptKey);
[ACommon changeStatus:[NSString stringWithFormat:@"valid vfdecrypt found!: %@", vfDecryptKey]];
//[ACommon updateMountVolume:mountedVolume2];
//return vfDecryptKey2;
return [NSDictionary dictionaryWithObjectsAndKeys:vfDecryptKey2, @"vfdecrypt", mountedVolume2, @"mountVolume", nil];
}
}
}
} else {
DebugLog(@"valid vfdecrypt found!: %@", vfDecryptKey);
//[ACommon updateMountVolume:mountedVolume];
return [NSDictionary dictionaryWithObjectsAndKeys:vfDecryptKey, @"vfdecrypt", mountedVolume, @"mountVolume", nil];
//return vfDecryptKey;
}
}
return nil;
}
@end