62
USERS’ DATA SECURITY IN IOS APPLICATIONS Odessa Innovation Week, WebCamp 2015

Users' Data Security in iOS Applications

  • Upload
    stanfy

  • View
    58

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Users' Data Security in iOS Applications

U S E R S ’ D ATA S E C U R I T YI N I O S A P P L I C A T I O N S

Odessa Innovation Week, WebCamp 2015

Page 2: Users' Data Security in iOS Applications

W H AT I S U S E R D ATA

Odessa Innovation Week, WebCamp 2015

User data is being generated during interactions with user

Page 3: Users' Data Security in iOS Applications

I T I S A D E V E L O P E R ’ S R E S P O N S I B I L I T Y T O P R O T E C T

S E N S I T I V E D ATA

Odessa Innovation Week, WebCamp 2015

Page 4: Users' Data Security in iOS Applications

U S E R D ATA S TAT E S

Odessa Innovation Week, WebCamp 2015

• Runtime data

• Stored data

• Transmitted data

Page 5: Users' Data Security in iOS Applications

W H AT C O U L D P O S S I B LY G O W R O N G ?

Odessa Innovation Week, WebCamp 2015

• Runtime data

• Stored data

• Transmitted data

C O U L D B E S T O L E N ,

M O D I F I E D O R D E L E T E D

Page 6: Users' Data Security in iOS Applications

W H AT S H O U L D W E D O ?

Odessa Innovation Week, WebCamp 2015

• Runtime data

• Stored data

• Transmitted data

encrypt storage

encrypt transport

use secure design

Page 7: Users' Data Security in iOS Applications

R U N T I M E D ATA

Odessa Innovation Week, WebCamp 2015

Page 8: Users' Data Security in iOS Applications

U I PA S T E B O A R D

• general Pasteboard is open for everyone

Odessa Innovation Week, WebCamp 2015

[[UIPasteboard generalPasteboard] setValue:@"1234 5689 3455 6604" forPasteboardType:@"card"];

• custom Pasteboards are open for current app and apps with same team ID

UIPasteboard * pasteboard = [UIPasteboard pasteboardWithName:@"my.app" create:YES];pasteboard.persistent = YES; [pasteboard setValue:@"1234 5689 3455 6604”forPasteboardType:@"card"];

Page 9: Users' Data Security in iOS Applications

U I PA S T E B O A R D

• prevent copying sensitive data

Odessa Innovation Week, WebCamp 2015

@implementation UITextView (DisableCopyPaste)- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { [UIMenuController sharedMenuController].menuVisible = NO;

return NO; } @end

Page 10: Users' Data Security in iOS Applications

S N A P S H O T T I N G & S C R E E N S H O T T I N G

Odessa Innovation Week, WebCamp 2015

- (void)applicationWillResignActive:(UIApplication *)application { UIImageView * imageView = [[UIImageView alloc]initWithFrame:[self.window frame]]; [imageView setImage:[UIImage imageNamed:@"mySplash"]]; [self.window addSubview:imageView]; }

• hide any sensitive data before snapshotting

• or set your picture as snapshot

- (void)applicationWillResignActive:(UIApplication *)application { self.window.rootViewController.view.cardLabel.hidden = YES;

}

Page 11: Users' Data Security in iOS Applications

PA S S W O R D - P R O T E C T I O N

Odessa Innovation Week, WebCamp 2015

• ask password on app launching (if it’s really needed)

• ask password on entering ‘sensitive’ areas

• ask password to confirm some actions

• ask password on configuration screen

Page 12: Users' Data Security in iOS Applications

T O U C H I D

iOS8+

LocalAuthentication Framework

use as alternative to your application’s password

Odessa Innovation Week, WebCamp 2015

Page 13: Users' Data Security in iOS Applications

T O U C H I D

#import <LocalAuthentication/LocalAuthentication.h>

LAContext * myContext = [LAContext new];NSError * authError = nil;NSString * myLocalizedReasonString = @"Authenticate using your finger";if ([myContext canEvaluatePolicy:LAPolicyDevice OwnerAuthenticationWithBiometrics error:&authError]) { ...} else { NSLog(@"Authentication error %@", authError); }

Odessa Innovation Week, WebCamp 2015

Page 14: Users' Data Security in iOS Applications

T O U C H I D

[myContext evaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:myLocalizedReasonString reply:^(BOOL success, NSError * error) { if (success) { NSLog(@"User is authenticated successfully”); } else { NSLog(@"Authentication failed with error %@", error); } }];

Odessa Innovation Week, WebCamp 2015

Page 15: Users' Data Security in iOS Applications

T O U C H I Dswitch (error.code) { case LAErrorAuthenticationFailed: NSLog(@"Authentication Failed"); break; case LAErrorUserCancel: NSLog(@"User pressed Cancel button"); break; case LAErrorUserFallback: NSLog(@"User pressed \"Enter Password\""); [self showPassword]; break; default: NSLog(@"Touch ID is not configured"); [self showPassword]; break;}

Odessa Innovation Week, WebCamp 2015

Page 16: Users' Data Security in iOS Applications

S T O R E D D ATA

Odessa Innovation Week, WebCamp 2015

Page 17: Users' Data Security in iOS Applications

D O N O T S T O R E

• passwords

• keys

• any financial data

• logs!

Odessa Innovation Week, WebCamp 2015

Page 18: Users' Data Security in iOS Applications

P L A I N T E X T K E Y S A R E B A D

Odessa Innovation Week, WebCamp 2015

[Parse setApplicationId:@"GpvoqI7Ut29H...2JL1dyceOv0hV1" clientKey:@“sv8wcfIf0neo...y2m9YIwu0OxQr”];

static NSString *kMyAPIKey = @"mySecretApiKey";

Page 19: Users' Data Security in iOS Applications

P L A I N T E X T K E Y S A R E B A D

Odessa Innovation Week, WebCamp 2015

strings MyApp.app/MyApp > myapp.txt

Page 20: Users' Data Security in iOS Applications

P L A I N T E X T K E Y S A R E B A D

Odessa Innovation Week, WebCamp 2015

GpvoqI7Ut29...2JL1dyceOv0hV1 sv8wcfIf0neo...y2m9YIwu0OxQr application did finish launching window T@"UIWindow",&,N T@"UIWindow",&,N,V_window

[Parse setApplicationId:@"GpvoqI7Ut29H...2JL1dyceOv0hV1" clientKey:@“sv8wcfIf0neo...y2m9YIwu0OxQr”];

static NSString *kMyAPIKey = @"mySecretApiKey";

Page 21: Users' Data Security in iOS Applications

AV O I D P L A I N T E X T K E Y S

• store like hex strings

Odessa Innovation Week, WebCamp 2015

unsigned char myApiKey[] = { 0xAB, 0xAA, 0xBE, 0xDE, 0xEF };

• obfuscateNSString * key = [NSString stringWithFormat:@"%@%@%@%@", @"my", @"secret", @"Api", @“Key"]; NSString * key = [NSString stringWithFormat:@"%3$@%2$@%1$@%4$@", @"Api", @"secret", @"my", @"Key"];

NSString * format = [[[@"x3$yx2$z%1$yx4$@" stringByReplacingOccurrencesOfString:@"x" withString:@"%"] stringByReplacingOccurrencesOfString:@"y" withString:@"@"] stringByReplacingOccurrencesOfString:@"z" withString:@"@"];NSString * key = [NSString stringWithFormat:format, @"Api", @"secret", @"my", @"Key"];

"mySecretApiKey"

Page 22: Users' Data Security in iOS Applications

U S E E N C R Y P T E D K E Y S

1. take key in plain text

Odessa Innovation Week, WebCamp 2015

static NSString *kMyAPIKey = @"mySecretApiKey";

NSData * encryptedData = [RNEncryptor encryptData:kMyAPIKey withSettings:kRNCryptorAES256Settings password:[self calculatePassword] error:&error]; NSString * resultString = [[NSString alloc] initWithData:encryptedData

encoding:NSUTF8StringEncoding];

2. encrypt it

Page 23: Users' Data Security in iOS Applications

U S E E N C R Y P T E D K E Y S

3. hardcode encrypted key

Odessa Innovation Week, WebCamp 2015

static NSString *kMyAPIKeyEncrypted = @“789fae43c3e66113e48cebfafeef6806542c0c9062b78b686f515acb09b2c0a6";

NSData *decryptedData = [RNDecryptor decryptData:encryptedData withPassword:[self calculatePassword] error:&error];

NSString * resultString = [[NSString alloc] initWithData:encryptedData encoding:NSUTF8StringEncoding];

4. decrypt it before using

Page 24: Users' Data Security in iOS Applications

U S E E N C R Y P T E D K E Y S

5. use generated password, not static string

Odessa Innovation Week, WebCamp 2015

- (NSString * )calculatePassword { CGFloat result = (CGFloat) (1.0/23.0); return [NSString stringWithFormat:@"%.10f", result];}

@“0.0434782609” this password is calculated each time;

is not stored as string

Page 25: Users' Data Security in iOS Applications

W H E R E T O S T O R E S E N S I T I V E D ATA ?

• NSUserDefaults

• plist

• files

• database

• keychain

Odessa Innovation Week, WebCamp 2015

Page 26: Users' Data Security in iOS Applications

W H E R E T O S T O R E S E N S I T I V E D ATA ?

• NSUserDefaults

• plist

• files

• database

• keychain

Odessa Innovation Week, WebCamp 2015

U S E K E Y C H A I N

Page 27: Users' Data Security in iOS Applications

W H E R E T O S T O R E S E N S I T I V E D ATA ?

• NSUserDefaults

• plist

• files

• database

• keychain

Odessa Innovation Week, WebCamp 2015

O R U S E E N C R Y P T I O N !

Page 28: Users' Data Security in iOS Applications

E N C R Y P T A L L D ATA

C O M M O N C R Y P T O F R A M E W O R K

Odessa Innovation Week, WebCamp 2015

Page 29: Users' Data Security in iOS Applications

E N C R Y P T A L L D ATA#import <CommonCrypto/CommonCryptor.h> - (NSData *)AES256EncryptWithKey:(NSString*)key { char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused) bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void* buffer = malloc(bufferSize); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL [self bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesEncrypted); if (cryptStatus == kCCSuccess) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } free(buffer); return nil; }

Odessa Innovation Week, WebCamp 2015

C O M M O N C R Y P T O

Page 30: Users' Data Security in iOS Applications

E N C R Y P T A L L D ATA#import <CommonCrypto/CommonCryptor.h> - (NSData *)AES256EncryptWithKey:(NSString*)key { char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused) bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void* buffer = malloc(bufferSize); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL [self bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesEncrypted); if (cryptStatus == kCCSuccess) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } free(buffer); return nil; }

Odessa Innovation Week, WebCamp 2015

C O M M O N C R Y P T O

Page 31: Users' Data Security in iOS Applications

E N C R Y P T A L L D ATA

#import <RNCryptor/RNCryptor.h>NSData *data = [@"Data" dataUsingEncoding:NSUTF8StringEncoding];NSError *error;NSData *encryptedData = [RNEncryptor encryptData:data withSettings:kRNCryptorAES256Settings password:aPassword error:&error];

NSData *decryptedData = [RNDecryptor decryptData:encryptedData withPassword:aPassword error:&error];

Odessa Innovation Week, WebCamp 2015

https://github.com/RNCryptor/RNCryptor

U S E W R A P P E R S

Page 32: Users' Data Security in iOS Applications

E N C R Y P T A L L D ATA

#import "scell_seal.h"

NSData *data = [@"Data" dataUsingEncoding:NSUTF8StringEncoding];

TSCellSeal * cellSeal = [[TSCellSeal alloc] initWithKey:masterKeyData];NSData * encryptedMessage = [cellSeal wrapData:data context:nil error:&error];NSData * decryptedMessage = [cellSeal unwrapData:encryptedMessage context:nil error:&error];

Odessa Innovation Week, WebCamp 2015

https://github.com/cossacklabs/themis

U S E C O O L L I B R A R I E S

Page 33: Users' Data Security in iOS Applications

E N C R Y P T D ATA B A S E

SQLCipher (+FMDB)

https://github.com/sqlcipher/sqlcipher

CoreData

https://github.com/project-imas/encrypted-core-data

Odessa Innovation Week, WebCamp 2015

Page 34: Users' Data Security in iOS Applications

F I L E S P R O T E C T I O N

Odessa Innovation Week, WebCamp 2015

allows to encrypt NSFiles

Page 35: Users' Data Security in iOS Applications

Odessa Innovation Week, WebCamp 2015

* NSFileProtectionNone

* NSFileProtectionComplete Cannot be accessed while device locked.

* NSFileProtectionCompleteUnlessOpen Opens only when device is unlocked; can be accessed whenever

* NSFileProtectionCompleteUntilFirstUserAuthentication Opens only after device is booted; can be accessed whenever.

F I L E S P R O T E C T I O N

Page 36: Users' Data Security in iOS Applications

Odessa Innovation Week, WebCamp 2015

NSFileManager * fileManager = [NSFileManager defaultManager]; NSDictionary * attributes = @{ NSFileProtectionKey : NSFileProtectionComplete };[fileManager createFileAtPath:path contents:data attributes:attributes];[fileManager setAttributes:attributes ofItemAtPath:path error:&errorFolderProtect];

F I L E S P R O T E C T I O N

Page 37: Users' Data Security in iOS Applications

Odessa Innovation Week, WebCamp 2015

[SSKeychain setPassword:password forService:@"com.my.app" account:@"MyAppUserPasswordKey"];

S E C U R E K E Y C H A I N

https://github.com/soffes/sskeychain

S E C U R I T Y F R A M E W O R K O R U S E W R A P P E R S

Page 38: Users' Data Security in iOS Applications

D I S A B L E D E B U G G I N G Y O U R A P P S

Odessa Innovation Week, WebCamp 2015

typedef int (*ptrace_ptr_t)(int _request, pid_t _pid, caddr_t _addr, int _data); #if !defined(PT_DENY_ATTACH) #define PT_DENY_ATTACH 31#endif

void disable_gdb() { void* handle = dlopen(0, RTLD_GLOBAL | RTLD_NOW); ptrace_ptr_t ptrace_ptr = dlsym(handle, "ptrace"); ptrace_ptr(PT_DENY_ATTACH, 0, 0, 0); dlclose(handle); }

int main(int argc, char * argv[]) { #if !(DEBUG) // Don't interfere with Xcode debugging sessions. disable_gdb();#endif @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } } https://www.theiphonewiki.com/wiki/Bugging_Debuggers

Page 39: Users' Data Security in iOS Applications

T R A N S M I T T E D D ATA

Odessa Innovation Week, WebCamp 2015

Page 40: Users' Data Security in iOS Applications

S E N D I N G P L A I N T E X T D ATA

• plain text could be easily stolen or modified

• sending passwords as plain text is bad idea

Odessa Innovation Week, WebCamp 2015

Page 41: Users' Data Security in iOS Applications

S E N D I N G P L A I N T E X T D ATA

• plain text could be easily stolen or modified

• sending passwords as plain text is bad idea

Odessa Innovation Week, WebCamp 2015

U S E H T T P S !

Page 42: Users' Data Security in iOS Applications

Odessa Innovation Week, WebCamp 2015

Page 43: Users' Data Security in iOS Applications

U S I N G H T T P S

Odessa Innovation Week, WebCamp 2015

HTTPs protects only POST requests (do not use GET*)

* https://blog.httpwatch.com/2009/02/20/how-secure-are-query-strings-over-https/

Page 44: Users' Data Security in iOS Applications

U S I N G H T T P S : B A D P R A C T I C E S

• allowing self-signed certificates

Odessa Innovation Week, WebCamp 2015

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; }

[AFSecurityPolicy setAllowInvalidCertificates:YES]; [AFSecurityPolicy setValidatesDomainName:NO];

• allowing invalid certificates

Page 45: Users' Data Security in iOS Applications

U S I N G H T T P S : B A D P R A C T I C E S

• allowing self-signed certificates

Odessa Innovation Week, WebCamp 2015

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; }

[AFSecurityPolicy setAllowInvalidCertificates:YES]; [AFSecurityPolicy setValidatesDomainName:NO];

• allowing invalid certificates

A F N E T W O R K I N G B U G S 2 . 5 . 1 - 2 . 5 . 2

Page 46: Users' Data Security in iOS Applications

H T T P S

O N I O S 9 H T T P I S T U R N E D O F F !

Odessa Innovation Week, WebCamp 2015

(as required by App Transport Security)

Page 47: Users' Data Security in iOS Applications

H T T P S

O N I O S 9 H T T P I S T U R N E D O F F !

Odessa Innovation Week, WebCamp 2015

you should disable ATS to use HTTP

(as required by App Transport Security)

Page 48: Users' Data Security in iOS Applications

H T T P S

O N I O S 9 H T T P I S T U R N E D O F F !

Odessa Innovation Week, WebCamp 2015

you should disable ATS to use HTTP

(as required by App Transport Security)

Page 49: Users' Data Security in iOS Applications

U S I N G H T T P S : B A D P R A C T I C E S

H T T P S D O E S N ’ T M E A N T O U S E P L A I N T E X T

Odessa Innovation Week, WebCamp 2015

Page 50: Users' Data Security in iOS Applications

U S I N G H T T P S : B A D P R A C T I C E S

H T T P S D O E S N ’ T M E A N T O U S E P L A I N T E X T

Odessa Innovation Week, WebCamp 2015

anyone can generate fake SSL certificate and your app will trust him

MitM attacks lead to stolen or modified data

Page 51: Users' Data Security in iOS Applications

H T T P S I S N O T S O S E C U R E

A L L D ATA S H O U L D B E E N C R Y P T E D

Odessa Innovation Week, WebCamp 2015

For messaging you should use special crypto, not just encrypting data like for storage.

Page 52: Users' Data Security in iOS Applications

E N C R Y P T I N G C O M M U N I C AT I O N

Odessa Innovation Week, WebCamp 2015

Communication is sequence of messages

Page 53: Users' Data Security in iOS Applications

E N C R Y P T I N G C O M M U N I C AT I O N

Odessa Innovation Week, WebCamp 2015

Communication is sequence of messages

When sequence get’s recorded, it is easier to crack the key based on captured data

Page 54: Users' Data Security in iOS Applications

E N C R Y P T I N G C O M M U N I C AT I O N

Odessa Innovation Week, WebCamp 2015

Communication is sequence of messages

When sequence get’s recorded, it is easier to crack the key based on captured data

Your keys should be ephemeral, your exchange should be sequence aware, authenticated and content-

validating

Page 55: Users' Data Security in iOS Applications

E N C R Y P T I N G C O M M U N I C AT I O N

Odessa Innovation Week, WebCamp 2015

Communication is sequence of messages

When sequence get’s recorded, it is easier to crack the key based on captured data

Your keys should be ephemeral, your exchange should be sequence aware, authenticated and content-

validating

E N D T O E N D

P E R F E C T F O R WA R D S E C R E C Y

P R O N E T O T I M I N G AT TA C K S

AUTHENT ICATE MESSAGES

U S E K E Y A G R E E M E N T P R O T O C O L

Page 56: Users' Data Security in iOS Applications

E N C R Y P T I N G C O M M U N I C AT I O N

Odessa Innovation Week, WebCamp 2015

Communication is sequence of messages

When sequence get’s recorded, it is easier to crack the key based on captured data

Your keys should be ephemeral, your exchange should be sequence aware, authenticated and content-

validating

E N D T O E N D

P E R F E C T F O R WA R D S E C R E C Y

P R O N E T O T I M I N G AT TA C K S

AUTHENT ICATE MESSAGES

U S E K E Y A G R E E M E N T P R O T O C O L

Page 57: Users' Data Security in iOS Applications

E N C R Y P T I N G C O M M U N I C AT I O N

Odessa Innovation Week, WebCamp 2015

Themis (Secure Message / Secure Session) (https://github.com/cossacklabs/themis)

OTRKit (https://github.com/ChatSecure/OTRKit)

libsodium / NaCL (https://github.com/mochtu/libsodium-ios)

T H E R E ’ S A L I B F O R T H AT !

Page 58: Users' Data Security in iOS Applications

M O R E T O R E A D

Odessa Innovation Week, WebCamp 2015

• The Mobile Application Hacker's Handbook • https://books.google.com.ua/books?

id=UgVhBgAAQBAJ

• Secure Developing on iOS • https://www.isecpartners.com/media/11221/

secure_development_on_ios.pdf

Page 59: Users' Data Security in iOS Applications

M O R E T O R E A D

Odessa Innovation Week, WebCamp 2015

• Security Coding Guide • https://developer.apple.com/library/ios/

documentation/Security/Conceptual/SecureCodingGuide/Introduction.html#//apple_ref/doc/uid/TP40002477-SW1

Page 60: Users' Data Security in iOS Applications

M O R E T O R E A D

Odessa Innovation Week, WebCamp 2015

• Designing Secure User Interfaces • https://developer.apple.com/library/ios/

documentation/Security/Conceptual/SecureCodingGuide/Articles/AppInterfaces.html#//apple_ref/doc/uid/TP40002862-SW1

Page 61: Users' Data Security in iOS Applications

M O R E T O R E A D

Odessa Innovation Week, WebCamp 2015

• https://www.theiphonewiki.com/wiki/Bugging_Debuggers

• http://www.splinter.com.au/2014/09/16/storing-secret-keys/

• http://priyaaank.tumblr.com/post/81172916565/validating-ssl-certificates-in-mobile-apps

• https://www.venafi.com/blog/post/ssl-vulnerabilities-in-your-mobile-apps-what-could-possibly-go-wrong

Page 62: Users' Data Security in iOS Applications

L A S T S L I D E

@vixentael

Odessa Innovation Week, WebCamp 2015

Anastasi, iOS dev @ Stanfy