Last active
March 19, 2023 11:21
-
-
Save rezzmk/963bd199d29c9465051170c768dddf1c to your computer and use it in GitHub Desktop.
Python Parse AndroidManifest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as ET | |
import sys | |
def parse(file_path): | |
tree = ET.parse(file_path) | |
root = tree.getroot() | |
ns = {"android": "http://schemas.android.com/apk/res/android"} | |
package_name = root.attrib["package"] | |
app = root.find("application") | |
allow_backup = app.attrib.get(f"{{{ns['android']}}}allowBackup") | |
debuggable = app.attrib.get(f"{{{ns['android']}}}debuggable") | |
test_only = app.attrib.get(f"{{{ns['android']}}}testOnly") | |
permissions = [elem.attrib[f"{{{ns['android']}}}name"] for elem in root.findall("uses-permission")] | |
activities = [activity.attrib[f"{{{ns['android']}}}name"] for activity in app.findall("activity")] | |
services = [service.attrib[f"{{{ns['android']}}}name"] for service in app.findall("service")] | |
receivers = [receiver.attrib[f"{{{ns['android']}}}name"] for receiver in app.findall("receiver")] | |
providers = [provider.attrib[f"{{{ns['android']}}}name"] for provider in app.findall("provider")] | |
intent_filters = [] | |
actions = [] | |
categories = [] | |
data_elements = [] | |
for component in app.findall("*"): | |
for intent_filter in component.findall("intent-filter"): | |
actions = [action.attrib[f"{{{ns['android']}}}name"] for action in intent_filter.findall("action")] | |
categories = [category.attrib[f"{{{ns['android']}}}name"] for category in intent_filter.findall("category")] | |
data_elements = [{k.replace(f"{{{ns['android']}}}", ""): v} for d in intent_filter.findall("data") for k, v in d.attrib.items()] | |
intent_filters.append({"component": component.tag, "name": component.attrib[f"{{{ns['android']}}}name"], "actions": actions, "categories": categories, "data": data_elements}) | |
metadata = [{k.replace(f"{{{ns['android']}}}", ""): v} for meta in app.findall("meta-data") for k, v in meta.attrib.items()] | |
return { | |
"package_name": package_name, | |
"allow_backup": allow_backup, | |
"debuggable": debuggable, | |
"test_only": test_only, | |
"permissions": permissions, | |
"activities": activities, | |
"services": services, | |
"receivers": receivers, | |
"providers": providers, | |
"metadata": metadata, | |
"actions": actions, | |
"categories": categories, | |
"data_elements": data_elements | |
} | |
if __name__ == "__main__": | |
manifest_file_path = sys.argv[1] | |
parsed = parse(manifest_file_path) | |
for key, value in parsed.items(): | |
print(f"{key.capitalize()}:") | |
if isinstance(value, list): | |
if value: | |
for item in value: | |
print(f"- {item}") | |
else: | |
print(value) | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment