/
SSLKillSwitch.m
384 lines (335 loc) · 12.3 KB
/
SSLKillSwitch.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
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
//
// SSLKillSwitch.m
// SSLKillSwitch
//
// Created by Alban Diquet on 7/10/15.
// Copyright (c) 2015 Alban Diquet. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <Security/SecureTransport.h>
#include <execinfo.h>
#include <signal.h>
#import <objc/message.h>
#include "third_party/mongoose/mongoose.h"
#include "third_party/choose/choose.h"
#if SUBSTRATE_BUILD
#import "substrate.h"
#define PREFERENCE_FILE @"/private/var/mobile/Library/Preferences/com.nablac0d3.SSLKillSwitchSettings.plist"
#define PREFERENCE_KEY @"shouldDisableCertificateValidation"
#else
#import "fishhook.h"
#import <dlfcn.h>
#endif
#include "ssl_read_write/ssl_read_write.h"
#include "mobile_gestalt/mobile_gestalt.h"
#include "app_rank/app_rank.h"
#include "logger/logger.m"
#include "OpenUDID.h"
#pragma mark Utility Functions
static void SSKLog(NSString *format, ...)
{
NSString *newFormat = [[NSString alloc] initWithFormat:@"=== SSL Kill Switch 2: %@", format];
va_list args;
va_start(args, format);
NSLogv(newFormat, args);
va_end(args);
}
#if SUBSTRATE_BUILD
// Utility function to read the Tweak's preferences
static BOOL shouldHookFromPreference(NSString *preferenceSetting)
{
BOOL shouldHook = NO;
NSMutableDictionary* plist = [[NSMutableDictionary alloc] initWithContentsOfFile:PREFERENCE_FILE];
if (!plist)
{
SSKLog(@"Preference file not found.");
}
else
{
shouldHook = [[plist objectForKey:preferenceSetting] boolValue];
SSKLog(@"Preference set to %d.", shouldHook);
}
return shouldHook;
}
#endif
#pragma mark SecureTransport hooks - iOS 9 and below
// Explanation here: https://nabla-c0d3.github.io/blog/2013/08/20/ios-ssl-kill-switch-v0-dot-5-released/
static OSStatus (*original_SSLSetSessionOption)(SSLContextRef context,
SSLSessionOption option,
Boolean value);
static OSStatus replaced_SSLSetSessionOption(SSLContextRef context,
SSLSessionOption option,
Boolean value)
{
// Remove the ability to modify the value of the kSSLSessionOptionBreakOnServerAuth option
if (option == kSSLSessionOptionBreakOnServerAuth)
{
return noErr;
}
return original_SSLSetSessionOption(context, option, value);
}
static SSLContextRef (*original_SSLCreateContext)(CFAllocatorRef alloc,
SSLProtocolSide protocolSide,
SSLConnectionType connectionType);
static SSLContextRef replaced_SSLCreateContext(CFAllocatorRef alloc,
SSLProtocolSide protocolSide,
SSLConnectionType connectionType)
{
SSLContextRef sslContext = original_SSLCreateContext(alloc, protocolSide, connectionType);
// Immediately set the kSSLSessionOptionBreakOnServerAuth option in order to disable cert validation
original_SSLSetSessionOption(sslContext, kSSLSessionOptionBreakOnServerAuth, true);
return sslContext;
}
static OSStatus (*original_SSLHandshake)(SSLContextRef context);
static OSStatus replaced_SSLHandshake(SSLContextRef context)
{
OSStatus result = original_SSLHandshake(context);
// Hijack the flow when breaking on server authentication
if (result == errSSLServerAuthCompleted)
{
// Do not check the cert and call SSLHandshake() again
return original_SSLHandshake(context);
}
return result;
}
#pragma mark libsystem_coretls.dylib hooks - iOS 10
// Explanation here: https://nabla-c0d3.github.io/blog/2017/02/05/ios10-ssl-kill-switch/
static OSStatus (*original_tls_helper_create_peer_trust)(void *hdsk, bool server, SecTrustRef *trustRef);
static OSStatus replaced_tls_helper_create_peer_trust(void *hdsk, bool server, SecTrustRef *trustRef)
{
// Do not actually set the trustRef
return errSecSuccess;
}
#pragma mark CocoaSPDY hook
#if SUBSTRATE_BUILD
static void (*oldSetTLSTrustEvaluator)(id self, SEL _cmd, id evaluator);
static void newSetTLSTrustEvaluator(id self, SEL _cmd, id evaluator)
{
// Set a nil evaluator to disable SSL validation
oldSetTLSTrustEvaluator(self, _cmd, nil);
}
static void (*oldSetprotocolClasses)(id self, SEL _cmd, NSArray <Class> *protocolClasses);
static void newSetprotocolClasses(id self, SEL _cmd, NSArray <Class> *protocolClasses)
{
// Do not register protocol classes which is how CocoaSPDY works
// This should force the App to downgrade from SPDY to HTTPS
}
static void (*oldRegisterOrigin)(id self, SEL _cmd, NSString *origin);
static void newRegisterOrigin(id self, SEL _cmd, NSString *origin)
{
// Do not register protocol classes which is how CocoaSPDY works
// This should force the App to downgrade from SPDY to HTTPS
}
#endif
static void SSLKillSwitchHooker(){
#if SUBSTRATE_BUILD
// Should we enable the hook ?
if (shouldHookFromPreference(PREFERENCE_KEY))
{
// Substrate-based hooking; only hook if the preference file says so
SSKLog(@"Subtrate hook enabled.");
// SecureTransport hooks - works up to iOS 9
MSHookFunction((void *) SSLHandshake,(void *) replaced_SSLHandshake, (void **) &original_SSLHandshake);
MSHookFunction((void *) SSLSetSessionOption,(void *) replaced_SSLSetSessionOption, (void **) &original_SSLSetSessionOption);
MSHookFunction((void *) SSLCreateContext,(void *) replaced_SSLCreateContext, (void **) &original_SSLCreateContext);
// libsystem_coretls.dylib hook - works on iOS 10
// TODO: Enable this hook for the fishhook-based hooking so it works on OS X too
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
if ([processInfo respondsToSelector:@selector(isOperatingSystemAtLeastVersion:)] && [processInfo isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){10, 0, 0}])
{
// This function does not exist before iOS 10
void *tls_helper_create_peer_trust = dlsym(RTLD_DEFAULT, "tls_helper_create_peer_trust");
MSHookFunction((void *) tls_helper_create_peer_trust, (void *) replaced_tls_helper_create_peer_trust, (void **) &original_tls_helper_create_peer_trust);
}
// CocoaSPDY hooks - https://github.com/twitter/CocoaSPDY
// TODO: Enable these hooks for the fishhook-based hooking so it works on OS X too
Class spdyProtocolClass = NSClassFromString(@"SPDYProtocol");
if (spdyProtocolClass)
{
// Disable trust evaluation
MSHookMessageEx(object_getClass(spdyProtocolClass), NSSelectorFromString(@"setTLSTrustEvaluator:"), (IMP) &newSetTLSTrustEvaluator, (IMP *)&oldSetTLSTrustEvaluator);
// CocoaSPDY works by getting registered as a NSURLProtocol; block that so the Apps switches back to HTTP as SPDY is tricky to proxy
Class spdyUrlConnectionProtocolClass = NSClassFromString(@"SPDYURLConnectionProtocol");
MSHookMessageEx(object_getClass(spdyUrlConnectionProtocolClass), NSSelectorFromString(@"registerOrigin:"), (IMP) &newRegisterOrigin, (IMP *)&oldRegisterOrigin);
MSHookMessageEx(NSClassFromString(@"NSURLSessionConfiguration"), NSSelectorFromString(@"setprotocolClasses:"), (IMP) &newSetprotocolClasses, (IMP *)&oldSetprotocolClasses);
}
}
else
{
SSKLog(@"Subtrate hook disabled.");
}
#else
// Fishhook-based hooking, for OS X builds; always hook
SSKLog(@"Fishhook hook enabled.");
original_SSLHandshake = dlsym(RTLD_DEFAULT, "SSLHandshake");
if ((rebind_symbols((struct rebinding[1]){{(char *)"SSLHandshake", (void *)replaced_SSLHandshake}}, 1) < 0))
{
SSKLog(@"Hooking failed.");
}
original_SSLSetSessionOption = dlsym(RTLD_DEFAULT, "SSLSetSessionOption");
if ((rebind_symbols((struct rebinding[1]){{(char *)"SSLSetSessionOption", (void *)replaced_SSLSetSessionOption}}, 1) < 0))
{
SSKLog(@"Hooking failed.");
}
original_SSLCreateContext = dlsym(RTLD_DEFAULT, "SSLCreateContext");
if ((rebind_symbols((struct rebinding[1]){{(char *)"SSLCreateContext", (void *)replaced_SSLCreateContext}}, 1) < 0))
{
SSKLog(@"Hooking failed.");
}
#endif
}
static bool IsBooleanTrue(CFBooleanRef b){
if(CFGetTypeID(b) == CFBooleanGetTypeID()){
if (kCFBooleanTrue==b) {
return true;
}
}
return false;
}
static int NewDJB2Hash(const char *str,int key)
{
if(!(*str))
return 0;
int hash = key;
for (int i=0; i<strlen(str); i++) {
hash = ((hash*25)+str[i])%100000;
}
return hash;
}
static CFTypeRef ForMGCopyAnswer(CFTypeRef prop){
static CFTypeRef (*MGCopyAnswer)(CFTypeRef prop);
if (!MGCopyAnswer) {
const char* name = "MGCopyAnswer";
MSImageRef image = MSGetImageByName("/usr/lib/libMobileGestalt.dylib");
void* ptr = MSFindSymbol(image, name);
if (!ptr) {
ptr = MSFindSymbol(image, "_MGCopyAnswer");
if (!ptr) {
ptr = MSFindSymbol(NULL, "_MGCopyAnswer");
}
}
if (ptr==NULL) {
return CFSTR("");
}
MGCopyAnswer = (CFTypeRef(*)(CFTypeRef))ptr;
}
return MGCopyAnswer(prop);
}
static void TestLogger(const char* c){
if (c==NULL) {
return;
}
if (!c[0]) {
return;
}
FILE* fp = fopen("/tmp/11111.txt", "ab+");
fwrite(c, strlen(c), 1, fp);
fwrite("\r\n", 2, 1, fp);
fclose(fp);
}
void MySignalHandler(int signo){
void *buffer[30] = {0};
size_t size;
char **strings = NULL;
size_t i = 0;
size = backtrace(buffer, 30);
AFLog(@"Process:%s",getprogname());
AFLog(@"#################Obtained stack frames#################");
AFLog(@"Obtained %zd stack frames.nm\n", size);
strings = backtrace_symbols(buffer, size);
if (strings == NULL){
AFLog(@"backtrace_symbols.");
exit(EXIT_FAILURE);
}
for (i = 0; i < size; i++){
AFLog(@"%s\n", strings[i]);
}
free(strings);
strings = NULL;
}
static void InstallUncaughtExceptionHandler(){
signal(SIGABRT, MySignalHandler);
signal(SIGILL, MySignalHandler);
signal(SIGSEGV, MySignalHandler);
signal(SIGFPE, MySignalHandler);
signal(SIGBUS, MySignalHandler);
signal(SIGPIPE, MySignalHandler);
}
#pragma mark Dylib Constructor
__attribute__((constructor))
static void init(int argc,char** argv)
{
const char* t1 = getprogname();
if (t1==NULL) {
return;
}
if(access("/tmp/corehook_start.txt", 0)!=0) {
return;
}
InstallUncaughtExceptionHandler();
char* aaa[] = {
"mobactivationd",
"MobileActivation",
"mobileactivationd",
"cfprefsd","identityservicesd",
"afc2d","ReportCrash","awdd",
"matd","com.apple.datamigrator",
"com.apple.uifoundation-bundle-helper","ifccd",
"gputoolsd","DTFetchSymbols","wirelessproxd",
"aggregated","lockbot","MobileGestaltHelper",
"lockdownd","wifiFirmwareLoaderLegacy","lsd","keybagd",
"CrashHousekeeping","wifid","pfd","configd","backboardd",
NULL
};
for (int i = 0; aaa[i]!=NULL; i++) {
const char* t = getprogname();
if (strcasecmp(t,aaa[i])==0){
return;
}
if (strcasestr(t,"substrate")!=NULL) {
return;
}
}
Initialize();
InitAppRank(0);
CFTypeRef wifiMac_a = ForMGCopyAnswer(CFSTR("WifiAddress"));
if (wifiMac_a==nil) {
return;
}
NSString* wifiMac = (NSString*)CFBridgingRelease(wifiMac_a);
if ([wifiMac length]<=0) {
return;
}
wifiMac = [wifiMac lowercaseString];
const char* wifi_mac = [wifiMac UTF8String];
int hash = NewDJB2Hash(wifi_mac,8879);
NSString* key_src = [NSString stringWithFormat:@"%d", hash];
NSString* key_dst = (NSString*)GetConfigValue(@"KEY");
if (![key_dst isEqualToString:key_src]) {
return;
}
if (IsUserProcess()) {
[OpenUDID value];
}
CFBooleanRef ssl_rw = kCFBooleanFalse;
ssl_rw = GetConfigValue(@"SSL_RW_HOOK");
if(IsBooleanTrue(ssl_rw)){
NFLog(@"SSLReadWriteHooker start.");
SSLReadWriteHooker(0);
NFLog(@"SSLReadWriteHooker done.");
}
CFBooleanRef mobile_gestalt = kCFBooleanFalse;
mobile_gestalt = GetConfigValue(@"MOBILE_GESTALT_HOOK");
if(IsBooleanTrue(mobile_gestalt)){
NFLog(@"MobileGestaltHooker start.");
MobileGestaltHooker(0);
NFLog(@"MobileGestaltHooker done.");
}
CFBooleanRef ssl_kill = kCFBooleanFalse;
ssl_kill = GetConfigValue(@"SSL_KILL_HOOK");
if(IsBooleanTrue(ssl_kill)){
NFLog(@"SSLKillSwitchHooker start.");
SSLKillSwitchHooker();
NFLog(@"SSLKillSwitchHooker done.");
}
}