How to check your app is installed on a jailbroken device

iOS jailbreaking is the process of by passing the security restrictions by Apple. It permits root access to iOS, allowing the downloading and installation of additional applications, extensions, and themes that are unavailable through the official Apple app store.

If you are working on an app, which could not compromise on security, then it becomes necessary to restrict your app to run only on non-jailbroken devices.

It’s not possible to detect jailbroken device in all cases, because every implemented jailbreak check can be fooled. However, making hacking of the application more difficult by introducing additional security checks is always welcome.

Here’re a couple of ways to check if application is currently running in jailbroken environment:

1. Existence of files that are common for jailbroken devices :

One of the most common and easiest way to detect a jailbroken device is to check for files that are common for jailbroken devices, like Cydia.app or MobileSubstrate.dylib.

2. Reading and writing in system directories (sandbox violation) :

In a non-jailbroken environment applications can only read and write inside the application sandbox. If the application is able to access files outside of its sandbox, it’s probably running on the jailbroken device.

Here is the method to check if the device is jailbroken :

ObjectiveC:

#if !(TARGET_IPHONE_SIMULATOR)
// Check 1 : existence of files that are common for jailbroken devices
if ([[NSFileManager defaultManager] fileExistsAtPath:@”/Applications/Cydia.app”] ||
[[NSFileManager defaultManager] fileExistsAtPath:@”/Library/MobileSubstrate/MobileSubstrate.dylib”] ||
[[NSFileManager defaultManager] fileExistsAtPath:@”/bin/bash”] ||
[[NSFileManager defaultManager] fileExistsAtPath:@”/usr/sbin/sshd”] ||
[[NSFileManager defaultManager] fileExistsAtPath:@”/etc/apt”] ||
[[NSFileManager defaultManager] fileExistsAtPath:@”/private/var/lib/apt/”] ||
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@”cydia://package/com.example.package”]]) {
return YES;
}
FILE *f = NULL ;
if ((f = fopen(“/bin/bash”, “r”)) ||
(f = fopen(“/Applications/Cydia.app”, “r”)) ||
(f = fopen(“/Library/MobileSubstrate/MobileSubstrate.dylib”, “r”)) ||
(f = fopen(“/usr/sbin/sshd”, “r”)) ||
(f = fopen(“/etc/apt”, “r”))) {
fclose(f);
return YES;
}
fclose(f);
// Check 2 : Reading and writing in system directories (sandbox violation)
NSError *error;
NSString *stringToBeWritten = @”Jailbreak Test.”;
[stringToBeWritten writeToFile:@”/private/jailbreak.txt” atomically:YES
encoding:NSUTF8StringEncoding error:&error];
if(error==nil){
//Device is jailbroken
return YES;
} else {
[[NSFileManager defaultManager] removeItemAtPath:@”/private/jailbreak.txt” error:nil];
}
#endif
return NO;

Swift:

if TARGET_IPHONE_SIMULATOR != 1
{
// Check 1 : existence of files that are common for jailbroken devices
if FileManager.default.fileExists(atPath: “/Applications/Cydia.app”)
|| FileManager.default.fileExists(atPath: “/Library/MobileSubstrate/MobileSubstrate.dylib”)
|| FileManager.default.fileExists(atPath: “/bin/bash”)
|| FileManager.default.fileExists(atPath: “/usr/sbin/sshd”)
|| FileManager.default.fileExists(atPath: “/etc/apt”)
|| FileManager.default.fileExists(atPath: “/private/var/lib/apt/”)
|| UIApplication.shared.canOpenURL(URL(string:”cydia://package/com.example.package”)!)
{
return true
}
// Check 2 : Reading and writing in system directories (sandbox violation)
let stringToWrite = “Jailbreak Test”
do
{
try stringToWrite.write(toFile:”/private/JailbreakTest.txt”, atomically:true, encoding:String.Encoding.utf8)
//Device is jailbroken
return true
}catch
{
return false
}
}else
{
return false
}

if it return yes, that means the device is jailbroken.

This is how you can detect if your application is being installed on an jailbroken iOS device programmatically.