forked from nygard/class-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deprotect.m
164 lines (141 loc) · 6.05 KB
/
deprotect.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
// -*- mode: ObjC -*-
// 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.
#include <stdio.h>
#include <libc.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <sysexits.h>
#include <mach-o/arch.h>
#import "CDClassDump.h"
#import "CDMachOFile.h"
#import "CDFatFile.h"
#import "CDLoadCommand.h"
#import "CDLCSegment.h"
void print_usage(void)
{
fprintf(stderr,
"deprotect %s\n"
"Usage: deprotect [options] <input file> <output file>\n"
"\n"
" where options are:\n"
" --arch <arch> choose a specific architecture from a universal binary (ppc, ppc64, i386, x86_64, armv6, armv7, armv7s, arm64)\n"
,
CLASS_DUMP_VERSION
);
}
BOOL saveDeprotectedFileToPath(CDMachOFile *file, NSString *path)
{
BOOL hasProtectedSegments = NO;
NSMutableData *mdata = [[NSMutableData alloc] initWithData:file.data];
for (CDLoadCommand *command in file.loadCommands) {
if ([command isKindOfClass:[CDLCSegment class]]) {
CDLCSegment *segment = (CDLCSegment *)command;
if (segment.isProtected) {
hasProtectedSegments = YES;
NSRange segmentRange = NSMakeRange([segment fileoff], [segment filesize]);
NSUInteger flagOffset;
NSData *decryptedData = [segment decryptedData];
NSCParameterAssert([decryptedData length] == segmentRange.length);
[mdata replaceBytesInRange:segmentRange withBytes:[decryptedData bytes]];
if (segment.machOFile.uses64BitABI) {
flagOffset = [segment commandOffset] + offsetof(struct segment_command_64, flags);
} else {
flagOffset = [segment commandOffset] + offsetof(struct segment_command, flags);
}
CDMachOFileDataCursor *cursor = [[CDMachOFileDataCursor alloc] initWithFile:file offset:flagOffset];
uint32_t flags = [cursor readInt32];
if (flags != [segment flags]) {
fprintf(stderr, "Internal Error: flags (0x%x) does not match segment flags (0x%x).\n", flags, [segment flags]);
exit(EX_SOFTWARE);
}
flags &= ~SG_PROTECTED_VERSION_1;
if (file.byteOrder == CDByteOrder_BigEndian) {
OSWriteBigInt32([mdata mutableBytes], flagOffset, flags);
} else {
OSWriteLittleInt32([mdata mutableBytes], flagOffset, flags);
}
}
}
}
if (hasProtectedSegments) {
[mdata writeToFile:path atomically:NO];
}
return hasProtectedSegments;
}
int main(int argc, char *argv[])
{
@autoreleasepool {
if (argc == 1) {
print_usage();
exit(EX_OK);
}
int ch;
BOOL errorFlag = NO;
CDArch targetArch = {CPU_TYPE_ANY, CPU_TYPE_ANY};
struct option longopts[] = {
{ "arch", required_argument, NULL, 'a' },
{ NULL, 0, NULL, 0 },
};
while ( (ch = getopt_long(argc, argv, "a:", longopts, NULL)) != -1) {
switch (ch) {
case 'a': {
NSString *name = [NSString stringWithUTF8String:optarg];
targetArch = CDArchFromName(name);
if (targetArch.cputype == CPU_TYPE_ANY) {
fprintf(stderr, "Error: Unknown arch %s\n\n", optarg);
errorFlag = YES;
}
break;
}
case '?':
default:
errorFlag = YES;
break;
}
}
argc -= optind;
argv += optind;
if (errorFlag || argc < 2) {
print_usage();
exit(EX_USAGE);
}
{
NSString *inputFile = [NSString stringWithFileSystemRepresentation:argv[0]];
NSString *outputFile = [NSString stringWithFileSystemRepresentation:argv[1]];
CDFile *file = [CDFile fileWithContentsOfFile:inputFile searchPathState:nil];
if (file == nil) {
fprintf(stderr, "Error: input file is neither a Mach-O file nor a fat archive.\n");
exit(EX_DATAERR);
}
CDMachOFile *thinFile = nil;
if ([file isKindOfClass:[CDMachOFile class]]) {
thinFile = (CDMachOFile *)file;
} else if ([file isKindOfClass:[CDFatFile class]]) {
if (targetArch.cputype == CPU_TYPE_ANY) {
if ([file bestMatchForLocalArch:&targetArch] == NO) {
fprintf(stderr, "Internal Error: Couldn't get local architecture.\n");
exit(EX_SOFTWARE);
}
}
thinFile = [(CDFatFile *)file machOFileWithArch:targetArch];
if (!thinFile) {
const NXArchInfo *arhcInfo = NXGetArchInfoFromCpuType(targetArch.cputype, targetArch.cpusubtype);
fprintf(stderr, "Error: input file does not contain the '%s' arch.\n", arhcInfo->name);
exit(EX_DATAERR);
}
} else {
fprintf(stderr, "Internal Error: file is neither a CDFatFile nor a CDMachOFile instance.\n");
exit(EX_SOFTWARE);
}
BOOL hasProtectedSegments = saveDeprotectedFileToPath(thinFile, outputFile);
if (!hasProtectedSegments) {
const NXArchInfo *arhcInfo = NXGetArchInfoFromCpuType(targetArch.cputype, targetArch.cpusubtype);
fprintf(stderr, "Error: input file (%s arch) is not protected.\n", arhcInfo->name);
exit(EX_DATAERR);
}
}
}
return 0;
}