I. Vulnerability description
The vulnerability was first detected and submitted to Google by the Chinese "android Security Team" (http://blog.sina.com.cn/u/3194858670), Google quickly fixed the vulnerability, corresponding to bug 9695860. This vulnerability is called bluebox
Security reports that the Android system successfully bypasses the application signature authentication vulnerability and then bypasses the android signature authentication vulnerability. This vulnerability works slightly differently from the bluebox security vulnerability. 2. Theoretically, the affected device will affect all devices before the vulnerability is fixed.
Iii. Vulnerability principles
Understanding the vulnerability requires some knowledge of the ZIP format (http://www.pkware.com/documents/casestudies/APPNOTE.TXT ).
Each zip file has a central directory, and each item in the central directory is a file header. The structure of this file header corresponds to the class of Android code, which is zipentry. The file header structure has an offset pointing to the local file header. The local file header is followed by file data. Next, let's take a closer look at the local
Structure of file header:
Local file header signature 4 bytes (0x04034b50)
Version needed to extract 2 bytes
General purpose Bit Flag 2 bytes
Compression Method 2 bytes
Last MOD file time 2 bytes
Last MOD file date 2 bytes
Crc-32 4 bytes
Compressed size 4 bytes
Uncompressed size 4 bytes
File Name Length 2 bytes
Extra field length 2 bytes
File Name (variable size)
Extra field (variable size)
We can see that except the last two fields, the other fields of the local file header are fixed. The length of the two variable length fields is determined by the file name length and extra field length. Again, next to the extra field is the file data.
How does Android perform APK verification?
Android adjusts to the public inputstream getinputstream (zipentry entry) function of zipfile during APK file verification. In this function, there is such a section:
Rafstream rafstrm = new rafstream (RAF, entry. mlocalheaderreloffset + 28 );
Datainputstream is = new datainputstream (rafstrm );
Int localextralenorwhatever = short. reversebytes (is. readshort ());
Is. Close ();
// Skip the name and this "extra" data or whatever it is:
Rafstrm. Skip (entry. namelength + localextralenorwhatever );
Rafstrm. mlength = rafstrm. moffset + entry. compressedsize;
If (entry. compressionmethod = zipentry. deflated ){
Int bufsize = math. Max (1024, (INT) math. Min (entry. getsize (), 65535l ));
Return new zipinflaterinputstream (rafstrm, new Inflater (true), bufsize, entry );
} Else {
Return rafstrm;
}
Note: The red part in the above Code. Localextralenorwhatever is the extra field length in the local file header structure. What if the extra filed length is greater than 2 ^ 15?
Yes, localextralenorwhatever will be a negative value. Therefore, rafstrm. Skip (entry. namelength + localextralenorwhatever) will not be able to skip the variable length domain file name (variable size) and extra field (variable size ). Instead, it may jump to file name (variable size) or even before file name (variable size. Of course, for the convenience of attacks, we still expect it to jump to file
Name (variable size.
Vulnerability attack methods:
To change the behavior of an APK, it is clear that the target of the attack is the classes. Dex file in the APK. For the local file header structure of the classes. Dex file in the APK file, the content of the file name (variable size) field must be "classes. Dex. Note that the suffix Dex here is exactly the same as the three bytes starting with the DEX file (For details, refer to the DEX file format ).
A) With this, we can write a complete Dex file after the file name (variable size) domain "classex. Dex. The Dex file must be the classes. Dex file in the original APK. Only in this way can signature verification be bypassed.
B) modify the extra field length to 0 xfffd. Because the value is-3. According to the vulnerability, rafstrm. Skip (entry. namelength + localextralenorwhatever) will jump to "." In the file name (variable size) field. That is, the beginning of a DEX file, which must be the content of the original Dex file.
C) modify the file data after the local file header. Write classes. Dex with attack code here.
D) the above modifications will bring about some structural adjustments to the APK file, such as expanding the extra field and adjusting the file data size.
Specific Attack models, such.
In general, this attack method first utilizes android to read the corresponding 16-bit domain of the ZIP file during signature verification, without considering the situation where it is larger than 2 ^ 15. (Because Java int, short, and long are all signed numbers, unlike C/C ++ ).
Second, the extra field of the local file header structure in the ZIP file is used to store the original classes. Dex. However, the maximum size of this domain is 2 ^ 16-1. Therefore, the classes. Dex size in the attacked APK must be within 64 KB. Otherwise, it cannot be attacked. This is a limitation of this attack method.
There is another problem: the reason for the successful attack is that the system extracts hacked classes at runtime. dex, while the classes in the extra domain are verified during signature verification. dex. The former is implemented in libdex. So, and the latter is implemented at the Java layer. This is caused by inconsistent Java and native layers.
4. Related http://www.androidpolice.com/2013/07/11/second-all-access-apk-exploit-is-revealed-just-two-days-after-master-key-goes-public-already-patched-by-google/