Root detection means the device contains privileged access such as super user (SU) permissions and rooting apps, which means programming to provide complete control over the phone.
Pros and cons of rooting:
Pros:
- It helps to improve the speed at the time of use.
- Increase battery life.
- Customise a phone structure.
Cons:
- It can void the warranty.
- It disables security features.
- Open to malware attack
Methods to implement root detection:
- SafetyNet attestation
- Jailbreak detection
- Native plugin code
SafetyNet attestation
SafetyNet attestation is a Flutter package to check whether the device contains root access.
Go to the registered project’s Google Cloud Console, enable the SafetyNet API, and get API credentials.
Step 1 :
Include the following lines in the Android manifest file :
<meta-data android:name=”safetynet_api_key”
android:value=”AIzaSyB2__n8RQh3KN0Eu2A5pnWgLtYe05CvJvs”/>
Step 2 :
Import the given below line in app/build gradle file:
implementation ‘com.google.android.play:integrity:1.1.0’
Step 3:
safetynet_attestation: ^0.0.3
Get the package from pub.dev and include the pubspec.yaml file
Step 4:
Go to main.dart file, first thing need to check the SafetyNet service is available for this project
The second thing, request payload from Google SafetyNet API
var noncevalue;
var rng = Random();
nonceValue = List.generate(12, (_) => rng.nextInt(100));
JWSPayloadModel jwsPayloadModel = await SafetynetAttestation().safetyNetAttestationPayload(nonceValue);
if(jwsPayloadModel.basicIntegrity && jwsPayloadModel.ctsProfileMatch){
print(‘The device is safe’,);
}else{
print(‘The device is rooted’);
}
CTS Profile Match:
- It checks the app profile of the device running the app, and that profile passed the Android compatibility test.
- The condition is true when the profiles match the return response as a Google-certified Android device.
Basic Integrity:
- If the condition is true, check whether the device has not been tampered with.
- Run before the application passes CTS Profile match and Basic Integrity if it is true, go to the inside of the application. If it is not true, close the application with a warning message as the device is rooted and you can’t use the application on the device.
Jailbreak detection :
Step 1 :
Import flutter_jailbreak_detection: ^1.10.0
Step2:
Check whether the conditions are satisfied given below code:
bool jailbroken = await FlutterJailbreakDetection.jailbroken;
bool developerMode = await FlutterJailbreakDetection.developerMode;
Step 3 :
Add app/build gradle file in the given below line:
implementation ‘com.scottyab:rootbeer-lib:0.1.0’
Step 4 :
Add the line in the gradle property which means it removes the unwanted code and helps minimize the application size
extra-gen-snapshot-options=–obfuscate
Step 5 :
Create a file as proguard-android.txt and use the lines of code inside the function.
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.** { *; }
-keep class io.flutter.util.** { *; }
-keep class io.flutter.view.** { *; }
-keep class io.flutter.** { *; }
-keep class io.flutter.plugins.** { *; }
-keep class com.google.firebase.** { *; }
-dontwarn io.flutter.embedding.**
Step 6:
Include the following lines of code inside the release build types in gradle file:
shrinkResources true
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
Native code implementation:
Implement native code inside the main activity.java and import using MethodChannel
inside the main activity file declare method channel and invoke the method inside the flutter application.
public boolean isDeviceRooted() {
// Check for presence of Superuser, SuperSU, or Magisk
if (new File(“/system/app/Superuser.apk”).exists() || new File(“/system/app/SuperSU.apk”).exists() || new File(“/system/app/MagiskManager.apk”).exists()) {
return true;
}
// Check for su binary
try {
Process process = Runtime.getRuntime().exec(new String[]{“su”, “-c”, “echo test”});
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
if (in.readLine() != null) {
return true;
}
process.destroy();
} catch (Exception e) {
// do nothing
}
// Check for modified system partition
if (new File(“/system/bin/.ext/.su”).exists() || new File(“/system/xbin/.ext/.su”).exists()) {
return true;
}
// Check for busybox
if (new File(“/system/xbin/busybox”).exists()) {
return true;
}
return false;
}
Summary:
Implement the application using these methods to prevent the mobile application from being compromised, and the customer can use the app without facing any security issues.
No Comment! Be the first one.