From 745b74c7ded1f8951940a6ea6f3f380028db6411 Mon Sep 17 00:00:00 2001 From: "mike@arpaia.co" Date: Tue, 19 Aug 2014 15:23:05 -0700 Subject: [PATCH] fixing an issue with json serializing raw data attributes in plists --- osquery/filesystem/plist.mm | 54 ++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/osquery/filesystem/plist.mm b/osquery/filesystem/plist.mm index 2e30e857..eb9cb145 100644 --- a/osquery/filesystem/plist.mm +++ b/osquery/filesystem/plist.mm @@ -15,13 +15,57 @@ namespace pt = boost::property_tree; namespace osquery { namespace fs { +NSMutableArray* filterArray(id dataStructure); + +NSMutableDictionary* filterDictionary(id dataStructure) { + NSMutableDictionary *result = [NSMutableDictionary new]; + for (id key in [dataStructure allKeys]) { + NSString *className = NSStringFromClass([[dataStructure objectForKey:key] class]); + if ([className isEqualToString:@"__NSArrayI"] || + [className isEqualToString:@"__NSArrayM"] || + [className isEqualToString:@"__NSCFArray"]) { + [result setObject:filterArray([dataStructure objectForKey:key]) forKey:key]; + } else if ([className isEqualToString:@"__NSCFDictionary"]) { + [result setObject:filterDictionary([dataStructure objectForKey:key]) forKey:key]; + } else if ([className isEqualToString:@"__NSCFData"]) { + [result setObject:@"NSData" forKey:key]; + } else { + [result setObject:[dataStructure objectForKey:key] forKey:key]; + } + } + return result; +} + +NSMutableArray* filterArray(id dataStructure) { + NSMutableArray *result = [NSMutableArray new]; + for (id value in dataStructure) { + NSString *className = NSStringFromClass([value class]); + if ([className isEqualToString:@"__NSCFDictionary"]) { + [result addObject:filterDictionary(value)]; + } else if ([className isEqualToString:@"__NSArrayI"] || + [className isEqualToString:@"__NSArrayM"] || + [className isEqualToString:@"__NSCFArray"]) { + [result addObject:filterArray(value)]; + } else if ([className isEqualToString:@"__NSCFData"]) { + [result addObject:@"NSData"]; + } else { + [result addObject:value]; + } + } + return result; +} + +NSMutableDictionary* filterPlist(NSMutableDictionary *plist) { + return filterDictionary(plist); +} + Status parsePlistContent(const std::string& fileContent, pt::ptree& tree) { NSData *plistContent = [NSData dataWithBytes:fileContent.c_str() length:fileContent.size()]; NSError *error; NSPropertyListFormat plistFormat; - NSDictionary *plist = (NSDictionary*) + NSMutableDictionary *plist = (NSMutableDictionary*) [NSPropertyListSerialization propertyListWithData:plistContent options:NSPropertyListImmutable format:&plistFormat @@ -48,6 +92,14 @@ Status parsePlistContent(const std::string& fileContent, pt::ptree& tree) { } } + try { + plist = filterPlist(plist); + } catch (const std::exception& e) { + LOG(ERROR) << "An exception occured while filtering the plist: " << + e.what(); + return Status(1, e.what()); + } + NSData *jsonDataObjc; if ([NSJSONSerialization isValidJSONObject:plist]) { jsonDataObjc = [NSJSONSerialization dataWithJSONObject:plist