Exemplo n.º 1
0
 def _init_org_manifest(self):
     ANDROID_MANIFEST = "AndroidManifest.xml"
     try:
         with apkfile.ZipFile(self.apk_path, mode="r") as zf:
             if ANDROID_MANIFEST in zf.namelist():
                 data = zf.read(ANDROID_MANIFEST)
                 try:
                     axml = AXML(data)
                     if axml.is_valid:
                         self.org_manifest = axml.get_xml()
                 except Exception as e:
                     raise e
     except Exception as e:
         raise e
Exemplo n.º 2
0
    def _init_org_manifest(self):
        ANDROID_MANIFEST = "AndroidManifest.xml"
        try:
            with apkfile.ZipFile(self.apk_path, mode="r") as zf:
                if ANDROID_MANIFEST in zf.namelist():
                    data = zf.read(ANDROID_MANIFEST)
                    try:
                        axml = AXML(data)
                        if axml.is_valid:
                            self.org_manifest = axml.get_xml()
                    except Exception as e:
                        raise e
        except Exception as e:
            raise e

        # fix manifest
        self.org_manifest = re.sub(r'\s:(="[\w]*?\.[\.\w]*")',
                                   r' android:name\1', self.org_manifest)
Exemplo n.º 3
0
    def _init_manifest(self):
        ANDROID_MANIFEST = "AndroidManifest.xml"
        try:
            with apkfile.ZipFile(self.apk_path, mode="r") as zf:
                if ANDROID_MANIFEST in zf.namelist():
                    data = zf.read(ANDROID_MANIFEST)
                    try:
                        axml = AXML(data)
                        if axml.is_valid:
                            self.org_manifest = axml.get_xml()
                    except Exception as e:
                        raise e

                    if self.org_manifest:
                        try:
                            self.manifest = xmltodict.parse(
                                self.org_manifest, False)['manifest']
                        except xml.parsers.expat.ExpatError as e:
                            print(self.apk_path, e)
                        except Exception as e:
                            raise e
        except Exception as e:
            raise e
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
Exemplo n.º 5
0
 def getManifest(self):
     return AXML(self._file.read('AndroidManifest.xml')).get_xml_obj()