-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRamdiskBuilder.m
More file actions
executable file
·258 lines (189 loc) · 7.09 KB
/
RamdiskBuilder.m
File metadata and controls
executable file
·258 lines (189 loc) · 7.09 KB
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
/*!
@source RamdiskBuilder.m
@project Pusher
@author Alexander Maksimenko
@copyright Copyright (c) 2009 Ripdev. All rights reserved.
*/
#import "RootFSDecrypt.h"
#import "Firmware.h"
#import "OpenSSLHelper.h"
#import "RamdiskBuilder.h"
@implementation RamdiskBuilder
-(id) init
{
self = [super init];
_firmware = nil;
_secureTask = [[SecureTask alloc] init];
return self;
}
-(void) dealloc
{
[_firmware release];
[_secureTask release];
[super dealloc];
}
-(void) threadTask:(NSDictionary*) dict
{
_abortBuilding = NO;
[self sendMessage:BuilderStatusMessage withData:[NSArchiver archivedDataWithRootObject:[NSNumber numberWithInt:BSStart]]];
if([[InstanceManager firmware] unzipTo:SystemTempDir] && [self buildRamdisk])
[self sendMessage:BuilderStatusMessage withData:[NSArchiver archivedDataWithRootObject:[NSNumber numberWithInt:BSSuccess]]];
else
[self sendMessage:BuilderStatusMessage withData:[NSArchiver archivedDataWithRootObject:[NSNumber numberWithInt:BSFailed]]];
}
-(BOOL) buildRamdisk
{
if(![self patchFirmware] || _abortBuilding)
return NO;
if(![self copyRamdisk] || _abortBuilding)
return NO;
return YES;
}
-(BOOL) copyRamdisk
{
// Copy ramdisk
NSFileManager* fileManager = [NSFileManager defaultManager];
NSString* ramdisk = [SystemTempDir stringByAppendingPathComponent:[[InstanceManager firmware] restoreRamdiskFile]];
NSString* ramdiskResource = [InstanceManager ramdiskPath];
[fileManager removeFileAtPath:ramdisk handler:nil];
if(![fileManager copyPath:ramdiskResource toPath:ramdisk handler:nil])
{
Log(@"Failed to copy pusher ramdisk to temp location");
return NO;
}
[OpenSSLHelper updateFile:ramdisk with:Decrypt];
return YES;
}
-(BOOL) rebuildRamdisk
{
// Copy ramdisk
NSFileManager* fileManager = [NSFileManager defaultManager];
NSString* ramdisk = [SystemTempDir stringByAppendingPathComponent:CustomRamdisk];
NSString* ramdiskResource = [InstanceManager ramdiskPath];
if(![fileManager copyPath:ramdiskResource toPath:ramdisk handler:nil])
{
Log(@"Failed to copy pusher ramdisk to temp location");
return NO;
}
if(![[InstanceManager firmware] makeRamdisk:rtRestore from:ramdisk encrypt:NO])
{
Log(@"Failed to inject pusher ramdisk to restore ramdisk");
return NO;
}
if(![fileManager removeFileAtPath:ramdisk handler:nil])
{
Log(@"Failed to remove temp copy of pusher ramdisk");
return NO;
}
return YES;
}
-(BOOL) patchFirmware
{
NSDictionary* patches = [[InstanceManager firmware] firmwarePatches];
NSArray* keys = [patches allKeys];
unsigned int keysCount = [keys count];
unsigned int i;
for(i = 0;i < keysCount;i++)
{
NSString* module = [keys objectAtIndex:i];
if([module isEqualToString:FWWTF2Key] || [module isEqualToString:FWIBSSKey] || [module isEqualToString:FWKernelCacheKey])
{
if([[patches objectForKey:module] objectForKey:PatchKey] != nil)
{
NSDictionary* dict = [patches objectForKey:module];
int type = [[dict objectForKey:TypeFlagKey] intValue];
NSString* file = [SystemTempDir stringByAppendingPathComponent:[dict objectForKey:FileKey]];
NSString* patch = [[InstanceManager firmware] bundlePath];
NSString* key = [dict objectForKey:KeyKey];
NSString* iv = [dict objectForKey:IVKey];
BOOL exploit = NO;
if([module isEqualToString:FWWTF2Key])
exploit = YES;
patch = [patch stringByAppendingPathComponent:[dict objectForKey:PatchKey]];
if(![[InstanceManager firmware] patchFirmwareModule:file ofType:type withPatch:patch key:key iv:iv exploit:exploit])
{
Log(@"Failed to patch %@", file);
return NO;
}
}
}
}
if(![[InstanceManager firmware] makeBootLogo:[[NSBundle mainBundle] pathForResource:@"logo" ofType:@"png"]])
{
Log(@"Failed to make boot logo");
return NO;
}
return YES;
}
-(BOOL) actionPatch:(NSString*) file :(NSString*) patch
{
char* args[5] = {"/usr/bin/bspatch", (char*)[[self safePath:file] UTF8String], (char*)[[self safePath:file] UTF8String], (char*)[[self safePath:patch] UTF8String], NULL};
// Log(@"patch %@ with %@", [self safePath:file], [self safePath:patch]);
if(![_secureTask runTaskWithArgs:args])
return NO;
return YES;
}
-(BOOL) actionAdd:(NSString*) file :(NSString*) path
{
BOOL isDir = NO;
char* args[4] = {"/bin/mkdir", "-p", (char*)[[self safePath:[path stringByDeletingLastPathComponent]] UTF8String], NULL};
// Log(@"mkdir %@", [self safePath:[path stringByDeletingLastPathComponent]]);
if(![_secureTask runTaskWithArgs:args])
return NO;
[[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:&isDir];
char* args2[5] = {"/bin/cp", isDir ? "-vfR" : "-vf", (char*)[[self safePath:file] UTF8String], (char*)[[self safePath:path] UTF8String], NULL};
// Log(@"cp %@ to %@", [self safePath:file], [self safePath:path]);
if(![_secureTask runTaskWithArgs:args2])
return NO;
if([[_secureTask output] rangeOfString:NoSpaceLeftString].length > 0)
{
Log(@"No free space left");
return NO;
}
return YES;
}
-(BOOL) actionReplaceKernel:(NSString*) file :(NSString*) path
{
char* args[5] = {"/bin/cp", "-vf", (char*)[[self safePath:file] UTF8String], (char*)[[self safePath:path] UTF8String], NULL};
// Log(@"cp %@ to %@", [self safePath:file], [self safePath:path]);
if(![_secureTask runTaskWithArgs:args])
return NO;
return YES;
}
-(BOOL) actionSetOwner:(NSString*) file :(NSString*) owner
{
BOOL isDir = NO;
[[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:&isDir];
char* args[5] = {"/usr/sbin/chown", "-v", (char*)[owner UTF8String], (char*)[[self safePath:file] UTF8String], NULL};
// Log(@"chown %@", [self safePath:file]);
if(![_secureTask runTaskWithArgs:args])
return NO;
return YES;
}
-(BOOL) actionSetPermission:(NSString*) file :(NSString*) permission
{
BOOL isDir = NO;
[[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:&isDir];
char* args[5] = {"/bin/chmod", "-v", (char*)[permission UTF8String], (char*)[[self safePath:file] UTF8String], NULL};
if(![_secureTask runTaskWithArgs:args])
return NO;
return YES;
}
-(BOOL) actionRunScript:(NSString*) file :(NSString*) path
{
char* args[4] = {"/bin/sh", (char*)[[self safePath:file] UTF8String], (char*)[[self safePath:path] UTF8String], NULL};
if(![_secureTask runTaskWithArgs:args])
return NO;
return YES;
}
-(NSString*) safePath:(NSString*) string
{
return [NSString stringWithFormat:@"\"%@\"", string];
}
-(void) handlePortMessage:(NSPortMessage*) portMessage
{
unsigned int message = [portMessage msgid];
if(message == BuilderStopMessage)
_abortBuilding = YES;
}
@end