本文博客地址: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 "%~dp0\apktool.jar" %*

2.从网址:https://bitbucket.org/iBotPeaches/apktool/downloads/ 下载最新版的Apktool工具并将下载后的 Apktool工具 重命名为 apktool.jar

3.将 apktool.jar
apktool.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 "%~dp0\smali-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 "%~dp0\baksmali-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 debugbaksmali\src @DEL /S /Q debugbaksmali\src\*
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 debugapktoolsmali\src @DEL /S /Q debugapktoolsmali\src\*
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工具不能通过这种方式进行启动;需要运行  \Android\sdk\tools\ddms.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/

Android APK程序的smali动态调试的更多相关文章

  1. Android动态方式破解apk前奏篇(Eclipse动态调试smail源码)

    一.前言 今天我们开始apk破解的另外一种方式:动态代码调试破解,之前其实已经在一篇文章中说到如何破解apk了: Android中使用静态方式破解Apk  主要采用的是静态方式,步骤也很简单,首先使用 ...

  2. apk逆向 - smali动态调试

    author: Dlive date: 2016/10/6 0x00 前言 ​ 之前有人问过smali的动态调试方法,其实网上已经有很多文章讲这些内容,但是为了方便大家学习,我还是写一下让大家少走点坑 ...

  3. Android应用程序用真机调试步骤

    仅供参考: 1.开启调试模式     2.安装 Adb.exe 将platform-tools文件夹里面adb.exe AdbWinApi.dll AdbWinUsbApi.dll拷贝到tools   ...

  4. Android apk程序调用其它的APK程序

    Intent mIntent = new Intent(); ComponentName comp = new ComponentName("启动的APK包名","启动的 ...

  5. [Android]APK程序卸载二次确认的实现

    严正声明        本人本着技术开放,思想分享的目的,撰写本文.文章仅供参考之用,请勿使之于非法或有害于社会和谐之用. Sodino 2011-01-24 Android上能不能实现卸载时提示呢, ...

  6. android apk程序升级

    1 .设置apk版本号 Androidmanifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/ ...

  7. Android Studio 动态调试 apk 反编译出的 smali 代码

    在信安大赛的准备过程中,主要通过 Android Studio 动态调试 apk 反编译出来的 smali 代码的方式来对我们分析的执行流程进行验证.该技巧的主要流程在此记录.以下过程使用 Andro ...

  8. Android动态调试so库JNI_Onload函数-----基于IDA实现

    之前看过吾爱破解论坛一个关于Android'逆向动态调试的经验总结帖,那个帖子写的很好,对Android的脱壳和破解很有帮助,之前我们老师在上课的时候也讲过集中调试的方法,但是现在不太实用.对吾爱破解 ...

  9. 开启Android Apk调试与备份选项的Xposed模块的编写

    本文博客地址:https://blog.csdn.net/QQ1084283172/article/details/80963610 在进行Android应用程序逆向分析的时候,经常需要进行Andro ...

随机推荐

  1. Linux速通04 用户、群组、权限

    用户及passwd文件 # /etc/passwd文件的功能:存储所有用户的相关信息,实际上是存放用户信息的数据库(database) # 各个字段的含义: * 第一个字段(列)记录的是这个用户的名字 ...

  2. Shiro反序列化<=1.2.4 复现

    Apache Shiro是一个Java安全框架,执行身份验证.授权.密码和会话管理.    shiro默认使用了CookieRememberMeManager,其处理cookie的流程是:得到reme ...

  3. css整理之-----------基本知识

    盒子模型 所有HTML元素可以看作盒子,它包括:边距,边框,填充,和实际内容. CSS中组成一个块级盒子需要: Content box: 这个区域是用来显示内容,大小可以通过设置 width 和 he ...

  4. Spring Boot 自动配置 源码分析

    Spring Boot 最大的特点(亮点)就是自动配置 AutoConfiguration 下面,先说一下 @EnableAutoConfiguration ,然后再看源代码,到底自动配置是怎么配置的 ...

  5. 深入理解Java并发框架AQS系列(二):AQS框架简介及锁概念

    深入理解Java并发框架AQS系列(一):线程 深入理解Java并发框架AQS系列(二):AQS框架简介及锁概念 一.AQS框架简介 AQS诞生于Jdk1.5,在当时低效且功能单一的synchroni ...

  6. fianl关键词

    一.final关键字概述 final关键字具有最终或不可改变的含义,可用于修饰类.变量.方法.因此被final修饰的类.变量.方法具有以下特征: --final修饰的类不能被继承: --final修饰 ...

  7. od快捷键

    视图.查看相关: Alt+l  记录 Alt+e 可执行模块 Alt+m 内存 Alt+c cpu(反汇编视图) Ctrl+p 补丁 Alt+k 调用堆栈 Alt+b 断点 Alt+f5 设置窗口总在 ...

  8. [BFS]P1434 [SHOI2002]滑雪

    P1434 [SHOI2002]滑雪 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者 ...

  9. 为什么要放弃 JSP?他们终于给出了答案

    前言 以前的项目大多数都是Java程序猿又当爹又当妈,既搞前,又搞后端. 随着时代的发展,渐渐的许多大中小公司开始把前后端的界限分的越来越明确,前端工程师只负责前端的事情,后端工程师只管后端的事情.正 ...

  10. java面试一日一题:如何优化sql

    问题:请讲下在mysql下如何优化sql 分析:该问题主要考察对mysql的优化,重点考虑对索引优化的掌握. 回答要点: 主要从以下几点去考虑, 1.什么样的sql需要优化? 2.怎么对sql进行优化 ...