Adding automatic patching for semi-corrupted APKs#2298
Merged
skylot merged 3 commits intoskylot:masterfrom Oct 10, 2024
Merged
Conversation
jpstotz
reviewed
Oct 10, 2024
skylot
approved these changes
Oct 10, 2024
Owner
|
@qfalconer great work! 👍
This is actually a very good approach: it allows to easily fall back to default implementation if not needed and easy to improve current code as a drop-in replacement. This PR also fixes #2171 🎉 As a possible improvement for future: we can replace input files directly in JadxArgs, so all future processing uses patched file directly (now zip file opens and got patched 7 times because of how plugins processing files 🤣). |
This was referenced Oct 10, 2024
3 tasks
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Threat actors are yet again exploiting another lenient-ism in the Android APK loader machinery. I've recently found two APKs whose AndroidManifest.xml ZIP headers was semi-corrupted in a way that prevents ordinary tools (including Jadx) from correctly processing them. Android was loading them just fine, though.
Corruption of the headers
The APKs are attached in the ZIP below (password:
infected) and belongs to two (presumably) very different TAs: one comes from a campaign against Russians using OnlyFans as a bait, the other targets the user of an Italian bank. Thus, I'm inclined to believe this corruptions are intentional and will be used again in the future.Since APKs are just ZIPs, a little knowledge of the PKZIP format is required. These APKs have the following anomalies:
Jadx patch
This corruption can be undone, this PR attempts to introduce such undoing into Jadx. I'm not totally satisfied with the approach I took but it was the quickest.
Since the APK is visited everywhere with the standard
ZipFileclass, I opted for subclassing it, copying the original file into a temporary one and preprocessing it before calling thesuperconstructor with the path of the temporary file. This allows for a drop-in replacement ofjava.util.zip.ZipFilewithjadx.core.utils.files.ZipFile. And that's just what the code in this PR does.The drawback is that a temporary file is created if the APK is detected as corrupted (if it is not, no temporary file is created). The temporary file is not deleted since it couldn't find a way to store the temporary filename in the instance without hacky code (it will be deleted by the OS, though).
Note however that the APK signature verification panel shows an exception because the original file is passed to the Android framework and this time it cannot correctly process it. I don't know if this is a show stopper.
I also forgot to comment the code 😳.
If somebody have suggestions they are welcome.
How the APK is patched
For the sake of completeness, the patching is done as follow when an invalid compression method is found:
Before
After
semi-corrupted-apks.zip