update: reject APKs with invalid file sig, probably Janus exploits

This just checks the first four bytes of the APK file, aka the "file
signature", to make sure it is the ZIP signature and not the DEX signature.
This was checked against the test APK, and I ran it against some known
malware and all of f-droid.org to make sure it works.

All valid ZIP files (therefore APK files) should start with the ZIP
Local File Header of four bytes.

https://www.guardsquare.com/en/blog/new-android-vulnerability-allows-attackers-modify-apps-without-affecting-their-signatures
This commit is contained in:
Hans-Christoph Steiner 2017-12-11 18:36:21 +01:00
parent 5ce950e748
commit bde0558d82
4 changed files with 40 additions and 2 deletions

View file

@ -496,8 +496,10 @@ def has_known_vulnerability(filename):
Checks whether there are more than one classes.dex or AndroidManifest.xml
files, which is invalid and an essential part of the "Master Key" attack.
http://www.saurik.com/id/17
Janus is similar to Master Key but is perhaps easier to scan for.
https://www.guardsquare.com/en/blog/new-android-vulnerability-allows-attackers-modify-apps-without-affecting-their-signatures
"""
found_vuln = False
@ -506,6 +508,13 @@ def has_known_vulnerability(filename):
if not hasattr(has_known_vulnerability, "pattern"):
has_known_vulnerability.pattern = re.compile(b'.*OpenSSL ([01][0-9a-z.-]+)')
with open(filename.encode(), 'rb') as fp:
first4 = fp.read(4)
if first4 != b'\x50\x4b\x03\x04':
raise FDroidException(_('{path} has bad file signature "{pattern}", possible Janus exploit!')
.format(path=filename, pattern=first4.decode().replace('\n', ' ')) + '\n'
+ 'https://www.guardsquare.com/en/blog/new-android-vulnerability-allows-attackers-modify-apps-without-affecting-their-signatures')
files_in_apk = set()
with zipfile.ZipFile(filename) as zf:
for name in zf.namelist():