那麼,如何判斷手機是否越獄呢。
需添加以下C語言庫
#import <sys/stat.h>
#import <dlfcn.h>
#import <mach-o/dyld.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#import <ifaddrs.h>
#import <arpa/inet.h>
#import <net/if.h>
1.通過越獄後增加的越獄檔案判斷
一般來說,手機越獄後會增加以下檔案/Applications/Cydia.app/Library/MobileSubstrate/MobileSubstrate.dylib/bin/bash/usr/sbin/sshd/etc/apt
判斷這些檔案是否存在,只要有存在的,就可以認為手機已經越獄了。
NSArray *jailbreak_tool_paths = @[ @"/Applications/Cydia.app", @"/Library/MobileSubstrate/MobileSubstrate.dylib", @"/bin/bash", @"/usr/sbin/sshd", @"/etc/apt"];- (BOOL)isJailBreak { for (int i=0; i<jailbreak_tool_paths.count; i++) { if ([[NSFileManager defaultManager] fileExistsAtPath:jailbreak_tool_paths[i]]) { NSLog(@"The device is jail broken!"); return YES; } } NSLog(@"The device is NOT jail broken!"); return NO;}
2.根據是否能開啟cydia判斷
- (BOOL)isJailBreak { if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"cydia://"]]) { NSLog(@"The device is jail broken!"); return YES; } NSLog(@"The device is NOT jail broken!"); return NO;}
3.根據是否能擷取所有應用的名稱判斷
沒有越獄的裝置是沒有讀取所有應用程式名稱的許可權的。
- (BOOL)isJailBreak { if ([[NSFileManager defaultManager] fileExistsAtPath:@"User/Applications/"]) { NSLog(@"The device is jail broken!"); NSArray *appList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"User/Applications/" error:nil]; NSLog(@"appList = %@", appList); return YES; } NSLog(@"The device is NOT jail broken!"); return NO;}
4.根據使用stat方法來判斷cydia是否存在來判斷
這個方法的思路還是通過判定cydia應用,但方法是使用stat函數,同時會判斷是否有注入動態庫。
int checkInject() { int ret; Dl_info dylib_info; int (*func_stat)(const char*, struct stat*) = stat; char *dylib_name = "/usr/lib/system/libsystem_kernel.dylib"; if ((ret = dladdr(func_stat, &dylib_info)) && strncmp(dylib_info.dli_fname, dylib_name, strlen(dylib_name))) { return 0; } return 1;}int checkCydia() { struct stat stat_info; if (!checkInject()) { if (0 == stat("/Applications/Cydia.app", &stat_info)) { return 1; } } else { return 1; } return 0;}
5.根據讀取的環境變數是否有值判斷
DYLD_INSERT_LIBRARIES環境變數在非越獄的裝置上應該是空的,而越獄的裝置基本上都會有Library/MobileSubstrate/MobileSubstrate.dylib
char* printEnv(void) { charchar *env = getenv("DYLD_INSERT_LIBRARIES"); NSLog(@"%s", env); return env;}- (BOOL)isJailBreak { if (printEnv()) { NSLog(@"The device is jail broken!"); return YES; } NSLog(@"The device is NOT jail broken!"); return NO;}
以上5種方法均可以判斷設定是否越獄,但為了保證判斷的準確性,通常會多種方法聯合使用。