text
stringlengths
0
1.99k
+---------+----+-----+ +--+---------+
| | |
| | | +-------------+
| +-----------+----> mapping.txt |
| | +-------------+
| |
| | above is accessible to developers
----------+----------------+-----------
| | below is running on devices
| |
+---------+----------------+---------+
| APK | | |
| v v |
| classes.dex nativelib.so |
| | | |
+---------+----------------+---------+
| |
+---------+----------------+---------+
| ART | | |
| +----v-----+ +--v--+ |
| | dex2oat | | JNI | |
| | JIT | +-----+ |
| | + | +----------+ |
| | AOT----+-----> app.odex | |
| | | +----------+ |
| +----------+ |
+------------------------------------+
```
--[ 2 - A Bit of the Current State
> What's in a name? An APK by any other name would smell of Java still.
---[ 2.1 - Building Android Apps and "Obfuscation"
While ProGuard is not new, its obfuscation capacities in the Android SDK
are limited to renaming identifiers of classes, methods, and fields. If an
app crashes and the stack trace is collected, this renaming would make it
inconvenient to debug, so a mapping.txt is created for the developer to
translate the "obfuscated" names back to the original ones. Sadly, we can't
find this in an APK, and if renaming took place, we can't trust the class
and method identifiers.
In some cases, this renaming should not be applied, for example, when
reflection is used to dynamically refer to a class by its name. This can
happen when Java/Kotlin code is called from native libraries. Since the NDK
and D8/R8 are mostly unaware of each other's internals, these names must
stay the same. Developers can exclude identifiers from renaming with the
appropriate entries in the ProGuard configuration.
In addition to ProGuard, the full capabilities of the R8 compiler further
aggressively optimize the code, which means two things: First, unused (or
"dead") code is removed from the app. This mainly affects libraries as they
can provide much more functionality in their precompiled .jar file than an
app uses. Second, methods can disappear due to inlining. If the compiler
can verify that a method is only ever called at specific places, it can
decide not to call the method from these places, but instead include the
target methods code where it would be called. This reduces the number of
unique methods (which is limited per DEX file!) and allows for additional
optimizations.
While renaming and compiler optimizations are not actual obfuscation, the
official docs called it such until very recently [4]. Shoutout to whoever
finally removed the example config of two lines containing two unrelated
bugs that would stop compilation, it's been bugging us for a couple years.
---[ 2.2 - Similarity-Calculating Tools
Some tools for app similarity calculation are unusable when class and
method identifiers are changed. SimiDroid [5] uses names to match methods
and then compares their instructions, classifying them as full, partial, or
non-matches. The ratio of the resulting sets is then used to calculate an
overall similarity.
Diffuse [6] also uses method names for matches and could use mapping.txt
files to account for obfuscation. Unfortunately, only developers have
those, but we'd like to compare any APKs we can get our hands on, sources
be damned.
Dexofuzzy [7] implements a fuzzy hash based on code that is robust against
small changes but not against structural ones -- eliminating large parts of
dead library code or inlining many methods results in entirely different
hashes.
Finally, Elsim [1] uses Normalized Compression Distance (NCD) to provide a
"rip-off indicator." It was still available when we started looking at this
topic but had trouble analyzing bigger apps. In some cases, we had to stop
the comparison after multiple days without a result. We pushed some
optimizations, but it still was too slow to analyze modern apps with
hundreds of thousands of methods.
Various approaches are described in academia, but since they do not provide
a PoC, they can Give Time For Others. In practice, the standard Android SDK
optimizations are indistinguishable from obfuscations for available
similarity tools.
---[ 2.3 - Dalvik is the Limit
The trouble with analyzing Android apps is that we must deal with Dalvik
bytecode. Since it effectively exists only in the Android ecosystem, any