{"id":570,"date":"2014-10-27T19:22:42","date_gmt":"2014-10-27T18:22:42","guid":{"rendered":"http:\/\/blog.ansuz.nl\/?p=570"},"modified":"2014-10-27T19:22:42","modified_gmt":"2014-10-27T18:22:42","slug":"android-reverse-engineering","status":"publish","type":"post","link":"http:\/\/blog.ansuz.nl\/index.php\/2014\/10\/27\/android-reverse-engineering\/","title":{"rendered":"Android reverse engineering"},"content":{"rendered":"<p>Often I am quite curious to see how an app is built and what tools the use under the hood. What libraries do they use, what clever tricks are used, etc, etc. Some companies will blog about this on their corporate site, others don&#8217;t. An easy way to figure things out is by reverse engineering the application.<\/p>\n<p><strong>Getting data off the phone<\/strong><br \/>\nThe first thing you need to do is get the application from your phone to your computer.<\/p>\n<p>To do this, you can use the <a href=\"https:\/\/play.google.com\/store\/apps\/details?id=com.ext.ui\" target=\"_blank\">APK Extractor<\/a> app.<\/p>\n<blockquote><p>&#8220;This application will extracts APK which is installed on android device and copy to SD card.&#8221;<\/p><\/blockquote>\n<p>From there it is a matter getting the data from the SD card. I normally just use adb pull. Have a look at the <a href=\"http:\/\/developer.android.com\/tools\/help\/adb.html#commandsummary\" target=\"_blank\">adb documentation<\/a> for more details.<\/p>\n<p><strong>Decompiling<\/strong><br \/>\nOnce you have de application on your computer, there are various tools you can use to reverse engineer it.<\/p>\n<p>The first step though, is to unpack the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Android_application_package\" target=\"_blank\">APK<\/a>. Luckily this is really easy, as an APK is just a ZIP file. Extract it with your favorite ZIP-tool.<\/p>\n<p>At this point all extracted data is mostly represented in binary form. Opening the AndroidManifest.xml for instance, won&#8217;t give you much information yet. This is where the various tools come in.<\/p>\n<p>To &#8220;decode resources to nearly original form&#8221; you can use <a href=\"https:\/\/code.google.com\/p\/android-apktool\/\" target=\"_blank\">apktool<\/a>. To decompile an application using apktool, run: <\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">bash<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"bash\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">apktool d name_of_apk.apk<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p> For any additional options, have a look at the <a href=\"https:\/\/code.google.com\/p\/android-apktool\/wiki\/ApktoolOptions\" target=\"_blank\">apktool documentation<\/a>.<\/p>\n<p>Decompiling using apktool will result in a new folder with the same name as the APK. This folder will contain all decoded data. If you open up the AndroidManifest.xml file from there, you&#8217;ll see it is now in a human-readable format. All code is decompiled to smali. Smali is disassembled code from the dex format used by <a href=\"http:\/\/source.android.com\/devices\/tech\/dalvik\/index.html\" target=\"_blank\">Dalvik<\/a>. It seems to follow the same package and class naming as the original Java code. If you want to learn more about smali, have a look at the links in the answer to &#8220;<a href=\"http:\/\/stackoverflow.com\/a\/5656979\" target=\"_blank\">What&#8217;s the best way to learn Smali<\/a>&#8221; on Stackoverflow.<\/p>\n<p>If you don&#8217;t want to change any code and just want to learn about the application&#8217;s inner workings, you can make your life a little easier by using a combination of two other tools: <a href=\"https:\/\/code.google.com\/p\/dex2jar\/\" target=\"_blank\">dex2jar<\/a> and <a href=\"http:\/\/jd.benow.ca\/\" target=\"_blank\">jd-gui<\/a>.<\/p>\n<p>As the name says, dex2jar will convert a .dex file to a jar. This, again, is a fairly simple process. Running the command below will generate name_of_apk.jar.\u00a0<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">bash<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"bash\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">d2j-dex2jar name_of_apk.apk<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>To explore the jar, open it in JD GUI. The dex2jar process does not guarantee to be able to convert everything. Sometimes you will find conversion errors in the exported code. These are indicated by a line starting with &#8220;\/\/ ERROR \/\/&#8221;, followed by the smali code.<\/p>\n<p><strong>Recompiling<\/strong><br \/>\nAfter decompiling, you can make changes to the code, recompile the application and install it on your device.<\/p>\n<p>Make any modifications you want to the code and recompile using <\/p>\n<div id=\"ig-sh-3\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">bash<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"bash\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">apktool b folder_of_decoded_apk<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Next, sign the new application. Google has great <a href=\"http:\/\/developer.android.com\/tools\/publishing\/app-signing.html#signing-manually\" target=\"_blank\">instructions on how to sign an application<\/a>, so I won&#8217;t repeat that here.<\/p>\n<p>To install the new application on your phone, run <\/p>\n<div id=\"ig-sh-4\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">bash<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"bash\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">adb <span style=\"color: #c20cb9;font-weight: bold\">install<\/span> <span style=\"color: #660033\">-r<\/span> name_of_apk.apk<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p> You will have to uninstall the original version first before you can install your modified one. This is because the version you created is signed with a different certificate.<\/p>\n<p><strong>Word to the wise<\/strong><\/p>\n<p>Make sure you are not breaking any laws when using reverse engineering. In some countries it is legal, in others it is illegal.<\/p>\n<p>When reverse engineering is legal in the country you live in, make sure the software&#8217;s (or application&#8217;s) Terms and Conditions don&#8217;t explicitly forbid you to reverse engineer.<\/p>\n<p>Even when the above hurdles are overcome, make sure to not blatantly copy and paste someone else&#8217;s code.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Often I am quite curious to see how an app is built and what tools the use under the hood. What libraries do they use, what clever tricks are used, etc, etc. Some companies will blog about this on their &hellip; <a href=\"http:\/\/blog.ansuz.nl\/index.php\/2014\/10\/27\/android-reverse-engineering\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112,52,133],"tags":[],"class_list":["post-570","post","type-post","status-publish","format-standard","hentry","category-android","category-java","category-smali"],"_links":{"self":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/comments?post=570"}],"version-history":[{"count":6,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/570\/revisions"}],"predecessor-version":[{"id":576,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/570\/revisions\/576"}],"wp:attachment":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/media?parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/categories?post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/tags?post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}