Esempio n. 1
0
    def testReplacement(self):
        """
        Test that the replacements for attributes, names and values are working
        :return:
        """
        # Fake, Empty AXML file
        a = axml.AXMLPrinter(b"\x03\x00\x08\x00\x24\x00\x00\x00"
                             b"\x01\x00\x1c\x00\x1c\x00\x00\x00"
                             b"\x00\x00\x00\x00\x00\x00\x00\x00"
                             b"\x00\x00\x00\x00"
                             b"\x00\x00\x00\x00\x00\x00\x00\x00")

        self.assertIsNotNone(a)

        self.assertEqual(a._fix_value(u"hello world"), u"hello world")
        self.assertEqual(a._fix_value(u"Foobar \u000a\u000d\u0b12"),
                         u"Foobar \u000a\u000d\u0b12")
        self.assertEqual(a._fix_value(u"hello \U00011234"),
                         u"hello \U00011234")
        self.assertEqual(a._fix_value(u"\uFFFF"), u"_")
        self.assertEqual(a._fix_value("hello\x00world"), u"hello")

        self.assertEqual(a._fix_name(u"foobar"), u"foobar")
        self.assertEqual(a._fix_name(u"5foobar"), u"_5foobar")
        self.assertEqual(a._fix_name(u"android:foobar"), u"foobar")
        self.assertEqual(a._fix_name(u"5:foobar"), u"_5_foobar")
Esempio n. 2
0
    def testAndroidManifest(self):
        filenames = [
            "examples/axml/AndroidManifest.xml",
            "examples/axml/AndroidManifest-Chinese.xml",
            "examples/axml/AndroidManifestDoubleNamespace.xml",
            "examples/axml/AndroidManifestExtraNamespace.xml",
            "examples/axml/AndroidManifest_InvalidCharsInAttribute.xml",
            "examples/axml/AndroidManifestLiapp.xml",
            "examples/axml/AndroidManifestMaskingNamespace.xml",
            "examples/axml/AndroidManifest_NamespaceInAttributeName.xml",
            "examples/axml/AndroidManifestNonZeroStyle.xml",
            "examples/axml/AndroidManifestNullbytes.xml",
            "examples/axml/AndroidManifestTextChunksXML.xml",
            "examples/axml/AndroidManifestUTF8Strings.xml",
            "examples/axml/AndroidManifestWithComment.xml",
            "examples/axml/AndroidManifest_WrongChunkStart.xml",
            "examples/axml/AndroidManifest-xmlns.xml",
        ]

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

            self.assertTrue(is_valid_manifest(ap.get_xml_obj()))

            e = minidom.parseString(ap.get_buff())
            self.assertIsNotNone(e)
Esempio n. 3
0
 def load_manifest(self, isbinary=False):
     if isbinary:
         with open(self.mxml, 'rb') as fp:
             axmlp = axml.AXMLPrinter(fp.read())
         etree = ET.fromstring(axmlp.get_xml())
     else:
         etree = ET.parse(self.mxml).getroot()
     self.appid = etree.get('package')
Esempio n. 4
0
 def testWrongStringPoolHeader(self):
     """Test if a wrong header type is rejected"""
     a = axml.AXMLPrinter(b"\x03\x00\x08\x00\x24\x00\x00\x00"
                          b"\xDE\xAD\x1c\x00\x1c\x00\x00\x00"
                          b"\x00\x00\x00\x00\x00\x00\x00\x00"
                          b"\x00\x00\x00\x00"
                          b"\x00\x00\x00\x00\x00\x00\x00\x00")
     self.assertFalse(a.is_valid())
Esempio n. 5
0
    def testWrongFilesize(self):
        """
        Assert that files with a broken filesize are not parsed
        """
        filename = "examples/axml/AndroidManifestWrongFilesize.xml"

        with open(filename, "rb") as f:
            a = axml.AXMLPrinter(f.read())
        self.assertFalse(a.is_valid())
Esempio n. 6
0
 def testWrongStringPoolSize(self):
     """Test if a wrong string pool header size is rejected"""
     a = axml.AXMLPrinter(b"\x03\x00\x08\x00\x2c\x00\x00\x00"
                          b"\x01\x00\x24\x00\x24\x00\x00\x00"
                          b"\x00\x00\x00\x00\x00\x00\x00\x00"
                          b"\x00\x00\x00\x00\x00\x00\x00\x00"
                          b"\x00\x00\x00\x00"
                          b"\x00\x00\x00\x00\x00\x00\x00\x00")
     self.assertFalse(a.is_valid())
Esempio n. 7
0
 def testWrongHeaderSize(self):
     """Test if a wrong header size is rejected"""
     a = axml.AXMLPrinter(b"\x03\x00\x10\x00\x2c\x00\x00\x00"
                          b"\x00\x00\x00\x00\x00\x00\x00\x00"
                          b"\x01\x00\x1c\x00\x1c\x00\x00\x00"
                          b"\x00\x00\x00\x00\x00\x00\x00\x00"
                          b"\x00\x00\x00\x00"
                          b"\x00\x00\x00\x00\x00\x00\x00\x00")
     self.assertFalse(a.is_valid())
Esempio n. 8
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:`~androguard.core.bytecodes.axml.AXMLPrinter` object
        """
        return axml.AXMLPrinter(fileraw)
Esempio n. 9
0
    def testWrongFilesize(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:
                axml.AXMLPrinter(f.read())
        self.assertTrue("Declared filesize does not match" in str(cnx.exception))
Esempio n. 10
0
    def testPackers(self):
        """
        Assert that Packed files are read
        """
        filename = "examples/axml/AndroidManifestLiapp.xml"

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

        self.assertTrue(ap.is_packed())
Esempio n. 11
0
    def testNonTerminatedString(self):
        """
        Test if non-null terminated strings are detected.
        This sample even segfaults aapt...
        """
        filename = "examples/axml/AndroidManifest_StringNotTerminated.xml"

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

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

        e = minidom.parseString(ap.get_buff())
        self.assertIsNotNone(e)
Esempio n. 13
0
    def testTextChunksWithXML(self):
        """
        Test for Text chunks containing XML
        """
        filename = "examples/axml/AndroidManifestTextChunksXML.xml"

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

        e = minidom.parseString(ap.get_buff())
        self.assertIsNotNone(e)
Esempio n. 14
0
    def testNullbytes(self):
        """
        Assert that Strings with nullbytes are handled correctly
        """
        filename = "examples/axml/AndroidManifestNullbytes.xml"

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

        e = minidom.parseString(ap.get_buff())
        self.assertIsNotNone(e)
Esempio n. 15
0
    def testDoubleNamespace(self):
        """
        Test if weird namespace constelations cause problems
        """
        filename = "examples/axml/AndroidManifestDoubleNamespace.xml"

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

        e = minidom.parseString(ap.get_buff())
        self.assertIsNotNone(e)
Esempio n. 16
0
    def testFileCompare(self):
        """
        Compare the binary version of a file with the plain text
        """
        binary = "examples/axml/AndroidManifest.xml"
        plain = "examples/android/TC/AndroidManifest.xml"

        with open(plain, "rb") as fp:
            x1 = etree.fromstring(fp.read())
        with open(binary, "rb") as fp:
            x2 = axml.AXMLPrinter(fp.read()).get_xml_obj()

        self.assertTrue(xml_compare(x1, x2, reporter=print))
Esempio n. 17
0
    def testMaskingNamespace(self):
        """
        Assert that Namespaces which are used in a tag and the tag is closed
        are actually correctly parsed.
        """
        filename = "examples/axml/AndroidManifestMaskingNamespace.xml"

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

        e = minidom.parseString(ap.get_buff())
        self.assertIsNotNone(e)
Esempio n. 18
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 = axml.AXMLPrinter(f.read())
        self.assertIsInstance(ap, axml.AXMLPrinter)

        e = minidom.parseString(ap.get_buff())
        self.assertIsNotNone(e)
Esempio n. 19
0
    def testNonManifest(self):
        filenames = [
            "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 fp:
                ap = axml.AXMLPrinter(fp.read())

            self.assertEqual(ap.get_xml_obj().tag, "LinearLayout")

            e = minidom.parseString(ap.get_buff())
            self.assertIsNotNone(e)
Esempio n. 20
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 = axml.AXMLPrinter(fd.read())
                self.assertIsNotNone(ap)

                e = minidom.parseString(ap.get_buff())
                self.assertIsNotNone(e)
Esempio n. 21
0
    def testReplacement(self):
        """
        Test that the replacements for attributes, names and values are working
        :return:
        """
        # Fake, Empty AXML file
        a = axml.AXMLPrinter(b"\x03\x00\x08\x00\x24\x00\x00\x00"
                             b"\x01\x00\x1c\x00\x1c\x00\x00\x00"
                             b"\x00\x00\x00\x00\x00\x00\x00\x00"
                             b"\x00\x00\x00\x00"
                             b"\x00\x00\x00\x00\x00\x00\x00\x00")

        self.assertIsNotNone(a)

        self.assertEqual(a._fix_value("hello world"), "hello world")
        self.assertEqual(a._fix_value("Foobar \u000a\u000d\u0b12"),
                         "Foobar \u000a\u000d\u0b12")
        self.assertEqual(a._fix_value("hello \U00011234"), "hello \U00011234")
        self.assertEqual(a._fix_value("\uFFFF"), "_")
        self.assertEqual(a._fix_value("hello\x00world"), "hello")

        self.assertEqual(a._fix_name('', 'foobar'), ('', 'foobar'))
        self.assertEqual(a._fix_name('', '5foobar'), ('', '_5foobar'))
        self.assertEqual(a._fix_name('', 'android:foobar'),
                         ('', 'android_foobar'))
        self.assertEqual(a._fix_name('', 'androiddd:foobar'),
                         ('', 'androiddd_foobar'))
        self.assertEqual(a._fix_name('', 'sdf:foobar'), ('', 'sdf_foobar'))
        self.assertEqual(a._fix_name('', 'android:sdf:foobar'),
                         ('', 'android_sdf_foobar'))
        self.assertEqual(a._fix_name('', '5:foobar'), ('', '_5_foobar'))

        self.assertEqual(
            a._fix_name('{http://schemas.android.com/apk/res/android}',
                        'foobar'),
            ('{http://schemas.android.com/apk/res/android}', 'foobar'))
        self.assertEqual(
            a._fix_name('{http://schemas.android.com/apk/res/android}',
                        '5foobar'),
            ('{http://schemas.android.com/apk/res/android}', '_5foobar'))
        self.assertEqual(
            a._fix_name('{http://schemas.android.com/apk/res/android}',
                        'android:foobar'),
            ('{http://schemas.android.com/apk/res/android}', 'android_foobar'))
        self.assertEqual(
            a._fix_name('{http://schemas.android.com/apk/res/android}',
                        'androiddd:foobar'),
            ('{http://schemas.android.com/apk/res/android}',
             'androiddd_foobar'))
        self.assertEqual(
            a._fix_name('{http://schemas.android.com/apk/res/android}',
                        'sdf:foobar'),
            ('{http://schemas.android.com/apk/res/android}', 'sdf_foobar'))
        self.assertEqual(
            a._fix_name('{http://schemas.android.com/apk/res/android}',
                        'android:sdf:foobar'),
            ('{http://schemas.android.com/apk/res/android}',
             'android_sdf_foobar'))
        self.assertEqual(
            a._fix_name('{http://schemas.android.com/apk/res/android}',
                        '5:foobar'),
            ('{http://schemas.android.com/apk/res/android}', '_5_foobar'))

        # Add a namespace mapping and try again
        def new_nsmap(self):
            return {
                "android": "http://schemas.android.com/apk/res/android",
                "something": "http://example/url"
            }

        setattr(axml.AXMLParser, 'nsmap', property(new_nsmap))

        self.assertEqual(a._fix_name('', 'foobar'), ('', 'foobar'))
        self.assertEqual(a._fix_name('', '5foobar'), ('', '_5foobar'))
        self.assertEqual(
            a._fix_name('', 'android:foobar'),
            ('{http://schemas.android.com/apk/res/android}', 'foobar'))
        self.assertEqual(a._fix_name('', 'something:foobar'),
                         ('{http://example/url}', 'foobar'))
        self.assertEqual(a._fix_name('', 'androiddd:foobar'),
                         ('', 'androiddd_foobar'))
        self.assertEqual(a._fix_name('', 'sdf:foobar'), ('', 'sdf_foobar'))
        self.assertEqual(
            a._fix_name('', 'android:sdf:foobar'),
            ('{http://schemas.android.com/apk/res/android}', 'sdf_foobar'))
        self.assertEqual(a._fix_name('', '5:foobar'), ('', '_5_foobar'))
        self.assertEqual(
            a._fix_name('{http://schemas.android.com/apk/res/android}',
                        'foobar'),
            ('{http://schemas.android.com/apk/res/android}', 'foobar'))
        self.assertEqual(
            a._fix_name('{http://schemas.android.com/apk/res/android}',
                        '5foobar'),
            ('{http://schemas.android.com/apk/res/android}', '_5foobar'))
        self.assertEqual(
            a._fix_name('{http://schemas.android.com/apk/res/android}',
                        'android:foobar'),
            ('{http://schemas.android.com/apk/res/android}', 'android_foobar'))
        self.assertEqual(
            a._fix_name('{http://schemas.android.com/apk/res/android}',
                        'androiddd:foobar'),
            ('{http://schemas.android.com/apk/res/android}',
             'androiddd_foobar'))
        self.assertEqual(
            a._fix_name('{http://schemas.android.com/apk/res/android}',
                        'sdf:foobar'),
            ('{http://schemas.android.com/apk/res/android}', 'sdf_foobar'))
        self.assertEqual(
            a._fix_name('{http://schemas.android.com/apk/res/android}',
                        'android:sdf:foobar'),
            ('{http://schemas.android.com/apk/res/android}',
             'android_sdf_foobar'))
        self.assertEqual(
            a._fix_name('{http://schemas.android.com/apk/res/android}',
                        '5:foobar'),
            ('{http://schemas.android.com/apk/res/android}', '_5_foobar'))
Esempio n. 22
0
 def _render_drawable_to_png(self, bxml, path):
     ap = axml.AXMLPrinter(bxml)
     print(ap.get_buff())
Esempio n. 23
0
 def testNoStringPool(self):
     """Test if a single header without string pool is rejected"""
     #                      |TYPE   |LENGTH |FILE LENGTH
     a = axml.AXMLPrinter(b"\x03\x00\x08\x00\x08\x00\x00\x00")
     self.assertFalse(a.is_valid())
Esempio n. 24
0
 def testTooSmallFile(self):
     """Test if a very short file is rejected"""
     #                      |TYPE   |LENGTH |FILE LENGTH
     a = axml.AXMLPrinter(b"\x03\x00\x08\x00\x08\x00\x00")
     self.assertFalse(a.is_valid())