Flutter如何监测root或越狱状态
在Flutter应用中,如何检测设备是否已root或越狱?目前项目需要增加安全性检测,防止用户使用修改过的设备进行操作。请问有哪些可靠的插件或方法可以实现这个功能?最好能兼容Android和iOS平台,并给出具体的实现示例。谢谢!
2 回复
Flutter可通过flutter_jailbreak_detection插件检测设备是否root或越狱。该插件支持Android和iOS,提供jailbroken和developerMode等属性判断设备安全状态。
更多关于Flutter如何监测root或越狱状态的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中监测root(Android)或越狱(iOS)状态,可以通过结合平台通道(Platform Channel)调用原生代码实现。以下是具体方法:
Android监测Root状态
原生代码(Kotlin):
class RootChecker {
companion object {
fun isDeviceRooted(): Boolean {
return checkRootMethod1() || checkRootMethod2() || checkRootMethod3()
}
private fun checkRootMethod1(): Boolean {
val buildTags = android.os.Build.TAGS
return buildTags != null && buildTags.contains("test-keys")
}
private fun checkRootMethod2(): Boolean {
val paths = arrayOf(
"/system/app/Superuser.apk",
"/sbin/su",
"/system/bin/su",
"/system/xbin/su",
"/data/local/xbin/su",
"/data/local/bin/su",
"/system/sd/xbin/su",
"/system/bin/failsafe/su",
"/data/local/su"
)
return paths.any { File(it).exists() }
}
private fun checkRootMethod3(): Boolean {
var process: Process? = null
return try {
process = Runtime.getRuntime().exec(arrayOf("/system/xbin/which", "su"))
process.inputStream.bufferedReader().readLine() != null
} catch (t: Throwable) {
false
} finally {
process?.destroy()
}
}
}
}
iOS监测越狱状态
原生代码(Swift):
class JailbreakChecker {
static func isJailbroken() -> Bool {
if TARGET_OS_SIMULATOR != 0 { return false }
let paths = [
"/Applications/Cydia.app",
"/Library/MobileSubstrate/MobileSubstrate.dylib",
"/bin/bash",
"/usr/sbin/sshd",
"/etc/apt"
]
if paths.contains(where { FileManager.default.fileExists(atPath: $0) }) {
return true
}
if canOpen("cydia://") { return true }
let path = "/private/" + NSUUID().uuidString
do {
try "test".write(toFile: path, atomically: true, encoding: .utf8)
try FileManager.default.removeItem(atPath: path)
return true
} catch {
return false
}
}
static func canOpen(_ scheme: String) -> Bool {
if let url = URL(string: scheme) {
return UIApplication.shared.canOpenURL(url)
}
return false
}
}
Flutter平台通道集成
- 创建MethodChannel:
class SecurityCheck {
static const platform = MethodChannel('security_check');
static Future<bool> get isRooted async {
try {
return await platform.invokeMethod('isRooted');
} catch (e) {
return false;
}
}
static Future<bool> get isJailbroken async {
try {
return await platform.invokeMethod('isJailbroken');
} catch (e) {
return false;
}
}
}
- 注册通道处理程序:
- Android(MainActivity.kt):
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "security_check").setMethodCallHandler {
call, result ->
when (call.method) {
"isRooted" -> result.success(RootChecker.isDeviceRooted())
else -> result.notImplemented()
}
}
}
- iOS(AppDelegate.swift):
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let controller = window?.rootViewController as! FlutterViewController
let channel = FlutterMethodChannel(name: "security_check", binaryMessenger: controller.binaryMessenger)
channel.setMethodCallHandler { call, result in
switch call.method {
case "isJailbroken":
result(JailbreakChecker.isJailbroken())
default:
result(FlutterMethodNotImplemented)
}
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
使用方式
bool isRooted = await SecurityCheck.isRooted;
bool isJailbroken = await SecurityCheck.isJailbroken;
注意:这些检测方法并非100%可靠,有些隐藏root/越狱的工具可能绕过检测。建议在敏感操作前结合其他安全措施。

