-
Notifications
You must be signed in to change notification settings - Fork 7
/
Tweak.xm
189 lines (129 loc) · 5.54 KB
/
Tweak.xm
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
#include <string.h>
#include <mach/mach.h>
#include <mach-o/loader.h>
#include <mach-o/dyld.h>
#include <pthread.h>
#define BUF_SIZE 2048
#define DLog(x, ...) NSLog(@"*****"x, __VA_ARGS__)
void showAlert(NSString *title, NSString *message){
UIAlertController *a = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"Okay & Exit" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
exit(0);
}];
[a addAction:ok];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:a animated:false completion:nil];
}
void copyFile(const char *from, const char *to){
FILE *src = fopen(from, "rb");
if(!src){
showAlert(@"Error", @"copyFile: couldn't open src");
return;
}
// find size of this file
fseek(src, 0, SEEK_END);
int fileSize = ftell(src);
rewind(src);
DLog("fileSize %d", fileSize);
char *srcBuffer = (char *)malloc(fileSize);
size_t read = fread(srcBuffer, sizeof(char), fileSize, src);
DLog("fread read %d bytes", read);
fclose(src);
FILE *dst = fopen(to, "wb");
if(!dst){
showAlert(@"Error", @"copyFile: couldn't open dst");
return;
}
size_t wrote = fwrite(srcBuffer, sizeof(char), fileSize, dst);
DLog("fwrite wrote %d bytes", wrote);
fclose(dst);
free(srcBuffer);
NSLog(@"*****copyFile: done!");
}
void *doDecrypt(void *arg){
uint64_t aslrSlide = _dyld_get_image_vmaddr_slide(0);
// get the target's mach_header
struct mach_header_64 *mach_header = (struct mach_header_64 *)malloc(sizeof(struct mach_header_64));
vm_size_t size = sizeof(struct mach_header_64);
kern_return_t err = vm_read_overwrite(mach_task_self(), (vm_address_t)(aslrSlide + 0x100000000), size, (pointer_t)mach_header, &size);
if(err){
showAlert(@"Error", @"Couldn't read mach_header");
pthread_exit(NULL);
}
DLog("ASLR: %llx", aslrSlide);
DLog("mach_header->magic %x, mach_header->sizeofcmds %x", mach_header->magic, mach_header->sizeofcmds);
if(mach_header->magic != MH_MAGIC_64){
showAlert(@"Error", @"mach_header->magic != feedfacf");
pthread_exit(NULL);
}
free(mach_header);
// go through the load commands to find the __TEXT segment command
struct segment_command_64 *__TEXT = NULL;
struct load_command *command = (struct load_command *)malloc(sizeof(struct load_command));
vm_address_t currentAddress = aslrSlide + 0x100000000 + sizeof(struct mach_header_64);
vm_size_t segmentSize = sizeof(struct load_command);
vm_read_overwrite(mach_task_self(), currentAddress, segmentSize, (pointer_t)command, &segmentSize);
// TODO: would this ever be an infinite loop? we already checked for feedfacf
while(strcmp(((struct segment_command_64 *)command)->segname, "__TEXT") != 0){
command = (struct load_command *)((uint8_t *)currentAddress + command->cmdsize);
}
__TEXT = (struct segment_command_64 *)malloc(sizeof(struct segment_command_64));
memcpy(__TEXT, (const void *)command, sizeof(struct segment_command_64));
DLog("found text! start: %llx, end: %llx", __TEXT->vmaddr, __TEXT->vmaddr + __TEXT->vmsize);
// read __TEXT segment
vm_size_t bytesLeftToRead = __TEXT->vmsize;
vm_size_t bytesRead = 0;
vm_address_t current = __TEXT->vmaddr + aslrSlide;
vm_address_t end = __TEXT->vmaddr + __TEXT->vmsize + aslrSlide;
unsigned char *textBuffer = (unsigned char *)malloc(bytesLeftToRead);
while(current < end){
vm_size_t chunk = 0x100000;
if(chunk > bytesLeftToRead)
chunk = bytesLeftToRead;
vm_read_overwrite(mach_task_self(), current, chunk, (vm_address_t)&textBuffer[bytesRead], &chunk);
bytesRead += chunk;
current += chunk;
bytesLeftToRead -= chunk;
DLog("%llx / %llx: reading __TEXT... %.2f%%", current-aslrSlide, __TEXT->vmaddr + __TEXT->vmsize, ((float)bytesRead/__TEXT->vmsize)*100);
}
const char *executablePath = [[[NSBundle mainBundle] bundlePath] UTF8String];
const char *executableName = getprogname();
char *pathToTarget = (char *)malloc(BUF_SIZE);
strcpy(pathToTarget, executablePath);
strcat(pathToTarget, "/");
strcat(pathToTarget, executableName);
DLog("pathToTarget %s", pathToTarget);
char *pathToDecryptedTarget = (char *)malloc(BUF_SIZE);
strcpy(pathToDecryptedTarget, [NSHomeDirectory() UTF8String]);
strcat(pathToDecryptedTarget, "/Documents/");
strcat(pathToDecryptedTarget, executableName);
strcat(pathToDecryptedTarget, " decrypted");
NSLog(@"*****copying file...");
copyFile(pathToTarget, pathToDecryptedTarget);
NSLog(@"*****done!");
FILE *decryptedTargetPtr = fopen(pathToDecryptedTarget, "r+b");
if(!decryptedTargetPtr){
showAlert(@"Error", @"Couldn't open decrypted target for writing");
pthread_exit(NULL);
}
uint64_t curFileOffset = 0x0;
long int amountToWrite = 0x100000;
uint64_t textEnd = __TEXT->vmsize;
while(amountToWrite != 0 && curFileOffset < textEnd){
if(textEnd - curFileOffset < amountToWrite)
amountToWrite = textEnd - curFileOffset;
fwrite(&textBuffer[curFileOffset], amountToWrite, sizeof(char), decryptedTargetPtr);
curFileOffset += amountToWrite;
DLog("%llx / %llx: writing to __TEXT... %.2f%%", curFileOffset, textEnd, ((float)curFileOffset/textEnd)*100);
}
NSLog(@"*****we're done with this part");
free(__TEXT);
free(textBuffer);
free(pathToTarget);
showAlert(@"Done!", [NSString stringWithFormat:@"Find your executable at %@", [NSString stringWithUTF8String:pathToDecryptedTarget]]);
free(pathToDecryptedTarget);
pthread_exit(NULL);
}
__attribute__ ((constructor)) static void decrypt() {
pthread_t decryptionThread;
pthread_create(&decryptionThread, NULL, doDecrypt, NULL);
}