本文博客地址: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
- Is at least Java 1.7 installed?
- Does executing java -version on command line / command prompt return 1.7 or greater?
- If not, please install Java 7+ and make it the default.
Installation for Apktool
- Windows:
- Download Windows wrapper
script (Right click, Save Link As
apktool.bat
) - Download apktool-2 (find newest here)
- Rename downloaded jar to
apktool.jar
- Move both files (
apktool.jar
&apktool.bat
) to your Windows directory (UsuallyC://Windows
) - 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. - Try running apktool via command prompt
- Download Windows wrapper
script (Right click, Save Link As
- Linux:
- Download Linux wrapper script (Right
click, Save Link As
apktool
) - Download apktool-2 (find newest here)
- 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. - (This helps provide support for the 32bit native binary aapt, which is required by apktool)
- Rename downloaded jar to
apktool.jar
- Move both files (
apktool.jar
&apktool
) to/usr/local/bin
(root needed) - Make sure both files are executable (
chmod +x
) - Try running apktool via cli
- Download Linux wrapper script (Right
click, Save Link As
- Mac OS X:
- Download Mac wrapper script (Right
click, Save Link As
apktool
) - Download apktool-2 (find newest here)
- Rename downloaded jar to
apktool.jar
- Move both files (
apktool.jar
&apktool
) to/usr/local/bin
(root needed) - Make sure both files are executable (
chmod +x
) - Try running apktool via cli
- Download Mac wrapper script (Right
click, Save Link As
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 <[email protected]>
Updated by Connor Tumbleson <[email protected]>
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 如下圖所示:
四、開啓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
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程序 以調試等待模式啓動成功後的結果截圖如下所示:
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