00:00:00.234-->00:00:06.173 Good afternoon, our one o’clock speaker is DokyDoky on One Step Before Game Hackers, >>Woo 00:00:06.173-->00:00:11.178 [applause] >>[Clears throat]. Hello, thanks for coming here. My name is Yong Song and I’m 00:00:20.587-->00:00:27.327 delighted to be here to talk in Defcon. Today I’m going to talk about android emulators like 00:00:27.327-->00:00:33.634 BlueStack, Nox or something like that and the hooking techniques on the emulator. Originally, 00:00:33.634-->00:00:38.639 Nevermoe, who’s my friend, was going to give this presentation, but unfortunately he was unable 00:00:42.042-->00:00:47.047 to come here, so I’m covering for him, um, he’s a security engineer working for DeNA and my 00:00:52.920-->00:00:57.925 name is Yong Song, I work for Line as a security engineer. Um, ok then, let's finish the 00:01:01.194-->00:01:06.199 introduction and let’s let's start with agenda. This is today’s agenda. Background of 00:01:14.474-->00:01:19.479 this research, emulator Internal, hookings and demo and conclusion. We define three 00:01:24.284-->00:01:29.289 roles in the game, cheating, threat, model. Users, cheaters and vendors. For game, for PC 00:01:35.762-->00:01:40.767 games, all three of these roles have full control over their PC’s. They are allowed to 00:01:43.570-->00:01:48.575 install or run pre released code, but on mobile devices, it’s different. The user usually 00:01:54.548-->00:01:59.553 don’t have permission to access other devices unless they rooted their devices. And this is the 00:02:03.991-->00:02:08.996 same for game developers, however, the cheaters usually run rooted devices then the 00:02:13.100-->00:02:18.105 cheaters are the most powerful, the world can’t stop them from cheating, but this doesn’t mean 00:02:21.074-->00:02:26.079 that cheaters can make a profit. Actually, yes it is true that the cheaters can hack their own 00:02:28.548-->00:02:33.553 devices, but if they want to sell their cheating tools to users, they will have to 00:02:35.555-->00:02:40.560 persuade their users to root their devices. It is not an easy task. So for cheaters, they 00:02:46.633-->00:02:51.638 think of an easy way to distribute their cheating tools: the emulators. Especially 00:02:54.941-->00:02:59.946 BlueStacks and Nox, but, not AVD from Android Studio. AVD’s from Android Studio are basically for 00:03:04.518-->00:03:09.523 X86 emulation which [indiscernible] APK’s with armed library only. Or for armed 00:03:14.227-->00:03:19.232 emulation which is super slow. On the other hand, commercial emulators like BlueStack and Nox 00:03:23.403-->00:03:28.408 use a technique called Houdini, which I, I will discuss later. For these commercial emulators 00:03:31.878-->00:03:37.217 they have both highly unified environment so you don’t need to ha, you don’t need to turn your 00:03:37.217-->00:03:42.222 software to different firmware. APL level etc. And what is better here is that the 00:03:46.793-->00:03:53.433 emulators are very easy to root or they are shifted with a rooted environment in the first 00:03:53.433-->00:03:58.438 place. . According to my investigation , um, ja, according to my investigation, 00:04:25.098-->00:04:30.103 the most popular cheating approach on emulator is touch simulation and it requires root 00:04:33.006-->00:04:38.011 privileges or shared access, but it does not involve modification of game binary or hooking 00:04:40.780-->00:04:45.785 skills. This means touch simulation is a gray zone. You can say it is cheating but , you 00:04:52.592-->00:04:57.597 can hardly say it is a crime. On the other hand you can say that a cheat by hooking does not show 00:05:02.702-->00:05:07.707 up on emulators, this is because game codes are usually native and on commercial emulators they 00:05:13.747-->00:05:18.752 use Houdini to translate ARM code to X86 and Runtime. So this make it difficult to hook on 00:05:23.390-->00:05:28.395 emulators than on physical, pure devices. So purpose on my research is to enable hookings 00:05:34.935-->00:05:39.940 on commercial and root emulators especially hooking native armed binaries on X86 emulator 00:05:41.942-->00:05:46.947 machines. The emulators targets I have investigated are below BlueStacks, Nox and Leidian. You 00:05:54.588-->00:05:59.593 can tell from the table that these emulators are very much alike. Maybe they chose these 00:06:04.764-->00:06:09.769 emulators coincidentally. I’m not sure but, it could be. If you try to run an armed binary 00:06:14.374-->00:06:19.379 on emulators comment line, we’ll find it, it will be executed properly. This is because the 00:06:25.518-->00:06:30.523 emulators is the feature on Linux called BNFMT misc with this future, feature, you can 00:06:35.328-->00:06:40.333 register on certain binary signature or magic number with a certain loader. In our case, 00:06:45.705-->00:06:50.710 when we execute on armed binary on an emulator it is on X86 machine, Houdini will be used to 00:06:55.548-->00:07:00.487 execute the armed binary. Um, In this case it is easy to inject your armed library to the type 00:07:07.093-->00:07:12.098 process using LD_preload and perform hookings from your injected library, however on 00:07:14.768-->00:07:19.773 other popular inline hooking techniques using ptrace doesn’t work. If you try to call X86 00:07:24.577-->00:07:29.582 versions ptrace, you won’t make it work directly, because you can’t call X86 versions steal 00:07:33.987-->00:07:38.992 open system call, to open armed library. Maybe you wanna try to run an ARM versions ptrace so 00:07:44.497-->00:07:49.502 that you can call ARMversions DL Open, but this doesn’t work either because ptrace is not 00:07:52.405-->00:07:57.410 fully translated by Houdini. Then, can we inject our ARM library using LD_preload and 00:08:04.584-->00:08:09.589 also take all advantage of it in our Java application? The answer is no and i’m going to show you 00:08:12.726-->00:08:17.731 why in following slides. Um, to use LD_preload we can use direct property provided by Android. 00:08:25.739-->00:08:30.744 When you set the right property the application startup is like this. The Jagged is listening on 00:08:34.714-->00:08:39.719 the circuit and ready to focus itself whenever, whenever a start up request comes. After 00:08:42.522-->00:08:47.527 the trial process it defunct from Jagged it executed the shell first instead of executing 00:08:50.463-->00:08:55.468 on application directly and then executes the application with the shell. This is the detail of 00:09:03.443-->00:09:08.448 the code fig. You can see that after the Java forked itself, the function exceed application 00:09:10.850-->00:09:15.855 will be called, and it’s function in concatenate some command line strings including 00:09:18.658-->00:09:23.663 our LD Preload parameters. And execute, executes the comments string with the shell. So you 00:09:30.570-->00:09:35.575 can see that the file command of starting an application with wrap property looks like this. 00:09:39.012-->00:09:44.017 The LD Preload is correctly injected into the command line. So far it looks fine, you can 00:09:47.253-->00:09:52.258 inject our library to the job application using LD Preload, but, look carefully, the shell 00:09:55.395-->00:10:00.400 command under system dash bin folder is of X86 and the library we want to inject is of a ARM. 00:10:03.870-->00:10:05.872 So we cannot use LD Preload in Java application. So now we have to dig deeper into Houdini. For 00:10:05.872-->00:10:07.874 Android 5 with ART if the application startup recast from Activity Manager to received, 00:10:07.874-->00:10:12.879 the Java forked itself and it is at a time this Houdini is initialised. By the function, 00:10:35.568-->00:10:40.573 initialise native breach. What initialise native breach function does is very simple. It 00:10:48.781-->00:10:53.786 just regis, registers some codex to structural, structure called native breach codex from the 00:10:58.291-->00:11:03.229 Houdini SL file. Not with the function load library and get trampoline. You can think of 00:11:07.233-->00:11:12.238 them as your ARM version DLopen and DLsym. Whenever the Java layer wants to load on native 00:11:17.210-->00:11:22.215 ARM library it calls this load library function. You can find that our structural called 00:11:26.486-->00:11:31.491 native breach LTF and some function pointers are there the function pointers did eventually 00:11:34.027-->00:11:39.032 registered to the native breach codex structures. In contrast, in Android 4 it comes with 00:11:48.775-->00:11:54.547 Dalvik virtual machine, we had to modify Dalvik virtual machines code directly to use 00:11:54.547-->00:12:00.987 Houdini, because Android developers hadn’t expected Android to run on X86 00:12:00.987-->00:12:05.992 architecture at that time. Here you can see how Houdini is initialised in hook dlopen 00:12:10.296-->00:12:15.301 function, it first tries to open target library by calling the X86 virtual dlopen and if it 00:12:19.706-->00:12:25.545 fails, it registers Houdini's DVM2DAH DLopen handler inside the Houdini hook in it function. 00:12:25.545-->00:12:30.550 Finally it caused the handler to open an ARM Library. Um, there were interestings facts that I 00:12:33.152-->00:12:38.157 found when conducting my research. As far as I know, Houdini was developed by Intel 00:12:53.773-->00:13:00.279 and they didn’t provide any commercial license of Houdini publically. As you can see there 00:13:00.279-->00:13:05.284 is another emulator called Genymotion. It is also famous but not included in my research. 00:13:09.489-->00:13:15.928 It was not bundled with Houdini when it was released. In order to avoid breaching license. 00:13:15.928-->00:13:20.933 Instead, in, it encourages users to download and install Houdini by themselves. But, you can see 00:13:27.774-->00:13:32.779 BlueStack, it is using Houdini from its release and deliberately or not, it seems 00:13:36.716-->00:13:42.822 that they are trying to hide they’re using Houdini binaries. As you can see from the 00:13:42.822-->00:13:47.827 decompiler code of DVN, it tries to open lib three b trans dot s as a file which doesn’t look 00:13:51.564-->00:13:56.569 like Houdini. But when it fails to open, it loads the message which says it fails to load the 00:14:01.407-->00:14:06.412 Houdini. Now lets see some existing hooking frameworks. First Xposed works by 00:14:14.654-->00:14:19.659 substituting the app process binary with a patched one. So it loads its one Jar file at 00:14:23.663-->00:14:28.668 startup, Xposed is the only Java Layer hooking framework and Frida is my favourite one and it 00:14:35.241-->00:14:42.048 can do almost across, almost everything across massive platforms, layers and 00:14:42.048-->00:14:47.053 architecture’s, but according to the authors, it will not support commercial emulators like Nox 00:14:50.323-->00:14:55.328 for now. And a Substrate also works in Android but it is outdated so I won’t discuss it 00:15:04.470-->00:15:09.475 now. Um, ya, this is a normal hooking process using ptrace. The tracer calls ptrace to 00:15:14.614-->00:15:19.619 attach to the target process and then uses ptrace to call DLopen to ope to load the hooking 00:15:24.724-->00:15:29.729 library into the target process. After injection library is loaded into the target process 00:15:32.632-->00:15:37.637 memory, the function in the library is executed to perform the hooking stage such as 00:15:40.006-->00:15:45.011 modifying the entry address of target function. Here comes here my first idea of hooking on 00:15:51.584-->00:15:56.589 emulators which use Houdini. It’s only one more step than normal process. I called ptrace 00:16:02.128-->00:16:07.133 to attach to a target process and to load the X86 versions hooking pro, hooking library. 00:16:13.406-->00:16:18.411 Inside the X86 SL file. We called the Houdini’s Load library to load the ARM versions 00:16:20.847-->00:16:25.852 hooking library. Finally, inside the ARM versions library, it, it hooks and modifies the 00:16:31.424-->00:16:36.429 instructions of the original ARM SL file on the memory. The best part is that all the 00:16:42.268-->00:16:47.273 modifications on the ARM SL file will be automatically retranslated to X86 instruction 00:16:49.575-->00:16:54.580 by Houdini. After I completed my hooking frame hook by utilizing convenient interface provided by 00:17:00.453-->00:17:05.458 Houdini, I realised that the key point to hook on the emulator is to inject your ARM library to 00:17:08.527-->00:17:13.532 the type process. And being aware that Xposed has already enabled you injecting Java code 00:17:16.202-->00:17:21.207 to targets process. You can just take advantage of this feature called system dot load library 00:17:24.877-->00:17:29.882 in the target process. Note that the system dot library function will take care of architecture 00:17:33.185-->00:17:38.190 dependant native library, so you don’t even need to know how Houdini works. Um, method A and 00:17:43.963-->00:17:48.968 B have their one marriager and marriage and their marriage. Method A utilised the ptrace 00:17:51.470-->00:17:56.475 function so its it is more stable and method B enables you to early trace excluding some 00:18:01.314-->00:18:06.319 extreme conditions and does not trigger any anti debug mechanism. I’ll give you a 00:18:11.223-->00:18:16.228 simple demo which just change, which just change the demo games result by hooking. This is the 00:18:32.311-->00:18:37.316 original play. And before the second play, I hooked the game to make it always win. And this 00:18:50.796-->00:18:55.801 is the second play. And even if you hit load, I always win. Ah, not that this game is chipped 00:19:06.312-->00:19:11.317 with ARM library only and it is running on Nox emulator. Conclusion, not only mobile 00:19:18.357-->00:19:24.630 games, but also cheating is getting more popular and I expect a cheating use hooking 00:19:24.630-->00:19:29.635 becomes more popular on mobile games like cheating on pc games and I would like to know what is 00:19:33.939-->00:19:40.379 going to be changed after my presentation, might nothing be happen but some of game vendors 00:19:40.379-->00:19:45.384 might reconsider their attitude to emulators. Thanks for listening and if you guys have 00:19:49.422-->00:19:54.427 any questions I will be outside so please ask. [Applause]