[紹棠] iOS 判斷手機是否越獄

來源:互聯網
上載者:User
那麼,如何判斷手機是否越獄呢。

需添加以下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種方法均可以判斷設定是否越獄,但為了保證判斷的準確性,通常會多種方法聯合使用。


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.