{"id":346,"date":"2013-03-04T21:58:37","date_gmt":"2013-03-05T02:58:37","guid":{"rendered":"http:\/\/sunapi386.ca\/wordpress\/?p=346"},"modified":"2015-10-28T16:51:14","modified_gmt":"2015-10-28T21:51:14","slug":"hacking_android_apk","status":"publish","type":"post","link":"https:\/\/sunapi386.ca\/wordpress\/hacking_android_apk\/","title":{"rendered":"Hacking Android APK Tutorial"},"content":{"rendered":"<h1 align=\"center\">Hacking Android APK Tutorial<\/h1>\n<p>Tutorial on hacking an Android APK file, which is an android app file, we decompile, hack it, and recompile. I will go through the setup and basic commands.<\/p>\n<h2><b>Introduction to APK format<\/b><\/h2>\n<p>Apps in Android have an extension of .apk format &#8211; which is basically a special .zip container that is signed with a certificate. The signer could be somebody like Google Apps Store. The idea is that modifying the .apk file means the signature is invalidated, to prevent installation of\u00a0modified\u00a0apps.<\/p>\n<p>The main line of defence from installing malicious APK files is to make sure they are downloaded from the Google Apps Store. But once the APK (app) is in the users\u2019 hands, it is unprotected. Most premium apps can be rooted, so ripping apps is easy.<\/p>\n<p><!--more--><\/p>\n<h2><b>Introduction to modifications<\/b><\/h2>\n<p>Modifying an APK file is somewhat difficult, depending on the quality of the app. The Dalvik (Android\u2019s virtual machine), prevents code obfuscation &#8211; which is the deliberate act of creating hard to understand code. Since the Dalvik supports <i>reflection<\/i>, and the virtual machine has to be able to interpret the byte code, no obfuscation can ever hope to compete. Note, <i>reflection <\/i>is the ability of a computer program to examine and modify the structure and behavior &#8211; specifically the values, meta-data, properties and functions of an object at runtime. Obfuscation products like ProGuard may become more advanced with time but intense obfuscation will likely have a very negative impact on performance.<\/p>\n<p>ProGuard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names. Finally, it preverifies the processed code for Java 6 or for Java Micro Edition.<\/p>\n<h2><b>High level overview:<\/b><\/h2>\n<ol>\n<li>Suppose I have an APK file<i><\/i><\/li>\n<li>I want to decompile it into something like assembly language<i><\/i><\/li>\n<li>We can do that using the APK Tool, which turns decompiles it into a folder with smali files.<\/li>\n<li>You should know what a smali file is \u2013 basically assembly code.<br \/>\nSee <a href=\"https:\/\/code.google.com\/p\/smali\/\">https:\/\/code.google.com\/p\/smali\/<\/a><\/li>\n<li>Then we dig through the smali code and make change to get our desired results.<\/li>\n<li>I will cover this lightly. Exactly what to change and how to find it is beyond my scope of knowledge, I only understand basic ideas. Also, there textbooks on how to analyse decompiled code.<\/li>\n<li>We recompile the smali file into APK file<\/li>\n<li>Install and test it on the emulator<\/li>\n<li>Done.<\/li>\n<\/ol>\n<h2><b>The Setup<\/b><\/h2>\n<p>To start playing with your own modifications, you need several tools.<\/p>\n<ol>\n<li>Eclipse, to code your own apps (optional again, but the package manager is nice to use)<br \/>\n<a href=\"http:\/\/www.eclipse.org\/downloads\/\">http:\/\/www.eclipse.org\/downloads\/<\/a><\/li>\n<li>Java SDK, to do Java development (optional really \u2013 but get it anyways)<br \/>\n<a href=\"http:\/\/www.oracle.com\/technetwork\/java\/javase\/downloads\/index.html\">http:\/\/www.oracle.com\/technetwork\/java\/javase\/downloads\/index.html<\/a><\/li>\n<li>Android SDK, for the Android emulator<br \/>\n<a href=\"http:\/\/developer.android.com\/sdk\/index.html\">http:\/\/developer.android.com\/sdk\/index.html<\/a><\/li>\n<li>APK Tool, to reverse engineer APK files and create smali files<br \/>\n<a href=\"http:\/\/code.google.com\/p\/android-apktool\/\">http:\/\/code.google.com\/p\/android-apktool\/<\/a><\/li>\n<li>Notepad++, a great program that has syntax highlighting addon pack for smali<br \/>\n<a href=\"http:\/\/notepad-plus-plus.org\/\">http:\/\/notepad-plus-plus.org\/<\/a><\/li>\n<li>Addon syntax highlighting for Smali<br \/>\n<a href=\"http:\/\/androidcracking.blogspot.com\/2011\/02\/smali-syntax-highlighting-for-notepad.html\">http:\/\/androidcracking.blogspot.com\/2011\/02\/smali-syntax-highlighting-for-notepad.html<\/a><\/li>\n<li>Amon_RA\u2019s Testsign tool, for signing the recompiled APK file<br \/>\n<a href=\"https:\/\/sites.google.com\/site\/chall32\/general\/testsign.jar\">https:\/\/sites.google.com\/site\/chall32\/general\/testsign.jar<\/a><\/li>\n<\/ol>\n<h2><b>The Usage<\/b><\/h2>\n<p>This will be a walkthrough on how to start your hack.<\/p>\n<p>There\u2019s a nice guide on how to setup everything on the Android SDK website; <a href=\"http:\/\/developer.android.com\/sdk\/installing\/index.html\">http:\/\/developer.android.com\/sdk\/installing\/index.html<\/a><\/p>\n<p>To run the Android emulator, use their Android Virtual Devices Manager. Make a choice in what you want your device to be; just keep in mind the higher the resolution the slower it\u2019ll be. Expect the emulator to run with heavy lag. My manager is located at:<\/p>\n<pre>C:\\Program Files (x86)\\Android\\android-sdk\\AVD Manager.exe<\/pre>\n<p>Open up a terminal, cmd. Most of the tools you require are in the <i>..\\android-sdk\\platform-tools<\/i> folder. Some of them may be in <i>..\\android-sdk\\tools<\/i><\/p>\n<p>In this terminal, there are some commands you want to be familiar with.<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p align=\"center\"><b>Command<\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p align=\"center\"><b>Effect<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">abd install [..\\location\\someapp.apk]<\/td>\n<td valign=\"top\" width=\"319\">Installs to emulator. Make sure emulator is on!<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">adb uninstall [com.someapp]<\/td>\n<td valign=\"top\" width=\"319\">Exactly what to type requires a bit of work, it\u2019s basically the path the android uses. I\u2019ll describe it more later.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">apktool.bat d someapp.apk dump- someapp<\/td>\n<td valign=\"top\" width=\"319\">Decompiles your APK to smali files<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">apktool.bat b dump- someapp someapp-new.apk<\/td>\n<td valign=\"top\" width=\"319\">Rebuild the edited smali files<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">java \u2013classpath testsign.jar testsign someapp-new.apk<\/td>\n<td valign=\"top\" width=\"319\">Fake sign the file with some certificate, so that when we install, emulator goes \u201coh ok here\u2019s the signature, and it matches the file, we can proceed\u201d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2><b>Digging through the decompiled smali code<\/b><\/h2>\n<p>I\u2019m going to cover this vaguely, because I myself am not that familiar with it. Basically you look at the smali files, the xml files, and find interesting stuff that you think is relevant to what you want to do.<\/p>\n<p>Start at a point, such an error message, and work backwords. Say the message \u201cInvalid serial key entered!\u201d \u2013 trace that back to where the key gets checked.<\/p>\n<p>When bypassing things like serial key checks, look for check conditions:<\/p>\n<ul>\n<li>If-eq<\/li>\n<li>If-ne<\/li>\n<li>If-nez<\/li>\n<li>If-eqz<\/li>\n<\/ul>\n<p>Since applications don\u2019t have a console to print to, you could have it print to logs or print as a toast popup message. <a href=\"http:\/\/en.wikipedia.org\/wiki\/Toast_(computing)\">http:\/\/en.wikipedia.org\/wiki\/Toast_(computing)<\/a> Also, logs can be viewed with the ..<i>\\tools\\monitor.bat<\/i> tool.<\/p>\n<p>When digging through smali code, you definitely need to refer to Dalvik opcodes for syntax and usage: <a href=\"http:\/\/pallergabor.uw.hu\/androidblog\/dalvik_opcodes.html\">http:\/\/pallergabor.uw.hu\/androidblog\/dalvik_opcodes.html<\/a><\/p>\n<h2><b>Terminate transmission<\/b><\/h2>\n<p>Thanks for reading. That\u2019s all folks. I\u2019ll update this as I figure out more and more.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hacking Android APK Tutorial Tutorial on hacking an Android APK file, which is an android app file, we decompile, hack it, and recompile. I will go through the setup and basic commands. Introduction to APK format Apps in Android have an extension of .apk format &#8211; which is basically a special .zip container that is &hellip; <a href=\"https:\/\/sunapi386.ca\/wordpress\/hacking_android_apk\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Hacking Android APK Tutorial<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,31],"tags":[],"class_list":["post-346","post","type-post","status-publish","format-standard","hentry","category-hack2600","category-tutorial"],"_links":{"self":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts\/346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/comments?post=346"}],"version-history":[{"count":10,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts\/346\/revisions"}],"predecessor-version":[{"id":375,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts\/346\/revisions\/375"}],"wp:attachment":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/media?parent=346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/categories?post=346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/tags?post=346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}