Bypassing Android Anti-Emulation, Part (I)
TL;DR: I'll talk about some Android protections and a way to bypass the anti-emulation process using apktool and a java decompiler to gain the goal. This is the Part I and I'll talk about introductory topics
This is the first of a series of posts where we will focus in solving Android Reversing challenges. The challenge is focused on a binary protection called "anti-emulation", (you can find more info in the OWASP Top Ten 2014/2016 article:). In the upcoming entries we will talk about other protections like root checker, certificate pinning, anti-tampering, obfuscation techniques, along with ways to protect our app from differents tools (Xposed tool, Frida, etc).
I found this challenge on my personal repository. It came from a Blackhat course I took last year, thanks! @din3zh and @prateekg147.
The download link for the apk is and the sha1 signature is:
Before the analysis of the challenge itself I will introduce the concept of "Anti-Emulation" on Android. A good reference for this topic is the Mobile Security Testing Guide by OWASP. They show some examples about these techniques, and different ways to analyze them. There is also an API called SafetyNet, which is an Android API that creates a profile of the device using software and hardware information which is useful for checking different Android protections.
If we see inside the Emulator Detection Examples section, an application has several ways to detect the emulation process.
For example, by checking differents methods like "Build", "TelephonyManager","android.os.SystemProperties", "ro.product.device", "ro.kernel.qemu", etc. Depending on the response it can infer if it is running on a physical device in an Android Emulator. To check if the app has this implementation in place, we can try to obtain its code. This can be done through differents techniques and we can use some tools such as apktool, jadx or cfr, etc.
We will see how we can make use of some of those tools to obtain a really good approximation of the application code. For example, using apktool we can decode resources to nearly original form. We can even rebuild them after making some modifications. With “jadx" or "cfr" (boths java decompilers) we can analyze the "java code" obtained after the decompilation process. This practice, allows us to look at the code in more natural way, since the output from the java decompilers are ".java" files whereas the output from apktool are ".smali" code files.
I will not get into Java decompilers in this post, because it is a out of the scope. will simply use them to analyze the code for the application in the challenge. Then, we will modify the application from the .smali code. We will show how to use apktool to obtain a good an approximation of the code, to be able to modify it as we need to and then re-build it.
With this in mind, we will take a look at which is the process to create an APK file, since it will be useful to start trying to solve the challenge.
The process of creating an APK file:
- First, the developer creates its application in .java to then be compiled into into .class files.
- Once these .class files are created, they are converted into .dex (Dalvik EXecutables) files. These files contain byte code for the Dalvik Virtual Machine (DVM) which is a non-standar JVM that runs on Android devices.
- The DVM runs the DEX files while ART runs OAT (ELF) files.
- Some other XML files are converted to a binary format optimized for space.
- The last step is the APK creation from the .dex files, binary XML files and other resources needed to run the application and are packaged into an Android Package file (.apk).
- After the APK file is signed by the developer (we’ll come back to this in the "Manual patching with apktool" section), the APK is ready to be installed.
- If we want to look at the APK file, we can check its content by unpacking it, for example: $unzip -e example.apk -d example_folder
In short, the APK file is just a signed zip file that we can unzip them using the unzip command:
$unzip ReverseMe1.apk -d reverseme_unzipped
If we take a look at the manifest, we notice that the resources are encoded, we can use apktool to decode them later. $more AndroidManifest.xml
As we mentioned earlier, there are several checks that an application can perform in order to detect whether we are running it on an emulated environment or an actual device. Usually malware APKs have these kind of protections to avoid any analisis. Some common validations are listed here (anti-emulation process), along with some examples.
Below are some code examples of different validations that I have encountered on applications while writing this post:
Some validation methods are even called “isEmulator()”, “carrierNameFromTelephonyManager()”, or my personal favorite so far, “smellsLikeAnEmulator()”. All of them look for the same, or similar validations. They test with “equals”, “contains”, “startsWith” or “endsWith” against some hardcoded strings that can be interpreted as being set by an emulator. But they all look pretty much the same.
I asked myself why this happened? I google it and I had the answer, of course, the first result was a stackoverflow response.
I started looking into some others apps, and I found some many more quite similar implementations:
The difference with the previous set of validation methods is that, while the first set validates through “string comparisons”, the second one does by looking at the “Android system properties” to try to detect emulated environments.
Then, by simply analyzing the implementation methods, we can identify two main approaches to implement an anti-emulation protection. We can use this link.
Let’s take look at the “isEmulator()” example and their validations:
I wrote this reference table:
We can check them in a easy way using the following command in our computers with adb:╰─$ adb shell getprop ro.build.fingerprint generic/vbox86p/vbox86p:5.1/LMY47D/genymotion08250738:userdebug/test-keys
Basically we can use $adb shell getprop < key > to check the differents values.
Android System Properties validations:
Now that we know how to check for validation through strings, we can do the same with the Android System Properties validations.
Android has a set of properties about the device that can be read using the getprop command line utility, like we saw recently. Those System Properties are stored in a key value pair format in the property files (default.prop, local.prop, etc). And we'll read those to check the Anti-Emulation process.
If we want to understand more about the property files, using “adb shell cat default.prop” we can check the property output:
$adb shell cat default.prop
But if we returned to the previous image:
They are checking ro.hardware, ro.kernel.qemu, ro.serialno, ro.product.name, ro.product.model, ro.hardware, etc. We can check this output too using:
╰─$ adb shell getprop ro.product.name
╰─$ adb shell getprop ro.product.device
╰─$ adb shell getprop ro.product.model
Custom Phone - 5.1.0 - API 22 - 768x1280
╰─$ adb shell getprop ro.kernel.qemu
╰─$ adb shell getprop ro.hardware
╰─$ adb shell getprop qemu.hw.mainkeys
╰─$ adb shell getprop ro.bootloader
╰─$ adb shell getprop ro.bootmode
╰─$ adb shell getprop ro.secure
╰─$ adb shell getprop ro.build.fingerprint
╰─$ adb shell getprop ro.build.version.sdk
And again if the value of ro.secure is 1, the app is running on a emulator. The same with ro.kernel.qemu and the others.
Now is easy to understand which part of the code we need to modify to bypass the emulation process. We need to check all the implementations inside the code to bypass the application.
And we'll discuss that in the next entry. Follow me at @juanurss bye!!