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).
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.
Jadx challenge interpretation:
If we install the application inside the emulator and run it, we will see something similar to the screenshot below.. If we write some alphanumeric input a warning stating «This Devices is not supported» will appear. Since we don’t know why this happened, we can use jadx to obtain the .java code and use it as a starting point to determine the reason.
Of course, we can also use apktool or unzip the APK file to know more about the application, and maybe obtain some other kind of information. In this approach, we will focus on the .java code and try to understand the application workflow.
To decompile the APK, using jadx is enough for this challenge, although there are lots of Java decompilers out there that we could also use.
We can see some errors and warnings in the images above, but for the purpose of this post they’re not important. Once the decompilation process has finished, the tool should have created a folder with all the decompiled files, which look like this:
If we look for the text with the warning we saw earlier, we’ll find a «toast», which is a view containing a quick little message for the user. The toast class helps you create and manage them. We can also note that the message is shown depending on the value returned by «ChallengeJNI.this.checkIfDeviceIsEmulator().booleanValue()».
What do you think about this line?? :).
Let’s take a look at the implementation of the «checkIfDeviceIsEmulator()» function:
Basically what it is doing is checking some strings against a set of predefined strings, like we saw in the “Anti-Emulation Checks” before. Now we will try to bypass them.
Apktool challenge interpretation:
Like we already saw, we need to modify the checkIfDeviceIsEmulator() function in order to bypass the application’s validation, so now we are going to use apktool to do that.
Apktool patching and reversing engineering:
After we have installed apktool, we can check the options tool. For now we will focus on the decode (‘d’) and build (‘b’) options. Apktool needs an input .apk, which in this case is the one from the challenge we are trying to solve.
To decode the application execute the following command:
$apktool d ReverseMe1.apk -output reverseme_apktool
We can see the internal structure of the decoded APK, the AndroidManifest.xml file and the differents folders like the smali code. Is important to remember the normal APK structure.
- smali — disassembled java code
- res — resources, strings
- assets — files bundled inside the APK
- lib — native libraries (*.so files)
- AndroidManifest.xml — decoded version
- original and apktool.yml — used by apktool
After decoding the app, we can see the AndroidManifest.xml.
If we look inside the Smali folder we can see all the smali files
$more ChallengeJNI\$1.smali$more ChallengeJNI.smali
As we can see, working with smali code is harder than with java, so we will move to java decompilers to analyze and interpreter the application code. And after that, we will modify the application to obtain the bypass’ smali code and re build the application. To do that we will make use of some dalvik opcodes.
Understanding dalvik opcodes:
This link is really useful, I used it to create a table showing some of the most interesting examples from the “dalvik opcodes” used by the application.
Something that we will see very often in the code is a line like this:
“.method private checkIfDeviceIsEmulator ()Ljava/lang/Boolean;”
It’s important to understand the meaning of this, so let’s break it down:
- “.method private” -> is the type of method.
- checkIfDeviceIsEmulator -> the method name.
- ()Ljava/lang/Boolean; -> the type of the return value, prefixed with L, dots “.” replaced with slashes “/” and suffixed with semicolon ;