{"id":6398,"date":"2017-01-26T11:58:24","date_gmt":"2017-01-26T19:58:24","guid":{"rendered":"http:\/\/www.palada.net\/index.php\/2017\/01\/26\/news-235\/"},"modified":"2017-01-26T11:58:24","modified_gmt":"2017-01-26T19:58:24","slug":"news-235","status":"publish","type":"post","link":"http:\/\/www.palada.net\/index.php\/2017\/01\/26\/news-235\/","title":{"rendered":"Deep Analysis of Android Rootnik Malware Using Advanced Anti-Debug and Anti-Hook, Part I: Debugging in The Scope of Native Layer"},"content":{"rendered":"<div class=\"entry\">\n<p>Recently, we found a new Android rootnik malware which uses open-sourced Android root exploit tools and the MTK root scheme from the dashi root tool to gain root access on an Android device. The malware disguises itself as a file helper app and then uses very advanced anti-debug and anti-hook techniques to prevent it from being reverse engineered. It also uses a multidex scheme to load a secondary dex file. After successfully gaining root privileges on the device, the rootnik malware can perform several malicious behaviors, including app and ad promotion, pushing porn, creating shortcuts on the home screen, silent app installation, pushing notification, etc.&nbsp; In this blog, I&rsquo;ll provide a deep analysis of this malware.<\/p>\n<h2>A Quick Look at the Malware<\/h2>\n<p>The malware app looks like a legitimate file helper app that manages your files and other resources stored on your Android phone.<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1001.png\" style=\"width: 451px; height: 750px;\" \/><\/p>\n<p align=\"center\">Figure 1. The malware app icon installed<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1003.png\" style=\"width: 452px; height: 797px;\" \/><\/p>\n<p align=\"center\">Figure 2. A view of the malware app<\/p>\n<p>We decompiled the APK file, as shown in Figure 3.<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1005.png\" style=\"width: 666px; height: 421px;\" \/><\/p>\n<p align=\"center\">Figure 3. Decompile the malware app&rsquo;s apk file<\/p>\n<p>Its package name is com.web.sdfile. First, let&rsquo;s look at its AndroidManifest.xml file.<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1007.png\" style=\"width: 1347px; height: 452px;\" \/><\/p>\n<p align=\"center\">Figure 4. AndroidManifest.xml file inside the malware app&rsquo;s apk file<\/p>\n<p>We can&rsquo;t find the main activity com.sd.clip.activity.FileManagerActivity, service class, or broadcast class in Figure 4.&nbsp; Obviously, the main logic of the file helper app is not located in the classes.dex.&nbsp; After analysis, it was discovered that the malware app uses the multidex scheme to dynamically load a secondary dex file and execute it.<\/p>\n<h2>How Rootnik Works<\/h2>\n<h3>I. Workflow of Rootnik<\/h3>\n<p>The following is the workflow of the android rootnik malware.<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1009.png\" style=\"width: 796px; height: 754px;\" \/><\/p>\n<p align=\"center\">Figure 5. An overview of the android rootnik malware&rsquo;s workflow<\/p>\n<h3>II. Going deep into the first dex file<\/h3>\n<p>The following is a code snippet of the class SecAppWrapper.<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1011.png\" style=\"width: 993px; height: 525px;\" \/><\/p>\n<p align=\"center\">Figure 6. A code snippet of the class SecAppWrapper<\/p>\n<p>The execution flow is shown below.<\/p>\n<blockquote>\n<p>Static code block -&gt;&nbsp;attachBaseContext -&gt;&nbsp;onCreate<\/p>\n<\/blockquote>\n<p>The static code block loads the dynamic link library libSecShell.so into the folder assets, and the program enters into the native layer, performs several anti-debug operations, decrypts the secondary dex file, and then uses a multidex scheme to load the decrypted secondary dex file, which is actually the main logic of the real application.<\/p>\n<p>The class DexInstall is actually the class MultiDex, and it refers to<\/p>\n<p><a href=\"https:\/\/android.googlesource.com\/platform\/frameworks\/multidex\/+\/d79604bd38c101b54e41745f85ddc2e04d978af2\/library\/src\/android\/support\/multidex\/MultiDex.java\">https:\/\/android.googlesource.com\/platform\/frameworks\/multidex\/+\/d79604bd38c101b54e41745f85ddc2e04d978af2\/library\/src\/android\/support\/multidex\/MultiDex.java<\/a><\/p>\n<p>The program then invokes the method install of DexInstall to load the secondary dex file.&nbsp; The invoking of the method install of DexInstall is executed in native layer.<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1013.png\" style=\"width: 794px; height: 184px;\" \/><\/p>\n<p align=\"center\">Figure 7. Installing the secondary dex file<\/p>\n<p>In function attachBaseContext, the program loads the class com.sd.clip.base.MyApplication, which is the execution entry of the secondary dex. The method attach of Helper is a native method.<\/p>\n<p>In function onCreate, the program executes the method onCreate of the class com.sd.clip.base.MyApplication.<\/p>\n<p>That&rsquo;s it. The first dex file is rather simple. Next, we&rsquo;ll do a deep analysis of the native layer code, which is very complicated and tricky.<\/p>\n<h3>III. The scope of the native layer code<\/h3>\n<p>As described above, the native layer code uses some advanced anti-debug and anti-hook techniques, and also uses several decryption algorithms to decrypt some byte arrays to get the plain text string.<\/p>\n<p>The following is part of the export functions in libSecShell.so.&nbsp; It becomes harder to analyze due to obfuscated function names.<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1015.png\" style=\"width: 770px; height: 630px;\" \/><\/p>\n<p align=\"center\">Figure 8. Part of export functions in libSecShell.so<\/p>\n<p>All anti-debug native code is located in function JNI_Onload.<\/p>\n<p>As described in the last section, the method attach of class Helper in java scope is a native method. The program dynamically registers this method in native layer.&nbsp; The following is a snippet of the ARM assembly code that registers native method in native layer.<\/p>\n<p align=\"center\" style=\"margin-left:.5in;\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1017.png\" style=\"width: 1160px; height: 380px;\" \/><\/p>\n<p align=\"center\">Figure 9. Dynamically register native method in native layer<\/p>\n<p>The function RegisterNatives is used to register a native method. Its prototype is shown below.<\/p>\n<blockquote>\n<p style=\"margin-left:.5in;\"><em>jint RegisterNatives(JNIEnv *env,jclass clazz, const JNINativeMethod* methods,jint nMethods) <\/em><\/p>\n<\/blockquote>\n<p>The definition of JNINativeMethod is shown below.<\/p>\n<blockquote>\n<p style=\"margin-left:1.0in;\"><em>typedef struct {<br \/>  const char* name;<br \/>  const char* signature;<br \/>  void* fnPtr;<br \/>  } JNINativeMethod;<\/em><\/p>\n<p style=\"margin-left:.5in;\">The first variable name is the method name in Java. Here, it&rsquo;s the string &ldquo;attach&rdquo;. The third variable, fnPtr, is a function pointer that points to a function in C code.&nbsp;<\/p>\n<\/blockquote>\n<p>We next need to find the location of the anti-debug code and bypass it, analyze how the secondary dex file is decrypted, and the dump the decrypted secondary dex file from memory.<\/p>\n<p>Let&rsquo;s look at the following code in IDA:<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1019.png\" style=\"width: 935px; height: 274px;\" \/><\/p>\n<p align=\"center\">Figure 10. Code snippet around anti-debug code<\/p>\n<p>Based on our deep analysis, the instruction at address 0xF832 is a jump to address loc_F924.<\/p>\n<p>After tracing some code, we found the anti-debug code.<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1021.png\" style=\"width: 1237px; height: 109px;\" \/><\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1023.png\" style=\"width: 1332px; height: 244px;\" \/><\/p>\n<p align=\"center\">Figure 11. The location of the anti-debug code<\/p>\n<p>The function p7E7056598F77DFCC42AE68DF7F0151CA() performs the anti-debug operations.<\/p>\n<p>The following is its graphic execution flow, which is very complicated.<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1025.png\" style=\"width: 1049px; height: 582px;\" \/><\/p>\n<p align=\"center\">Figure 12. The graphic execution flow of anti-debug code<\/p>\n<p>The following are some methods of anti-debug and anti-hook used in the malware.<\/p>\n<p>1. Detect some popular hook frameworks, such as Xposed, substrate, adbi, ddi, dexposed. Once found, hook it using these popular hook frameworks. It then kills the related process.<img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1027.png\" style=\"width: 817px; height: 274px;\" \/><\/p>\n<p align=\"center\">Figure 13. Detection of Xposed hook framwork<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1029.png\" style=\"width: 919px; height: 73px;\" \/><\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1031.png\" style=\"width: 1105px; height: 440px;\" \/><\/p>\n<p align=\"center\" style=\"margin-left:.25in;\">Figure 14. Finding the hook feature<\/p>\n<p>2. It then uses an kind of multi-process ptrace to implement anti-debug, which is tricky a little.&nbsp; Here we don&rsquo;t plan to provide a deailed analysis of the anti-debugging implementation mechanism, but only give some simple explanations.<\/p>\n<p>We can see that there are two processes named com.web.sdfile.<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1033.png\" style=\"width: 661px; height: 78px;\" \/><\/p>\n<p align=\"center\">Figure 15. Two processes named com.web.sdfile<\/p>\n<p>&nbsp; &nbsp;&nbsp;The following is a snippet of multi-process anti-debug code.<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1035.png\" style=\"width: 828px; height: 360px;\" \/><\/p>\n<p align=\"center\">Figure 16. A snippet of anti-debug code<\/p>\n<p>3. The program also uses inotify to monitor the memory and pagemap of the main process. It causes the memory dumping to be incomplete.&nbsp; The two processes use pipe to communicate with each other.<\/p>\n<p>In short, these anti-debug and anti-hook methods create a big obstacle for reversing engineering.&nbsp; So bypassing these anti- methods is our first task.&nbsp;<\/p>\n<p>Let&rsquo;s try to bypass them.<\/p>\n<p>As described in Figure 10, the instruction at offset 0x0000F832 jumps to loc_F924, and then the program executes these anti-debug codes. We can dynamically modify the values of some registers or some ARM instructions to change the execution flow of the program when dynamically debugging.&nbsp; When the program executes the instruction &ldquo;SUBS R1, R0, #0&rdquo; at offset 0xF828, we modify the value of register R0 to a non-zero value, which makes the condition of the instruciotn &ldquo;BNE&nbsp; loc_F834&rdquo; become true. This allows the program to jump to loc_F834.&nbsp;&nbsp;<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1037.png\" style=\"width: 919px; height: 231px;\" \/><\/p>\n<p align=\"center\" style=\"margin-left:.5in;\">Figure 17. How to bypass the anti-debug code<\/p>\n<p>Next, we dynamically debug it, bypass the anti-debug, and then dump the decrypted secondary dex file. The dynamic debugging is shown below.<\/p>\n<p align=\"center\" style=\"margin-left:.5in;\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1039.png\" style=\"width: 1086px; height: 365px;\" \/><\/p>\n<p align=\"center\">Figure 18. Modifying the value of R0 to non-zero<\/p>\n<p align=\"center\" style=\"margin-left:.5in;\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1041.png\" style=\"width: 1164px; height: 342px;\" \/><\/p>\n<p align=\"center\">Figure 19. Jump to local_75178834<\/p>\n<p>Next, &nbsp;jump to local_751788D8, as follows.<\/p>\n<p align=\"center\" style=\"margin-left:.5in;\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1043.png\" style=\"width: 1224px; height: 657px;\" \/><\/p>\n<p align=\"center\">Figure 20. Decryption function for the secondary dex<\/p>\n<p>The function p34D946B85C4E13BE6E95110517F61C41 is the decryption function. The register R0 points to the memory storing the encrypted dex file, and the value of R1 is the size of file and is equal to 0x94A3E(608830). The encrypted dex file is secData0.jar in the folder assets in the apk package. The following is the file secData0.jar.<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1045.png\" style=\"width: 658px; height: 275px;\" \/><\/p>\n<p align=\"center\">Figure 21. The file secData0.jar in the folder assets in the apk package<\/p>\n<p align=\"center\" style=\"margin-left:.5in;\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1047.png\" style=\"width: 1200px; height: 648px;\" \/><\/p>\n<p align=\"center\">Figure 22.&nbsp; The content of the decrypted secondary apk file in memory<\/p>\n<p>We can now dump the memory of the decrypted file to the file decrypt.dump.<\/p>\n<p>The decrypted file is a zip format file, and it contains the secondary dex file. After decryption, the program decompresses the decrypted secondary apk to a dex file. The function p3CBBD6F30D91F38FCD0A378BE7E54877 is used to decompress the file.<\/p>\n<p>Next, the function unk_75176334 invokes the java method install of class com.secshell.shellwrapper.DexInstall to load the secondary dex.<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1049.png\" style=\"width: 1330px; height: 645px;\" \/><\/p>\n<p align=\"center\">Figure 23. Decompressing the decrypted secondary apk and loading the secondary dex file<\/p>\n<p align=\"center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1051.png\" style=\"width: 915px; height: 278px;\" \/><\/p>\n<p align=\"center\">Figure 24. Calling the method install via Jni<\/p>\n<p>Here we finish the analysis of native layer and get the decrypted the secondary apk file, then will analyze the apk file in the <a href=\"https:\/\/blog.fortinet.com\/2017\/01\/24\/deep-analysis-of-android-rootnik-malware-using-advanced-anti-debug-and-anti-hook-part-ii-analysis-of-the-scope-of-java\">part II <\/a>of this blog.<\/p>\n<h2>The Decryption Function That Decrypts secData0.jar in Native Layer:<\/h2>\n<p>  <script src=\"https:\/\/gist.github.com\/anonymous\/f601582922c84bd73f8b8623c39a0b21.js\"><\/script><\/div<br \/><a href=\"http:\/\/blog.fortinet.com\/2017\/01\/26\/deep-analysis-of-android-rootnik-malware-using-advanced-anti-debug-and-anti-hook-part-i-debugging-in-the-scope-of-native-layer\" target=\"bwo\" >https:\/\/blog.fortinet.com\/feed<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/d3gpjj9d20n0p3.cloudfront.net\/ngblog\/uploads\/files\/Rootnikpt1001.png\"\/><br \/>Recently, we found a new Android rootnik malware which uses open-sourced Android root exploit tools and the MTK root scheme from the dashi root tool to gain root access on an Android device. The malware disguises itself as a file helper app and then uses very advanced anti-debug and anti-hook techniques to prevent it from being reverse engineered. It also uses a multidex scheme to load a secondary dex file. After successfully gaining root privileges on the device, the rootnik malware can perform several malicious behaviors, including app and ad&#8230;<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[10424,10378],"tags":[],"class_list":["post-6398","post","type-post","status-publish","format-standard","hentry","category-fortinet","category-security"],"_links":{"self":[{"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/posts\/6398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/comments?post=6398"}],"version-history":[{"count":0,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/posts\/6398\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/media?parent=6398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/categories?post=6398"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/tags?post=6398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}