forked from Muirey03/Cr4shed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharedutils.mm
226 lines (207 loc) · 6.62 KB
/
sharedutils.mm
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
@import Foundation;
#import <CoreGraphics/CoreGraphics.h>
#import <objc/runtime.h>
#include <stdlib.h>
#include <string.h>
#include <mach-o/dyld_images.h>
#import "MobileGestalt/MobileGestalt.h"
#import "sharedutils.h"
#import "Cephei/HBPreferences.h"
extern "C" {
NSString* getImage(NSString* symbol)
{
int startingI = -1;
int endingI = -1;
for (int i = 0; i < symbol.length - 1; i++)
{
char c = [symbol characterAtIndex:i];
char nextC = [symbol characterAtIndex:i+1];
if (startingI == -1)
{
if (c == ' ' && nextC != ' ')
{
startingI = i+1;
}
}
else
{
if (nextC == ' ')
{
endingI = i+1;
break;
}
}
}
return [symbol substringWithRange:NSMakeRange(startingI, endingI-startingI)];
}
NSString* determineCulprit(NSArray* symbols)
{
for (int i = 0; i < symbols.count; i++)
{
NSString* symbol = symbols[i];
NSString* image = getImage(symbol);
if (![image isEqualToString:@"Cr4shed.dylib"])
{
if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/Library/MobileSubstrate/DynamicLibraries/%@", image]])
return image;
}
}
return @"Unknown";
}
NSString* stringFromDate(NSDate* date, CR4DateFormat type)
{
BOOL needsLowercase = NO;
NSDateFormatter* formatter = [NSDateFormatter new];
switch (type)
{
case CR4DateFormatPretty:
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
break;
case CR4DateFormatTimeOnly:
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
break;
default:
case CR4DateFormatFilename:
needsLowercase = YES;
[formatter setDateFormat:@"yyyy-MM-dd_h:mm_a"];
break;
}
NSString* ret = [formatter stringFromDate:date];
return needsLowercase ? [ret lowercaseString] : ret;
}
NSString* deviceVersion()
{
return (__bridge NSString*)MGCopyAnswer(CFSTR("ProductVersion")) ?: @"Unknown";
}
NSString* deviceName()
{
return (__bridge NSString*)MGCopyAnswer(CFSTR("marketing-name")) ?: @"Unknown";
}
#define MAX_CHUNK_SIZE 0xFFF
#ifdef __cplusplus
extern "C" {
#endif
kern_return_t mach_vm_read_overwrite(vm_map_t target_task, mach_vm_address_t address, mach_vm_size_t size, mach_vm_address_t data, mach_vm_size_t *outsize);
kern_return_t mach_vm_write(vm_map_t target_task, mach_vm_address_t address, vm_offset_t data, mach_msg_type_number_t dataCnt);
#ifdef __cplusplus
}
#endif
size_t rread(mach_port_t task, mach_vm_address_t where, void* p, size_t size)
{
kern_return_t rv;
size_t offset = 0;
while (offset < size)
{
mach_vm_size_t sz, chunk = MAX_CHUNK_SIZE;
if (chunk > size - offset)
chunk = size - offset;
rv = mach_vm_read_overwrite(task, where + offset, chunk, (mach_vm_address_t)p + offset, &sz);
if (rv || sz == 0)
break;
offset += sz;
}
return offset;
}
size_t rwrite(mach_port_t task, mach_vm_address_t where, const void* p, size_t size)
{
kern_return_t rv;
size_t offset = 0;
while (offset < size)
{
size_t chunk = MAX_CHUNK_SIZE;
if (chunk > size - offset)
chunk = size - offset;
rv = mach_vm_write(task, where + offset, (mach_vm_offset_t)p + offset, chunk);
if (rv)
break;
offset += chunk;
}
return offset;
}
mach_vm_address_t taskGetImageInfos(mach_port_t task)
{
struct task_dyld_info dyld_info = {0};
mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
kern_return_t kr = task_info(task, TASK_DYLD_INFO, (task_info_t)&dyld_info, &count);
if (kr == KERN_SUCCESS && dyld_info.all_image_info_addr && dyld_info.all_image_info_size)
return dyld_info.all_image_info_addr;
return 0;
}
//used to let Cr4shedMach know that this process' crash
//has been dealt with
const char* const flag = "com.muirey03.cr4shed-exceptionHandled";
void markProcessAsHandled()
{
struct dyld_all_image_infos* image_infos = (struct dyld_all_image_infos*)taskGetImageInfos(mach_task_self());
if (image_infos)
image_infos->errorMessage = flag;
}
bool processHasBeenHandled(mach_port_t task)
{
bool handled = false;
mach_vm_address_t image_infos = taskGetImageInfos(task);
if (image_infos)
{
uint64_t errorMsgAddr = 0;
rread(task, image_infos + offsetof(struct dyld_all_image_infos, errorMessage), (void*)&errorMsgAddr, sizeof(uint64_t));
if (errorMsgAddr)
{
void* mem = malloc(strlen(flag) + 1);
if (mem)
{
rread(task, errorMsgAddr, mem, strlen(flag) + 1);
handled = (memcmp(mem, flag, strlen(flag)) == 0);
free(mem);
}
}
}
return handled;
}
HBPreferences* sharedPreferences()
{
static HBPreferences* prefs = nil;
if (!prefs)
{
NSString* const frameworkPath = @"/usr/lib/Cephei.framework";
NSBundle* bundle = [NSBundle bundleWithPath:frameworkPath];
if (!bundle.loaded)
[bundle load];
prefs = [[objc_getClass("HBPreferences") alloc] initWithIdentifier:@"com.muirey03.cr4shedprefs"];
}
return prefs;
}
NSString* addInfoToLog(NSString* logContents, NSDictionary* info)
{
if ([NSJSONSerialization isValidJSONObject:info])
{
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info options:0 error:NULL];
if (jsonData)
{
NSString* infoString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
return [logContents stringByAppendingFormat:@"\n\n%@", infoString];
}
}
return logContents;
}
NSDictionary* getInfoFromLog(NSString* logContents)
{
if (logContents.length)
{
NSRange lastLineRange = [logContents lineRangeForRange:NSMakeRange(logContents.length - 1, 1)];
NSString* jsonString = [logContents substringWithRange:lastLineRange];
NSDictionary* info = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL];
if ([info isKindOfClass:[NSDictionary class]])
return info;
}
return nil;
}
}
bool isBlacklisted(NSString* procName)
{
if (!procName) procName = [NSProcessInfo processInfo].processName;
HBPreferences* prefs = sharedPreferences();
NSArray<NSString*>* blacklist = [prefs objectForKey:kProcessBlacklist];
return (blacklist && [blacklist containsObject:procName]);
}