How do I detect that an iOS app is running on a jailbroken phone?

后端 未结 17 1347
情书的邮戳
情书的邮戳 2020-11-22 10:48

If I want my app to behave differently on a jailbroken iPhone, how would I go about determining this?

相关标签:
17条回答
  • 2020-11-22 11:35
    BOOL isJailbroken()
    {
    #if TARGET_IPHONE_SIMULATOR
        return NO;
    #else
        FILE *f = fopen("/bin/bash", "r");
    
        if (errno == ENOENT)
        {
            // device is NOT jailbroken
            fclose(f);
            return NO;
        }
        else {
            // device IS jailbroken
            fclose(f);
            return YES;
        }
    #endif
    }
    
    0 讨论(0)
  • 2020-11-22 11:38

    It depends what you mean by jailbreak. In the simple case, you should be able to see if Cydia is installed and go by that - something like

    NSString *filePath = @"/Applications/Cydia.app";
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
       // do something useful
    }
    

    For hacked kernels, it's a little (lot) more involved.

    0 讨论(0)
  • 2020-11-22 11:41

    Please use following code for Swift 4 and above: Add the following code in the appdelegate:

    private func getJailbrokenStatus() -> Bool {
        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
        }
    }
    

    func applicationDidBecomeActive (_ application: UIApplication) {

        if getJailbrokenStatus() {
            let alert = UIAlertController(title: LocalizedKeys.Errors.jailbreakError, message: LocalizedKeys.Errors.jailbreakErrorMessage, preferredStyle: UIAlertController.Style.alert)
            let jailBrokenView = UIViewController()
    
            jailBrokenView.view.frame = UIScreen.main.bounds
            jailBrokenView.view.backgroundColor = .white
            self.window?.rootViewController = jailBrokenView
            jailBrokenView.present(alert, animated: true, completion: nil)
        }
    
        if #available(iOS 11.0, *) {
            if !UIScreen.main.isCaptured {
                DispatchQueue.main.async {
                    self.blockImageView.removeFromSuperview()
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 11:42

    I'd suggest looking for files that aren't present on a "vanilla" iPhone. All jailbreak kits I've seen install ssh. That might be a good indicator of a jailbroken phone.

    0 讨论(0)
  • 2020-11-22 11:43

    Try to find a file which cydia or jailbroken device create. Or try to write in a file outside the app's blackbox. If you succeed to do that, the device is compromised/jailbroken :)

    - (BOOL)jailbroken
    {
        NSFileManager * fileManager = [NSFileManager defaultManager];
        return [fileManager fileExistsAtPath:@"/private/var/lib/apt/"];
    }
    
    0 讨论(0)
提交回复
热议问题