Ejemplo n.º 1
0
    def __ext4Xtract(self,
                     symlinks_file='symlinks.txt',
                     fs_config_file='fs_config.txt'):
        import ext4, string, struct

        def scan_dir(root_inode, root_path=""):
            for entry_name, entry_inode_idx, entry_type in root_inode.open_dir(
            ):
                if entry_name in ['.', '..', 'lost+found'
                                  ] or entry_name.endswith(' (2)'):
                    continue
                entry_inode = root_inode.volume.get_inode(
                    entry_inode_idx, entry_type)
                entry_inode_path = root_path + '/' + entry_name
                mode = self.__getperm(entry_inode.mode_str)
                uid = entry_inode.inode.i_uid
                gid = entry_inode.inode.i_gid
                con = '?'
                cap = ''
                for i in list(entry_inode.xattrs()):
                    if i[0] == 'security.selinux':
                        con = i[1].decode('utf8')[:-1]
                    elif i[0] == 'security.capability':
                        cap = ' capabilities=' + str(
                            hex(struct.unpack("<IIIII", i[1])[1]))
                if entry_inode.is_dir:
                    dir_target = self.EXTRACT_DIR + entry_inode_path
                    if not os.path.isdir(dir_target):
                        os.makedirs(dir_target)
                    if os.name == 'posix':
                        os.chmod(dir_target, int(mode, 8))
                        os.chown(dir_target, uid, gid)
                    self.fsconfig.append(
                        '%s %s %s %s' %
                        (self.DIR + entry_inode_path, uid, gid, mode + cap))
                    scan_dir(entry_inode, entry_inode_path)
                elif entry_inode.is_file:
                    try:
                        raw = entry_inode.open_read().read()
                    except Exception as e:
                        self.__appendf(entry_inode_path,
                                       self.BASE_DIR + os.sep + 'unpack.log')
                        self.__appendf(self.__logtb(e),
                                       self.BASE_DIR + os.sep + 'unpack.log')
                        continue
                    wdone = None
                    if os.name == 'nt':
                        if entry_name.endswith('/'):
                            entry_name = entry_name[:-1]
                        file_target = self.EXTRACT_DIR + entry_inode_path.replace(
                            '/', os.sep)
                        if not os.path.isdir(os.path.dirname(file_target)):
                            os.makedirs(os.path.dirname(file_target))
                        with open(file_target, 'wb') as out:
                            out.write(raw)
                    if os.name == 'posix':
                        file_target = self.EXTRACT_DIR + entry_inode_path
                        if not os.path.isdir(os.path.dirname(file_target)):
                            os.makedirs(os.path.dirname(file_target))
                        with open(file_target, 'wb') as out:
                            out.write(raw)
                        os.chmod(file_target, int(mode, 8))
                        os.chown(file_target, uid, gid)
                    self.fsconfig.append(
                        '%s %s %s %s' %
                        (self.DIR + entry_inode_path, uid, gid, mode + cap))
                elif entry_inode.is_symlink:
                    try:
                        link_target = entry_inode.open_read().read().decode(
                            "utf8")
                        target = self.EXTRACT_DIR + entry_inode_path
                        if os.path.islink(target):
                            try:
                                os.remove(target)
                            except OSError:
                                pass
                        if os.path.isfile(target):
                            try:
                                os.remove(target)
                            except OSError:
                                pass
                        if os.name == 'posix':
                            os.symlink(link_target, target)
                        if os.name == 'nt':
                            with open(target.replace('/', os.sep),
                                      'wb') as out:
                                tmp = bytes.fromhex('213C73796D6C696E6B3EFFFE')
                                for index in list(link_target):
                                    tmp = tmp + struct.pack(
                                        '>sx', index.encode('utf-8'))
                                out.write(tmp + struct.pack('xx'))
                                os.system('attrib +s "%s"' %
                                          target.replace('/', os.sep))
                        if not all(c in string.printable for c in link_target):
                            raise LinkError
                        # if entry_inode_path[1:] == entry_name or link_target[1:] == entry_name:
                        #     self.symlinks.append('%s %s' % (link_target, entry_inode_path[1:]))
                        # else:
                        if len(entry_inode_path.split('/')) <= 2:
                            if entry_inode_path == '/vendor':
                                self.symlinks.append(
                                    '%s %s' %
                                    (link_target, self.DIR + entry_inode_path))
                            else:
                                self.symlinks.append(
                                    '%s %s' %
                                    (link_target, entry_inode_path[1:]))
                        elif entry_inode_path.find('/' + self.DIR) == -1:
                            self.symlinks.append(
                                '%s %s' %
                                (link_target, self.DIR + entry_inode_path))
                        else:
                            self.symlinks.append(
                                '%s %s' % (link_target, entry_inode_path[1:]))
                        self.fsconfig.append('%s %s %s %s' %
                                             (self.DIR + entry_inode_path, uid,
                                              gid, mode + cap))
                    except LinkError:
                        try:
                            link_target_block = int.from_bytes(
                                entry_inode.open_read().read(), "little")
                            link_target = root_inode.volume.read(
                                link_target_block *
                                root_inode.volume.block_size,
                                entry_inode.inode.i_size).decode("utf8")

                            if link_target and all(c in string.printable
                                                   for c in link_target):
                                self.symlinks.append(
                                    '%s %s' %
                                    (link_target, self.DIR + entry_inode_path))
                            else:
                                self.__appendf(
                                    'Failed symlink: ' + entry_inode_path,
                                    self.BASE_DIR + os.sep + 'unpack.log')
                        except:
                            self.__appendf(
                                'Failed symlink: ' + entry_inode_path,
                                self.BASE_DIR + os.sep + 'unpack.log')
                self.context.append('/%s %s' %
                                    (self.DIR + entry_inode_path, con))

        with open(self.OUTPUT_IMAGE_FILE, 'rb') as file:
            root = ext4.Volume(file).root
            dirlist = []
            for file_name, inode_idx, file_type in root.open_dir():
                dirlist.append(file_name)
            dirr = os.path.basename(self.OUTPUT_IMAGE_FILE).split('.')[0]
            setattr(self, 'DIR', dirr)
            if dirr == 'system':
                self.fsconfig = [dirr + ' 0 0 0755']
            elif dirr == 'vendor':
                self.fsconfig = [dirr + ' 0 2000 0755']
            self.context = ['/' + dirr + ' u:object_r:' + dirr + '_file:s0']
            if not os.path.exists(self.EXTRACT_DIR):
                os.makedirs(self.EXTRACT_DIR)
            scan_dir(root)
            for item in self.symlinks:
                self.__appendf(
                    'symlink("' + item.split()[0] + '", "/' + item.split()[1] +
                    '");', self.BASE_DIR + os.sep + symlinks_file)
            self.__appendf('\n'.join(self.fsconfig),
                           self.BASE_DIR + os.sep + fs_config_file)
Ejemplo n.º 2
0
    def __ext4extractor(self):
        import ext4, string, struct
        #fs_config_file = self.FileName + '_statfile.txt'
        contexts = self.BASE_DIR + self.FileName + "_file_contexts"  #08.05.18
        fs_config_file = self.FileName + '_fs_config'

        def scan_dir(root_inode, root_path=""):
            for entry_name, entry_inode_idx, entry_type in root_inode.open_dir(
            ):
                if entry_name in ['.', '..', 'lost+found'
                                  ] or entry_name.endswith(' (2)'):
                    continue
                entry_inode = root_inode.volume.get_inode(
                    entry_inode_idx, entry_type)
                entry_inode_path = root_path + '/' + entry_name
                mode = self.__getperm(entry_inode.mode_str)
                uid = entry_inode.inode.i_uid
                gid = entry_inode.inode.i_gid
                con = ''
                cap = ''
                for i in list(entry_inode.xattrs()):
                    if i[0] == 'security.selinux':
                        con = i[1].decode('utf8')[:-1]
                    elif i[0] == 'security.capability':
                        cap = ' capabilities=' + str(
                            hex(struct.unpack("<IIIII", i[1])[1]))
                if entry_inode.is_dir:
                    dir_target = self.EXTRACT_DIR + entry_inode_path
                    if not os.path.isdir(dir_target):
                        os.makedirs(dir_target)
                    if os.name == 'posix':
                        os.chmod(dir_target, int(mode, 8))
                        os.chown(dir_target, uid, gid)
                    scan_dir(entry_inode, entry_inode_path)
                    if cap == '' and con == '':
                        self.fsconfig.append(
                            '%s %s %s %s' %
                            (self.DIR + entry_inode_path, uid, gid, mode))
                    else:
                        if cap == '':
                            self.fsconfig.append(
                                '%s %s %s %s' %
                                (self.DIR + entry_inode_path, uid, gid, mode))
                            if con != 'u:object_r:' + self.FileName + '_file:s0':  #11.05.18
                                self.context.append(
                                    '/%s %s' %
                                    (self.DIR + entry_inode_path, con))
                        else:
                            if con == '':
                                self.fsconfig.append(
                                    '%s %s %s %s' %
                                    (self.DIR + entry_inode_path, uid, gid,
                                     mode + cap))
                            else:
                                self.fsconfig.append(
                                    '%s %s %s %s' %
                                    (self.DIR + entry_inode_path, uid, gid,
                                     mode + cap))
                                if con != 'u:object_r:' + self.FileName + '_file:s0':  #11.05.18
                                    self.context.append(
                                        '/%s %s' %
                                        (self.DIR + entry_inode_path, con))
                elif entry_inode.is_file:
                    try:
                        raw = entry_inode.open_read().read()
                    except:
                        continue
                    wdone = None
                    if os.name == 'nt':
                        if entry_name.endswith('/'):
                            entry_name = entry_name[:-1]
                        file_target = self.EXTRACT_DIR + entry_inode_path.replace(
                            '/', os.sep)
                        if not os.path.isdir(os.path.dirname(file_target)):
                            os.makedirs(os.path.dirname(file_target))
                        with open(file_target, 'wb') as out:
                            out.write(raw)
                    if os.name == 'posix':
                        file_target = self.EXTRACT_DIR + entry_inode_path
                        if not os.path.isdir(os.path.dirname(file_target)):
                            os.makedirs(os.path.dirname(file_target))
                        with open(file_target, 'wb') as out:
                            out.write(raw)
                        os.chmod(file_target, int(mode, 8))
                        os.chown(file_target, uid, gid)
                    if cap == '' and con == '':
                        self.fsconfig.append(
                            '%s %s %s %s' %
                            (self.DIR + entry_inode_path, uid, gid, mode))
                    else:
                        if cap == '':
                            self.fsconfig.append(
                                '%s %s %s %s' %
                                (self.DIR + entry_inode_path, uid, gid, mode))
                            if con != 'u:object_r:' + self.FileName + '_file:s0':  #11.05.18
                                self.context.append(
                                    '/%s %s' %
                                    (self.DIR + entry_inode_path, con))
                        else:
                            if con == '':
                                self.fsconfig.append(
                                    '%s %s %s %s' %
                                    (self.DIR + entry_inode_path, uid, gid,
                                     mode + cap))
                            else:
                                self.fsconfig.append(
                                    '%s %s %s %s' %
                                    (self.DIR + entry_inode_path, uid, gid,
                                     mode + cap))
                                if con != 'u:object_r:' + self.FileName + '_file:s0':  #11.05.18
                                    self.context.append(
                                        '/%s %s' %
                                        (self.DIR + entry_inode_path, con))
                elif entry_inode.is_symlink:
                    try:
                        link_target = entry_inode.open_read().read().decode(
                            "utf8")
                        target = self.EXTRACT_DIR + entry_inode_path
                        if cap == '' and con == '':
                            self.fsconfig.append('%s %s %s %s %s' %
                                                 (self.DIR + entry_inode_path,
                                                  uid, gid, mode, link_target))
                        else:
                            if cap == '':
                                self.fsconfig.append(
                                    '%s %s %s %s %s' %
                                    (self.DIR + entry_inode_path, uid, gid,
                                     mode, link_target))
                                if con != 'u:object_r:' + self.FileName + '_file:s0':  #11.05.18
                                    self.context.append(
                                        '/%s %s' %
                                        (self.DIR + entry_inode_path, con))
                            else:
                                if con == '':
                                    self.fsconfig.append(
                                        '%s %s %s %s %s' %
                                        (self.DIR + entry_inode_path, uid, gid,
                                         mode + cap, link_target))
                                else:
                                    self.fsconfig.append(
                                        '%s %s %s %s %s' %
                                        (self.DIR + entry_inode_path, uid, gid,
                                         mode + cap, link_target))
                                    if con != 'u:object_r:' + self.FileName + '_file:s0':  #11.05.18
                                        self.context.append(
                                            '/%s %s' %
                                            (self.DIR + entry_inode_path, con))
                        if os.path.islink(target):
                            try:
                                os.remove(target)
                            except:
                                pass
                        if os.path.isfile(target):
                            try:
                                os.remove(target)
                            except:
                                pass
                        if os.name == 'posix':
                            os.symlink(link_target, target)
                        if os.name == 'nt':
                            with open(target.replace('/', os.sep),
                                      'wb') as out:
                                tmp = bytes.fromhex('213C73796D6C696E6B3EFFFE')
                                for index in list(link_target):
                                    tmp = tmp + struct.pack(
                                        '>sx', index.encode('utf-8'))
                                out.write(tmp + struct.pack('xx'))
                                os.system('attrib +s "%s"' %
                                          target.replace('/', os.sep))
                        if not all(c in string.printable for c in link_target):
                            pass
                        if entry_inode_path[1:] == entry_name or link_target[
                                1:] == entry_name:
                            self.symlinks.append(
                                '%s %s' % (link_target, entry_inode_path[1:]))
                        else:
                            self.symlinks.append(
                                '%s %s' %
                                (link_target, self.DIR + entry_inode_path))
                    except:
                        try:
                            link_target_block = int.from_bytes(
                                entry_inode.open_read().read(), "little")
                            link_target = root_inode.volume.read(
                                link_target_block *
                                root_inode.volume.block_size,
                                entry_inode.inode.i_size).decode("utf8")
                            target = self.EXTRACT_DIR + entry_inode_path
                            if link_target and all(c in string.printable
                                                   for c in link_target):
                                if cap == '' and con == '':
                                    self.fsconfig.append(
                                        '%s %s %s %s %s' %
                                        (self.DIR + entry_inode_path, uid, gid,
                                         mode, link_target))
                                else:
                                    if cap == '':
                                        self.fsconfig.append(
                                            '%s %s %s %s %s' %
                                            (self.DIR + entry_inode_path, uid,
                                             gid, mode, link_target))
                                        if con != 'u:object_r:' + self.FileName + '_file:s0':  #11.05.18
                                            self.context.append(
                                                '/%s %s' %
                                                (self.DIR + entry_inode_path,
                                                 con))
                                    else:
                                        if con == '':
                                            self.fsconfig.append(
                                                '%s %s %s %s %s' %
                                                (self.DIR + entry_inode_path,
                                                 uid, gid, mode + cap,
                                                 link_target))
                                        else:
                                            self.fsconfig.append(
                                                '%s %s %s %s %s' %
                                                (self.DIR + entry_inode_path,
                                                 uid, gid, mode + cap,
                                                 link_target))
                                            if con != 'u:object_r:' + self.FileName + '_file:s0':  #11.05.18
                                                self.context.append(
                                                    '/%s %s' %
                                                    (self.DIR +
                                                     entry_inode_path, con))
                                if os.name == 'posix':
                                    os.symlink(link_target, target)
                                if os.name == 'nt':
                                    with open(target.replace('/', os.sep),
                                              'wb') as out:
                                        tmp = bytes.fromhex(
                                            '213C73796D6C696E6B3EFFFE')
                                        for index in list(link_target):
                                            tmp = tmp + struct.pack(
                                                '>sx', index.encode('utf-8'))
                                        out.write(tmp + struct.pack('xx'))
                                        os.system('attrib +s %s' %
                                                  target.replace('/', os.sep))
                            else:
                                pass
                        except:
                            pass

        f = open(self.EXTRACT_DIR + '_size.txt', 'tw', encoding='utf-8')
        self.__appendf(os.path.getsize(self.OUTPUT_IMAGE_FILE),
                       self.EXTRACT_DIR + '_size.txt')
        f.close()
        with open(self.OUTPUT_IMAGE_FILE, 'rb') as file:
            root = ext4.Volume(file).root
            dirlist = []
            for file_name, inode_idx, file_type in root.open_dir():
                dirlist.append(file_name)
            dirr = self.__file_name(
                os.path.basename(
                    self.OUTPUT_IMAGE_FILE).split('.')[0])  #11.05.18
            setattr(self, 'DIR', dirr)
            if dirr == 'system':
                self.fsconfig = [dirr + '/lost+found 0 0 0700']  #11.05.18
            elif dirr == 'vendor':
                self.fsconfig = [dirr + '/lost+found 0 0 0700']  #11.05.18
            self.context = [
                '/' + dirr + '(/.*)? u:object_r:' + dirr + '_file:s0'
            ]  #11.05.18
            scan_dir(root)
            self.fsconfig.sort()
            self.__appendf('\n'.join(self.fsconfig),
                           self.BASE_DIR + os.sep + fs_config_file)
            if self.context:  #11.05.18
                self.context.sort()  #11.05.18
                self.__appendf('\n'.join(self.context), contexts)  #11.05.18
Ejemplo n.º 3
0
    def __ext4extractor(self):
        import ext4, string, struct
        fs_config_file = self.FileName + '_fs_config'
        fuking_symbols = '\\^$.|?*+(){}[]'
        contexts = self.BASE_MYDIR + 'config' + os.sep + self.FileName + "_file_contexts"  #08.05.18

        def scan_dir(root_inode, root_path=""):
            for entry_name, entry_inode_idx, entry_type in root_inode.open_dir(
            ):
                if entry_name in ['.', '..'] or entry_name.endswith(' (2)'):
                    continue
                entry_inode = root_inode.volume.get_inode(
                    entry_inode_idx, entry_type)
                entry_inode_path = root_path + '/' + entry_name
                mode = self.__getperm(entry_inode.mode_str)
                uid = entry_inode.inode.i_uid
                gid = entry_inode.inode.i_gid
                con = ''
                cap = ''
                for i in list(entry_inode.xattrs()):
                    if i[0] == 'security.selinux':
                        con = i[1].decode('utf8')[:-1]
                    elif i[0] == 'security.capability':
                        raw_cap = struct.unpack("<5I", i[1])
                        if raw_cap[1] > 65535:
                            cap = '' + str(
                                hex(
                                    int('%04x%04x' %
                                        (raw_cap[3], raw_cap[1]), 16)))
                        else:
                            cap = '' + str(
                                hex(
                                    int(
                                        '%04x%04x%04x' %
                                        (raw_cap[3], raw_cap[2], raw_cap[1]),
                                        16)))
                        cap = ' capabilities={cap}'.format(cap=cap)
                if entry_inode.is_dir:
                    dir_target = self.EXTRACT_DIR + entry_inode_path.replace(
                        ' ', '_')
                    if not os.path.isdir(dir_target):
                        os.makedirs(dir_target)
                    if os.name == 'posix':
                        os.chmod(dir_target, int(mode, 8))
                        os.chown(dir_target, uid, gid)
                    scan_dir(entry_inode, entry_inode_path)
                    if cap == '' and con == '':
                        tmppath = self.DIR + entry_inode_path
                        if (tmppath).find(' ', 1, len(tmppath)) > 0:
                            spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                            if not os.path.isfile(spaces_file):
                                f = open(spaces_file, 'tw', encoding='utf-8')
                                self.__appendf(tmppath, spaces_file)
                                f.close
                            else:
                                self.__appendf(tmppath, spaces_file)
                            tmppath = tmppath.replace(' ', '_')
                            self.fsconfig.append('%s %s %s %s' %
                                                 (tmppath, uid, gid, mode))
                        else:
                            self.fsconfig.append(
                                '%s %s %s %s' %
                                (self.DIR + entry_inode_path, uid, gid, mode))
                    else:
                        if cap == '':
                            tmppath = self.DIR + entry_inode_path
                            if (tmppath).find(' ', 1, len(tmppath)) > 0:
                                spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                                if not os.path.isfile(spaces_file):
                                    f = open(spaces_file,
                                             'tw',
                                             encoding='utf-8')
                                    self.__appendf(tmppath, spaces_file)
                                    f.close
                                else:
                                    self.__appendf(tmppath, spaces_file)
                                tmppath = tmppath.replace(' ', '_')
                                self.fsconfig.append('%s %s %s %s' %
                                                     (tmppath, uid, gid, mode))
                            else:
                                self.fsconfig.append(
                                    '%s %s %s %s' %
                                    (self.DIR + entry_inode_path, uid, gid,
                                     mode))
                            for fuk_symb in fuking_symbols:
                                tmppath = tmppath.replace(
                                    fuk_symb, '\\' + fuk_symb)
                            self.context.append('/%s %s' % (tmppath, con))
                        else:
                            if con == '':
                                tmppath = self.DIR + entry_inode_path
                                if (tmppath).find(' ', 1, len(tmppath)) > 0:
                                    spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                                    if not os.path.isfile(spaces_file):
                                        f = open(spaces_file,
                                                 'tw',
                                                 encoding='utf-8')
                                        self.__appendf(tmppath, spaces_file)
                                        f.close
                                    else:
                                        self.__appendf(tmppath, spaces_file)
                                    tmppath = tmppath.replace(' ', '_')
                                    self.fsconfig.append(
                                        '%s %s %s %s' %
                                        (tmppath, uid, gid, mode + cap))
                                else:
                                    self.fsconfig.append(
                                        '%s %s %s %s' %
                                        (self.DIR + entry_inode_path, uid, gid,
                                         mode + cap))
                            else:
                                tmppath = self.DIR + entry_inode_path
                                if (tmppath).find(' ', 1, len(tmppath)) > 0:
                                    spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                                    if not os.path.isfile(spaces_file):
                                        f = open(spaces_file,
                                                 'tw',
                                                 encoding='utf-8')
                                        self.__appendf(tmppath, spaces_file)
                                        f.close
                                    else:
                                        self.__appendf(tmppath, spaces_file)
                                    tmppath = tmppath.replace(' ', '_')
                                    self.fsconfig.append(
                                        '%s %s %s %s' %
                                        (tmppath, uid, gid, mode + cap))
                                else:
                                    self.fsconfig.append(
                                        '%s %s %s %s' %
                                        (self.DIR + entry_inode_path, uid, gid,
                                         mode + cap))
                                for fuk_symb in fuking_symbols:
                                    tmppath = tmppath.replace(
                                        fuk_symb, '\\' + fuk_symb)
                                self.context.append('/%s %s' % (tmppath, con))
                elif entry_inode.is_file:
                    try:
                        raw = entry_inode.open_read().read()
                    except:
                        continue
                    wdone = None
                    if os.name == 'nt':
                        if entry_name.endswith('/'):
                            entry_name = entry_name[:-1]
                        file_target = self.EXTRACT_DIR + entry_inode_path.replace(
                            '/', os.sep).replace(' ', '_')
                        if not os.path.isdir(os.path.dirname(file_target)):
                            os.makedirs(os.path.dirname(file_target))
                        with open(file_target, 'wb') as out:
                            out.write(raw)
                    if os.name == 'posix':
                        file_target = self.EXTRACT_DIR + entry_inode_path.replace(
                            ' ', '_')
                        if not os.path.isdir(os.path.dirname(file_target)):
                            os.makedirs(os.path.dirname(file_target))
                        with open(file_target, 'wb') as out:
                            out.write(raw)
                        os.chmod(file_target, int(mode, 8))
                        os.chown(file_target, uid, gid)
                    if cap == '' and con == '':
                        tmppath = self.DIR + entry_inode_path
                        if (tmppath).find(' ', 1, len(tmppath)) > 0:
                            spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                            if not os.path.isfile(spaces_file):
                                f = open(spaces_file, 'tw', encoding='utf-8')
                                self.__appendf(tmppath, spaces_file)
                                f.close
                            else:
                                self.__appendf(tmppath, spaces_file)
                            tmppath = tmppath.replace(' ', '_')
                            self.fsconfig.append('%s %s %s %s' %
                                                 (tmppath, uid, gid, mode))
                        else:
                            self.fsconfig.append(
                                '%s %s %s %s' %
                                (self.DIR + entry_inode_path, uid, gid, mode))
                    else:
                        if cap == '':
                            tmppath = self.DIR + entry_inode_path
                            if (tmppath).find(' ', 1, len(tmppath)) > 0:
                                spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                                if not os.path.isfile(spaces_file):
                                    f = open(spaces_file,
                                             'tw',
                                             encoding='utf-8')
                                    self.__appendf(tmppath, spaces_file)
                                    f.close
                                else:
                                    self.__appendf(tmppath, spaces_file)
                                tmppath = tmppath.replace(' ', '_')
                                self.fsconfig.append('%s %s %s %s' %
                                                     (tmppath, uid, gid, mode))
                            else:
                                self.fsconfig.append(
                                    '%s %s %s %s' %
                                    (self.DIR + entry_inode_path, uid, gid,
                                     mode))
                            for fuk_symb in fuking_symbols:
                                tmppath = tmppath.replace(
                                    fuk_symb, '\\' + fuk_symb)
                            self.context.append('/%s %s' % (tmppath, con))
                        else:
                            if con == '':
                                tmppath = self.DIR + entry_inode_path
                                if (tmppath).find(' ', 1, len(tmppath)) > 0:
                                    spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                                    if not os.path.isfile(spaces_file):
                                        f = open(spaces_file,
                                                 'tw',
                                                 encoding='utf-8')
                                        self.__appendf(tmppath, spaces_file)
                                        f.close
                                    else:
                                        self.__appendf(tmppath, spaces_file)
                                    tmppath = tmppath.replace(' ', '_')
                                    self.fsconfig.append(
                                        '%s %s %s %s' %
                                        (tmppath, uid, gid, mode + cap))
                                else:
                                    self.fsconfig.append(
                                        '%s %s %s %s' %
                                        (self.DIR + entry_inode_path, uid, gid,
                                         mode + cap))
                            else:
                                tmppath = self.DIR + entry_inode_path
                                if (tmppath).find(' ', 1, len(tmppath)) > 0:
                                    spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                                    if not os.path.isfile(spaces_file):
                                        f = open(spaces_file,
                                                 'tw',
                                                 encoding='utf-8')
                                        self.__appendf(tmppath, spaces_file)
                                        f.close
                                    else:
                                        self.__appendf(tmppath, spaces_file)
                                    tmppath = tmppath.replace(' ', '_')
                                    self.fsconfig.append(
                                        '%s %s %s %s' %
                                        (tmppath, uid, gid, mode + cap))
                                else:
                                    self.fsconfig.append(
                                        '%s %s %s %s' %
                                        (self.DIR + entry_inode_path, uid, gid,
                                         mode + cap))
                                for fuk_symb in fuking_symbols:
                                    tmppath = tmppath.replace(
                                        fuk_symb, '\\' + fuk_symb)
                                self.context.append('/%s %s' % (tmppath, con))
                elif entry_inode.is_symlink:
                    try:
                        link_target = entry_inode.open_read().read().decode(
                            "utf8")
                        target = self.EXTRACT_DIR + entry_inode_path.replace(
                            ' ', '_')
                        if cap == '' and con == '':
                            tmppath = self.DIR + entry_inode_path
                            if (tmppath).find(' ', 1, len(tmppath)) > 0:
                                spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                                if not os.path.isfile(spaces_file):
                                    f = open(spaces_file,
                                             'tw',
                                             encoding='utf-8')
                                    self.__appendf(tmppath, spaces_file)
                                    f.close
                                else:
                                    self.__appendf(tmppath, spaces_file)
                                tmppath = tmppath.replace(' ', '_')
                                self.fsconfig.append(
                                    '%s %s %s %s %s' %
                                    (tmppath, uid, gid, mode, link_target))
                            else:
                                self.fsconfig.append(
                                    '%s %s %s %s %s' %
                                    (self.DIR + entry_inode_path, uid, gid,
                                     mode, link_target))
                        else:
                            if cap == '':
                                tmppath = self.DIR + entry_inode_path
                                if (tmppath).find(' ', 1, len(tmppath)) > 0:
                                    spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                                    if not os.path.isfile(spaces_file):
                                        f = open(spaces_file,
                                                 'tw',
                                                 encoding='utf-8')
                                        self.__appendf(tmppath, spaces_file)
                                        f.close
                                    else:
                                        self.__appendf(tmppath, spaces_file)
                                    tmppath = tmppath.replace(' ', '_')
                                    self.fsconfig.append(
                                        '%s %s %s %s %s' %
                                        (tmppath, uid, gid, mode, link_target))
                                else:
                                    self.fsconfig.append(
                                        '%s %s %s %s %s' %
                                        (self.DIR + entry_inode_path, uid, gid,
                                         mode, link_target))
                                for fuk_symb in fuking_symbols:
                                    tmppath = tmppath = tmppath.replace(
                                        fuk_symb, '\\' + fuk_symb)
                                self.context.append('/%s %s' % (tmppath, con))
                            else:
                                if con == '':
                                    tmppath = self.DIR + entry_inode_path
                                    if (tmppath).find(' ', 1,
                                                      len(tmppath)) > 0:
                                        spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                                        if not os.path.isfile(spaces_file):
                                            f = open(spaces_file,
                                                     'tw',
                                                     encoding='utf-8')
                                            self.__appendf(
                                                tmppath, spaces_file)
                                            f.close
                                        else:
                                            self.__appendf(
                                                tmppath, spaces_file)
                                        tmppath = tmppath.replace(' ', '_')
                                        self.fsconfig.append(
                                            '%s %s %s %s %s' %
                                            (tmppath, uid, gid, mode + cap,
                                             link_target))
                                    else:
                                        self.fsconfig.append(
                                            '%s %s %s %s %s' %
                                            (self.DIR + entry_inode_path, uid,
                                             gid, mode + cap, link_target))
                                else:
                                    tmppath = self.DIR + entry_inode_path
                                    if (tmppath).find(' ', 1,
                                                      len(tmppath)) > 0:
                                        spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                                        if not os.path.isfile(spaces_file):
                                            f = open(spaces_file,
                                                     'tw',
                                                     encoding='utf-8')
                                            self.__appendf(
                                                tmppath, spaces_file)
                                            f.close
                                        else:
                                            self.__appendf(
                                                tmppath, spaces_file)
                                        tmppath = tmppath.replace(' ', '_')
                                        self.fsconfig.append(
                                            '%s %s %s %s %s' %
                                            (tmppath, uid, gid, mode + cap,
                                             link_target))
                                    else:
                                        self.fsconfig.append(
                                            '%s %s %s %s %s' %
                                            (self.DIR + entry_inode_path, uid,
                                             gid, mode + cap, link_target))
                                    for fuk_symb in fuking_symbols:
                                        tmppath = tmppath.replace(
                                            fuk_symb, '\\' + fuk_symb)
                                    self.context.append('/%s %s' %
                                                        (tmppath, con))
                        if os.path.islink(target):
                            try:
                                os.remove(target)
                            except:
                                pass
                        if os.path.isfile(target):
                            try:
                                os.remove(target)
                            except:
                                pass
                        if os.name == 'posix':
                            os.symlink(link_target, target)
                        if os.name == 'nt':
                            with open(target.replace('/', os.sep),
                                      'wb') as out:
                                tmp = bytes.fromhex('213C73796D6C696E6B3EFFFE')
                                for index in list(link_target):
                                    tmp = tmp + struct.pack(
                                        '>sx', index.encode('utf-8'))
                                out.write(tmp + struct.pack('xx'))
                                os.system('attrib +s "%s"' %
                                          target.replace('/', os.sep))
                        if not all(c in string.printable for c in link_target):
                            pass
                        if entry_inode_path[1:] == entry_name or link_target[
                                1:] == entry_name:
                            self.symlinks.append(
                                '%s %s' % (link_target, entry_inode_path[1:]))
                        else:
                            self.symlinks.append(
                                '%s %s' %
                                (link_target, self.DIR + entry_inode_path))
                    except:
                        try:
                            link_target_block = int.from_bytes(
                                entry_inode.open_read().read(), "little")
                            link_target = root_inode.volume.read(
                                link_target_block *
                                root_inode.volume.block_size,
                                entry_inode.inode.i_size).decode("utf8")
                            target = self.EXTRACT_DIR + entry_inode_path.replace(
                                ' ', '_')
                            if link_target and all(c in string.printable
                                                   for c in link_target):
                                if cap == '' and con == '':
                                    tmppath = self.DIR + entry_inode_path
                                    if (tmppath).find(' ', 1,
                                                      len(tmppath)) > 0:
                                        spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                                        if not os.path.isfile(spaces_file):
                                            f = open(spaces_file,
                                                     'tw',
                                                     encoding='utf-8')
                                            self.__appendf(
                                                tmppath, spaces_file)
                                            f.close
                                        else:
                                            self.__appendf(
                                                tmppath, spaces_file)
                                        tmppath = tmppath.replace(' ', '_')
                                        self.fsconfig.append(
                                            '%s %s %s %s %s' %
                                            (tmppath, uid, gid, mode,
                                             link_target))
                                    else:
                                        self.fsconfig.append(
                                            '%s %s %s %s %s' %
                                            (self.DIR + entry_inode_path, uid,
                                             gid, mode, link_target))
                                else:
                                    if cap == '':
                                        tmppath = self.DIR + entry_inode_path
                                        if (tmppath).find(
                                                ' ', 1, len(tmppath)) > 0:
                                            spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                                            if not os.path.isfile(spaces_file):
                                                f = open(spaces_file,
                                                         'tw',
                                                         encoding='utf-8')
                                                self.__appendf(
                                                    tmppath, spaces_file)
                                                f.close
                                            else:
                                                self.__appendf(
                                                    tmppath, spaces_file)
                                            tmppath = tmppath.replace(' ', '_')
                                            self.fsconfig.append(
                                                '%s %s %s %s %s' %
                                                (tmppath, uid, gid, mode,
                                                 link_target))
                                        else:
                                            self.fsconfig.append(
                                                '%s %s %s %s %s' %
                                                (self.DIR + entry_inode_path,
                                                 uid, gid, mode, link_target))
                                        for fuk_symb in fuking_symbols:
                                            tmppath = tmppath.replace(
                                                fuk_symb, '\\' + fuk_symb)
                                        self.context.append('/%s %s' %
                                                            (tmppath, con))
                                    else:
                                        if con == '':
                                            tmppath = self.DIR + entry_inode_path
                                            if (tmppath).find(
                                                    ' ', 1, len(tmppath)) > 0:
                                                spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                                                if not os.path.isfile(
                                                        spaces_file):
                                                    f = open(spaces_file,
                                                             'tw',
                                                             encoding='utf-8')
                                                    self.__appendf(
                                                        tmppath, spaces_file)
                                                    f.close
                                                else:
                                                    self.__appendf(
                                                        tmppath, spaces_file)
                                                tmppath = tmppath.replace(
                                                    ' ', '_')
                                                self.fsconfig.append(
                                                    '%s %s %s %s %s' %
                                                    (tmppath, uid, gid,
                                                     mode + cap, link_target))
                                            else:
                                                self.fsconfig.append(
                                                    '%s %s %s %s %s' %
                                                    (self.DIR +
                                                     entry_inode_path, uid,
                                                     gid, mode + cap,
                                                     link_target))
                                        else:
                                            tmppath = self.DIR + entry_inode_path
                                            if (tmppath).find(
                                                    ' ', 1, len(tmppath)) > 0:
                                                spaces_file = self.BASE_MYDIR + 'config' + os.sep + self.FileName + '_space.txt'
                                                if not os.path.isfile(
                                                        spaces_file):
                                                    f = open(spaces_file,
                                                             'tw',
                                                             encoding='utf-8')
                                                    self.__appendf(
                                                        tmppath, spaces_file)
                                                    f.close
                                                else:
                                                    self.__appendf(
                                                        tmppath, spaces_file)
                                                tmppath = tmppath.replace(
                                                    ' ', '_')
                                                self.fsconfig.append(
                                                    '%s %s %s %s %s' %
                                                    (tmppath, uid, gid,
                                                     mode + cap, link_target))
                                            else:
                                                self.fsconfig.append(
                                                    '%s %s %s %s %s' %
                                                    (self.DIR +
                                                     entry_inode_path, uid,
                                                     gid, mode + cap,
                                                     link_target))
                                            for fuk_symb in fuking_symbols:
                                                tmppath = tmppath.replace(
                                                    fuk_symb, '\\' + fuk_symb)
                                            self.context.append('/%s %s' %
                                                                (tmppath, con))
                                if os.name == 'posix':
                                    os.symlink(link_target, target)
                                if os.name == 'nt':
                                    with open(target.replace('/', os.sep),
                                              'wb') as out:
                                        tmp = bytes.fromhex(
                                            '213C73796D6C696E6B3EFFFE')
                                        for index in list(link_target):
                                            tmp = tmp + struct.pack(
                                                '>sx', index.encode('utf-8'))
                                        out.write(tmp + struct.pack('xx'))
                                        os.system('attrib +s %s' %
                                                  target.replace('/', os.sep))
                            else:
                                pass
                        except:
                            pass

        dir_my = self.BASE_MYDIR + 'config' + os.sep
        if not os.path.isdir(dir_my):
            os.mkdir(dir_my)

    # f = open(dir_my + self.FileName + '_pack.sh', 'tw', encoding='utf-8')
    # self.__appendf('make_ext4fs -T -1 -S ./file_contexts -C ./fs_config -l ' +str(os.path.getsize(self.OUTPUT_IMAGE_FILE))+ ' -a /'+self.FileName+' "$outdir"/'+self.FileName+'.new.img '+self.FileName+'', dir_my + self.FileName + '_pack.sh')
    # f.close()
    # f = open(dir_my + self.FileName + '_pack_sparse.sh', 'tw', encoding='utf-8')
    # self.__appendf('make_ext4fs -s -T -1 -S ./file_contexts -C ./fs_config -l ' +str(os.path.getsize(self.OUTPUT_IMAGE_FILE))+ ' -a /'+self.FileName+' "$outdir"/'+self.FileName+'.new.img '+self.FileName+'', dir_my + self.FileName + '_pack_sparse.sh')
    # f.close()
        f = open(dir_my + self.FileName + '_size.txt', 'tw', encoding='utf-8')
        self.__appendf(os.path.getsize(self.OUTPUT_IMAGE_FILE),
                       dir_my + self.FileName + '_size.txt')
        f.close()
        # f = open(dir_my + self.FileName + '_name.txt', 'tw', encoding='utf-8')
        # self.__appendf(os.path.basename(self.OUTPUT_IMAGE_FILE).replace(".img", ""), dir_my + self.FileName + '_name.txt')
        # f.close()
        with open(self.OUTPUT_IMAGE_FILE, 'rb') as file:
            root = ext4.Volume(file).root
            dirlist = []
            for file_name, inode_idx, file_type in root.open_dir():
                dirlist.append(file_name)
            dirr = self.__file_name(
                os.path.basename(
                    self.OUTPUT_IMAGE_FILE).split('.')[0])  #11.05.18
            setattr(self, 'DIR', dirr)
            scan_dir(root)
            for c in self.fsconfig:
                if dirr == 'vendor':
                    self.fsconfig.insert(0, '/' + ' 0 2000 0755')
                    self.fsconfig.insert(1, dirr + ' 0 2000 0755')
                    break
                else:
                    self.fsconfig.insert(0, '/' + ' 0 0 0755')
                    self.fsconfig.insert(1, dirr + ' 0 0 0755')
                    break
            self.__appendf(
                '\n'.join(self.fsconfig),
                self.BASE_MYDIR + 'config' + os.sep + fs_config_file)
            if self.context:  #11.05.18
                self.context.sort()  #11.05.18
                for c in self.context:
                    if re.search('lost..found', c):
                        self.context.insert(0, '/' + ' ' + c.split(" ")[1])
                        self.context.insert(
                            1, '/' + dirr + '(/.*)? ' + c.split(" ")[1])
                        self.context.insert(2,
                                            '/' + dirr + ' ' + c.split(" ")[1])
                        break
                for c in self.context:
                    if re.search('/system/system/build..prop ', c):
                        self.context.insert(
                            2, '/' + dirr + '/' + dirr + '(/.*)? ' +
                            c.split(" ")[1])
                        break
                self.__appendf('\n'.join(self.context), contexts)  #11.05.18