Ejemplo n.º 1
0
    def get_android_resources(self):
        """
        Return the :class:`~androguard.core.bytecodes.axml.ARSCParser` object which corresponds to the resources.arsc file

        :rtype: :class:`~androguard.core.bytecodes.axml.ARSCParser`
        """
        try:
            return self.arsc["resources.arsc"]
        except KeyError:
            if "resources.arsc" not in self.zip.namelist():
                # There is a rare case, that no resource file is supplied.
                # Maybe it was added manually, thus we check here
                return None
            self.arsc["resources.arsc"] = ARSCParser(self.zip.read("resources.arsc"))
            return self.arsc["resources.arsc"]
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("PATH", help="Path of the resource file")
    args = parser.parse_args()

    ret_type = androconf.is_android(args.PATH)
    if ret_type == "APK":
        a = apk.APK(args.PATH)
        arscobj = a.get_android_resources()
        if not arscobj:
            print("The APK does not contain a resources file!", file=sys.stderr)
            sys.exit(0)
    elif ret_type == "ARSC":
        with open(args.PATH, 'rb') as fp:
            arscobj = ARSCParser(fp.read())
            if not arscobj:
                print("The resources file seems to be invalid!", file=sys.stderr)
                sys.exit(1)
    else:
        print("Unknown file type!", file=sys.stderr)
        sys.exit(1)

    xmltree = arscobj.get_public_resources(arscobj.get_packages_names()[0])
    x = etree.fromstring(xmltree)
    for elt in x:
        if elt.get('type') == 'string':
            val = arscobj.get_resolved_res_configs(int(elt.get('id')[2:], 16))[0][1]
            print('{}\t{}\t{}'.format(
                elt.get('id'),
                elt.get('name'),
Ejemplo n.º 3
0
 def extract_client_id(apk_file):
     with ZipFile(apk_file) as apk:
         with apk.open("resources.arsc") as res:
             return ARSCParser(res.read()).get_string("com.aspiro.tidal", "default_client_id")[1]
Ejemplo n.º 4
0
    def parse_manifest(self, manifest_file, resource_file):
        information = []

        apk_info = defaultdict(list)
        print "Parsing Resource XML"
        if resource_file is not None:
            self.resource_parser = arcParser = ARSCParser(resource_file)
            for p in arcParser.get_packages_names():
                apk_info['packages'].append(p)

                for locale in arcParser.get_locales(p):
                    for t in arcParser.get_types(p, locale):
                        for x in arcParser.values[p][locale][t]:
                            try:
                                if t == "public":
                                    (type, value, id) = x

                                    if isinstance(value, unicode):
                                        value = unidecode(value)

                                    information.append(
                                        self.createData("main",
                                                        "RESOURCE",
                                                        RESOURCE_VALUE=value,
                                                        RESOURCE_LOCALE=locale,
                                                        RESOURCE_PACKAGE=p,
                                                        RESOURCE_TYPE=t,
                                                        RESOURCE_TYPE2=type,
                                                        RESOURCE_ID=id))
                                elif len(x) == 2:
                                    (key, value) = x

                                    if isinstance(value, unicode):
                                        value = unidecode(value)

                                    information.append(
                                        self.createData("main",
                                                        "RESOURCE",
                                                        RESOURCE_VALUE=value,
                                                        RESOURCE_LOCALE=locale,
                                                        RESOURCE_PACKAGE=p,
                                                        RESOURCE_TYPE=t,
                                                        RESOURCE_KEY=key))
                                else:
                                    value = x[0]
                                    if isinstance(value, unicode):
                                        value = unidecode(value)

                                    information.append(
                                        self.createData("main",
                                                        "RESOURCE",
                                                        RESOURCE_VALUE=value,
                                                        RESOURCE_LOCALE=locale,
                                                        RESOURCE_PACKAGE=p,
                                                        RESOURCE_TYPE=t))
                            except Exception as e:
                                print x
                                print e

        print "Parsing Manifest XML"
        xmlPrinter = AXMLPrinter(manifest_file)

        root = xmlPrinter.get_xml_obj()

        # Get Permissions
        for e in root.findall('uses-permission'):
            attributes = self.extract_all_attributes(e)
            information.append(
                self.createData(
                    "main", "PERMISSION", **{
                        'PERMISSION_' + k.upper(): v
                        for k, v in attributes.items()
                    }))

        for e in root.findall('uses-permission-sdk-23'):
            attributes = self.extract_all_attributes(e)
            information.append(
                self.createData(
                    "main", "PERMISSION", **{
                        'PERMISSION_' + k.upper(): v
                        for k, v in attributes.items()
                    }))

        for e in root.findall('uses-feature'):
            attributes = self.extract_all_attributes(e)
            information.append(
                self.createData(
                    "main", "FEATURES", **{
                        'FEATURES_' + k.upper(): v
                        for k, v in attributes.items()
                    }))

        app = root.find('application')
        attributes = self.extract_all_attributes(app)
        information.append(
            self.createData(
                "main", "APP",
                **{'APP_' + k.upper(): v
                   for k, v in attributes.items()}))

        if app is not None:
            for e in app.findall('.//meta-data'):
                attributes = self.extract_all_attributes(e)
                information.append(
                    self.createData(
                        "main", "META", **{
                            'META_' + k.upper(): v
                            for k, v in attributes.items()
                        }))

            # for e in app.findall('uses-library'):
            #     attributes = self.extract_all_attributes(e)
            #     information.append(self.createData("main", "APK-USES-LIB" ,**attributes))

            for tagtype in ['activity', 'receiver', 'service']:
                for e in app.findall(tagtype):
                    attributes = self.extract_all_attributes(e)
                    information.append(
                        self.createData(
                            "main", tagtype.upper(), **{
                                tagtype.upper() + '_' + k.upper(): v
                                for k, v in attributes.items()
                            }))

                    for intent in e.findall('intent-filter'):
                        intentions = defaultdict(list)
                        for e2 in intent.getchildren():
                            # print e2.tag
                            attributes = self.extract_all_attributes(
                                e2, prefix=e2.tag + ".")
                            for k, v in attributes.iteritems():
                                intentions[tagtype.upper() + "_" + k].append(v)

                        information.append(
                            self.createData("main", tagtype.upper(),
                                            **intentions))

        return information