Example #1
0
def ReadSFLPlist(file_handle, recent_items, source, user=''):
    try:
        basename = os.path.basename(source).lower()
        ccl_bplist.set_object_converter(
            ccl_bplist.NSKeyedArchiver_common_objects_convertor)
        plist = ccl_bplist.load(file_handle)

        ns_keyed_archiver_obj = ccl_bplist.deserialise_NsKeyedArchiver(
            plist, parse_whole_structure=True)

        root = ns_keyed_archiver_obj['root']
        log.debug('Version of SFL is {}'.format(
            root['version']))  # Currently we parse version 1
        items = root['items']

        for item in items:
            url = ''
            name = ''
            try:
                url = item['URL']['NS.relative']
            except KeyError:
                pass
            if url.find('x-apple-findertag') == 0:
                continue  # skipping these items
            name = item.get('name', '')
            if name or url:
                recent_type = RecentType.UNKNOWN
                if basename.find('recentservers') >= 0:
                    recent_type = RecentType.SERVER
                elif basename.find('recenthosts') >= 0:
                    recent_type = RecentType.HOST
                elif basename.find('recentdocuments') >= 0:
                    recent_type = RecentType.DOCUMENT
                elif basename.find('recentapplications') >= 0:
                    recent_type = RecentType.APPLICATION

                ri = RecentItem(name, url, '', source, recent_type, user)
                recent_items.append(ri)
                # try: # Not reading bookmark right now, but this code should work!
                #     bm = item['bookmark']
                #     if type(bm) == ccl_bplist.NsKeyedArchiverDictionary: # Sometimes its 'str', otherwise this
                #         bm = bm['NS.data']
                #     #print "bookmark bytes=", len(bm)
                # except:
                #     pass # Not everything has bookmarks
    except (ccl_bplist.BplistError, ValueError, TypeError) as ex:
        log.exception('Error reading SFL plist')
Example #2
0
def process_nsa_plist(input_path, f):
    '''Returns a deserialized plist. Input is NSKeyedArchive'''
    global use_as_library
    try:
        if not use_as_library:
            print('Reading file .. ' + input_path)
        f = extract_nsa_plist(f)
        ccl_bplist.set_object_converter(
            ccl_bplist.NSKeyedArchiver_common_objects_convertor)
        plist = ccl_bplist.load(f)
        ns_keyed_archiver_obj = ccl_bplist.deserialise_NsKeyedArchiver(
            plist, parse_whole_structure=True)

        root_names = getRootElementNames(f)
        top_level = []

        for root_name in root_names:
            root = ns_keyed_archiver_obj[root_name]
            if not use_as_library:
                print('Trying to deserialize binary plist $top = {}'.format(
                    root_name))
            if isinstance(root, dict):
                plist = {}
                recurseCreatePlist(plist, root,
                                   ns_keyed_archiver_obj.object_table)
                if root_name.lower() != 'root':
                    plist = {root_name: plist}
            elif isinstance(root, list):
                plist = []
                recurseCreatePlist(plist, root,
                                   ns_keyed_archiver_obj.object_table)
                if root_name.lower() != 'root':
                    plist = {root_name: plist}
            else:
                plist = {root_name: root}

            if len(root_names) == 1:
                top_level = plist
            else:  # > 1
                top_level.append(plist)

    except Exception as ex:
        log.exception('Had an exception (error)')
        #traceback.print_exc()

    return top_level
def DeserializeNSKeyedArchive(file_pointer):
    '''Pass an open file pointer (file must be opened as rb) and get a dict or list representation
       of the plist returned back
    '''
    ccl_bplist.set_object_converter(
        ccl_bplist.NSKeyedArchiver_common_objects_convertor)
    plist = ccl_bplist.load(file_pointer)
    ns_keyed_archiver_obj = ccl_bplist.deserialise_NsKeyedArchiver(
        plist, parse_whole_structure=True)
    root = ns_keyed_archiver_obj['root']

    if isinstance(root, dict):
        plist = {}
    else:
        plist = []

    recurseCreatePlist(plist, root)
    return plist