如何读懂新的Dex格式

瞅了瞅上次写文章竟然还是六年前,这些年都是在不断的攻克问题,很少整理,还是来继续偶尔整理下吧。

这一篇基于当时自己做的内部指导:如何读懂安卓下新的Dex格式。

Here are two references on the official site:

https://source.android.com/devices/tech/dalvik/dalvik-bytecode                        

-   site1 : Introduce the correspondence between mnemonics in dalvik-bytecode and actual mnemonics/syntax

https://source.android.com/devices/tech/dalvik/instruction-formats?hl=en      

-   site2 : Introduce the format in dalvik-bytecode

Take the new instruction code 45cc as example.

From site1, you will find info below:

From table above, we know the op code is 'fa', and the format/id is '45cc'. (if you want to know more about '45cc', you can find details in site1 at the beginning)

    

And '45cc' is the ID we use to find the instruction formats in site2, the matched info is below:

For us, the above table is useful as we would use it to read the bytecode in dex file.

As we can see from the format below, each array in one dex file, if the operation is invoke-polymorphic, the data would follow the format below:

Explanation:

A represents the number of parameter in this method.

BBBB represents the index of the method in this dex file

HHHH represents the index of the proto type in this dex file

C represents the method return value

 

Explanation:

In this column, it explains if different number of parameters, which of the parameter is used.

In other words, although G, F, E and D represents the arguments, but not all used.

That is the two sites tell. 

So in dex file, if we think it use the method of invoke-polymorphic, for sure, the format is like:

A|G|op BBBB F|E|D|C HHHH

Let's take a look at in real dex file.

You can download some libraries to read the dex file.

         (Smali Libraries download: JesusFreke / smali / Downloads — Bitbucket )

Use command below to make the dex file readable.

java -jar baksmali-2.5.2.jar dump  [dex file] > [output file]

Then we got info very clearly:

In line64396, we got some bytes which is invoke-polymorphic: fa30 cb00 0301 0e00

The byte codes follow the format we introduce before. 

But we should not read as what we see!

Normally, if you read site2 document, in bitwise descriptions chapter:

It consists of one or more space-separated "words" each of which describes a 16-bit code unit. Each character in a word represents four bits, read from high bits to low, with vertical bars ("|") interspersed to aid in reading.

So, we should read as : 30fa 00cb 0103 000e

That is the right data matched the format: A|G|op BBBB F|E|D|C HHHH

So the analysis result is :

A: 3

G: 0 unused

Op code: 0xfa

BBBB: 0x00cb (method section index)

F: 0 unused

E: 1

D: 0

C: 3

HHHH: 0x000e (protocol section index)

That is all for this part, hope you have ability read from dex file now.

Ref: Android O and DEX 38: Dalvik Opcodes for Dynamic Invocation – JEB in Action

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DEX文件格式Android中的一种可执行文件格式,全称为Dalvik Executable Format。DEX文件主要用于存储和执行Android应用程序的字节码。 DEX文件的结构由标头区域、字符串表、类型表、字段表、方法表、类定义和数据区组成。 标头区域是DEX文件的开始部分,包含了一些元数据信息,例如魔数、版本号、校验和等。字符串表用于存储DEX文件中使用的字符串常量,例如类名、方法名、变量名等。类型表用于存储类的完整类型信息,包括类名、父类名、接口等。字段表和方法表分别用于存储类中的字段和方法的详细信息,例如访问权限、名称、类型等。 类定义部分是DEX文件的核心,用于存储所有类的定义信息。每个类都有一个类定义项,包含了该类的访问标志、类名、父类名、实现的接口、字段和方法等详细信息。 数据区是DEX文件的最后部分,用于存储类及其相关信息的具体数据。数据区包括常量池、方法体、注解和调试信息等。常量池存储了类中使用的常量,例如字符串、数字、类引用等。方法体包含了方法的字节码指令和异常处理表。注解用于存储类、字段和方法的元数据信息。调试信息用于存储调试器需要的信息,例如行号映射表和本地变量表等。 总之,DEX文件格式是一种用于存储和执行Android应用程序字节码的文件格式,通过包含标头区域、字符串表、类型表、字段表、方法表、类定义和数据区等部分,有效地组织和管理类及其相关信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值