Iphone 实施和测试iOS数据保护

Iphone 实施和测试iOS数据保护,iphone,objective-c,security,ios,data-protection,Iphone,Objective C,Security,Ios,Data Protection,刚才看到了第209次会议-保护来自de 2010 WWDC的应用程序数据 该主题说明了许多内容,包括为文件设置数据保护属性的方式(NSFileProtectionComplete、NSFileProtectionNone)以及如何决定哪种保护最适合您的情况 我刚刚实现了它,但不知道如何测试安全性是否开启,有什么想法吗 此外,我有一个sql lite数据库,需要不时在后台访问,而这种数据保护方法似乎不够好。。是否有任何链接或教程可以指导我了解最佳db保护?(发现了sql密码,但在演化项目中添加有点

刚才看到了第209次会议-保护来自de 2010 WWDC的应用程序数据

该主题说明了许多内容,包括为文件设置数据保护属性的方式(NSFileProtectionComplete、NSFileProtectionNone)以及如何决定哪种保护最适合您的情况

我刚刚实现了它,但不知道如何测试安全性是否开启,有什么想法吗

此外,我有一个sql lite数据库,需要不时在后台访问,而这种数据保护方法似乎不够好。。是否有任何链接或教程可以指导我了解最佳db保护?(发现了sql密码,但在演化项目中添加有点重)

谢谢

从类文档:

该文件以加密格式存储在磁盘上,在设备锁定或引导时无法读取或写入

设置文件属性时,只需传递常量

使用writeToFile:options:error:方法将NSData对象的内容写入磁盘时,请包括NSDataWritingFileProtectionComplete选项

使用NSFileManager的setAttributes:OfiItemPath:error:方法将NSFileProtectionKey属性(具有NSFileProtectionComplete值)添加到现有文件中

编辑(确定受保护文件的可用性)

受保护的文件只有在设备解锁时才可访问。由于应用程序可能会在设备锁定时继续运行,因此您的代码应准备好处理受保护文件随时不可用的可能性。UIKit框架提供了跟踪当前是否启用数据保护的方法

*

  Use applicationProtectedDataWillBecomeUnavailable: and applicationProtectedDataDidBecomeAvailable: methods and use them to track changes to the availability of protected data.
*

  An application can register for the UIApplicationProtectedDataWillBecomeUnavailable and UIApplicationProtectedDataDidBecomeAvailable notifications.
*

  The protectedDataAvailable property of the shared UIApplication object indicates whether protected files are currently accessible. 
任何使用受保护文件的应用程序都应该实现应用程序委托方法。调用applicationProtectedDataWillBecomeUnavailable:方法时,应用程序应立即关闭所有受保护的文件,并在调用applicationProtectedDataDidBecomeAvailable:方法之前不再使用这些文件。当受保护的文件不可用时,任何访问这些文件的尝试都将失败

验证越狱设备上的文件保护

更进一步,如果您想测试精确文件的文件保护,那么您需要一个越狱设备。为此,以下是(非详细)步骤:

1) 越狱一台iOS设备

2) 通过Cydia安装Open SSH(这是从该设备远程访问文件所必需的)()

3) 以root用户身份从计算机(使用Mac客户端或终端)登录到设备

要查找应用程序目录和文件的位置,有多种方法。要么你可以

  • grep
    应用程序的进程(例如
    ps ax | grep YourAppName
    )-确保应用程序正在设备上运行以获取进程详细信息。它应该给出应用程序包的位置
  • 或者,您也可以使用感兴趣的
    find
    搜索特定文件。例如,
    find/-type f-name YouAppName.sqlite
    。它应该给出设备上的文件位置
从这里,当手机被密码锁定时,您可以尝试查看文件是否真的可以访问;或者不是。 -您只需运行
cat YouAppName.sqlite
即可查看内容是否可访问。Ia f文件受保护,它应该显示

不允许操作

错误;否则将显示文件的内容

同样,如果您真的想检查单个文件的文件保护,这是必需的。如果权限和功能设置正确,验证权限就足以保护文件

在侧节点上,文件资源管理器工具(如iExplorer)在验证文件保护方面没有多大帮助,因为此类工具要求设备处于“受信任”模式,因此它们具有访问设备/应用程序内容的权限


祝你好运

更新:使用iOS 6,您的应用程序可能需要数据保护,方法是使用需要在iOS配置文件中的应用程序ID上配置的权限。我还没有测试过,这是我能找到的最好的信息


我对此事的调查使我相信,很难确定设备上是否启用了数据保护

通过将
NSFileProtectionKey
File属性设置为
NSFileProtectionComplete

例如,要创建受保护的文件,可以运行以下代码:

[[NSFileManager defaultManager] createFileAtPath:[self filePath]
                                        contents:[@"super secret file contents" dataUsingEncoding:NSUTF8StringEncoding]
                                      attributes:[NSDictionary dictionaryWithObject:NSFileProtectionComplete
                                                                             forKey:NSFileProtectionKey]];
不幸的是,即使设备上未启用数据保护(或者如果代码在数据保护不可用的模拟器上运行),此代码也将执行而不出错

更糟糕的是,无论文件是否受保护,都将设置
NSFileProtectionComplete
属性。以下是:

self.fileProtectionValue = [[[NSFileManager defaultManager] attributesOfItemAtPath:[self filePath]
                                                                             error:NULL] valueForKey:NSFileProtectionKey];

NSLog(@"file protection value: %@", self.fileProtectionValue);
无论是否启用数据保护,都将吐出
文件保护值:NSFileProtectionComplete

我可以使用两种方法来发现文件保护是否按预期工作。不幸的是,这两种方法都不适用于检测现场设备上是否启用了数据保护

这两种方法的工作原理是,如果设备被锁定,则无法读取受保护的文件

方法一涉及在设备锁定后,但在应用程序继续运行时,使用计时器尝试读取文件:

[self performSelector:@selector(doReload) withObject:nil afterDelay:20];

- (void)doReload {

    NSLog(@"protected data available: %@",[[UIApplication sharedApplication] isProtectedDataAvailable] ? @"yes" : @"no");

    NSError *error;

    self.fileContents = [NSString stringWithContentsOfFile:[self filePath]
                                              encoding:NSUTF8StringEncoding
                                                 error:&error];

    NSLog(@"file contents: %@\nerror: %@", self.fileContents, error);
}
如果运行上述代码并锁定受数据保护的设备,它将吐出:

protected data available: no
file contents: (null)
error: Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn’t be completed. (Cocoa error 257.)" UserInfo=0x16e110 {NSFilePath=/var/mobile/Applications/D71F1F1F-6C25-4848-BB1F-51539B47EC79/Documents/protected_file, NSUnderlyingError=0x16e010 "The operation couldn’t be completed. Operation not permitted"}
20秒的延迟是必要的,因为锁定启用数据保护的设备后,有10秒左右的宽限期,受保护的数据仍然可用

第二种方法是在应用程序中创建受保护的文件,退出应用程序,锁定设备,等待10秒,然后重试
- (BOOL) isProtectedItemAtURL:(NSURL *)URL {
    BOOL            result                      = YES;
    NSDictionary    *attributes                 = nil;
    NSString        *protectionAttributeValue   = nil;
    NSFileManager   *fileManager                = nil;

    fileManager = [[NSFileManager alloc] init];
    attributes = [fileManager attributesOfItemAtPath:[URL path] error:&error];
    if (attributes != nil){
        protectionAttributeValue = [attributes valueForKey:NSFileProtectionKey];
        if ((protectionAttributeValue == nil) || [protectionAttributeValue isEqualToString:NSFileProtectionNone]){
            result = NO;
        }
    } else {
        // handle the error
    }
    return result;
}
- (BOOL) isItemAtURLAvailable:(NSURL *)URL {
    BOOL            result                      = NO;

    if ([self isProtectedItemAtURL:URL]){
        // Item is protected
        if ([[UIApplication sharedApplication] isProtectedDataAvailable]){
            // Protected content is available
            result = YES;
        }
    } else {
        result = YES;
    }

    return result;
}