Good afternoon. Our 1 o'clock speaker is Doki Doki on One Step Before Game Hackers. Hello. Thanks for coming here. My name is Young Sung and I'm delighted to be here to talk in DEF CON. Today I'm going to talk about Android emulators like BlueStack, Nox or something like that, and the hooking techniques on the emulator. Originally, Nevermoe, who is my friend, was going to give this presentation, but unfortunately he was unable to come here, so I'm covering for him. He's a security engineer working for DNA. And my name is Young Sung. I work for Line as a security engineer. Um... Okay, then let's finish the introduction and let's start with the agenda. This is today's agenda. Background of this research, emulator internals, hookings, and demo and conclusion. We define three roles in the game cheating and hacking. The first one is , the user, cheater, and the vendor. For PC games, all three of these roles have full control over their PCs. They're allowed to install or run privileged code. But on mobile devices, it's different. The user usually don't have permission to access over devices devices unless they rooted their devices. And this is the same for game developers. However, the cheaters usually want rooted devices, then the cheaters are the most powerful, no one can stop them from cheating, but this doesn't mean the cheaters can make a profit. Actually, yes, it is true that the cheaters can hack their own devices, but if they want to sell their cheating tools to users, they will have to persuade their users to root their devices, which is not an easy task. So for cheaters, they think of an easy way to distribute their cheating tools, the emulators. Especially BlueStacks and Nox, but not AVD from Android Studio. AVDs from Android Studio are basically for x86 emulation, which does not support APKs with ARM library only, or for ARM emulation, which is super slow. On the other hand, commercial emulators like BlueStacks and Nox, use a technique called Houdini, which I will discuss later. For these commercial emulators, they have a highly unified environment, so you don't need to turn your software to a different firmware, API level, etc. And what is better here is that the emulators are usually very easy to root, or they are shipped with a rooted environment, in the first place. According to my investigation, Yeah, according to my investigation, the most popular cheating approach on emulator is touch simulation. And it requires root privilege or shell access. But it does not involve modification of game binary or hooking skills. This means touch simulation is a gray zone. You can say it is cheating, but you can hardly say it is a crime. On the other hand, you can say that a cheat by hooking does not show up on emulators. This is because game codes are usually native and on commercial emulators they use Houdini to translate ARM codes to x86 and runtime. So this make it difficult to hook on emulators than on physical pure devices. So, purpose of my research is to enable hookings on commercial Android emulators, especially hooking native ARM binaries on x86 emulator machines. The emulator's targets I have investigated are below, blue stacks, nox, and radian. You can tell from the table that these emulators are very much alike. Maybe they chose these emulators coincidentally. I'm not sure, but it could be. If you try to run an ARM binary on emulator's command line, you will find it will be executed, but not executed properly. This is because the emulator's is the feature on Linux called binfmt-misc. With this feature, you can register on certain binary, signature, or magic number with a certain loader. In our case, when we execute an ARM binary on an emulator, which is on x86 machine, we can see that the emulator has been executed in the same way that the emulator has been executed. In this case, it is easy to inject your ARM library to the target process using LD preload, and perform hookings from your injected library. However, another popular inline hooking technique using ptrace doesn't work. If you try to call x86 version ptrace, you won't make it work directly, because you can't call x86 version DL open system call to open ARM library. Maybe you want to try to run an ARM version ptrace so that you can call ARM version DL open, but this doesn't work either, because ptrace doesn't work. And ptrace is not fully translated by Houdini. Then, can we inject our ARM library using LD preload, and also take advantage of it in our Java application? The answer is no. And I will show you why in following slides. To use LD preload, we can use the rep property provided by Android. When you set the rep property, the application startup is like this. The gigode is listening on a socket and ready to fork itself whenever a startup request comes. After the child process is forked from gigode, it executes the shell first, instead of executing an application directly, and then executes the application with the shell. This is the detail of the call stack. You can see that after the gigode forked itself, the function exit application will be called. And this function in concatenates some command line strings. And this function in concatenates some command line strings. Including our LD preload parameters. Including our LD preload parameters. And it queues the command string with the shell. And it queues the command string with the shell. So you can see that the final command of starting an application with the rep property looks like this. The LD preload is correctly injected into the command line. So far, it looks fine. So far, it looks fine. our library to the Java application using LDPreload, but look carefully. The shell command under system-bin folder is of x86. And the library we want to inject is of arm. So we cannot use LDPreload in Java application. So now we have to dig deeper into Houdini. For Android 5 with art, if the application startup request from activity manager is received, the JIGA forked itself, and it is at the time that Houdini is initialized. By the function initializeNativeBridge. What initializeNativeBridge function does is very simple. It just registers some callbacks to a structural structure called NativeBridge callbacks from libhoudini.so file. Note with the function load library and get trampling. You can think of them as an ARM version DL open and DL theme. Whenever the Java layer wants to load a native ARM library, it calls this load library function. You can find that a structural called NativeBridgeLTF and some function pointers are there. These function pointers will be eventually registered to the NativeBridge callback structure. In contrast, in Android 4, which comes with Dalvik Virtual Machine, we have to modify Dalvik Virtual Machine's code directly to use Houdini, because Android developers hadn't expected Android to run on x86 architecture at that time. Here you can see how Houdini is initialized. In hook DL open function, it first tries to open target library, by calling the x86 version's DL open. And if it fails, it registers Houdini's DVM2DH DL open handler inside the Houdini hook init function. Finally, it calls the handler to open an ARM library. There were interesting facts that I found when conducting my research. As far as I know, Houdini was developed by Intel, and they didn't provide any commercial license of Houdini publicly. As you can see, there is another emulator called Genymotion, which is also famous, but not included in my research. It was not bundled with Houdini when it was released, in order to avoid breaching license. Instead, it encourages users to download and install Houdini by themselves. But you can see BlueStack, it is using libHoudini from its release. And deliberately or not, it seems that Houdini is still trying to hide that they are using Houdini binaries. As you can see from the decompiled code of DVM, it tries to open lib3 bittrans.so file, which doesn't look like Houdini. But when it fails to open, it logs the message which says it fails to load libHoudini. Now, let's see how it works. Let's see some existing hooking frameworks. First, Xposed works by substituting the app process binary with a patched one. So it loads each one jar file at startup. Xposed is the only Java layer hooking framework. And Frida is my favorite one, and it can do almost across Java. It can load almost everything across massive platforms, layers, and architectures. But according to the authors, it will not support commotion emulators like notes for now. And substitute also works in Android, but it is outdated, so I won't discuss it now. This is a normal hooking process using ptrace. The tracer calls ptrace to attach to the target process. And then, use ptrace to call dlopen to load a hooking library into the target process. After injection library is loaded into the target process memory, the function in the library is executed to perform the hooking stage, such as modifying the entry address of target function. Here comes my first idea of hooking on emulators, which use Soudini. It's only one more step than normal process. I called ptrace to attach to a target process and to load the x86 version's hooking library inside the x86 version's SO file. We call the houdini's load library to load the ARM version's hooking library. Finally, inside the ARM version's library, it hooks and modifies the instructions of the original ARM SO file on a memory. The best part is that all the modifications on the ARM SO file will be automatically retranslated to x86 instruction by houdini. After I completed my hooking framework by utilizing the convenient interface provided by houdini, I realize that the key point to hooking on emulator is to inject the ARM library to the target process. And be aware that Xposed has already enabled you injecting Java code to the target process. You can just take advantage of this feature to call system.loadlibrary in the target process. Note that the system.loadLibrary function will take care of architecture-dependent native library, so you don't even need to know how Houdini works. Method A and B have their own marriage and de-marriage. Method A utilized the ptrace function, so it is more stable, and method B enables you to early trace, excluding some extreme conditions, and does not trigger any anti-debug mechanism. I'll give you a simple demo, which just change, which just change the demo game's result a few seconds after it's done. this is the account we build by hooking. This is the original play. Before the second play, I hooked the game to make it always win. second play. And even if it loads, I always win. Note that this game is shipped with ARM library only and it is running on Nox emulator. Conclusion. Not only mobile games, but also cheating is getting more popular and I expect cheating use hooking becomes more popular on mobile games like cheating on PC games. And I would like to know what is going to be changed after my presentation. Might nothing be happen, but some of game vendors might reconsider their attitude to emulators. Thanks for listening and if you guys have any questions, feel free to ask.