forked from preemptive/PPiOS-Rename
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CDLCSegment.m
324 lines (258 loc) · 10.9 KB
/
CDLCSegment.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
// -*- mode: ObjC -*-
/********************************************
Copyright 2016 PreEmptive Solutions, LLC
See LICENSE.txt for licensing information
********************************************/
// This file is part of class-dump, a utility for examining the Objective-C segment of Mach-O files.
// Copyright (C) 1997-1998, 2000-2001, 2004-2015 Steve Nygard.
#import "CDLCSegment.h"
#import "CDMachOFile.h"
#import "CDSection.h"
#include <CommonCrypto/CommonCrypto.h>
NSString *CDSegmentEncryptionTypeName(CDSegmentEncryptionType type)
{
switch (type) {
case CDSegmentEncryptionType_None: return @"None";
case CDSegmentEncryptionType_AES: return @"Protected Segment Type 1 (prior to 10.6)";
case CDSegmentEncryptionType_Blowfish: return @"Protected Segment Type 2 (10.6)";
case CDSegmentEncryptionType_Unknown: return @"Unknown";
}
}
@implementation CDLCSegment
{
struct segment_command_64 _segmentCommand; // 64-bit, also holding 32-bit
NSString *_name;
NSArray *_sections;
NSMutableData *_decryptedData;
}
- (id)initWithDataCursor:(CDMachOFileDataCursor *)cursor;
{
if ((self = [super initWithDataCursor:cursor])) {
_segmentCommand.cmd = [cursor readInt32];
_segmentCommand.cmdsize = [cursor readInt32];
_name = [cursor readStringOfLength:16 encoding:NSASCIIStringEncoding];
memcpy(_segmentCommand.segname, [_name UTF8String], sizeof(_segmentCommand.segname));
_segmentCommand.vmaddr = [cursor readPtr];
_segmentCommand.vmsize = [cursor readPtr];
_segmentCommand.fileoff = [cursor readPtr];
_segmentCommand.filesize = [cursor readPtr];
_segmentCommand.maxprot = [cursor readInt32];
_segmentCommand.initprot = [cursor readInt32];
_segmentCommand.nsects = [cursor readInt32];
_segmentCommand.flags = [cursor readInt32];
NSMutableArray *sections = [[NSMutableArray alloc] init];
for (NSUInteger index = 0; index < _segmentCommand.nsects; index++) {
CDSection *section = [[CDSection alloc] initWithDataCursor:cursor segment:self];
[sections addObject:section];
}
_sections = [sections copy];
}
return self;
}
#pragma mark - Debugging
- (NSString *)extraDescription;
{
int padding = (int)self.machOFile.ptrSize * 2;
return [NSString stringWithFormat:@"vmaddr: 0x%0*llx - 0x%0*llx [0x%0*llx], offset: %lld, flags: 0x%x (%@), nsects: %d, sections: %@",
padding, _segmentCommand.vmaddr, padding, _segmentCommand.vmaddr + _segmentCommand.vmsize - 1, padding, _segmentCommand.vmsize,
_segmentCommand.fileoff, self.flags, [self flagDescription], _segmentCommand.nsects, self.sections.count > 0 ? self.sections : @"N/A"];
}
#pragma mark -
- (uint32_t)cmd;
{
return _segmentCommand.cmd;
}
- (uint32_t)cmdsize;
{
return _segmentCommand.cmdsize;
}
- (NSUInteger)vmaddr;
{
return _segmentCommand.vmaddr;
}
- (NSUInteger)vmsize;
{
return _segmentCommand.vmsize;
}
- (NSUInteger)fileoff;
{
return _segmentCommand.fileoff;
}
- (NSUInteger)filesize;
{
return _segmentCommand.filesize;
}
- (vm_prot_t)initprot;
{
return _segmentCommand.initprot;
}
- (uint32_t)flags;
{
return _segmentCommand.flags;
}
- (BOOL)isProtected;
{
return (self.flags & SG_PROTECTED_VERSION_1) == SG_PROTECTED_VERSION_1;
}
- (CDSegmentEncryptionType)encryptionType;
{
//NSLog(@"%s, isProtected? %u, filesize: %lu, fileoff: %lu", __cmd, [self isProtected], [self filesize], [self fileoff]);
if (self.isProtected) {
if (self.filesize <= 3 * PAGE_SIZE) {
// First three pages aren't encrypted, so we can't tell. Let's pretent it's something we can decrypt.
return CDSegmentEncryptionType_AES;
} else {
const void *src = (uint8_t *)[self.machOFile.data bytes] + self.fileoff + 3 * PAGE_SIZE;
uint32_t magic = OSReadLittleInt32(src, 0);
//NSLog(@"%s, magic= 0x%08x", __cmd, magic);
switch (magic) {
case CDSegmentProtectedMagic_None: return CDSegmentEncryptionType_None;
case CDSegmentProtectedMagic_AES: return CDSegmentEncryptionType_AES;
case CDSegmentProtectedMagic_Blowfish: return CDSegmentEncryptionType_Blowfish;
}
return CDSegmentEncryptionType_Unknown;
}
}
return CDSegmentEncryptionType_None;
}
- (BOOL)canDecrypt;
{
CDSegmentEncryptionType encryptionType = self.encryptionType;
return (encryptionType == CDSegmentEncryptionType_None)
|| (encryptionType == CDSegmentEncryptionType_AES)
|| (encryptionType == CDSegmentEncryptionType_Blowfish);
}
- (NSString *)flagDescription;
{
NSMutableArray *setFlags = [NSMutableArray array];
uint32_t flags = self.flags;
if (flags & SG_HIGHVM) [setFlags addObject:@"HIGHVM"];
if (flags & SG_FVMLIB) [setFlags addObject:@"FVMLIB"];
if (flags & SG_NORELOC) [setFlags addObject:@"NORELOC"];
if (flags & SG_PROTECTED_VERSION_1) [setFlags addObject:@"PROTECTED_VERSION_1"];
if ([setFlags count] == 0)
return @"none";
return [setFlags componentsJoinedByString:@" "];
}
- (BOOL)containsAddress:(NSUInteger)address;
{
return (address >= _segmentCommand.vmaddr) && (address < _segmentCommand.vmaddr + _segmentCommand.vmsize);
}
- (BOOL)containsOffset:(NSUInteger)address
{
return (address >= _segmentCommand.fileoff) && (address < _segmentCommand.fileoff + _segmentCommand.filesize);
}
- (CDSection *)sectionContainingAddress:(NSUInteger)address;
{
for (CDSection *section in self.sections) {
if ([section containsAddress:address])
return section;
}
return nil;
}
- (CDSection *)sectionWithName:(NSString *)name;
{
for (CDSection *section in self.sections) {
if ([[section sectionName] isEqual:name])
return section;
}
return nil;
}
- (NSUInteger)fileOffsetForAddress:(NSUInteger)address;
{
return [[self sectionContainingAddress:address] fileOffsetForAddress:address];
}
- (NSUInteger)addressForDataOffset:(NSUInteger)offset
{
return self.vmaddr + (offset - self.fileoff);
}
- (NSUInteger)segmentOffsetForAddress:(NSUInteger)address;
{
return [self fileOffsetForAddress:address] - self.fileoff;
}
- (void)appendToString:(NSMutableString *)resultString verbose:(BOOL)isVerbose;
{
[super appendToString:resultString verbose:isVerbose];
}
- (void)writeSectionData;
{
[self.sections enumerateObjectsUsingBlock:^(CDSection *section, NSUInteger index, BOOL *stop){
[[section data] writeToFile:[NSString stringWithFormat:@"/tmp/%02ld-%@", index, section.sectionName] atomically:NO];
}];
}
- (NSData *)decryptedData;
{
if (self.isProtected == NO)
return nil;
if (_decryptedData == nil) {
//NSLog(@"filesize: %08x, pagesize: %04x", [self filesize], PAGE_SIZE);
NSParameterAssert((self.filesize % PAGE_SIZE) == 0);
_decryptedData = [[NSMutableData alloc] initWithLength:self.filesize];
const uint8_t *src = (uint8_t *)[self.machOFile.data bytes] + self.fileoff;
uint8_t *dest = [_decryptedData mutableBytes];
if (self.filesize <= PAGE_SIZE * 3) {
memcpy(dest, src, [self filesize]);
} else {
uint8_t keyData[64] = { 0x6f, 0x75, 0x72, 0x68, 0x61, 0x72, 0x64, 0x77, 0x6f, 0x72, 0x6b, 0x62, 0x79, 0x74, 0x68, 0x65,
0x73, 0x65, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x67, 0x75, 0x61, 0x72, 0x64, 0x65, 0x64, 0x70, 0x6c,
0x65, 0x61, 0x73, 0x65, 0x64, 0x6f, 0x6e, 0x74, 0x73, 0x74, 0x65, 0x61, 0x6c, 0x28, 0x63, 0x29,
0x41, 0x70, 0x70, 0x6c, 0x65, 0x43, 0x6f, 0x6d, 0x70, 0x75, 0x74, 0x65, 0x72, 0x49, 0x6e, 0x63, };
// First three pages aren't encrypted, just copy
memcpy(dest, src, PAGE_SIZE * 3);
src += PAGE_SIZE * 3;
dest += PAGE_SIZE * 3;
NSUInteger count = (self.filesize / PAGE_SIZE) - 3;
uint32_t magic = OSReadLittleInt32(src, 0);
if (magic == CDSegmentProtectedMagic_None) {
memcpy(dest, src, [self filesize] - PAGE_SIZE * 3);
} else if (magic == CDSegmentProtectedMagic_Blowfish) {
// 10.6 decryption
CCCryptorRef cryptor;
CCCryptorStatus status = CCCryptorCreate(kCCDecrypt, kCCAlgorithmBlowfish, 0, keyData, sizeof(keyData), NULL, &cryptor);
NSParameterAssert(status == kCCSuccess);
for (NSUInteger index = 0; index < count; index++) {
status = CCCryptorReset(cryptor, NULL);
NSParameterAssert(status == kCCSuccess);
size_t moved;
status = CCCryptorUpdate(cryptor, src, PAGE_SIZE, dest, PAGE_SIZE, &moved);
NSParameterAssert(status == kCCSuccess);
NSParameterAssert(moved == PAGE_SIZE);
src += PAGE_SIZE;
dest += PAGE_SIZE;
}
CCCryptorRelease(cryptor);
} else if (magic == CDSegmentProtectedMagic_AES) {
// 10.5 decryption
CCCryptorRef cryptor1, cryptor2;
CCCryptorStatus status;
status = CCCryptorCreate(kCCDecrypt, kCCAlgorithmAES, 0, keyData, 32, NULL, &cryptor1);
NSParameterAssert(status == kCCSuccess);
status = CCCryptorCreate(kCCDecrypt, kCCAlgorithmAES, 0, keyData + 32, 32, NULL, &cryptor2);
NSParameterAssert(status == kCCSuccess);
size_t halfPageSize = PAGE_SIZE / 2;
for (NSUInteger index = 0; index < count; index++) {
status = CCCryptorReset(cryptor1, NULL);
NSParameterAssert(status == kCCSuccess);
status = CCCryptorReset(cryptor2, NULL);
NSParameterAssert(status == kCCSuccess);
size_t moved;
status = CCCryptorUpdate(cryptor1, src, halfPageSize, dest, halfPageSize, &moved);
NSParameterAssert(status == kCCSuccess);
NSParameterAssert(moved == halfPageSize);
status = CCCryptorUpdate(cryptor2, src + halfPageSize, halfPageSize, dest + halfPageSize, halfPageSize, &moved);
NSParameterAssert(status == kCCSuccess);
NSParameterAssert(moved == halfPageSize);
src += PAGE_SIZE;
dest += PAGE_SIZE;
}
CCCryptorRelease(cryptor1);
CCCryptorRelease(cryptor2);
} else {
NSLog(@"Unknown encryption type: 0x%08x", magic);
exit(99);
}
}
}
return _decryptedData;
}
@end