Skip to content

Commit ea6553d

Browse files
authored
Ios jailbreak bybass detect (#3838)
* feat: add ios jailbreak detection via build hint new build hint ios.detectJailbreak=true|false * fixup * fix compile errors
1 parent 97f9b18 commit ea6553d

File tree

4 files changed

+146
-3
lines changed

4 files changed

+146
-3
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
//#define CN1_DETECT_JAILBREAK 1
24+
#ifdef CN1_DETECT_JAILBREAK
25+
void cn1DetectJailbreakBypassesAndExit();
26+
#endif
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* need additional information or have any questions.
2222
*/
2323
#import "CodenameOne_GLAppDelegate.h"
24+
#import "CN1JailbreakDetector.h"
2425
#include "xmlvm.h"
2526
#import "EAGLView.h"
2627
#import "CodenameOne_GLViewController.h"
@@ -115,6 +116,9 @@ @implementation CodenameOne_GLAppDelegate
115116

116117
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
117118
{
119+
#ifdef CN1_DETECT_JAILBREAK
120+
cn1DetectJailbreakBypassesAndExit();
121+
#endif
118122
//beforeDidFinishLaunchingWithOptionsMarkerEntry
119123

120124
// Override point for customization after application launch.

maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/IPhoneBuilder.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public class IPhoneBuilder extends Executor {
6666
// which adds localized strings files to the project.
6767
private StringBuilder installLocalizedStringsScript = new StringBuilder();
6868

69+
private boolean detectJailbreak;
70+
6971
private boolean runPods=false;
7072
private boolean photoLibraryUsage;
7173
private String buildVersion;
@@ -212,6 +214,7 @@ private int getDeploymentTargetInt(BuildRequest request) {
212214
@Override
213215
public boolean build(File sourceZip, BuildRequest request) throws BuildException {
214216
addMinDeploymentTarget(DEFAULT_MIN_DEPLOYMENT_VERSION);
217+
detectJailbreak = request.getArg("ios.detectJailbreak", "false").equals("true");
215218
defaultEnvironment.put("LANG", "en_US.UTF-8");
216219
tmpFile = tmpDir = getBuildDirectory();
217220
useMetal = "true".equals(request.getArg("ios.metal", "false"));
@@ -939,9 +942,6 @@ public void usesClassMethod(String cls, String method) {
939942
+ " }\n"
940943
+ " });\n";
941944

942-
943-
944-
945945
try (OutputStream stubSourceStream = new FileOutputStream(new File(stubSource, request.getMainClass() + "Stub.java"))) {
946946
String stubSourceCode = "package " + request.getPackageName() + ";\n\n"
947947
+ "import com.codename1.ui.*;\n"
@@ -1283,6 +1283,11 @@ public void usesClassMethod(String cls, String method) {
12831283
replaceInFile(glAppDelegate, "//GL_APP_DELEGATE_INCLUDE", glAppDelegeateHeader);
12841284
}
12851285

1286+
File jailbreakH = new File(buildinRes, "CN1JailbreakDetector.h");
1287+
if (jailbreakH.exists() && detectJailbreak) {
1288+
replaceInFile(jailbreakH, "//#define CN1_DETECT_JAILBREAK", "#define CN1_DETECT_JAILBREAK");
1289+
}
1290+
12861291
String glAppDelegeateBody = request.getArg("ios.glAppDelegateBody", null);
12871292
if (glAppDelegeateBody != null && glAppDelegeateBody.length() > 0) {
12881293
replaceInFile(glAppDelegate, "//GL_APP_DELEGATE_BODY", glAppDelegeateBody);

0 commit comments

Comments
 (0)