Search code examples
androidpythonprintingpermissionsmanifest

Extract "permissions" from Manifest by Python


I want to print the "permissions" from a Android Manifest file. I have already reverse engineered the apk and got the Android Manifest file and want to extract the permissions by using Python.

import string
test=string.printable
f=open('AndroidManifest.xml', 'r').read()
x=""
for n in f:
    if n in test:
     x+=n
    print (x)

This is my Python Script by which I can parse the XML file. How can I find the permissions and print it. Can anyone help me with this?


Solution

  • XML parsing is a very common task and by writing a module to do that you are reinventing the wheel only to end up with a rough non-circular design.

    Python provides a much better designed, rounded wheel for that: xml.etree.ElementTree. You can use this module to effortlessly parse XML files.

    import xml.etree.ElementTree as ET
    
    root = ET.parse("AndroidManifest.xml").getroot()
    permissions = root.findall("uses-permission")
    
    for perm in permissions:
        for att in perm.attrib:
            print("{}\t:\t{}\n".format(att, perm.attrib[att]))
    

    This will print all the attributes of the <uses-permission> tag. Refer to the link above for more details.

    Note: For more details on the structure of the Manifest file, see the App Manifest page on Android Devs.