-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmain.m
More file actions
455 lines (382 loc) · 19 KB
/
Copy pathmain.m
File metadata and controls
455 lines (382 loc) · 19 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
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
//
// main.m
// dylibify
//
// Created by Jake James on 7/13/18.
// Copyright © 2018 Jake James. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <mach-o/loader.h>
#import <mach-o/swap.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define SWAP32(p) __builtin_bswap32(p)
static void *load_bytes(FILE *obj_file, off_t offset, size_t size) {
void *buf = calloc(1, size);
fseek(obj_file, offset, SEEK_SET);
fread(buf, size, 1, obj_file);
return buf;
}
void write_bytes(FILE *obj_file, off_t offset, size_t size, void *bytes) {
fseek(obj_file, offset, SEEK_SET);
fwrite(bytes, size, 1, obj_file);
}
void patch_mach_header(FILE *obj_file, off_t offset, void *mh, BOOL is64bit) {
if (is64bit) {
printf("[*] Patching filetype & flags\n");
printf("[-] FILETYPE was 0x%x\n", ((struct mach_header_64 *)mh)->filetype);
printf("[-] FLAGS were: 0x%x\n", ((struct mach_header_64 *)mh)->flags);
//----Change MH_EXECUTE to MH_DYLIB----//
((struct mach_header_64 *)mh)->filetype = MH_DYLIB;
((struct mach_header_64 *)mh)->flags |= MH_NO_REEXPORTED_DYLIBS;
printf("[+] FILETYPE is 0x%x\n", ((struct mach_header_64 *)mh)->filetype);
printf("[+] FLAGS are: 0x%x\n", ((struct mach_header_64 *)mh)->flags);
write_bytes(obj_file, offset, sizeof(struct mach_header_64), mh);
}
else {
printf("[*] Patching filetype & flags\n");
printf("[-] FILETYPE was 0x%x\n", ((struct mach_header *)mh)->filetype);
printf("[-] FLAGS were: 0x%x\n", ((struct mach_header *)mh)->flags);
((struct mach_header *)mh)->filetype = MH_DYLIB;
((struct mach_header *)mh)->flags |= MH_NO_REEXPORTED_DYLIBS;
printf("[+] FILETYPE is 0x%x\n", ((struct mach_header *)mh)->filetype);
printf("[+] FLAGS are: 0x%x\n", ((struct mach_header *)mh)->flags);
write_bytes(obj_file, offset, sizeof(struct mach_header), mh);
}
}
void patch_pagezero(FILE *obj_file, off_t offset, struct load_command *cmd, BOOL copied, void *seg, size_t sizeofseg, const char *target) {
uint32_t size = cmd->cmdsize;
printf("\t\t[*] Patching __PAGEZERO\n");
printf("\t\t[*] Nullifying\n");
//----Nullify it----//
memset(seg, 0, sizeofseg);
//----Allocate data for our new command + @executable_path/NAME_OF_TARGET.dylib----//
//----So, if you plan to link with it, don't rename the file and put it on same location as binary----//
//----Obviously, you can easily patch that yourself, if for some reason you want to----//
struct dylib_command *dylib_cmd = (struct dylib_command*)malloc(sizeof(struct dylib_command) + [@(target) lastPathComponent].length + 18);
dylib_cmd->cmd = LC_ID_DYLIB;
dylib_cmd->cmdsize = size;
//----The string will be located where our dylib command ends----//
dylib_cmd->dylib.name.offset = sizeof(struct dylib_command);
dylib_cmd->dylib.timestamp = 1;
dylib_cmd->dylib.current_version = 0;
dylib_cmd->dylib.compatibility_version = 0;
//----If it's a FAT binary do not copy it twice----//
if (!copied) {
strcpy((char *)dylib_cmd + sizeof(struct dylib_command), ([[NSString stringWithFormat:@"@executable_path/%@", [@(target) lastPathComponent]] UTF8String]));
}
printf("\t\t[*] Doing the magic\n");
write_bytes(obj_file, offset, sizeofseg, dylib_cmd);
free(dylib_cmd);
}
void patch_dyldinfo(FILE *file, off_t offset, struct dyld_info_command *dyldinfo) {
if (dyldinfo->rebase_off != 0) {
//----Some maths takes place in here, we need to iterate over the opcodes----//
//----Some of them are just 1 byte, some are 2 bytes, some are whole strings----//
//----We only need the ones referencing to segments, which are 1 byte----//
for (int i = 0; i < dyldinfo->rebase_size; i++) {
uint8_t *bytes = load_bytes(file, offset + dyldinfo->rebase_off + i, sizeof(uint8_t));
if ((*bytes & REBASE_OPCODE_MASK) == REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB) {
printf("\t\t[-] REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB before = 0x%x\n", *bytes);
*bytes -= 1; // "-1" -> one less segment = previous segment
write_bytes(file, offset + dyldinfo->rebase_off + i, sizeof(uint8_t), bytes);
bytes = load_bytes(file, offset + dyldinfo->rebase_off + i, sizeof(uint8_t));
printf("\t\t[+] REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB now = 0x%x\n", *bytes);
break;
}
free(bytes);
}
}
if(dyldinfo->bind_off != 0) {
for (int i = 0; i < dyldinfo->bind_size; i++) {
uint8_t *bytes = load_bytes(file, offset + dyldinfo->bind_off + i, sizeof(uint8_t));
switch (*bytes & BIND_OPCODE_MASK) {
case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
printf("\t\t[-] BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB before = 0x%x\n", *bytes);
if ((*bytes & 0xF) == 2) *bytes -= 1;
write_bytes(file, offset + dyldinfo->bind_off + i, sizeof(uint8_t), bytes);
bytes = load_bytes(file, offset + dyldinfo->bind_off + i, sizeof(uint8_t));
printf("\t\t[+] BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB now = 0x%x\n", *bytes);
while (*bytes != BIND_OPCODE_DO_BIND) {
i += 1;
bytes = load_bytes(file, offset + dyldinfo->bind_off + i, sizeof(uint8_t));
}
break;
case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: // this means a string is coming next
while (*bytes != 0) { //all strings end with 0 (null byte) and don't have 0 in their body
i += 1;
bytes = load_bytes(file, offset + dyldinfo->bind_off + i, sizeof(uint8_t));
}
break;
case BIND_OPCODE_ADD_ADDR_ULEB:
while (*bytes != BIND_OPCODE_DO_BIND) {
i += 1;
bytes = load_bytes(file, offset + dyldinfo->bind_off + i, sizeof(uint8_t));
}
break;
default:
break;
}
free(bytes);
}
}
if(dyldinfo->lazy_bind_off != 0) {
for (int i = 0; i < dyldinfo->lazy_bind_size; i++) {
uint8_t *bytes = load_bytes(file, offset + dyldinfo->lazy_bind_off + i, sizeof(uint8_t));
switch (*bytes & BIND_OPCODE_MASK) {
case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
printf("\t\t[-] BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB before = 0x%x\n", *bytes);
if ((*bytes & 0xF) == 2) *bytes -= 1;
write_bytes(file, offset + dyldinfo->lazy_bind_off + i, sizeof(uint8_t), bytes);
bytes = load_bytes(file, offset + dyldinfo->lazy_bind_off + i, sizeof(uint8_t));
printf("\t\t[+] BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB now = 0x%x\n", *bytes);
while (*bytes != BIND_OPCODE_DO_BIND) {
i += 1;
bytes = load_bytes(file, offset + dyldinfo->lazy_bind_off + i, sizeof(uint8_t));
}
break;
case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
while (*bytes != 0) {
i += 1;
bytes = load_bytes(file, offset + dyldinfo->lazy_bind_off + i, sizeof(uint8_t));
}
break;
case BIND_OPCODE_ADD_ADDR_ULEB:
while (*bytes != BIND_OPCODE_DO_BIND) {
i += 1;
bytes = load_bytes(file, offset + dyldinfo->lazy_bind_off + i, sizeof(uint8_t));
}
break;
default:
break;
}
free(bytes);
}
}
if(dyldinfo->weak_bind_off != 0) {
for (int i = 0; i < dyldinfo->weak_bind_size; i++) {
uint8_t *bytes = load_bytes(file, offset + dyldinfo->weak_bind_off + i, sizeof(uint8_t));
switch (*bytes & BIND_OPCODE_MASK) {
case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
printf("\t\t[-] BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB before = 0x%x\n", *bytes);
if ((*bytes & 0xF) == 2) *bytes -= 1;
write_bytes(file, offset + dyldinfo->weak_bind_off + i, sizeof(uint8_t), bytes);
bytes = load_bytes(file, offset + dyldinfo->weak_bind_off + i, sizeof(uint8_t));
printf("\t\t[+] BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB now = 0x%x\n", *bytes);
while (*bytes != BIND_OPCODE_DO_BIND) {
i += 1;
bytes = load_bytes(file, offset + dyldinfo->weak_bind_off + i, sizeof(uint8_t));
}
break;
case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
while (*bytes != 0) {
i += 1;
bytes = load_bytes(file, offset + dyldinfo->weak_bind_off + i, sizeof(uint8_t));
}
break;
case BIND_OPCODE_ADD_ADDR_ULEB:
while (*bytes != BIND_OPCODE_DO_BIND) {
i += 1;
bytes = load_bytes(file, offset + dyldinfo->weak_bind_off + i, sizeof(uint8_t));
}
break;
default:
break;
}
free(bytes);
}
}
}
int dylibify(const char *macho, const char *saveto) {
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
//----Make sure we don't overwrite any file----//
if ([fileManager fileExistsAtPath:@(saveto)]) {
printf("[!] %s file exists!\n", saveto);
return -1;
}
//----Create a copy of the file on the target destination----//
[fileManager copyItemAtPath:@(macho) toPath:@(saveto) error:&error];
//----Handle errors----//
if (error) {
printf("[!] %s\n", [[error localizedDescription] UTF8String]);
return -1;
}
//----Open the copied file for updating, in binary mode----//
FILE *file = fopen(saveto, "r+b");
//----This variable will hold the binary location as we move on through reading it----//
size_t offset = 0;
BOOL copied = false;
int ncmds = 0;
struct load_command *cmd = NULL;
uint32_t *magic = load_bytes(file, offset, sizeof(uint32_t)); //at offset 0 we have the magic number
printf("[i] MAGIC = 0x%x\n", *magic);
//----64bit magic number----//
if (*magic == 0xFEEDFACF) {
printf("[i] 64bit binary\n");
struct mach_header_64 *mh64 = load_bytes(file, offset, sizeof(struct mach_header_64));
//----Patch filetype and add MH_NO_REEXPORTED_DYLIB flag (required for linking with it)----//
patch_mach_header(file, offset, mh64, true); //patch
offset += sizeof(struct mach_header_64);
ncmds = mh64->ncmds;
free(mh64);
printf("[i] %d LOAD COMMANDS\n", ncmds);
for (int i = 0; i < ncmds; i++) {
cmd = load_bytes(file, offset, sizeof(struct load_command));
if (cmd->cmd == LC_SEGMENT_64) {
struct segment_command_64 *seg64 = load_bytes(file, offset, sizeof(struct segment_command_64));
printf("\t[i] LC_SEGMENT_64 (%s)\n", seg64->segname);
//----Dylibs don't have the PAGEZERO segment, replace it with a LC_ID_DYLIB command----//
if (!strcmp(seg64->segname, "__PAGEZERO")) {
patch_pagezero(file, offset, cmd, copied, seg64, sizeof(struct segment_command_64), saveto);
}
free(seg64);
}
else if (cmd->cmd == LC_DYLD_INFO_ONLY) {
printf("[*] Found DYLD_INFO_ONLY!\n");
struct dyld_info_command *dyldinfo = load_bytes(file, offset, sizeof(struct dyld_info_command));
//----Since we removed one segment we have to to rework opcodes so DATA is not confused with LINKEDIT----//
patch_dyldinfo(file, 0, dyldinfo);
free(dyldinfo);
}
else {
printf("[i] LOAD COMMAND %d = 0x%x\n", i, cmd->cmd);
}
offset += cmd->cmdsize;
free(cmd);
}
}
//----32bit magic number----//
else if (*magic == 0xFEEDFACE) {
printf("[i] 32bit binary\n");
struct mach_header *mh = load_bytes(file, offset, sizeof(struct mach_header));
patch_mach_header(file, offset, mh, false);
offset += sizeof(struct mach_header);
ncmds = mh->ncmds;
free(mh);
printf("[i] %d LOAD COMMANDS\n", ncmds);
for (int i = 0; i < ncmds; i++) {
cmd = load_bytes(file, offset, sizeof(struct load_command));
if (cmd->cmd == LC_SEGMENT) {
struct segment_command *seg = load_bytes(file, offset, sizeof(struct segment_command));
printf("\t[i] LC_SEGMENT (%s)\n", seg->segname);
if (!strcmp(seg->segname, "__PAGEZERO")) {
patch_pagezero(file, offset, cmd, copied, seg, sizeof(struct segment_command), saveto);
}
free(seg);
}
else if (cmd->cmd == LC_DYLD_INFO_ONLY) {
printf("[*] Found DYLD_INFO_ONLY!\n");
struct dyld_info_command *dyldinfo = load_bytes(file, offset, sizeof(struct dyld_info_command));
patch_dyldinfo(file, 0, dyldinfo);
free(dyldinfo);
}
else {
printf("[i] LOAD COMMAND %d = 0x%x\n", i, cmd->cmd);
}
offset += cmd->cmdsize;
free(cmd);
}
}
//----More than one architecture----//
else if (*magic == 0xBEBAFECA) {
printf("[i] FAT binary\n");
size_t arch_offset = sizeof(struct fat_header);
struct fat_header *fat = load_bytes(file, offset, sizeof(struct fat_header));
struct fat_arch *arch = load_bytes(file, arch_offset, sizeof(struct fat_arch));
int n = SWAP32(fat->nfat_arch);
printf("[i] %d ARCHS\n", n);
while (n-- > 0) {
offset = SWAP32(arch->offset);
magic = load_bytes(file, offset, sizeof(uint32_t));
if (*magic == 0xFEEDFACF) {
printf("[i] Found 64bit architecture\n");
struct mach_header_64 *mh64 = load_bytes(file, offset, sizeof(struct mach_header_64));
patch_mach_header(file, offset, mh64, true);
offset += sizeof(struct mach_header_64);
ncmds = mh64->ncmds;
free(mh64);
printf("[i] %d LOAD COMMANDS\n", ncmds);
for (int i = 0; i < ncmds; i++) {
cmd = load_bytes(file, offset, sizeof(struct load_command));
if (cmd->cmd == LC_SEGMENT_64) {
struct segment_command_64 *seg64 = load_bytes(file, offset, sizeof(struct segment_command_64));
printf("\t[i] LC_SEGMENT_64 (%s)\n", seg64->segname);
if (!strcmp(seg64->segname, "__PAGEZERO")) {
patch_pagezero(file, offset, cmd, copied, seg64, sizeof(struct segment_command_64), saveto);
copied = true;
}
free(seg64);
}
else if (cmd->cmd == LC_DYLD_INFO_ONLY) {
printf("[*] Found DYLD_INFO_ONLY!\n");
struct dyld_info_command *dyldinfo = load_bytes(file, offset, sizeof(struct dyld_info_command));
patch_dyldinfo(file, SWAP32(arch->offset), dyldinfo);
free(dyldinfo);
}
else {
printf("[i] LOAD COMMAND %d = 0x%x\n", i, cmd->cmd);
}
offset += cmd->cmdsize;
free(cmd);
}
}
else if (*magic == 0xFEEDFACE) {
printf("[i] Found 32bit architecture\n");
struct mach_header *mh = load_bytes(file, offset, sizeof(struct mach_header));
patch_mach_header(file, offset, mh, false);
offset += sizeof(struct mach_header);
ncmds = mh->ncmds;
free(mh);
printf("[i] %d LOAD COMMANDS\n", ncmds);
for (int i = 0; i < ncmds; i++) {
cmd = load_bytes(file, offset, sizeof(struct load_command));
if (cmd->cmd == LC_SEGMENT) {
struct segment_command *seg = load_bytes(file, offset, sizeof(struct segment_command));
printf("\t[i] LC_SEGMENT (%s)\n", seg->segname);
if (!strcmp(seg->segname, "__PAGEZERO")) {
patch_pagezero(file, offset, cmd, copied, seg, sizeof(struct segment_command), saveto);
copied = true;
}
free(seg);
}
else if (cmd->cmd == LC_DYLD_INFO_ONLY) {
printf("[*] Found DYLD_INFO_ONLY!\n");
struct dyld_info_command *dyldinfo = load_bytes(file, offset, sizeof(struct dyld_info_command));
patch_dyldinfo(file, SWAP32(arch->offset), dyldinfo);
free(dyldinfo);
}
else {
printf("[i] LOAD COMMAND %d = 0x%x\n", i, cmd->cmd);
}
offset += cmd->cmdsize;
free(cmd);
}
}
else {
printf("[!] Unrecognized architecture with MAGIC = 0x%x\n", *magic);
continue;
}
arch_offset += sizeof(struct fat_arch);
arch = load_bytes(file, arch_offset, sizeof(struct fat_arch));
}
free(fat);
free(arch);
}
else {
printf("[!] Unrecognized file\n");
goto err;
}
err:
fclose(file);
return -1;
}
int main(int argc, const char * argv[]) {
if (argc != 3) {
printf("Usage:\n\t%s <in> <out>\nExample:\n\t%s /usr/bin/executable /usr/lib/dylibified.dylib\n", argv[0], argv[0]);
return -1;
}
dylibify(argv[1], argv[2]);
return 0;
}