【Android 逆向】ART 脱壳 ( InMemoryDexClassLoader 脱壳 | dex_file.cc 中创建 DexFile 实例对象的相关函数分析 )
文章目录
前言
在上一篇博客 【Android 逆向】ART 脱壳 ( InMemoryDexClassLoader 脱壳 | DexFile.java 对应的 dalvik_system_DexFile.cc 本地函数分析 ) 中 , 分析了 DexFile.java 中的 createCookieWithDirectBuffer
和 createCookieWithArray
函数对应的 native 函数 ,
定义在 /art/runtime/native/dalvik_system_DexFile.cc 中的 dalvik_system_DexFile.cc 的 DexFile_createCookieWithDirectBuffer
函数 ,
这两个函数都调用了 CreateSingleDexFileCookie
函数 , 在该函数中创建了 dex_file 对象 , 传入了 CreateDexFile(env, std::move(data)) 参数 ;
一、dalvik_system_DexFile.cc#CreateDexFile 函数分析
在开始处调用 DexFile::Open
函数 , 返回 std::unique_ptr<const DexFile>
传入的参数 std::string location
是 dex 文件在内存映射的起始和结束地址 ;
dalvik_system_DexFile.cc#CreateDexFile 函数源码 :
static const DexFile* CreateDexFile(JNIEnv* env, std::unique_ptr<MemMap> dex_mem_map) {
std::string location = StringPrintf("Anonymous-DexFile@%p-%p",
dex_mem_map->Begin(),
dex_mem_map->End());
std::string error_message;
// ★ 核心跳转
std::unique_ptr<const DexFile> dex_file(DexFile::Open(location,
0,
std::move(dex_mem_map),
/* verify */ true,
/* verify_location */ true,
&error_message));
if (dex_file == nullptr) {
ScopedObjectAccess soa(env);
ThrowWrappedIOException("%s", error_message.c_str());
return nullptr;
}
if (!dex_file->DisableWrite()) {
ScopedObjectAccess soa(env);
ThrowWrappedIOException("Failed to make dex file read-only");
return nullptr;
}
return dex_file.release();
}
源码路径 : /art/runtime/native/dalvik_system_DexFile.cc#CreateDexFile
二、dex_file.cc#DexFile::Open 函数分析
传入的 const std::string& location
参数是 dex 文件在内存中的映射起止地址 ;
在该函数中 , 又调用了 OpenCommon
函数 ;
std::unique_ptr<const DexFile> DexFile::Open(const std::string& location,
uint32_t location_checksum,
std::unique_ptr<MemMap> map,
bool verify,
bool verify_checksum,
std::string* error_msg) {
ScopedTrace trace(std::string("Open dex file from mapped-memory ") + location);
CHECK(map.get() != nullptr);
if (map->Size() < sizeof(DexFile::Header)) {
*error_msg = StringPrintf(
"DexFile: failed to open dex file '%s' that is too short to have a header",
location.c_str());
return nullptr;
}
// ★ 核心跳转
std::unique_ptr<DexFile> dex_file = OpenCommon(map->Begin(),
map->Size(),
location,
location_checksum,
kNoOatDexFile,
verify,
verify_checksum,
error_msg);
if (dex_file != nullptr) {
dex_file->mem_map_.reset(map.release());
}
return dex_file;
}
源码路径 : /art/runtime/dex_file.cc
三、dex_file.cc#DexFile::OpenCommon 函数分析
在 OpenCommon
函数中 , 又新建了 DexFile
对象 , 此处调用了 DexFile
的构造函数 ;
std::unique_ptr<DexFile> DexFile::OpenCommon(const uint8_t* base,
size_t size,
const std::string& location,
uint32_t location_checksum,
const OatDexFile* oat_dex_file,
bool verify,
bool verify_checksum,
std::string* error_msg,
VerifyResult* verify_result) {
if (verify_result != nullptr) {
*verify_result = VerifyResult::kVerifyNotAttempted;
}
// ★ 核心跳转 新建 DexFile 对象
std::unique_ptr<DexFile> dex_file(new DexFile(base,
size,
location,
location_checksum,
oat_dex_file));
if (dex_file == nullptr) {
*error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location.c_str(),
error_msg->c_str());
return nullptr;
}
if (!dex_file->Init(error_msg)) {
dex_file.reset();
return nullptr;
}
if (verify && !DexFileVerifier::Verify(dex_file.get(),
dex_file->Begin(),
dex_file->Size(),
location.c_str(),
verify_checksum,
error_msg)) {
if (verify_result != nullptr) {
*verify_result = VerifyResult::kVerifyFailed;
}
return nullptr;
}
if (verify_result != nullptr) {
*verify_result = VerifyResult::kVerifySucceeded;
}
return dex_file;
}
源码路径 : /art/runtime/dex_file.cc
四、dex_file.cc#DexFile 构造函数分析
在 dex_file.cc
中的 DexFile
构造函数中 , 也存在 dex 文件在内存中的首地址 , 该地址也可以作为脱壳点 ;
DexFile::DexFile(const uint8_t* base,
size_t size,
const std::string& location,
uint32_t location_checksum,
const OatDexFile* oat_dex_file)
: begin_(base),
size_(size),
location_(location),
location_checksum_(location_checksum),
header_(reinterpret_cast<const Header*>(base)),
string_ids_(reinterpret_cast<const StringId*>(base + header_->string_ids_off_)),
type_ids_(reinterpret_cast<const TypeId*>(base + header_->type_ids_off_)),
field_ids_(reinterpret_cast<const FieldId*>(base + header_->field_ids_off_)),
method_ids_(reinterpret_cast<const MethodId*>(base + header_->method_ids_off_)),
proto_ids_(reinterpret_cast<const ProtoId*>(base + header_->proto_ids_off_)),
class_defs_(reinterpret_cast<const ClassDef*>(base + header_->class_defs_off_)),
method_handles_(nullptr),
num_method_handles_(0),
call_site_ids_(nullptr),
num_call_site_ids_(0),
oat_dex_file_(oat_dex_file) {
CHECK(begin_ != nullptr) << GetLocation();
CHECK_GT(size_, 0U) << GetLocation();
// Check base (=header) alignment.
// Must be 4-byte aligned to avoid undefined behavior when accessing
// any of the sections via a pointer.
CHECK_ALIGNED(begin_, alignof(Header));
InitializeSectionsFromMapList();
}
源码路径 : /art/runtime/dex_file.cc#DexFile
总结
在 InMemoryDexClassLoader
类加载器中 , 加载 dex 文件时 , 没有对 dex 文件进行优化 ,
DexClassLoader
加载 dex 的同时 , 会对 dex 文件进行优化 ;
上面分析的所有带 dex 文件起始地址和大小的函数 , 都可以作为脱壳点 ;
相关文章
- [Android Pro] 自己动手编译Android源码(超详细)
- [Android Pro] static 和 Volatile 的区别
- Android实例-GestureManager手势支持放大缩小左右移动上下移动
- Android实例-实现扫描二维码并生成二维码(XE8+小米5)
- Android实例-IdHTTP下载(并实现自动安装)(XE10+小米2)
- Android实例-获取安卓手机WIFI信息(XE8+小米2)
- Android实例-处理隐藏输入法后不再显示问题(XE8+小米2)
- Android实例-使用电话拨号器在移动设备上(官方)(XE8+小米2)
- 深入理解Android 自定义attr Style styleable以及其应用
- Unable to determine application id: com.android.tools.idea.run.ApkProvisionException: No outputs for the main artifact of variant: debug
- Android之Fragment简介和使用实例
- android源码分析辅助工具
- Android kotlin自定义RecyclerView实现横竖滚动功能
- Android 10.0 Launcher3中workspaces桌面去掉下拉状态栏功能
- Android设置头像,手机拍照或从本地相冊选取图片作为头像
- 关于Android SDK Manager更新速度慢的解决方法
- 【Android 逆向】类加载器 ClassLoader ( 加载 Android 组件的类加载器 | 双亲委派机制实例分析 )
- 【Android 插件化】插件化原理 ( 类加载器 )
- android widget 开发实例 : 桌面便签程序的实现具体解释和源代码 (上)
- Ant自己主动编译打包&公布 android项目
- Android笔记三十四.Service综合实例二
- Android BroadcastReceiver实例Demo(有序广播的发送)
- android外设开发实战智能家居系统
- 解读Android 4.0 Camera原生应用程序的设计思路