Android APK程序的smali动态调试

本文博客地址:http://blog.csdn.net/qq1084283172/article/details/71250622


一、Apktool的下载和安装

Apktool是Android逆向分析的基础工具,在进行Android APK的smali动态调试的时候会使用到它。有关Apktool工具的下载、安装、使用以及源码的编译可以参考Apktool工具的官网:https://ibotpeaches.github.io/Apktool/,Apktool工具的官网将Apktool工具的使用和版本更新的说明都讲的很清楚了,后面很多的操作都根据Apktool工具官网的帮助说明来进行实践。

Apktool工具源码下载地址:https://github.com/iBotPeaches/Apktool

Apktool工具的下载和安装,可以参考Apktool工具官网的安装说明,具体的网址为:https://ibotpeaches.github.io/Apktool/install/,Apktool工具在 Windows、Linux、MAC OS X平台的安装步骤如下:

Install Instructions

Quick Check

  1. Is at least Java 1.7 installed?
  2. Does executing java -version on command line / command prompt return 1.7 or greater?
  3. If not, please install Java 7+ and make it the default.

Installation for Apktool

  • Windows:
    1. Download Windows wrapper script (Right click, Save Link As apktool.bat)
    2. Download apktool-2 (find newest here)
    3. Rename downloaded jar to apktool.jar
    4. Move both files (apktool.jar & apktool.bat) to your Windows directory (Usually C://Windows)
    5. If you do not have access to C://Windows, you may place the two files anywhere then add that directory to your Environment Variables System PATH variable.
    6. Try running apktool via command prompt
  • Linux:
    1. Download Linux wrapper script (Right click, Save Link As apktool)
    2. Download apktool-2 (find newest here)
    3. Make sure you have the 32bit libraries (ia32-libs) downloaded and installed by your linux package manager, if you are on a 64bit unix system.
    4. (This helps provide support for the 32bit native binary aapt, which is required by apktool)
    5. Rename downloaded jar to apktool.jar
    6. Move both files (apktool.jar & apktool) to /usr/local/bin (root needed)
    7. Make sure both files are executable (chmod +x)
    8. Try running apktool via cli
  • Mac OS X:
    1. Download Mac wrapper script (Right click, Save Link As apktool)
    2. Download apktool-2 (find newest here)
    3. Rename downloaded jar to apktool.jar
    4. Move both files (apktool.jar & apktool) to /usr/local/bin (root needed)
    5. Make sure both files are executable (chmod +x)
    6. Try running apktool via cli

Note - Wrapper scripts are not needed, but helpful so you don’t have to type java -jar apktool.jar over and over.


Windows平台下Apktool工具安装步骤的整理

1.从网址:https://raw.githubusercontent.com/iBotPeaches/Apktool/master/scripts/windows/apktool.bat 获取文件内容创建脚本文件 apktool.bat ,脚本文件apktool.bat 的内容如下:

@echo off
if "%PATH_BASE%" == "" set PATH_BASE=%PATH%
set PATH=%CD%;%PATH_BASE%;
java -jar -Duser.language=en "%~dp0apktool.jar" %*
2.从网址:https://bitbucket.org/iBotPeaches/apktool/downloads/  下载最新版的Apktool工具并将下载后的 Apktool工具 重命名为 apktool.jar

3.将 apktool.jarapktool.bat  文件拷贝到windows系统的 C://Windows 目录下,这样就可以在windows系统的cmd命令行终端上直接使用 apktool 命令进行工作了。如果不喜欢这样做,也可以 apktool.jar 和 apktool.bat  文件的文件路径添加到windows系统的环境变量path中,效果一样。如果觉得这些操作比较繁琐,也可以直接在 apktool.jar 文件下进行Android APK的解包反编译和打包工作,具体的使用命令如下:

$ java -jar apktool.jar
Apktool工具安装成功后,配置好的效果图如下:


4.Apktool工具的命令行帮助如下:

Apktool v2.2.2 - a tool for reengineering Android apk files
with smali v2.1.3 and baksmali v2.1.3
Copyright 2014 Ryszard Wi?niewski <brut.alll@gmail.com>
Updated by Connor Tumbleson <connor.tumbleson@gmail.com>

usage: apktool
 -advance,--advanced   prints advance information.
 -version,--version    prints the version then exits
usage: apktool if|install-framework [options] <framework.apk>
 -p,--frame-path <dir>   Stores framework files into <dir>.
 -t,--tag <tag>          Tag frameworks using <tag>.
usage: apktool d[ecode] [options] <file_apk>
 -f,--force              Force delete destination directory.
 -o,--output <dir>       The name of folder that gets written. Default is apk.out
 -p,--frame-path <dir>   Uses framework files located in <dir>.
 -r,--no-res             Do not decode resources.
 -s,--no-src             Do not decode sources.
 -t,--frame-tag <tag>    Uses framework files tagged by <tag>.
usage: apktool b[uild] [options] <app_path>
 -f,--force-all          Skip changes detection and build all files.
 -o,--output <dir>       The name of apk that gets written. Default is dist/name.apk
 -p,--frame-path <dir>   Uses framework files located in <dir>.

For additional info, see: http://ibotpeaches.github.io/Apktool/ 
For smali/baksmali info, see: https://github.com/JesusFreke/smali
提示

Apktool工具运行正常的前提条件是 主机系统上安装了 Oracle JDK 1.7 或者 更高版本的 Oracle JDK,不能使用 OpenJDK。


二、Android APK 程序动态调试插件smalidea的下载

在这之前smali 动态调试Android APK程序需要apktool命令的  -d 参数选项 的支持,但是 apktool 2.1.0以后 ,不再提供 apktool d -d 功能生成 .java 的 smali 文件,Android APK程序的动态调试需要借助调试插件 smalidea 具体可以参考官方文档:https://ibotpeaches.github.io/Apktool/documentation/


smalidea插件的官方参考文档地址为:https://github.com/JesusFreke/smali/wiki/smalidea

smalidea插件、smali工具、baksmali工具的下载地址为:https://bitbucket.org/JesusFreke/smali/downloads/

smalidea插件的官方安装说明,如下图所示:



在Android Studio中,选择 "File —> Settings"  (旧版的是 "Perference" ) —> "Plugins" —> "Install plugin from disk..." 选项,在打开的窗口中选择已经下载的 smalidea-0.03.zip ,确认后可直接安装,如下图所示。smalidea插件安装成功以后,Android Studio会提示重启,单击 "Restart" 按钮重启即可。


提示

Android Studio和smalidea插件可能会存在兼容性的问题,在进行smalidea插件安装的时候,需要根据自己安装的Android Studio版本选择合适版本的smalidea插件进行安装。


三、反编译需要调试的APK包

使用 baksmali 或者 Apktool工具 反编译需要调试的APK程序包,这里以 RouterCheck-0.9.9-83.apk 为例。为反编译APK程序包和重建APK程序包方便起见,使用前面同样的方式,创建 baksmali.bat脚本文件 和 smali.bat脚本文件,然后拷贝 baksmali.bat 脚本文件 和 smali.bat 脚本文件以及 baksmali-2.2.0.jar 和 smali-2.2.0.jar 到windows系统 C://Windows 目录下。baksmali.bat脚本文件 和 smali.bat脚本文件的文件内容分别如下:

baksmali.bat 脚本文件

@echo off
if "%PATH_BASE%" == "" set PATH_BASE=%PATH%
set PATH=%CD%;%PATH_BASE%;
java -jar -Duser.language=en "%~dp0smali-2.2.0.jar" %*

smali.bat 脚本文件

@echo off
if "%PATH_BASE%" == "" set PATH_BASE=%PATH%
set PATH=%CD%;%PATH_BASE%;
java -jar -Duser.language=en "%~dp0aksmali-2.2.0.jar" %*

baksmali使用的帮助命令如下:

$ baksmali
usage: baksmali [--version] [--help] [<command [<args>]]

Options:
  --help,-h,-? - Show usage information
  --version,-v - Print the version of baksmali and then exit

Commands:
  deodex(de,x) - Deodexes an odex/oat file
  disassemble(dis,d) - Disassembles a dex file.
  dump(du) - Prints an annotated hex dump for the given dex file
  help(h) - Shows usage information
  list(l) - Lists various objects in a dex file.

See baksmali help <command> for more information about a specific command

smali使用的帮助命令如下:

$ smali
usage: smali [-v] [-h] [<command [<args>]]

Options:
  -h,-?,--help - Show usage information
  -v,--version - Print the version of baksmali and then exit

Commands:
  assemble(ass,as,a) - Assembles smali files into a dex file.
  help(h) - Shows usage information

See smali help <command> for more information about a specific command

使用smalidea插件对Android APK程序进行smali动态调试的步骤,可以参考smalidea插件的官方文档,具体的步骤如下:


1.使用 baksmali 或者 Apktool工具 对 RouterCheck-0.9.9-83.apk 程序包进行反编译的命令:

@echo off

: 使用baksmali解包apk反编译dex文件
if exist debugbaksmalisrc @DEL /S /Q  debugbaksmalisrc* 
baksmali d RouterCheck-0.9.9-83.apk -o ./debugbaksmali/src
pause

: ##########################################################

: 使用smali打包生成dex文件
smali a ./debugbaksmali/src -o debugbaksmali.dex
pause

: ###################### 或者 ##############################

@echo off

: apktool解包apk
if exist debugapktoolsmalisrc @DEL /S /Q  debugapktoolsmalisrc* 
apktool d RouterCheck-0.9.9-83.apk -o ./debugapktoolsmali/src
pause

: ##########################################################

: apktool打包apk
apktool b ./debugapktoolsmali/src -o debugapktool.apk
pause
使用 baksmali 对需要调试的 RouterCheck-0.9.9-83.apk 程序包进行反编译处理的结果截图如下:


2.将 RouterCheck-0.9.9-83.apk 解包反编译后的smali目录 /debugbaksmali/src 导入到Android Studio中,如下图所示:


选择需要动态调试的目标工程目录 /debugbaksmali 


导入时选择 Create project from existing sources 如下图所示,接着一路 Next 即可导入成功:



3.导入工程成功后,右键点击 src 目录,设定 Mark Directory As->Sources Root 如下图所示:



4.打开 "File"—>"Project Structure"为导入成功需要动态的目标工程设置对应的 JDK  和 Android SDK 版本,如下图所示:



四、开启Android Apk应用的调试选项

根据Android的官方文档,如果要调试一个Apk里面的dex代码,必须满足以下两个条件中的任何一个:

1.Apk的 AndroidManifest.xm 中 Application标签 必选包含属性 android:debuggable="true";
2./default.prop 中 ro.debuggable 的值为1;

但是很多发布版的Android apk中没有开启调试选项,设置 android:debuggable="false"。因此,需要使用Apktool工具解包需要动态调试的目标Apk程序包,在目标Apk程序包的 AndroidManifest.xml 中 Application标签 里添加 调试选项 android:debuggable="true",然后使用Apktool工具重新打包、签名生成能够被调试的目标Apk程序包;如果觉得这样操作比较繁琐,也可以通过直接修改Android设备的 boot.img镜像文件设置 /default.prop 中 ro.debuggable 的值为 1,不需要每次修改需要调试的目标Apk的配置和重新打包,一劳永逸。这里使用添加 android:debuggable="true" 的方法,使目标Apk程序能够被调试。


1.使用Apktool工具解包 RouterCheck-0.9.9-83.apk 程序,添加  android:debuggable="true" 属性。

: 使用Apktool工具解包Apk程序
$ apktool d RouterCheck-0.9.9-83.apk -o ./debugapktoolsmali/src  
为解包的 RouterCheck-0.9.9-83.apk 程序添加 android:debuggable="true" 属性,如下图所示:



2.使用Apktool对解包的 RouterCheck-0.9.9-83.apk 程序进行打包处理,执行下面的命令:

: 使用Apktool工具打包Apk程序
$ apktool b ./debugapktoolsmali/src -o debugapktool.apk  
3.使用AndroidKiller工具对重新打包生成的 debugapktool.apk 进行签名处理。



4.将重新打包、重新签名后的 debugapktool_sign.apk  程序安装到Android手机设备(Nexus 5)上,执行下面的命令:

: 手机设备USB连接电脑正常
$ adb install -r debugapktool_sign.apk  


五、动态调试目标Apk程序

1.查看目标Apk程序 RouterCheck-0.9.9-83.apk  的AndroidManifest.xml 获取到 包名 为 com.Sericon.RouterCheck.client.android 主Activity类名 com.Sericon.RouterCheck.client.android.MainActivity  ,截图如下所示:



2.以调试等待模式启动目标Apk程序 RouterCheck-0.9.9-83.apk(即重新打包、签名生成的 debugapktool_sign.apk),执行下面的命令:

: 以调试等待模式启动需要调试的Apk程序
: 格式 adb shell am start -D -W -n 包名/主类名
$ adb shell am start -D -W -n com.Sericon.RouterCheck.client.android/.MainActivity

debugapktool_sign.apk程序 以调试等待模式启动成功后的结果截图如下所示:

                                                          


3.目标工程导入到Android Studio中成功后,发现Android Studio工具的 Android Android Device Monitor 按钮 不能点击使用,DDMS工具不能通过这种方式进行启动;需要运行  Androidsdk oolsddms.bat  才能实现DDMS工具的启动。DDMS工具的启动成功以后,选择需要动态调试的目标Apk程序 RouterCheck-0.9.9-83.apk 的进程 com.Sericon.RouterCheck.client.android ,如下图所示:



4.在 Android Studio 中配置远程调试 (Run —> Edit Configurations...),单击  "+"  按钮,选中 "Remote" 添加远程调试的配置,如下图所示,根据上面 步骤3 中的信息更改Debug端口为 8700 ,并指定源文件的目录为 需要调试的目标Apk解包后的 smali文件路径 (如果有必要)。



5.在需要动态调试的目标Apk程序的smali源码上下有效的断点,然后单击 "Run"——> "Debug 'debug_apk'" 即可开始远程调试了,需要动态调试的目标Apk程序的调试等待界面结束,启动Apk应用,直到运行到有效断点被触发的地方。执行结果,如下图所示,可以看到此时的栈回溯、变量值等信息。



6.OK,目标Apk程序被动态调试起来了,后面我们就可以进行  F7、F8  愉快的单步调试Apk程序了。


参考文档

《漏洞战争-软件漏洞分析精要》

https://ibotpeaches.github.io/Apktool/

https://ibotpeaches.github.io/Apktool/documentation/

https://github.com/JesusFreke/smali/wiki/smalidea

http://blog.csdn.net/linchaolong/article/details/51146492

http://blog.csdn.net/justfwd/article/details/52461188

http://www.droidsec.cn/smalidea%E6%97%A0%E6%BA%90%E7%A0%81%E8%B0%83%E8%AF%95-android-%E5%BA%94%E7%94%A8/


原文地址:https://www.cnblogs.com/csnd/p/11800614.html