| 5 |
| while True: |
| 6 | firstBytes = first.read(4096) |
| 7 | secondBytes = second.read(4096) |
| 8 | if firstBytes != secondBytes: |
| 9 | return False |
| 10 |
| 11 | if firstBytes == b"" and secondBytes == b"": |
| 12 | break |
| 13 |
| 14 | return True |
| 15 |
| 16 | def remove_prefix(text, prefix): |
| 17 | if text.startswith(prefix): |
| 18 | return text[len(prefix):] |
| 19 | return text |
| 20 |
| 21 | def compareApkFromBundle(bundle, apk): |
| 22 | FILES_TO_IGNORE = ["resources.arsc", "stamp-cert-sha256"] |
| 23 |
| 24 | apkZip = ZipFile(apk, 'r') |
| 25 | bundleZip = ZipFile(bundle, 'r') |
| 26 |
| 27 | firstList = list(filter(lambda info: info.filename not in FILES_TO_IGNORE, apkZip.infolist())) |
| 28 | secondList = list(filter(lambda secondInfo: secondInfo.filename not in FILES_TO_IGNORE, bundleZip.infolist())) |
| 29 |
| 30 | for apkInfo in firstList: |
| 31 | if (apkInfo.filename.startswith("META-INF/")): |
| 32 | continue |
| 33 | if (apkInfo.filename.startswith("res/")): |
| 34 | continue |
| 35 | if (apkInfo.filename.startswith("AndroidManifest.xml")): |
| 36 | continue |
| 37 |
| 38 | found = False |
| 39 | for bundleInfo in secondList: |
| 40 | fileName = bundleInfo.filename |
| 41 | fileName = remove_prefix(fileName, "base/root/") |
| 42 | fileName = remove_prefix(fileName, "base/dex/") |
| 43 | fileName = remove_prefix(fileName, "base/manifest/") |
| 44 | fileName = remove_prefix(fileName, "base/") |
| 45 | if (fileName.startswith("BUNDLE-METADATA")): |
| 46 | fileName = "META-INF" + remove_prefix(fileName, "BUNDLE-METADATA/") |
| 47 | if fileName == apkInfo.filename: |
| 48 | found = True |
| 49 | firstFile = apkZip.open(apkInfo, 'r') |
| 50 | secondFile = bundleZip.open(bundleInfo, 'r') |
| 51 | if compareFiles(firstFile, secondFile) != True: |
| 52 | print("APK file %s does not match" % apkInfo.filename) |
| 53 | return False |
| 54 | break |
| 55 |
| 56 | if found == False: |
| 57 | print("file %s not found in APK" % apkInfo.filename) |
| 58 | return False |
| 59 |
| 60 | return True |
| 61 |
| 62 | if __name__ == '__main__': |
| 63 | if len(sys.argv) != 3: |
| 64 | print("Usage: apkfrombundle <pathToBundle> <pathToApk>") |
| 65 | sys.exit(1) |
| 66 |
| 67 |
| 68 | if sys.argv[1] == sys.argv[2] or compareApkFromBundle(sys.argv[1], sys.argv[2]) == True: |
| 69 | print("APK from bundle!") |
| 70 | else: |
| 71 | print("APK has difference!") |
| 72 |