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/中



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