-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.c
481 lines (393 loc) · 13 KB
/
main.c
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
/*
* This file is based on code from Amit Singh, published here:
* http://osxbook.com/book/bonus/chapter7/tpmdrmmyth/
* Copyright (c) 2008 Amit Singh. All Rights Reserved.
*
* This implementation copyright (c) 2009 Jim Dovey.
*/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sysexits.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <copyfile.h>
#include <mach/mach.h>
#include <mach/machine.h>
#include <mach-o/fat.h>
#include <mach-o/loader.h>
#include <openssl/aes.h>
#include <IOKit/IOKitLib.h>
#define APB_UNPROTECTED_HEADER_SIZE (3 * PAGE_SIZE)
#define APB_CRYPT_AES_KEY_SIZE (256)
#define APB_FAT_MAX_ARCH (5)
#define EX_TEXTTOOSMALL (EX__MAX + 1)
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
# ifndef MIN
# define MIN(A,B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; })
# endif
#else
# ifndef MIN
# define MIN(A,B) ((A) < (B) ? (A) : (B))
# endif
#endif
static char header_page[PAGE_SIZE];
static char arch_page[PAGE_SIZE];
static char data_page[PAGE_SIZE];
static char xcrypted_page[PAGE_SIZE];
int fd_in = -1;
int fd_out = -1;
static boolean_t apb_set_key( int mode, uint8_t * data, AES_KEY * key );
static boolean_t apb_initialize( int mode, AES_KEY * key1, AES_KEY * key2 );
static int apb_encrypt_page( int mode, const void * src, void * dst );
static io_connect_t AppleSMC_Connect( void );
static void AppleSMC_Disconnect( io_connect_t smc );
static IOReturn AppleSMC_Read32( io_connect_t smc, uint32_t key, uint8_t *pData );
typedef struct
{
uint32_t key;
uint8_t __d0[22];
uint32_t datasize;
uint8_t __d1[10];
uint8_t cmd;
uint32_t __d2;
uint8_t data[32];
} AppleSMCBuffer_t;
#pragma mark -
static io_connect_t AppleSMC_Connect( void )
{
io_connect_t port = (io_connect_t)0;
io_service_t service = IOServiceGetMatchingService( kIOMasterPortDefault, IOServiceMatching("AppleSMC") );
if ( service == 0 )
return ( 0 );
kern_return_t kr = IOServiceOpen( service, mach_task_self(), 0, &port );
IOObjectRelease( service );
if ( kr != kIOReturnSuccess )
return ( 0 );
return ( port );
}
static void AppleSMC_Disconnect( io_connect_t smc )
{
(void) IOServiceClose( smc );
}
static IOReturn AppleSMC_Read32( io_connect_t smc, uint32_t key, uint8_t * data32 )
{
AppleSMCBuffer_t input = { 0, {0}, 32, {0}, 5, };
AppleSMCBuffer_t output;
size_t outputSize = sizeof(AppleSMCBuffer_t);
input.key = key;
IOReturn kr = IOConnectCallStructMethod( (mach_port_t)smc, 2, (const void *)&input,
sizeof(AppleSMCBuffer_t), (void *)&output, &outputSize );
if ( kr != kIOReturnSuccess )
return ( kr );
(void) memcpy( data32, output.data, 32 );
return ( kIOReturnSuccess );
}
#pragma mark -
static boolean_t apb_set_key( int mode, uint8_t * data, AES_KEY * key )
{
switch ( mode )
{
case AES_ENCRYPT:
AES_set_encrypt_key( data, APB_CRYPT_AES_KEY_SIZE, key );
break;
case AES_DECRYPT:
AES_set_decrypt_key( data, APB_CRYPT_AES_KEY_SIZE, key );
break;
default:
return ( FALSE );
break;
}
return ( TRUE );
}
static boolean_t apb_initialize( int mode, AES_KEY * key1, AES_KEY * key2 )
{
boolean_t result = FALSE;
io_connect_t smc = AppleSMC_Connect();
if ( smc == 0 )
return ( FALSE );
do
{
IOReturn ret;
uint8_t data32[32] = { 0 };
ret = AppleSMC_Read32( smc, 'OSK0', data32 );
if ( ret != kIOReturnSuccess )
break;
if ( apb_set_key(mode, data32, key1) == FALSE )
break;
ret = AppleSMC_Read32( smc, 'OSK1', data32 );
if ( ret != kIOReturnSuccess )
break;
if ( apb_set_key(mode, data32, key2) == FALSE )
break;
result = TRUE;
} while (0);
AppleSMC_Disconnect( smc );
return ( result );
}
static int apb_encrypt_page( int mode, const void * src, void * dst )
{
static AES_KEY key1, key2;
static boolean_t initialized = FALSE;
if ( initialized == FALSE )
{
initialized = apb_initialize( mode, &key1, &key2 );
if ( initialized == FALSE )
return ( -1 );
}
const unsigned char * in = (const unsigned char *) src;
unsigned char * out = (unsigned char *) dst;
unsigned char apb_null_iv1[AES_BLOCK_SIZE] = { 0 };
unsigned char apb_null_iv2[AES_BLOCK_SIZE] = { 0 };
AES_cbc_encrypt( in, out, PAGE_SIZE >> 1, &key1, apb_null_iv1, mode );
in += (PAGE_SIZE >> 1);
out += (PAGE_SIZE >> 1);
AES_cbc_encrypt( in, out, PAGE_SIZE >> 1, &key2, apb_null_iv2, mode );
return ( 0 );
}
static int crypt_text_segment( int mode, off_t base, off_t fileoff, off_t filesize )
{
off_t archbase_begin = (off_t)(fileoff + APB_UNPROTECTED_HEADER_SIZE);
off_t archbase_end = archbase_begin + (off_t)(filesize - APB_UNPROTECTED_HEADER_SIZE);
off_t ebase_begin = base + archbase_begin;
off_t ebase_end = base + archbase_end;
off_t count = ebase_end - ebase_begin;
if ( (count % PAGE_SIZE) != 0 )
{
fprintf( stderr, "text segment is not a multiple of page size.\n" );
return ( EX_SOFTWARE );
}
while ( count > 0 )
{
ssize_t nbytes = pread( fd_in, data_page, PAGE_SIZE, ebase_begin );
if ( nbytes != PAGE_SIZE )
{
perror( "pread" );
return ( EX_IOERR );
}
int err = apb_encrypt_page( mode, data_page, xcrypted_page );
if ( err != 0 )
{
fprintf( stderr, "failed to %s page.\n", (mode == AES_ENCRYPT ? "encrypt" : "decrypt") );
return ( EX_SOFTWARE );
}
nbytes = pwrite( fd_out, xcrypted_page, PAGE_SIZE, ebase_begin );
if ( nbytes != PAGE_SIZE )
{
perror( "pwrite" );
return ( EX_IOERR );
}
ebase_begin += (off_t)PAGE_SIZE;
count -= (off_t)PAGE_SIZE;
}
return ( EX_OK );
}
static int crypt_binary_64( const struct mach_header_64 * mh, off_t base )
{
int mode = AES_ENCRYPT;
struct segment_command_64 * text = (struct segment_command_64 *)0;
uint32_t ncmds = mh->ncmds;
struct load_command * lc = (struct load_command *)((char *)mh + sizeof(struct mach_header_64));
uint32_t n;
for ( n = 0; n < ncmds; n++ )
{
if ( lc->cmd == LC_SEGMENT_64 )
{
struct segment_command_64 * sc = (struct segment_command_64 *) lc;
if ( strncmp(sc->segname, SEG_TEXT, 16) == 0 )
{
text = sc;
break;
}
}
lc = (struct load_command *)((char *)lc + lc->cmdsize);
}
if ( text == NULL )
{
fprintf( stderr, "failed to find text segment.\n" );
return ( EX_SOFTWARE );
}
if ( (text->flags & SG_PROTECTED_VERSION_1) == SG_PROTECTED_VERSION_1 )
{
mode = AES_DECRYPT;
fprintf( stdout, "binary is encrypted - will decrypt it.\n" );
}
else if ( text->filesize < APB_UNPROTECTED_HEADER_SIZE )
{
fprintf( stderr, "text segment is too small to protect\n" );
return ( EX_TEXTTOOSMALL );
}
if ( mode == AES_ENCRYPT )
text->flags |= SG_PROTECTED_VERSION_1;
else
text->flags &= ~SG_PROTECTED_VERSION_1;
ssize_t nbytes = pwrite( fd_out, arch_page, PAGE_SIZE, base );
if ( nbytes != PAGE_SIZE )
{
perror( "pwrite" );
return ( EX_IOERR );
}
return ( crypt_text_segment(mode, base, (off_t)text->fileoff, (off_t)text->filesize) );
}
static int crypt_binary( const struct mach_header * mh, off_t base )
{
int mode = AES_ENCRYPT;
struct segment_command * text = (struct segment_command *)0;
uint32_t ncmds = mh->ncmds;
struct load_command * lc = (struct load_command *)((char *)mh + sizeof(struct mach_header));
uint32_t n;
for ( n = 0; n < ncmds; n++ )
{
if ( lc->cmd == LC_SEGMENT )
{
struct segment_command * sc = (struct segment_command *) lc;
if ( strncmp(sc->segname, SEG_TEXT, 16) == 0 )
{
text = sc;
break;
}
}
lc = (struct load_command *)((char *)lc + lc->cmdsize);
}
if ( text == NULL )
{
fprintf( stderr, "failed to find text segment.\n" );
return ( EX_SOFTWARE );
}
if ( (text->flags & SG_PROTECTED_VERSION_1) == SG_PROTECTED_VERSION_1 )
{
mode = AES_DECRYPT;
fprintf( stdout, "binary is encrypted - will decrypt it.\n" );
}
else if ( text->filesize < APB_UNPROTECTED_HEADER_SIZE )
{
fprintf( stderr, "text segment is too small to protect\n" );
return ( EX_TEXTTOOSMALL );
}
if ( mode == AES_ENCRYPT )
text->flags |= SG_PROTECTED_VERSION_1;
else
text->flags &= ~SG_PROTECTED_VERSION_1;
ssize_t nbytes = pwrite( fd_out, arch_page, PAGE_SIZE, base );
if ( nbytes != PAGE_SIZE )
{
perror( "pwrite" );
return ( EX_IOERR );
}
return ( crypt_text_segment(mode, base, (off_t)text->fileoff, (off_t)text->filesize) );
}
int main( int argc, char * const argv[] )
{
if ( argc != 3 )
{
fprintf( stderr, "usage: %s <infile> <outfile>\n", argv[0] );
exit( EX_USAGE );
}
int ret = 0;
fd_in = open( argv[1], O_RDONLY );
if ( fd_in < 0 )
{
perror( "open" );
exit( EX_IOERR );
}
fd_out = open( argv[2], O_RDWR | O_CREAT | O_EXCL, 0775 );
if ( fd_out < 0 )
{
perror( "open" );
ret = EX_IOERR;
goto out;
}
// I copy the file earlier, so I can encrypt multiple text segments
// in a fat binary
ret = fcopyfile( fd_in, fd_out, (copyfile_state_t)0, COPYFILE_ALL );
if ( ret != 0 )
{
perror( "copyfile" );
ret = EX_OSERR;
goto out;
}
off_t base = 0;
uint32_t n = 0;
ssize_t nbytes = pread( fd_in, header_page, PAGE_SIZE, (off_t)0 );
if ( nbytes != PAGE_SIZE )
{
perror( "pread" );
ret = EX_IOERR;
goto out;
}
uint32_t magic = *(uint32_t *) header_page;
struct mach_header * mh = (struct mach_header *) NULL;
#if defined(__ppc__) || defined(__ppc64__)
# error This code won't compile for PPC, where encrypted binaries are not supported.
#endif
if ( magic == FAT_CIGAM )
{
// byte-swapped FAT header
struct fat_header * fh = (struct fat_header *) header_page;
uint32_t nfat_arch = OSSwapConstInt32( fh->nfat_arch );
if ( nfat_arch > APB_FAT_MAX_ARCH )
{
fprintf( stderr, "too many architectures in Universal binary.\n" );
ret = EX_SOFTWARE;
goto out;
}
struct fat_arch * fa = (struct fat_arch *)((char *)header_page + sizeof(struct fat_header));
for ( n = 0; n < nfat_arch; n++, fa++ )
{
// match against either i386 or x86-64
cpu_type_t cputype = OSSwapConstInt32(fa->cputype);
if ( (cputype & ~CPU_ARCH_MASK) == CPU_TYPE_X86 )
{
base = (off_t) OSSwapConstInt32(fa->offset);
nbytes = pread( fd_in, arch_page, PAGE_SIZE, base );
if ( nbytes != PAGE_SIZE )
{
fprintf( stderr, "failed to read Universal binary.\n" );
ret = EX_IOERR;
goto out;
}
int subret;
if ( (cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64 )
subret = crypt_binary_64( (struct mach_header_64 *)arch_page, base );
else
subret = crypt_binary( (struct mach_header *)arch_page, base );
if ( (ret != EX_OK) && (ret != EX_TEXTTOOSMALL) )
{
ret = subret;
break;
}
ret = MIN(ret, subret);
}
}
}
else if ( magic == MH_MAGIC )
{
memcpy( arch_page, header_page, PAGE_SIZE );
mh = (struct mach_header *) arch_page;
if ( (mh->cputype & ~CPU_ARCH_MASK) != CPU_TYPE_X86 )
{
fprintf( stderr, "this program only supports x86 or x86-64 architectures.\n" );
ret = EX_USAGE;
goto out;
}
ret = crypt_binary( mh, 0 );
}
else
{
fprintf( stderr, "not an appropriate Mach-O file.\n" );
ret = EX_USAGE;
goto out;
}
out:
if ( fd_in >= 0 )
close( fd_in );
if ( fd_out >= 0 )
{
close( fd_out );
if ( ret != EX_OK )
unlink( argv[2] );
}
return ( ret );
}