Data Protection API
Digress
In the beginning, I spoke a few other things. Many of my friends asked me why I didn't write defense. I did hesitate.
Hackers always imagine how to write a hacker if he is a developer before he can find the starting point. Similarly, developers also need to think about what hackers will do to take appropriate defense measures. Then there is a recursive game.
Take the jailbreak detection as an example. At first, you only need to determine whether Cydia is installed or not. hackers say yes, so I can do it without installing Cydia. The developers also said that you must use MobileSubstrate, bash, and ssh to check whether these tools are installed on your mobile phone. But what's the purpose? You can determine what I went around.
When class-dump becomes popular and function symbols are exposed, developers try their best to hide their own sensitive function code. Hackers also know where class-dump is, so new search methods emerge. That is to say, when a defense means becomes popular, it will no longer be a defense means that makes hackers scold "really difficult. For example, if hackers knows that all developers have erased data in the memory, it's okay to hook memset and read it before you erase it. The developer said: I directly write the hard disk and delete it! Hackers said: Have you heard of file restoration?
Okay, there are a lot of poor ones. This article introduces defense-related topics-Data Protection APIs for iOS.
Data Protection API
Files in the file system and items in the keychain are encrypted and stored. After the user unlocks the device, the system generates a password key for decryption through the UDID key and the password set by the user, which is stored in the memory until the device is locked again, developers can use the Data Protection API to set when files in the file system and items in the keychain should be decrypted.
1) File Protection
- /* Set the protection level for the filePath file */
- NSDictionary * attributes = [NSDictionary dictionaryWithObject: NSFileProtectionComplete
- ForKey: NSFileProtectionKey];
- [[NSFileManager defamanager manager] setAttributes: attributes
- OfItemAtPath: filePath
- Error: nil];
- // File Protection Level Attribute list
- NSFileProtectionNone // The file is unprotected and can be accessed at any time (Default)
- NSFileProtectionComplete // The file is protected and accessible only when the device is not locked
- NSFileProtectionCompleteUntilFirstUserAuthentication // The file is protected until the device is started and the user enters the password for the first time.
- NSFileProtectionCompleteUnlessOpen // The file is protected and can be opened only when the device is not locked. However, even when the device is locked, files that have been opened can still be used and written.
2) keychain item Protection
- /* Set the keychain item protection level */
- NSDictionary * query =@{ (_ bridge id) kSecClass: (_ bridge id) kSecClassGenericPassword,
- (_ Bridge id) kSecAttrGeneric: @ "MyItem ",
- (_ Bridge id) kSecAttrAccount: @ "username ",
- (_ Bridge id) kSecValueData: @ "password ",
- (_ Bridge id) kSecAttrService: [NSBundle mainBundle]. bundleIdentifier,
- (_ Bridge id) kSecAttrLabel :@"",
- (_ Bridge id) kSecAttrDescription :@"",
- (_ Bridge id) kSecAttrAccessible :( _ bridge id) kSecAttrAccessibleWhenUnlocked };
- OSStatus result = SecItemAdd (_ bridge CFDictionaryRef) (query), NULL );
- // List of keychain protection levels
- KSecAttrAccessibleWhenUnlocked // The keychain item is protected and can be accessed only when the device is not locked.
- KSecAttrAccessibleAfterFirstUnlock // The keychain item is protected until the device starts and the user enters the password for the first time
- KSecAttrAccessibleAlways // The keychain is unprotected and can be accessed at any time (Default)
- KSecAttrAccessibleWhenUnlockedThisDeviceOnly // The keychain item is protected and can be accessed only when the device is not locked and cannot be transferred to another device
- KSecAttrAccessibleAfterFirstUnlockThisDeviceOnly // The keychain item is protected until the device starts and the user enters the password for the first time and cannot be transferred to another device
- KSecAttrAccessibleAlwaysThisDeviceOnly // The keychain is unprotected and can be accessed at any time, but cannot be transferred to other devices
Application Instance
Write a piece of information infoStrng string into the file, and then set Protection through the Data Protection API.
- NSString * documentsPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
- NSString * filePath = [documentsPath stringByAppendingPathComponent: @ "DataProtect"];
- [InfoString writeToFile: filePath
- Atomically: YES
- Encoding: NSUTF8StringEncoding
- Error: nil];
- NSDictionary * attributes = [NSDictionary dictionaryWithObject: NSFileProtectionComplete
- ForKey: NSFileProtectionKey];
- [[NSFileManager defamanager manager] setAttributes: attributes
- OfItemAtPath: filePath
- Error: nil];
After the Device Locks the screen (with password protection), the cat will be denied to read the file even if it is a jailbreaking machine with the root permission.