Пример #1
0
 def _init_dex_files(self):
     self.dex_files = []
     try:
         with apkfile.ZipFile(self.apk_path, 'r') as z:
             for name in z.namelist():
                 data = z.read(name)
                 if name.startswith('classes') and name.endswith('.dex') \
                         and Magic(data).get_type() == 'dex':
                     dex_file = DexFile(data)
                     self.dex_files.append(dex_file)
     except Exception as ex:
         raise ex
def extract_feature_from_APK(path, permission_dict, file_type='manifest'):
    '''This function extract features of interest takes as 
    Input: 
        the path : String, path where the zipfiles are
        permission_dict : Dictionary; containing all the permissions
        the type : String in ['manifest','dex']
    Output:
        return a dataframe containing the requested features
    '''
    if file_type == 'manifest':

        df = pd.DataFrame(columns=sorted(
            list(permission_dict.keys())).insert(0, "id"))
    else:
        df = pd.DataFrame()

    i = 0

    for _, _, f in os.walk(path):
        for file in f:
            i = i + 1
            if i == 8:
                break

            apk_path = path+file
            try:
                with apkfile.ZipFile(apk_path, 'r') as unzip_file:
                    for name in unzip_file.namelist():
                        if file_type == 'manifest' and name.startswith('AndroidManifest') and name.endswith('.xml'):
                            try:
                                data = unzip_file.read(name)
                                axml = AXML(data).get_xml()
                                dat = xmltodict.parse(axml, False)['manifest']
                                print(name + " " + str(i))
                                features = generate_xml_features(
                                    dat, file, permission_dict)
                                print(features)
                                df = pd.concat([df, features])
                                print(name + " " + str(i))
                            except Exception as e:
                                print("manifest error", e)

                        elif file_type == 'dex' and name.startswith('classes') and name.endswith('.dex'):
                            try:
                                data = unzip_file.read(name)
                                dat = DexFile(data)
                                print(name + " " + str(i))
                                features = generate_dex_features(dat, file)

                                df = pd.concat(
                                    [df, features], ignore_index=True)

                                print(name + " " + str(i))
                            except Exception as e:
                                print("dex error", e)
                        else:
                            pass
            except Exception as e:
                print("ERROR:")
                print(e)
                pass

    return df