{"id":823,"date":"2014-09-07T13:59:41","date_gmt":"2014-09-07T12:59:41","guid":{"rendered":"http:\/\/www.floyd.ch\/?p=823"},"modified":"2023-05-31T08:06:46","modified_gmt":"2023-05-31T07:06:46","slug":"android-app-disassembling-modification-and-reassembling","status":"publish","type":"post","link":"https:\/\/www.floyd.ch\/?p=823","title":{"rendered":"Android app disassembling, modification and reassembling"},"content":{"rendered":"<p>This is actually nothing very new, but what probably a lot of people do for a long time already. You can use this technique to do security reviews, to crack license mechanisms of apps, check how easy it is to modify your own app or do malware research. I&#8217;m not saying you should or shouldn&#8217;t do any of these. As usually tested on Mac OSX only but should work on Linux or other Unix, too.<\/p>\n<p>You need the following folder structure (or simply download the <a href=\"https:\/\/www.floyd.ch\/wp-content\/uploadedFilesToWordpress\/Android-app-disassembling-reassembling.zip\">Android-app-disassembling-reassembling.zip<\/a>):<\/p>\n<ul>\n<li>Folder called &#8220;apks-to-process&#8221;<\/li>\n<li>Folder called &#8220;external-tools&#8221;<\/li>\n<li>File &#8220;disassemble.sh&#8221; (see below)<\/li>\n<li>File &#8220;reassemble.sh&#8221; (see below)<\/li>\n<li>In the &#8220;external-tools&#8221; put the <a href=\"https:\/\/ibotpeaches.github.io\/Apktool\/\" title=\"apktool\" target=\"_blank\" rel=\"noopener\">apktool.jar<\/a><\/li>\n<li>In the &#8220;apks-to-process&#8221; folder put your Android app apk file<\/li>\n<\/ul>\n<p>After you run the disassemble.sh file you find the smali code for your app in the &#8220;outputs\/smali-output&#8221; directory. Now you can change the app as you like. Here are three suggestions:<\/p>\n<ul>\n<li>I recommend to add the android:debuggable=&#8221;true&#8221; attribute in the AndroidManifest.xml to your application tag. Afterwards you will be able to see the log messages of the application in logcat (&#8220;adb logcat&#8221; command when your phone is connected via USB).<\/li>\n<li>Replace one of the png files in the ressources folder<\/li>\n<li>If your application is making a new instance of a SecreKeySpec for encryption (something like &#8220;new-instance v1, Ljavax\/crypto\/spec\/SecretKeySpec&#8221; in smali, grep for it), try to dump the contents of the secret key. That&#8217;s pretty easy with <a href=\"https:\/\/github.com\/intrepidusgroup\/IGLogger\" title=\"IGLogger\" target=\"_blank\" rel=\"noopener\">IGLogger<\/a>. Download the IGLogger files and put the iglogger.smali file in the folder &#8220;outputs\/smali-output\/<you app's name>\/smali\/&#8221;. Then open the file where you found the SecreKeySpec intialisation. Add a new instruction after the invoke-direct line which will initialize the SecretKeySpec (e.g. &#8220;invoke-direct {v4, v5, v6}, Ljavax\/crypto\/spec\/SecretKeySpec;-><init>([BLjava\/lang\/String;)V&#8221;). This is the place where the secret key is passed to the SecretKeySpec constructor. As we know that the first argument is the secret key, we have to log the Dalvik VM&#8217;s register v4. Add &#8220;invoke-static {v4}, Liglogger;->d([B)I&#8221; after the initialisation statement.<\/li>\n<\/ul>\n<p>After you have done all your modifications, run reassemble.sh. There will be an apk file you can install on your device (see the last message that reassemble.sh will print). If you have added IGLogger, you will see a line in logcat that prints the secret key (for example run &#8220;adb logcat|grep -i IGLogger&#8221;).<\/p>\n<p>Happy hacking<br \/>\nfloyd<\/p>\n<p>Here&#8217;s the disassemble.sh that will disassemble your apk file to smali code:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\nORGWD=`pwd`\r\n\r\n#Configurable Parameters\r\nAPKLOCATION=$ORGWD\/apks-to-process #where the APK files are stored that should be processed\r\n\r\n#Disassembling\r\nSMALI_TARGET=$ORGWD\/outputs\/smali-output #Where to save the results\r\nAPKTOOLSTART=&quot;java -jar $ORGWD\/external-tools\/apktool.jar&quot; #The apktool\r\n\r\n########\r\n#Normally you should not need to change anything below here\r\n########\r\n\r\n#Look for the files to dissassemble\r\ncd $APKLOCATION\r\nFILES=`ls *.apk`\r\n\r\nif &#x5B; -e $SMALI_TARGET ]\r\nthen\r\n    echo &quot;&#x5B;ERROR] Please delete\/rename $SMALI_TARGET folder first!&quot;\r\n    exit\r\nelse\r\n    mkdir $SMALI_TARGET\r\nfi\r\n\r\nfor f in $FILES\r\ndo\r\n  echo &quot;&#x5B;INFO] Disassembling $f&quot;  \r\n  $APKTOOLSTART d $f $SMALI_TARGET\/$f\r\ndone\r\n\r\ncd $ORGWD\r\n\r\n<\/pre>\n<p>Here&#8217;s the reassemble.sh code that will reassemble your app to a signed and ready to be installed Android app apk file:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\nORGWD=`pwd`\r\n\r\n#Configurable Parameters\r\nAPKLOCATION=&quot;$ORGWD\/outputs\/faked-apks&quot; #where the APK files will be stored that should be produced\r\n\r\n#Reassembling\r\nSMALI_TARGET=&quot;$ORGWD\/outputs\/smali-output&quot; #Where to get the apps to reassemble\r\nAPKTOOLSTART=&quot;java -jar $ORGWD\/external-tools\/apktool.jar&quot; #The apktool\r\n\r\n########\r\n#Normally you should not need to change anything below here\r\n########\r\n\r\n#Look for the files to dissassemble\r\ncd &quot;$SMALI_TARGET&quot;\r\nFILES=`ls`\r\n\r\nif &#x5B; -e &quot;$APKLOCATION&quot; ]\r\nthen\r\n    echo &quot;&#x5B;ERROR] Please delete\/rename $APKLOCATION folder first!&quot;\r\n    exit\r\nelse\r\n    mkdir &quot;$APKLOCATION&quot;\r\nfi\r\n\r\nfor f in $FILES\r\ndo\r\n  echo &quot;&#x5B;INFO] Reassembling $f&quot;  \r\n  $APKTOOLSTART b &quot;$SMALI_TARGET\/$f&quot; &quot;$APKLOCATION\/$f&quot;\r\n  if &#x5B; ! -f &quot;$APKLOCATION\/someone.keystore&quot; ]\r\n  then\r\n    keytool -genkey -noprompt -dname &quot;CN=example.ch, OU=floydsReassembling, O=example, L=example, S=example, C=CH&quot; -storepass password -keypass password -alias someone -validity 100000 -keystore &quot;$APKLOCATION\/someone.keystore&quot; -keyalg RSA -keysize 2048\r\n  fi\r\n  jarsigner -verbose -storepass password -keypass password -sigalg SHA1withRSA -digestalg SHA1 -keystore &quot;$APKLOCATION\/someone.keystore&quot; &quot;$APKLOCATION\/$f&quot; someone\r\n  mv &quot;$APKLOCATION\/$f&quot; &quot;$APKLOCATION\/$f.unaligned&quot;\r\n  zipalign -v 4 &quot;$APKLOCATION\/$f.unaligned&quot; &quot;$APKLOCATION\/$f&quot;\r\ndone\r\n\r\necho &quot;TODO:&quot;\r\necho &quot;adb install \\&quot;$APKLOCATION\/$f\\&quot;&quot;\r\n\r\ncd &quot;$ORGWD&quot;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is actually nothing very new, but what probably a lot of people do for a long time already. You can use this technique to do security reviews, to crack license mechanisms of apps, check how easy it is to &hellip; <a href=\"https:\/\/www.floyd.ch\/?p=823\">Continue reading <span class=\"meta-nav\">&rarr;<\/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,90,10,19],"tags":[158,139,45,137,24,138,140],"class_list":["post-823","post","type-post","status-publish","format-standard","hentry","category-android","category-code-review","category-mobile-security","category-useful-scripts","tag-android","tag-app-cracking","tag-bash","tag-disassembling","tag-encryption","tag-reassembling","tag-smali"],"_links":{"self":[{"href":"https:\/\/www.floyd.ch\/index.php?rest_route=\/wp\/v2\/posts\/823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.floyd.ch\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.floyd.ch\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.floyd.ch\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.floyd.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=823"}],"version-history":[{"count":13,"href":"https:\/\/www.floyd.ch\/index.php?rest_route=\/wp\/v2\/posts\/823\/revisions"}],"predecessor-version":[{"id":1357,"href":"https:\/\/www.floyd.ch\/index.php?rest_route=\/wp\/v2\/posts\/823\/revisions\/1357"}],"wp:attachment":[{"href":"https:\/\/www.floyd.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.floyd.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.floyd.ch\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}