odex知多少

odex顧名思義,它就是optimized dex,之所以要優化,就是爲了節省內存空間和加速app啓動。因爲當我們安裝一個app到android設備上時,首先會對應用對應中dex文件進行優化,優化產物就會存在/data/dalvik-cache/中,這樣每次加載應用時就沒必要每次都進行優化。此外,從逆向角度來看odex化的apk,也增加了逆向的難度,下面就反編譯odex後的apk步驟:


 smali/baksmali是odex與dex文件格式互相轉換的兩個工具,dex2jar則是將dex文件轉爲java的jar文件,JD Gui用於反編譯jar文件。也就是說,經過以上一系列的操作,我們最終可以從一個odex文件得到一個可讀的java文件。(事實上,也不是完全可讀,與源碼上還是有差別,有時候部分代碼還無法反編譯過來,只能以jdk虛擬機指令的方式存在了)。此外,一個 odex 文件的生成過程是:java -> class -> dex -> odex,那麼反編譯的就是上面過程的逆操作了:odex -> dex -> class -> java。


工具使用方法(命令)

準備工作

所需工具以及下載地址:

Baksmali :

http://code.google.com/p/smali/downloads/list

 

Smali :

http://code.google.com/p/smali/downloads/list

 

Dex2jar :

http://code.google.com/p/dex2jar/downloads/list

 

JD-GUI (Java Decompile GUI) :

http://java.decompiler.free.fr/?q=jdgui<!--[if !supportNestedAnchors]--><!--[endif]-->

 

AutoSign :

http://d.download.csdn.net/down/2768910/fjfdszj

 

Apktool

http://code.google.com/p/android-apktool/downloads/list

反編譯步驟:

下面以app.odex和app.apk爲例進行分析,該文件均存放在test目錄:

Step1. 使用 baksmali.jar 將 odex 文件分解爲 smali 文件

$ java –jar baksmali-1.2.5.jar –x app.odex

如果成功的話,會在 test目錄下生成一個 out目錄,裏面是一些以“.smali”爲後綴名的文件,在此不深究這些文件的作用。

關於命令的使用,直接執行 java -jar baksmali-2.0.2.jar 可以得到相關的使用說明。這裏要用到的參數主要是:

  • [-a | --api-level]: Android API等級,Android 4.1.2是16
  • [-x | --deodex]: 操作,反編譯
  • [-d|--bootclasspath-dir]: 依賴包的目錄,我們用當前目錄.

開始反編譯,執行以下命令:

<span style="font-family:KaiTi_GB2312;">D:\test>java -jar baksmali-2.0.2.jar -a 16 -x app.odex -d .
 
Error occured while loading boot class path files. Aborting.
org.jf.util.ExceptionWithContext: Cannot locate boot class path file /system/framework/core-junit.odex
        at org.jf.dexlib2.analysis.ClassPath.loadClassPathEntry(ClassPath.java:217)
        at org.jf.dexlib2.analysis.ClassPath.fromClassPath(ClassPath.java:161)
        at org.jf.baksmali.baksmali.disassembleDexFile(baksmali.java:59)
        at org.jf.baksmali.main.main(main.java:274)</span>


以上的異常表明,反編譯的過程缺少依賴包/system/framework/core-junit.odex,那就從系統中提取。

採用adb pull/system/framework/core-junit.odex . 即可

 

Step2. 使用 smali.jar將 out/目錄下的smali文件轉換爲 classes.dex

$ java -Xmx512M –jar smali-1.2.5.jar out –o classes.dex

classes.dex便是Dalvik VM所使用的編譯後的類文件格式,在正常的apk文件裏都會有。

這步常會報如下錯誤:


出現此類錯誤,只要在out目錄下找到out\android\support\v4\app\ActivityOptionsCompatJB.smali刪掉,重新執行java -Xmx512M –jar smali-1.2.5.jar out –o classes.dex即可 

Step3. 使用 dex2jar將classes.dex反編譯爲jar文件

將下載後的dex2jar壓縮包解壓後,裏面會有dex2jar.sh(和dex2jar.bat)文件,假如classes.dex文件與dex2jar.sh在同一目錄下,使用以下方式將classes.dex反編譯爲jar文件:

$dex2jar.sh classes.dex

如果執行成功,則會在當前目錄下生成反編譯後的文件classes_dex2jar.jar。

dex2jar即可以操作dex文件,也可以直接操作apk文件,它的使用規則爲:

dex2jar file1.dexORapk file2.dexORapk ...

 

Step4. 使用JD-GUI查看反編譯後的jar文件

JD-GUI是一個可視化的Java反編譯代碼查看器,它可以實時的將class文件反編譯成java文件進行查看。解壓下載的jd-gui文件,執行目錄中的jd-gui可執行文件啓動,然後加載上一步中反編譯好的classes_dex2jar.jar文件即可。

 

Step5. 將從odex反編譯後的classes.dex與其他資源文件重新打包成一個完整的apk

以上我們假設的情況是應用程序編譯後的類文件從apk文件中被剝離出來,下面要做的是如何將上述步驟中得到的classes.dex與apk中的其他文件重新打包成一個可用的apk。

首先將反編譯後的classes.dex和原先的app.apk(不含classes.dex)重新壓縮成一個完整的app.apk(apk文件可用壓縮工具打開),也就是說將classes.dex放進app.apk中。

將下載的AutoSign文件解壓,可以看到有signapk.jar(還有個Sign.bat)文件,執行以下命令給app.apk文件簽名,就可以生成一個可以運行的apk文件了。

$ java -jar signapk.jar testkey.x509.pem testkey.pk8 app.apk app_signed.apk

 

Step6. apktool的使用

網上還有個工具是apktool,可以對apk進行解析,反編譯資源文件,並將類文件解析成smali文件;同時還可以將解析後的文件重新打包成apk。功能和以上介紹的幾個工具類似,它的使用方法如下:

apktool d app.apk and    反編譯 app.apk到文件夾and

apktool b app                從文件夾app重建APK,輸出到ABC\dist\out.apk

具體的使用方法在此不再贅述,請參考官方網站,或者:

http://www.geeka.net/2010/05/apktool-decode-android-google-code/

 

相關概念:

WHAT IS AN ODEX FILE?

In Android file system, applications come in packages with the extension .apk. 

These application packages, or APKs contain certain .odex files whose supposed 

function is to save space. These ‘odex’ files are actually collections of parts of an

 application that are optimized before booting. Doing so speeds up the boot process,

 as it preloads part of an application. On the other hand, it also makes hacking those 

applications difficult because a part of the coding has already been extracted to

 another location before execution.

THEN COMES DEODEX

Deodexing is basically repackaging of these APKs in a certain way, such that they

 are reassembled intoclasses.dex files. By doing that, all pieces of an application 

package are put together back in one place, thus eliminating the worry of a modified 

APK conflicting with some separate odexed parts.

In summary, Deodexed ROMs (or APKs) have all their application packages put back

 together in one place, allowing for easy modification such as theming. Since no pieces 

of code are coming from any external location, custom ROMs or APKs are always

 deodexed to ensure integrity.

HOW THIS WORKS

For the more geeky amongst us, Android OS uses a Java-based virtual machine for running

 applications, called the Dalvik Virtual Machine.

 A deodexed, or .dex file contains the cache used by this virtual machine (referred to as Dalvik-cache) for a program, and it is stored inside the APK. An .odex file, on the other hand, is an

 optimized version of this same .dex file that is stored next to the APK as opposed to inside it. 

Android applies this technique by default to all the system applications.

Now, when an Android-based system is booting, the davlik cache for the Davlik VM is built

 using these.odex files, allowing the OS to learn in advance what applications will be loaded, 

and thus speeds up the booting process.

By deodexing these APKs, a developer actually puts the .odex files back inside their respective 

APK packages. Since all code is now contained within the APK itself, it becomes possible to modify 

any application package without conflicting with the operating system’s execution environment.


alvik-cache是什麼?Android系統對dalvik-cache的解釋是: 當Android啓動時,DalvikVM監視所有的程序(APK文件)和框架,並且爲他們創建一個依存關係樹。DalvikVM通過這個依存關係樹來爲每個程序優化代碼並存儲在Dalvik-cache緩存中。這樣,所有程序在運行時都會使用優化過的代碼。這就是當你刷一個新的ROM時,有時候第一次啓動時間非常非常長的原因。當一個程序(或者框架庫)發生變更,DalvikVM將會重新優化代碼並且再次將其存在緩存中。也就是說:Android安裝的程序除了apk包存儲在/data/app/之外,還會產生所有程序的緩存文件存儲在/data/dalvik-cache/中



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章