|
| 1 | +/* |
| 2 | + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * This code is free software; you can redistribute it and/or modify it |
| 5 | + * under the terms of the GNU General Public License version 2 only, as |
| 6 | + * published by the Free Software Foundation. Codename One designates this |
| 7 | + * particular file as subject to the "Classpath" exception as provided |
| 8 | + * by Oracle in the LICENSE file that accompanied this code. |
| 9 | + * |
| 10 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | + * accompanied this code). |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License version |
| 17 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + * |
| 20 | + * Please contact Codename One through http://www.codenameone.com/ if you |
| 21 | + * need additional information or have any questions. |
| 22 | + */ |
| 23 | + |
| 24 | +#import "CN1JailbreakDetector.h" |
| 25 | +#ifdef CN1_DETECT_JAILBREAK |
| 26 | +#import <UIKit/UIKit.h> |
| 27 | +#import <dlfcn.h> |
| 28 | +#import <sys/sysctl.h> |
| 29 | +#import <mach-o/dyld.h> |
| 30 | + |
| 31 | +void cn1DetectJailbreakBypassesAndExit() { |
| 32 | +#if (TARGET_IPHONE_SIMULATOR) |
| 33 | + return; |
| 34 | +#endif |
| 35 | + // List of known libraries used by bypass tools like Liberty Lite and Substrate |
| 36 | + NSArray *bypassLibraries = @[ |
| 37 | + @"LibertyLite.dylib", |
| 38 | + @"Substrate.dylib", |
| 39 | + @"MobileSubstrate.dylib", |
| 40 | + @"SubstrateInserter.dylib", |
| 41 | + @"tsProtector.dylib", |
| 42 | + @"FridaGadget" |
| 43 | + ]; |
| 44 | + |
| 45 | + // Check all loaded dynamic libraries |
| 46 | + for (int i = 0; i < _dyld_image_count(); i++) { |
| 47 | + const char *imageName = _dyld_get_image_name(i); |
| 48 | + NSString *libraryName = [NSString stringWithUTF8String:imageName]; |
| 49 | + |
| 50 | + // Check if the library name matches any known bypass tool libraries |
| 51 | + for (NSString *bypassLibrary in bypassLibraries) { |
| 52 | + if ([libraryName containsString:bypassLibrary]) { |
| 53 | + // Jailbreak bypass detected, exit the app |
| 54 | + NSLog(@"Bypass library detected: %@", bypassLibrary); |
| 55 | + exit(0); // Exit the app if a bypass tool is detected |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Additional check for file access to system areas (indicates potential bypass) |
| 61 | + NSArray *restrictedPaths = @[ |
| 62 | + @"/Applications/Cydia.app", |
| 63 | + @"/usr/sbin/sshd", |
| 64 | + @"/bin/bash", |
| 65 | + @"/etc/apt", |
| 66 | + @"/Library/MobileSubstrate/MobileSubstrate.dylib" |
| 67 | + ]; |
| 68 | + |
| 69 | + NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 70 | + for (NSString *path in restrictedPaths) { |
| 71 | + if ([fileManager fileExistsAtPath:path]) { |
| 72 | + // Jailbreak files detected, exit the app |
| 73 | + NSLog(@"Jailbreak-related file detected: %@", path); |
| 74 | + exit(0); // Exit the app if a jailbreak-related file is found |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Check if we can write to a restricted area (bypasses may allow this) |
| 79 | + NSString *testPath = @"/private/jailbreakTest.txt"; |
| 80 | + NSError *error; |
| 81 | + [@"Test" writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:&error]; |
| 82 | + if (!error) { |
| 83 | + // Able to write to restricted area, exit the app |
| 84 | + NSLog(@"Write access to restricted area detected."); |
| 85 | + exit(0); // Exit the app if write access to restricted areas is detected |
| 86 | + } |
| 87 | + |
| 88 | + // Check for abnormal system behavior like successful fork() |
| 89 | + if (fork() == 0) { |
| 90 | + // fork() should not succeed on non-jailbroken devices, exit if it does |
| 91 | + NSLog(@"Fork succeeded, indicating jailbreak bypass."); |
| 92 | + exit(0); // Exit the app if fork() succeeds |
| 93 | + } |
| 94 | + |
| 95 | + // Check for process tracing (which could indicate Liberty Lite tampering) |
| 96 | + struct kinfo_proc info; |
| 97 | + size_t size = sizeof(info); |
| 98 | + int name[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()}; |
| 99 | + if (sysctl(name, 4, &info, &size, NULL, 0) == 0 && (info.kp_proc.p_flag & P_TRACED) != 0) { |
| 100 | + // Process is being traced, likely due to a jailbreak bypass |
| 101 | + NSLog(@"Process tracing detected, indicating jailbreak bypass."); |
| 102 | + exit(0); // Exit the app if process tracing is detected |
| 103 | + } |
| 104 | + |
| 105 | + // If no jailbreak bypass was detected, the app continues as normal |
| 106 | + NSLog(@"No jailbreak bypass detected."); |
| 107 | +} |
| 108 | +#endif |
0 commit comments