From 223c1b5e996f019dbe0d74c48fb9bc4001b9f857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Vieira?= Date: Fri, 2 Feb 2018 09:57:24 +0000 Subject: [PATCH] Reimplemented jailbrake detection based on https://github.com/masbog/isJB --- package.json | 27 ++++ plugin.xml | 4 +- src/ios/UIDevice+JBDetect.h | 17 +++ src/ios/UIDevice+JBDetect.m | 225 ++++++++++++++++++++++++++++++++++ src/ios/UIDevice+JailBroken.m | 4 + src/ios/secureDevice.m | 4 +- 6 files changed, 277 insertions(+), 4 deletions(-) create mode 100644 package.json create mode 100644 src/ios/UIDevice+JBDetect.h create mode 100644 src/ios/UIDevice+JBDetect.m diff --git a/package.json b/package.json new file mode 100644 index 0000000..e8af46f --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "cordova-secure-device", + "version": "1.0.3", + "description": "Stops app from running if device is not secure, i.e. it is jailbroken, rooted, or doesn't have a passcode set.", + "cordova": { + "id": "cordova-secure-device", + "platforms": [ + "android", + "ios" + ] + }, + "repository": { + "type": "git", + "url": "git+https://github.com/agmv/cordova-secure-device.git" + }, + "keywords": [ + "ecosystem:cordova", + "cordova-android", + "cordova-ios" + ], + "author": "Andre Vieira", + "license": "Apache 2.0 License", + "bugs": { + "url": "https://github.com/agmv/cordova-secure-device/issues" + }, + "homepage": "https://github.com/agmv/cordova-secure-device#readme" +} diff --git a/plugin.xml b/plugin.xml index 1f17ff6..59d05b0 100644 --- a/plugin.xml +++ b/plugin.xml @@ -34,8 +34,8 @@ - - + + diff --git a/src/ios/UIDevice+JBDetect.h b/src/ios/UIDevice+JBDetect.h new file mode 100644 index 0000000..3f39b00 --- /dev/null +++ b/src/ios/UIDevice+JBDetect.h @@ -0,0 +1,17 @@ +// UIDevice+JBDetect.h +// Copyright (c) 2018 André Vieira +// Adapted from + +// +// JBDetect.h +// JailbreakDetection +// +// Created by Augusta Bogie on 2/13/16. +// Copyright © 2016 Augusta Bogie. All rights reserved. +// + +#import + +@interface UIDevice (JBDetect) + @property (readonly) BOOL isJB; +@end diff --git a/src/ios/UIDevice+JBDetect.m b/src/ios/UIDevice+JBDetect.m new file mode 100644 index 0000000..44a42be --- /dev/null +++ b/src/ios/UIDevice+JBDetect.m @@ -0,0 +1,225 @@ +// UIDevice+JBDetect.m +// Copyright (c) 2018 André Vieira +// Adapted from + +// +// JBDetect.m +// JailbreakDetection +// +// Created by Augusta Bogie on 2/13/16. +// Copyright © 2016 Augusta Bogie. All rights reserved. +// + +#import "UIDevice+JBDetect.h" +#include +#import + +#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame) +#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending) +#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) +#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) +#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending) + +@implementation UIDevice (JBDetect) + +- (BOOL)isJB +{ + +#if !TARGET_IPHONE_SIMULATOR + + //Apps and System check list + NSString *isJB=@"76f6243716d4029726022224a43796220237960256d616e40247365746f627050237968645";NSMutableString *a=[NSMutableString new];while([isJB length]!=[a length]){NSRange range=NSMakeRange([isJB length]-[a length]-1,1);[a appendString:[isJB substringWithRange:range]];}NSMutableString *b=[[NSMutableString alloc]init];int c=0;while(c<[a length]){ NSString *d = [a substringWithRange:NSMakeRange(c,2)];int e=0;sscanf([d cStringUsingEncoding:NSASCIIStringEncoding],"%x",&e);[b appendFormat:@"%c",(char)e];c+=2;} + + NSLog(@"%@", b); + BOOL isDirectory; + if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@%@", @"App", @"lic",@"ati", @"ons/", @"Cyd", @"ia.a", @"pp"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@%@", @"App", @"lic",@"ati", @"ons/", @"bla", @"ckra1n.a", @"pp"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@%@", @"App", @"lic",@"ati", @"ons/", @"Fake", @"Carrier.a", @"pp"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@%@", @"App", @"lic",@"ati", @"ons/", @"Ic", @"y.a", @"pp"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@%@", @"App", @"lic",@"ati", @"ons/", @"Inte", @"lliScreen.a", @"pp"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@%@", @"App", @"lic",@"ati", @"ons/", @"MxT", @"ube.a", @"pp"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@%@", @"App", @"lic",@"ati", @"ons/", @"Roc", @"kApp.a", @"pp"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@%@", @"App", @"lic",@"ati", @"ons/", @"SBSet", @"ttings.a", @"pp"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@%@", @"App", @"lic",@"ati", @"ons/", @"Wint", @"erBoard.a", @"pp"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@", @"pr", @"iva",@"te/v", @"ar/l", @"ib/a", @"pt/"] isDirectory:&isDirectory] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@", @"pr", @"iva",@"te/v", @"ar/l", @"ib/c", @"ydia/"] isDirectory:&isDirectory] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@", @"pr", @"iva",@"te/v", @"ar/mobile", @"Library/SBSettings", @"Themes/"] isDirectory:&isDirectory] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@", @"pr", @"iva",@"te/v", @"ar/t", @"mp/cyd", @"ia.log"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@", @"pr", @"iva",@"te/v", @"ar/s", @"tash/"] isDirectory:&isDirectory] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@", @"us", @"r/l",@"ibe", @"xe", @"c/cy", @"dia/"] isDirectory:&isDirectory] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@", @"us", @"r/b",@"in", @"s", @"shd"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@", @"us", @"r/sb",@"in", @"s", @"shd"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@", @"us", @"r/l",@"ibe", @"xe", @"c/cy", @"dia/"] isDirectory:&isDirectory] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@", @"us", @"r/l",@"ibe", @"xe", @"c/sftp-", @"server"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@",@"Syste",@"tem/Lib",@"rary/Lau",@"nchDae",@"mons/com.ike",@"y.bbot.plist"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@%@%@",@"Sy",@"stem/Lib",@"rary/Laun",@"chDae",@"mons/com.saur",@"ik.Cy",@"@dia.Star",@"tup.plist"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@", @"Libr",@"ary/Mo",@"bileSubstra",@"te/MobileSubs",@"trate.dylib"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@", @"va",@"r/c",@"ach",@"e/a",@"pt/"] isDirectory:&isDirectory] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@", @"va",@"r/l",@"ib",@"/apt/"] isDirectory:&isDirectory] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@", @"va",@"r/l",@"ib/c",@"ydia/"] isDirectory:&isDirectory] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@", @"va",@"r/l",@"og/s",@"yslog"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@", @"private/va",@"r/c",@"ach",@"e/a",@"pt/"] isDirectory:&isDirectory] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@", @"private/va",@"r/l",@"ib",@"/apt/"] isDirectory:&isDirectory] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@", @"private/va",@"r/l",@"ib/c",@"ydia/"] isDirectory:&isDirectory] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@", @"private/va",@"r/l",@"og/s",@"yslog"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@", @"bi",@"n/b",@"ash"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@", @"b",@"in/",@"sh"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@", @"private/et",@"c/a",@"pt/"]isDirectory:&isDirectory] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@", @"et",@"c/a",@"pt/"]isDirectory:&isDirectory] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@", @"private/etc/s",@"sh/s",@"shd_config"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@", @"etc/s",@"sh/s",@"shd_config"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@", @"us",@"r/li",@"bexe",@"c/ssh-k",@"eysign"]] + || [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"cydia://package/com.masbog.com"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@%@", @"App", @"lic",@"ati", @"ons/", @"Snoop-it", @" Config.a", @"pp"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@%@%@%@%@", @"Library/MobileS", @"ubstrate/Dy",@"nami", @"cLi", @"braries/", @" xCon.", @"dylib"]] + || [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/%@%@%@", @"priv",@"ate/etc/dpkg/",@"origins/debian"]]) + + { + return YES; + } + + // SandBox Integrity Check + int pid = fork(); + if(!pid){ + exit(0); + } + if(pid>=0) + { + return YES; + } + + //Symbolic link verification + struct stat s; + if(lstat("/Applications", &s) || lstat("/var/stash/Library/Ringtones", &s) || lstat("/var/stash/Library/Wallpaper", &s) + || lstat("/var/stash/usr/include", &s) || lstat("/var/stash/usr/libexec", &s) || lstat("/var/stash/usr/share", &s) || lstat("/var/stash/usr/arm-apple-darwin9", &s)) + { + if(s.st_mode & S_IFLNK){ + return YES; + } + } + + //Try to write file in private + NSError *error; + + FILE *f = NULL ; + if ((f = fopen("/bin/bash", "r")) || + (f = fopen("/bin/sh", "r")) || + (f = fopen("/Applications/Cydia.app", "r")) || + (f = fopen("/Library/MobileSubstrate/MobileSubstrate.dylib", "r")) || + (f = fopen("/usr/sbin/sshd", "r")) || + (f = fopen("/etc/apt", "r"))) { + fclose(f); + return YES; + } + fclose(f); + + NSString *stringToBeWritten = @"Hello, MasBog Here!!!"; + [stringToBeWritten writeToFile:@"/private/masbog.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error]; + [[NSFileManager defaultManager] removeItemAtPath:@"/private/masbog.txt" error:nil]; + if(error == nil) + { + return YES; + } + + NSArray *blah = [NSArray arrayWithObjects:@"f28637164737f2271667f2", @"f28637164737f2271667f256471667962707f2", @"f28637164737f22646f2271667f256471667962707f2", nil]; + NSMutableString *hihi = [NSMutableString new]; + + while ([blah[0] length]!=[hihi length]) { + NSRange range = NSMakeRange([blah[0] length]-[hihi length]-1, 1); + [hihi appendString: [blah[0] substringWithRange:range]]; + } + + NSMutableString *haha = [[NSMutableString alloc] init]; + int i = 0; + while (i < [hihi length]) + { + NSString *hehe = [hihi substringWithRange: NSMakeRange(i, 2)]; + int value = 0; + sscanf([hehe cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value); + [haha appendFormat:@"%c", (char)value]; + i+=2; + } + + NSArray *hahaList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:haha error:nil]; + if (hahaList.count > 0) { + for (NSString *fufufufu in hahaList){ + if (![fufufufu containsString:@"lnk"]) { + NSArray *hahaListSub = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@%@/DynamicLibraries", haha, fufufufu] error:nil]; + for (NSString *wkwkwkwk in hahaListSub){ + if ([wkwkwkwk containsString:@".dylib"] || [wkwkwkwk containsString:@".plist"]) { + return YES; + } + } + } + } + } + + //============== array index 1 ===========// + hihi = [NSMutableString new]; + while ([blah[1] length]!=[hihi length]) { + NSRange range = NSMakeRange([blah[1] length]-[hihi length]-1, 1); + [hihi appendString: [blah[1] substringWithRange:range]]; + } + + haha = [[NSMutableString alloc] init]; + i = 0; + while (i < [hihi length]) + { + NSString *hehe = [hihi substringWithRange: NSMakeRange(i, 2)]; + int value = 0; + sscanf([hehe cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value); + [haha appendFormat:@"%c", (char)value]; + i+=2; + } + + hahaList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:haha error:nil]; + if (hahaList.count > 0) { + for (NSString *fufufufu in hahaList){ + if (![fufufufu containsString:@"lnk"]) { + NSArray *hahaListSub = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@%@/DynamicLibraries", haha, fufufufu] error:nil]; + for (NSString *wkwkwkwk in hahaListSub){ + if ([wkwkwkwk containsString:@".dylib"] || [wkwkwkwk containsString:@".plist"]) { + return YES; + } + } + } + } + } + + + //============== array index 2 ===========// + hihi = [NSMutableString new]; + while ([blah[2] length]!=[hihi length]) { + NSRange range = NSMakeRange([blah[2] length]-[hihi length]-1, 1); + [hihi appendString: [blah[2] substringWithRange:range]]; + } + + haha = [[NSMutableString alloc] init]; + i = 0; + while (i < [hihi length]) + { + NSString *hehe = [hihi substringWithRange: NSMakeRange(i, 2)]; + int value = 0; + sscanf([hehe cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value); + [haha appendFormat:@"%c", (char)value]; + i+=2; + } + + hahaList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:haha error:nil]; + if (hahaList.count > 0) { + for (NSString *fufufufu in hahaList){ + if (![fufufufu containsString:@"lnk"]) { + NSArray *hahaListSub = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@%@/DynamicLibraries", haha, fufufufu] error:nil]; + for (NSString *wkwkwkwk in hahaListSub){ + if ([wkwkwkwk containsString:@".dylib"] || [wkwkwkwk containsString:@".plist"]) { + return YES; + } + } + } + } + } +#endif + return NO; +} + +@end \ No newline at end of file diff --git a/src/ios/UIDevice+JailBroken.m b/src/ios/UIDevice+JailBroken.m index 5da069d..23b66da 100644 --- a/src/ios/UIDevice+JailBroken.m +++ b/src/ios/UIDevice+JailBroken.m @@ -34,6 +34,10 @@ - (BOOL)isJailBroken { { return YES; } + else if ([[NSFileManager defaultManager] fileExistsAtPath:@"/usr/bin/ssh"]) + { + return YES; + } NSError *error; NSString *testWriteText = @"Jailbreak test"; diff --git a/src/ios/secureDevice.m b/src/ios/secureDevice.m index 0529c1a..e2c2f27 100644 --- a/src/ios/secureDevice.m +++ b/src/ios/secureDevice.m @@ -20,7 +20,7 @@ #import #import "secureDevice.h" #import "UIDevice+PasscodeStatus.h" -#import "UIDevice+JailBroken.h" +#import "UIDevice+JBDetect.h" @implementation secureDevice @@ -38,7 +38,7 @@ - (void) onResume:(UIApplication *)application - (void) checkDevice { - BOOL jailbroken = [UIDevice currentDevice].isJailBroken; + BOOL jailbroken = [UIDevice currentDevice].isJB; LNPasscodeStatus status = [UIDevice currentDevice].passcodeStatus; if (jailbroken || status == LNPasscodeStatusDisabled || status == LNPasscodeStatusUnknown) {