移动安全Android逆向系列:Dalvik概念&破解实例

本篇文章是Android逆向系列的第三篇,开始介绍Dalvik虚拟机的相关知识,认识dex和smali文件格式和熟悉Dalvik字节码及指令集,对Dalvik指令集有个大概的了解就可以开始简单的反编译静态分析了,后面提及了安卓开发四大组件和使用Eclipse开发一个简单的apk例子,最后以一个破解实例加深全文的知识概念,进一步熟悉工具的使用及Dalvik指令集。

一、Dalvik

1、Dalvik介绍

Dalvik是google专门为Android操作系统设计的一个虚拟机,Dalvik VM是基于寄存器的,而JVM是基于栈的;Dalvik有专属的文件执行格式dex(dalvik executable),而JVM则执行的是java字节码。Dalvik VM比JVM速度更快,占用空间更少。

在Java代码中我们无法修改某个逻辑,所以需要将java代码翻译成smali代码,也就是将dex文件转换为smali文件。可以这样理解,dalvik里面的smali是可以修改的,而java代码是修改不了的,那么我们想要去破解也就是把Java代码改成smali代码,修改smali代码之后再回编译回去同时java逻辑也发生了改变,这是一种破解的思路。

Smali格式是dex格式的一种直观可读形式

Smali文件可以认为是Davilk的字节码文件

详见后续的Smali介绍

2、Dalvik寄存器命名法

Dalvik虚拟机参数传递方式中的规定:假设一个函数使用到M个寄存器,其中函数的参数是N个,那么参数使用最后的N个寄存器,局部变量使用从头开始的前M-N个寄存器

Dalvik寄存器有两种命名法

v命名法

v命名法采用以小写字母“v”开头的方式表示函数中用到的局部变量与参数,所有的寄存器命名从v0开始,依次递增。

参数寄存器 v(m-n)~vm
局部变量寄存器 v0~vn

p命名法

基本上类似,主要是参数寄存器是使用p命名寄存器,而局部变量寄存器还是使用v命名寄存器

参数寄存器 p0~pn
变量寄存器 v0~vn

3、v命名法Smali代码分析

Smali代码如下图,首先看第一行

static public DecryptDemo->getHelloWorld(Ljava/lang/string;I)Ljava/lang/string;

第一行中调用了一个getHelloWorld()方法,括号内的表示有两个参数Ljava/lang/StringI,用分号;隔开,返回值的类型为Ljava/lang/String

中间部分的.regsize:[5]表示有5个寄存器

第一个红框中调用了方法将v2、v3寄存器值存入,返回了一个v2。第二个红框中调用了方法将v0、v4寄存器值存入,返回一个v0。

invoke-virtual虚方法调用,调用的方法运行时确认实际调用,和实例引用的实际对象有关,动态确认的

h8anhT.png

4、p命名法Smali代码分析

同样第一行可以看出调用了一个getHelloWorld()方法,两个参数Ljava/lang/StringI,用分号;隔开,返回值的类型为Ljava/lang/String

invoke-virtual {v1, p0}, Ljava/lang/stringBuilder;->append (Ljava/lang/String;)Ljava/lang/StringBuilder;move-result-object v1

第一个红框在LJava/lang/StringBuilder类中调用了一个append的方法拼接传来的String,返回一个LJava/lang/StringBuilder类型,传入参数位于p0处,传出参数位于v1处,返回的是一个move-result-object

第二个红框类似,调用了一个append的方法拼接传来的String返回一个LJava/lang/StringBuilder类型,传入参数位于p1处,传出参数位于v0处

h8amNV.png

5、dex文件反编译工具

Dalvik 虚拟机并不支持直接执行 JAVA 字节码,所以会对编译生成的 .class 文件进行翻译、重构、解释、压缩等处理,这个处理过程是由 dx 进行处理,处理完成后生成的产物会以 .dex 结尾,称为 Dex 文件。

整个编译/反编译涉及到的工具及流程如下:

1)编译出smali文件流程

.java ==> .class ==> .dex ==> .smali

2)dx.jar脚本将class文件打包成dex文件

dx 
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android虚拟机Dalvik完整源码,宝贵资源,欢迎下载! This directory contains the Dalvik virtual machine and core class library, as well as related tools, libraries, and tests. A note about the licenses and header comments --------------------------------------------- Much of the code under this directory originally came from the Apache Harmony project, and as such contains the standard Apache header comment. Some of the code was written originally for the Android project, and as such contains the standard Android header comment. Some files contain code from both projects. In these cases, the header comment is a combination of the other two, and the portions of the code from Harmony are identified as indicated in the comment. Here is the combined header comment: /* * Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ---------- * * Portions of the code surrounded by "// BEGIN Harmony code" and * "// END Harmony code" are copyrighted and licensed separately, as * follows: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Native SH call bridge --------------------- Native SH call bridge is written by Shin-ichiro KAWASAKI and Contributed to Android by Hitachi, Ltd. and Renesas Solutions Corp.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

网安溦寀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值