26
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SwiftでiOS脱獄チェック

Last updated at Posted at 2017-07-26

はじめに

チェック項目は以下記事を参考にしました。
iOS脱獄チェックメモ

自分も確認した感じこれだけ確認しておけば十分対処できるのではないかと思ったのでそのままSwiftに移植しました。

ソースコード

JailbreakCheck.swift
import Darwin.C

class JailbreakCheck {

    static var isJailbreak: Bool {
        #if !(TARGET_IPHONE_SIMULATOR)
            if FileManager.default.fileExists(atPath: "/Application/Cydia.app") {
                return true
            } else if FileManager.default.fileExists(atPath: "/Library/MobileSubstrate/MobileSubstrate.dylib") {
                return true
            } else if FileManager.default.fileExists(atPath: "/bin/bash") {
                return true
            } else if FileManager.default.fileExists(atPath: "/usr/sbin/sshd") {
                return true
            } else if FileManager.default.fileExists(atPath: "/etc/apt") {
                return true
            } else if FileManager.default.fileExists(atPath: "/private/var/lib/apt") {
                return true
            }

            let stringToBeWritten = "This is jailbreak test."
            var isWrote = false

            do {
                try stringToBeWritten.write(toFile: "/private/jailbreak.txt", atomically: true, encoding: String.Encoding.utf8)

                defer {
                    try! FileManager.default.removeItem(atPath: "/private/jailbreak.txt")
                    isWrote = true
                }
            } catch {}

            if isWrote {
                return true
            }

            if UIApplication.shared.canOpenURL(URL(string: "cydia://package/com.example.package")!) {
                return true
            }

            var isJailbreak = false
            var f: UnsafeMutablePointer<FILE>?

            let checkUrls: [String] = [
                "/bin/bash",
                "/bin/ssh",
                "/Application/Cydia.app",
                "/Library/MobileSubstrate/MobileSubstrate.dylib",
                "/usr/sbin/sshd",
                "/etc/apt"
            ]

            for url in checkUrls {
                do {
                    defer {
                        fclose(f)
                    }

                    f = try JailbreakCheck.fopen(path: url)

                    defer {
                        if f != nil {
                            isJailbreak = true
                        }
                    }
                } catch {}
            }

            return isJailbreak
        #else
            return false
        #endif
    }

    fileprivate class func fopen(path: String) throws -> UnsafeMutablePointer<FILE>? {
        let f = Darwin.fopen(path, "r")
        return f
    }
}

使い方

if JailbreakCheck.isJailbreak {
    something()
}

最後に

/private/jailbreak.txt を削除するところの処理がちょっと不安ですが、まぁいいことにしておきましょうw
Cの fopen を使ったところこれ間違えてるゾイ!っていうところあれば編集リクやコメントでご指摘いただければ幸いです。

26
30
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?