From f7f9363423ac76e0384ff4142e9538eaef1f70e3 Mon Sep 17 00:00:00 2001 From: xaxtix Date: Tue, 24 Jan 2023 22:08:20 +0400 Subject: [PATCH] add apkfrombundle.py --- apkfrombundle.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 apkfrombundle.py diff --git a/apkfrombundle.py b/apkfrombundle.py new file mode 100644 index 000000000..aacfa887c --- /dev/null +++ b/apkfrombundle.py @@ -0,0 +1,71 @@ +import sys +from zipfile import ZipFile + +def compareFiles(first, second): + while True: + firstBytes = first.read(4096); + secondBytes = second.read(4096); + if firstBytes != secondBytes: + return False + + if firstBytes != b"" or secondBytes != b"": + break + + return True + +def remove_prefix(text, prefix): + if text.startswith(prefix): + return text[len(prefix):] + return text + +def compareApkFromBundle(bundle, apk): + FILES_TO_IGNORE = ["META-INF/MANIFEST.MF", "META-INF/CERT.RSA", "META-INF/CERT.SF", "resources.arsc"] + + apkZip = ZipFile(apk, 'r') + bundleZip = ZipFile(bundle, 'r') + + firstList = list(filter(lambda info: info.filename not in FILES_TO_IGNORE, apkZip.infolist())) + secondList = list(filter(lambda secondInfo: secondInfo.filename not in FILES_TO_IGNORE, bundleZip.infolist())) + + for apkInfo in firstList: + if (apkInfo.filename.startswith("META-INF/")): + continue + if (apkInfo.filename.startswith("res/")): + continue + if (apkInfo.filename.startswith("AndroidManifest.xml")): + continue + + found = False + for bundleInfo in secondList: + fileName = bundleInfo.filename + fileName = remove_prefix(fileName, "base/root/") + fileName = remove_prefix(fileName, "base/dex/") + fileName = remove_prefix(fileName, "base/manifest/") + fileName = remove_prefix(fileName, "base/") + if (fileName.startswith("BUNDLE-METADATA")): + fileName = "META-INF" + remove_prefix(fileName, "BUNDLE-METADATA/") + if fileName == apkInfo.filename: + found = True + firstFile = apkZip.open(apkInfo, 'r') + secondFile = bundleZip.open(bundleInfo, 'r') + if compareFiles(firstFile, secondFile) != True: + print("APK file %s does not match" % apkInfo.filename) + return False + break + + if found == False: + print("file %s not found in APK" % apkInfo.filename) + return False + + return True + +if __name__ == '__main__': + if len(sys.argv) != 3: + print("Usage: apkfrombundle ") + sys.exit(1) + + + if sys.argv[1] == sys.argv[2] or compareApkFromBundle(sys.argv[1], sys.argv[2]) == True: + print("APKs are from bundle!") + else: + print("APKs are different!")