odex转dex

1、下载4个文件 smali.jar backsmali.jar  smali backsmali

https://bitbucket.org/JesusFreke/smali/downloads/

2、假设现在我们要对CardManager.odex进行反编译

在当前文件夹下新建system文件夹,并在system文件夹下建立framework文件夹,然后将常用的一些odex放入framework文件夹下,如core.odex, ext.odex, framework.odex, android.policy.odex, services.odex

当前文件夹下还要有第一步下载的四个文件

G:\framework>java -jar E:\Android\dextojarAndbaksmali\dextojar\smali\baksmali-2.0.3.jar --help
usage: java -jar baksmali.jar [options] <dex-file>
disassembles and/or dumps a dex file
 -?,--help                                  prints the help message then exits.
                                            Specify twice for debug options
 -a,--api-level <API_LEVEL>                 The numeric api-level of the file
                                            being disassembled. If not
                                            specified, it defaults to 15 (ICS).
 -b,--no-debug-info                         don't write out debug info (.local,
                                            .param, .line, etc.)
 -c,--bootclasspath <BOOTCLASSPATH>         the bootclasspath jars to use, for
                                            analysis. Defaults to
                                            core.jar:ext.jar:framework.jar:andro

                                            id.policy.jar:services.jar. If the
                                            value begins with a :, it will be
                                            appended to the default
                                            bootclasspath instead of replacing
                                            it
 -d,--bootclasspath-dir <DIR>               the base folder to look for the
                                            bootclasspath files in. Defaults to
                                            the current directory
 -f,--code-offsets                          add comments to the disassembly
                                            containing the code offset for each
                                            address
 -i,--resource-id-files <FILES>             the resource ID files to use, for
                                            analysis. A colon-separated list of
                                            prefix=file pairs.  For example
                                            R=res/values/public.xml:android.R=$A

                                            NDROID_HOME/platforms/android-19/dat

                                            a/res/values/public.xml
 -j,--jobs <NUM_THREADS>                    The number of threads to use.
                                            Defaults to the number of cores
                                            available, up to a maximum of 6
 -l,--use-locals                            output the .locals directive with
                                            the number of non-parameter
                                            registers, rather than the .register

                                            directive with the total number of
                                            register
 -m,--no-accessor-comments                  don't output helper comments for
                                            synthetic accessors
 -o,--output <DIR>                          the directory where the disassembled

                                            files will be placed. The default is

                                            out
 -p,--no-parameter-registers                use the v<n> syntax instead of the
                                            p<n> syntax for registers mapped to
                                            method parameters
 -r,--register-info <REGISTER_INFO_TYPES>   print the specificed type(s) of
                                            register information for each
                                            instruction. "ARGS,DEST" is the
                                            default if no types are specified.
                                            Valid values are:
                                            ALL: all pre- and post-instruction
                                            registers.
                                            ALLPRE: all pre-instruction
                                            registers
                                            ALLPOST: all post-instruction
                                            registers
                                            ARGS: any pre-instruction registers
                                            used as arguments to the instruction

                                            DEST: the post-instruction
                                            destination register, if any
                                            MERGE: Any pre-instruction register
                                            has been merged from more than 1
                                            different post-instruction register
                                            from its predecessors
                                            FULLMERGE: For each register that
                                            would be printed by MERGE, also show

                                            the incoming register types that
                                            were merged
 -s,--sequential-labels                     create label names using a
                                            sequential numbering scheme per
                                            label type, rather than using the
                                            bytecode address
 -v,--version                               prints the version then exits
 -x,--deodex                                deodex the given odex file. This
                                            option is ignored if the input file
                                            is not an odex file



在当前目录下运行,-d属性最好指定

java -jar baksmali-2.0.3.jar -a 12 -d ./system/framework -o out -x CardManager.odex 

成功后会在当前文件夹下会生成一个out文件夹

3、生成 classes.dex文件

G:\11\framework>java -jar E:\Android\dextojarAndbaksmali\dextojar\smali\smali-2.0.3.jar --h
usage: java -jar smali.jar [options] [--] [<smali-file>|folder]*
assembles a set of smali files into a dex file
 -?,--help                      prints the help message then exits. Specify
                                twice for debug options
 -a,--api-level <API_LEVEL>     The numeric api-level of the file to generate,
                                e.g. 14 for ICS. If not specified, it defaults
                                to 15 (ICS).
 -j,--jobs <NUM_THREADS>        The number of threads to use. Defaults to the
                                number of cores available, up to a maximum of 6
 -o,--output <FILE>             the name of the dex file that will be written.
                                The default is out.dex
 -v,--version                   prints the version then exits
 -x,--allow-odex-instructions   allow odex instructions to be compiled into the
                                dex file. Only a few instructions are supported
                                - the ones that can exist in a dead code path
                                and not cause dalvik to reject the class


运行 java -jar smali-2.0.3.jar out/ -a 12 -o classes.dex out

成功后会在当前文件夹下会生成一个classes.dex文件




Android odex文件反编译

转:http://www.cnblogs.com/ungshow/p/3511330.html

odex 是经过优化的dex文件,且独立存在于apk文件。odex 多用于系统预制应用或服务。通过将apk中的dex文件进行 odex,可以加载 apk 的启动速度,同时减小空间的占用。请参考ODEX关于 odex 的说明。

在反编译 odex 文件的过程中,我们需要使用到以下工具

smali/baksmali是odex(dex)与smali文件格式互相转换的两个工具,dex2jar则是将dex文件转为java的jar文件,JD Compiler用于反编译jar文件。也就是说,经过以上一系列的操作,我们最终可以从一个odex文件得到一个可读的java文件。(事实上,也不是完全可读,与源码上还是有差别,有时候部分代码还无法反编译过来,只能以jdk虚拟机指令的方式存在了)。

首先,一个 odex 文件的生成过程是:java -> class -> dex -> odex,那么反编译的就是上面过程的逆操作了:odex -> dex -> class -> java。

我的测试环境:

  • Android 4.1.2
  • Samsung Galaxy II

以Android系统中的 uiautomator.odex 文件为例,目标是反编译其源码(其实它的源码grepcode).


工具准备

创建一个临时目录test,将 smali/baksmali 相关的工具都放入其中。

反编译 (odex -> dex)

首先,将目标 odex 文件拿出来。

cd test
adb pull /system/framework/uiautomator.odex

在合成 odex 文件过程中,还需要用到很多依赖文件,它们同样也是 odex 格式的。因此在合成时,我们需要根据情况反复从手机中抽取相关的依赖包。

关于命令的使用,直接执行 java -jar baksmali-2.0.2.jar --help可以得到相关的使用说明。这里要用到的参数主要是:

  • [-a | --api-level]: Android API等级,Android 4.1.2是16
  • [-x | --deodex]: 操作,反编译odex
  • [-d|--bootclasspath-dir]: 依赖包的目录,我们用当前目录.

开始反编译,执行以下命令:

D:\test>java -jar baksmali-2.0.2.jar -a 16 -d . -x uiautomator.odex

 
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)


以上的异常表明,反编译的过程缺少依赖包/system/framework/core-junit.odex,那就从系统中提取。

D:\test>adb pull /system/framework/core-junit.odex
 
# 重复
D:\test>java -jar baksmali-2.0.2.jar -a 16 -d . -x uiautomator.odex
# 如果还有缺失的依赖包,则反复从手机上提取

反编译 uiautomator.odex 总共需要使用到以下依赖包:

D:\test>ls *.odex
android.policy.odex  bouncycastle.odex  core.odex  framework.odex   sec_edm.odex    services.odex
apache-xml.odex      core-junit.odex    ext.odex   framework2.odex  seccamera.odex  uiautomator.odex

baksmali 执行成功后,会产生一个  out  目录,里面放的是中间文件。这时,可以使用这些中间文件来生成 dex 文件:

D:\test>java -jar smali-2.0.2.jar -a 16 -o classes.dex out
 
## 解压 dex2jar 到 test 目录
D:\test\dex2jar-0.0.9.15>d2j-dex2jar.bat ..\classes.dex
dex2jar classes.dex -> classes-dex2jar.jar


classes-dex2jar.jar 便是我们要得到java jar包。通过JD Compiler打开这个jar可以看到反编译后的java内容。

之所以反编译 uiautomator,是因为Android SDK中给出的 uiautomator.jar 包中很多API都没有包含其中,也没有在其官方文档中给予说明。


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值