Esempio n. 1
0
def main(options, filename):
    if filename != None:
        buff = ""

        ret_type = androconf.is_android(filename)
        if ret_type == "APK":
            a = apk.APK(filename)
            dom = a.get_android_manifest_xml()
            #buff = a.get_android_manifest_xml().toprettyxml(encoding="utf-8")
            #a.get_activities()
            xml2parse(dom)
        elif ".xml" in filename:
            ap = apk.AXMLPrinter(open(filename, "rb").read())
            buff = minidom.parseString(
                ap.get_buff()).toprettyxml(encoding="utf-8")
        else:
            print "Unknown file type"
            return

        #if options.output != None :
        #    fd = codecs.open(options.output, "w", "utf-8")
        #    fd.write( buff )
        #    fd.close()
        #else :
        #    print buff

    elif options.version != None:
        print "Androaxml version %s" % androconf.ANDROGUARD_VERSION
 def check_phonegap_config_file(self):
     try:
         raw_file = self.apk.get_file("res/xml/config.xml")
         if not raw_file:
             raw_file = self.apk.get_file(
                 "res/xml/cordova.xml"
             )  # some versions use this file instead
         if not raw_file:
             raw_file = self.apk.get_file(
                 "res/xml/phonegap.xml")  # 1.5 or previous
         if not raw_file:
             return
         axml = apk.AXMLPrinter(raw_file)
         # problem xmlns="a" translates to xmlns:= which is error, removing xmlns
         xmlstring = re.sub('\sxmlns[^"]+"[^"]+"',
                            '',
                            axml.get_buff(),
                            count=1)
         root_config = ET.fromstring(xmlstring)
         self.check_phonegap_origin(root_config)
         self.check_phonegap_log_level(root_config)
     except:
         traceback.print_exc()
         # file not found, probably some old version that doesn't have whitelist
         pass
Esempio n. 3
0
def get_androidmanifest(apk_file):
    z=zipfile.ZipFile(apk_file,"r")
    for i in z.namelist():
        if i=="AndroidManifest.xml":
            ap=apk.AXMLPrinter(z.read(i))
            xml=parseString(ap.get_buff())
    return xml
Esempio n. 4
0
def main(options, arguments):
    if options.input != None:
        buff = ""

        ret_type = androconf.is_android(options.input)
        if ret_type == "APK":
            a = apk.APK(options.input)
            buff = a.get_android_manifest_xml().toprettyxml(encoding="utf-8")
        elif ".xml" in options.input:
            ap = apk.AXMLPrinter(read(options.input))
            buff = minidom.parseString(
                ap.get_buff()).toprettyxml(encoding="utf-8")
        else:
            print "Unknown file type"
            return

        if options.output != None:
            fd = codecs.open(options.output, "w", "utf-8")
            fd.write(buff)
            fd.close()
        else:
            print buff

    elif options.version != None:
        print "Androaxml version %s" % androconf.ANDROGUARD_VERSION
Esempio n. 5
0
    def create_axml(self, log, fileraw):
        """
        This method is called in order to create a new AXML object

        :param log: an object which corresponds to a unique app
        :param fileraw: the raw axml (a string)
        :rtype: an :class:`APK` object
        """
        return apk.AXMLPrinter(fileraw)
Esempio n. 6
0
    def testExtraNamespace(self):
        """
        Assert that files with a broken filesize are not parsed
        """
        filename = "examples/axml/AndroidManifestWrongFilesize.xml"

        with self.assertRaises(AssertionError) as cnx:
            with open(filename, "rb") as f:
                apk.AXMLPrinter(f.read())
        self.assertTrue(
            "Declared filesize does not match" in str(cnx.exception))
Esempio n. 7
0
    def testExtraNamespace(self):
        """
        Test if extra namespaces cause problems
        """
        filename = "examples/axml/AndroidManifestExtraNamespace.xml"

        with open(filename, "rb") as f:
            ap = apk.AXMLPrinter(f.read())
        self.assertIsInstance(ap, apk.AXMLPrinter)

        e = minidom.parseString(ap.get_buff())
        self.assertIsNotNone(e)
Esempio n. 8
0
    def testNonZeroStyleOffset(self):
        """
        Test if a nonzero style offset in the string section causes problems
        if the counter is 0
        """
        filename = "examples/axml/AndroidManifestNonZeroStyle.xml"

        with open(filename, "rb") as f:
            ap = apk.AXMLPrinter(f.read())
        self.assertIsInstance(ap, apk.AXMLPrinter)

        e = minidom.parseString(ap.get_buff())
        self.assertIsNotNone(e)
Esempio n. 9
0
    def testAXML(self):
        filenames = [
            "examples/axml/AndroidManifest-Chinese.xml",
            "examples/axml/AndroidManifest-xmlns.xml",
            "examples/axml/AndroidManifest.xml", "examples/axml/test.xml",
            "examples/axml/test1.xml", "examples/axml/test2.xml",
            "examples/axml/test3.xml"
        ]

        for filename in filenames:
            with open(filename, "rb") as fd:
                ap = apk.AXMLPrinter(fd.read())
                self.assertTrue(ap)
Esempio n. 10
0
    def run(self):
        self.view.set_name("%s.uaxml" % (self.filename))

        self.view.set_scratch(True)
        edit = self.view.begin_edit()
        self.view.sel().clear()
        self.view.set_syntax_file("Packages/XML/XML.tmLanguage")

        ap = apk.AXMLPrinter(self.raw_object)
        i_buffer = ap.get_xml()

        self.view.replace(edit, sublime.Region(0, self.view.size()), i_buffer)
        self.view.end_edit(edit)
        self.view.set_read_only(True)
Esempio n. 11
0
    def testAXML(self):
        filenames = [
            "examples/axml/AndroidManifest-Chinese.xml",
            "examples/axml/AndroidManifest-xmlns.xml",
            "examples/axml/AndroidManifest.xml", "examples/axml/test.xml",
            "examples/axml/test1.xml", "examples/axml/test2.xml",
            "examples/axml/test3.xml"
        ]

        for filename in filenames:
            with open(filename, "rb") as fd:
                ap = apk.AXMLPrinter(fd.read())
                self.assertIsNotNone(ap)

                e = minidom.parseString(ap.get_buff())
                self.assertIsNotNone(e)
Esempio n. 12
0
def androaxml_main(inp, outp=None):
    ret_type = androconf.is_android(inp)
    if ret_type == "APK":
        a = apk.APK(inp)
        axml = a.get_android_manifest_xml()
    elif ".xml" in inp:
        axml = apk.AXMLPrinter(read(inp)).get_xml_obj()
    else:
        print("Unknown file type")
        return

    buff = etree.tostring(axml, pretty_print=True, encoding="utf-8")
    if outp:
        with open(outp, "wb") as fd:
            fd.write(buff)
    else:
        print(buff.decode("UTF-8"))
Esempio n. 13
0
def main(options, arguments):
    if options.input is not None:
        ret_type = androconf.is_android(options.input)
        if ret_type == "APK":
            a = apk.APK(options.input)
            axml = a.get_android_manifest_xml()
        elif ".xml" in options.input:
            axml = apk.AXMLPrinter(read(options.input)).get_xml_obj()
        else:
            print("Unknown file type")
            return

        buff = etree.tostring(axml, pretty_print=True)
        if options.output:
            with open(options.output, "wb") as fd:
                fd.write(buff)
        else:
            print(buff.decode("UTF-8"))

    elif options.version is not None:
        print("Androaxml version %s" % androconf.ANDROGUARD_VERSION)
Esempio n. 14
0
def read_manifest(self, manifest):
    # 要求安装Androguard的androaxml.py
    pinfo('Now Getting AndroidManifest.xml from Androguard module')
    ap = apk.AXMLPrinter(read(manifest))
    buff = ap.get_buff()
    return buff
Esempio n. 15
0
#!/usr/bin/env python

import sys

PATH_INSTALL = "./"
sys.path.append(PATH_INSTALL)

from androguard.core.bytecodes import apk

from xml.dom import minidom

ap = apk.AXMLPrinter(open("examples/axml/AndroidManifest2.xml", "r").read())

print minidom.parseString(ap.getBuff()).toxml()
Esempio n. 16
0
    def fast_load_from_io(file_like_object=None,
                          apk_file_path=None,
                          calculate_hash=True):
        ''' Load a FastApk from file-like object or path by unzipping only the manifest file
        and calculating the hash.

        Parameters
        ----------
        file_like_object : file-like-object, optional (default is None)
            A file-like obj that points to the apk.
            If non given, try to open a file_like_object from the given `apk_file_path`.
        apk_file_path: str, optional (default is "not set")
            Path of apk
        calculate_hash : bool
            If true calculate the hash.
            This means the file has be to loaded completely into memory.
            If False, the hash will be calculated the first time it gets retrieved.

        Returns
        -------
        apk: FastApk

        Raises
        ------
        CouldNotOpenApk
            If the apk file could not be opened
        CouldNotReadManifest
            If the manifest file could not be read
        '''
        from androguard.core.bytecodes import apk as androapk
        from androguard.patch import zipfile

        # apk will be loaded from `flo` variable
        flo = file_like_object

        # indicates if file has been opened from path
        file_open_from_path = False
        # no file_like_object given, open file from path
        if file_like_object is None and isinstance(apk_file_path, str):
            try:
                flo = open(apk_file_path, "rb")
                file_open_from_path = True
            except IOError as e:
                flo.close()
                raise CouldNotOpenApk(apk_file_path,
                                      e), None, sys.exc_info()[2]

        # if file path not set, show at least that it's not seen in the exceptions
        if apk_file_path is None:
            apk_file_path = "not set"

        # load apk into memory and calculate hash if option set
        flo.seek(0)
        _hash = None
        if calculate_hash:
            data = flo.read()
            # calculate hash
            _hash = Util.sha256(data)
            flo.seek(0)

        try:
            if zipfile.is_zipfile(flo):
                z = zipfile.ZipFile(flo)
                # only read manifest from zip file
                binary_manifest = z.read(MANIFEST_FILENAME)
                ap = androapk.AXMLPrinter(binary_manifest)
                dom = minidom.parseString(ap.get_buff())
                manifest_tag = dom.getElementsByTagName(MANIFEST_TAG_NAME)
                # check that manifest tag is available
                if len(manifest_tag) > 0:

                    # get size of uncompresses .dex file
                    size_app_code = z.getinfo(COMPILED_APP_CODE).file_size
                    # get build date (last timestamp of classes.dex in zipfile)
                    build_date = datetime(
                        # use tuple from zipfile and pass the unpacked content to the constructor
                        *z.getinfo(COMPILED_APP_CODE).date_time)

                    manifest_items = manifest_tag[0].attributes
                    # use the namespace to ignore wrong prefixes like "ns0"
                    version_name = manifest_items.getNamedItemNS(
                        MANIFEST_NS, MANIFEST_VERSION_NAME).nodeValue
                    package = manifest_items.getNamedItem(
                        MANIFEST_PACKAGE).nodeValue
                    return FastApk(package,
                                   version_name,
                                   path=apk_file_path,
                                   _hash=_hash,
                                   size_app_code=size_app_code,
                                   build_date=build_date)
            raise CouldNotOpenManifest(apk_file_path), None, sys.exc_info()[2]
        except Exception as e:
            raise CouldNotOpenApk(apk_file_path,
                                  caused_by=e), None, sys.exc_info()[2]
        finally:
            # close file if manually opened from path
            if file_open_from_path:
                flo.close()
# This script can be decode apk encoded AndroidManifest.xml.
# pip install androguard
# python decode_apk_AndroidManifest_xml.py AndroidManifest.xml
# 
import sys
from androguard.core.bytecodes import apk

file_name = sys.argv[1]
filep = open(file_name)
axml_printer = apk.AXMLPrinter(filep.read())
android_xml = axml_printer.get_xml()
print(android_xml)
Esempio n. 18
0
#!/usr/bin/env python

import sys

PATH_INSTALL = "./"
sys.path.append(PATH_INSTALL)

from androguard.core.bytecodes import apk
from androguard.util import read


from xml.dom import minidom

ap = apk.AXMLPrinter( read("examples/axml/AndroidManifest2.xml", binary=False) )

print(minidom.parseString( ap.getBuff() ).toxml())
Esempio n. 19
0
#!/usr/bin/env python

import sys
import os
sys.path.append("..")
from androguard.core.bytecodes import apk
from androguard.util import read
import xml.dom.minidom

isAPK = True

if isAPK:
    apkFile = sys.argv[1]
    a = apk.APK(apkFile)
    manifest = a.get_android_manifest_xml()
    print manifest.toprettyxml()
else:
    axml = sys.argv[1]
    #apk.AXMLPrinter(read(axml))
    manifest = apk.AXMLPrinter(read(axml)).get_xml()
    print manifest

#print manifest.toprettyxml()
#out.write(manifest.toprettyxml())
#out.close()