- 一、ART 虚拟机下 DexClassLoader 类加载器脱壳点总结
- 1、file_magic.cc#OpenAndReadMagic 函数
- 2、dex_file.cc#DexFile::OpenCommon
- 3、dex_file.cc#DexFile::DexFile
- 总结 ( 兼容 InMemoryDexClassLoader 和 DexClassLoader 两种类加载器的 脱壳点 )
从 /art/runtime/dex_file.cc#DexFile::Open 函数开始分析脱壳点位置 ;
其中调用的 /art/runtime/base/file_magic.cc#OpenAndReadMagic 函数 , 可以作为脱壳点 ;
在 /art/runtime/dex_file.cc#OpenFile 函数中 , 调用了 /art/runtime/dex_file.cc#OpenCommon 函数 , 是脱壳点 ;
bool DexFile::Open(const char* filename,
const std::string& location,
bool verify_checksum,
std::string* error_msg,
std::vector* dex_files) {
ScopedTrace trace(std::string("Open dex file ") + std::string(location));
DCHECK(dex_files != nullptr) push_back(std::move(dex_file));
return true;
} else {
return false;
}
}
*error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
return false;
}
1、file_magic.cc#OpenAndReadMagic 函数
file_magic.cc#OpenAndReadMagic 函数是 脱壳点 , 第一个参数 const char* filename 是 Dex 文件的路径 ;
调用该函数的上一个调用位置是 /art/runtime/dex_file.cc#DexFile::Open 函数 ;
file_magic.cc 源码 :
#include "file_magic.h"
#include
#include
#include
#include "android-base/stringprintf.h"
#include "base/logging.h"
#include "base/unix_file/fd_file.h"
#include "dex_file.h"
namespace art {
using android::base::StringPrintf;
File OpenAndReadMagic(const char* filename, uint32_t* magic, std::string* error_msg) {
CHECK(magic != nullptr);
File fd(filename, O_RDONLY, /* check_usage */ false);
if (fd.Fd() == -1) {
*error_msg = StringPrintf("Unable to open '%s' : %s", filename, strerror(errno));
return File();
}
int n = TEMP_FAILURE_RETRY(read(fd.Fd(), magic, sizeof(*magic)));
if (n != sizeof(*magic)) {
*error_msg = StringPrintf("Failed to find magic in '%s'", filename);
return File();
}
if (lseek(fd.Fd(), 0, SEEK_SET) != 0) {
*error_msg = StringPrintf("Failed to seek to beginning of file '%s' : %s", filename,
strerror(errno));
return File();
}
return fd;
}
bool IsZipMagic(uint32_t magic) {
return (('P' == ((magic >> 0) & 0xff)) &&
('K' == ((magic >> 8) & 0xff)));
}
bool IsDexMagic(uint32_t magic) {
return DexFile::IsMagicValid(reinterpret_cast(&magic));
}
} // namespace art
源码路径 : /art/runtime/base/file_magic.cc#OpenAndReadMagic
2、dex_file.cc#DexFile::OpenCommondex_file.cc#DexFile::OpenCommon 函数中 , 可以获取 Dex 文件在内存中的起始地址 ;
注意 : 该脱壳点 与 InMemoryDexClassLoader 类加载器的脱壳点重合 ;
std::unique_ptr 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;
}
std::unique_ptr 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#OpenCommon
3、dex_file.cc#DexFile::DexFile在 dex_file.cc#DexFile::DexFile 构造函数中 , 可以获取到 Dex 文件地址 ;
注意 : 该脱壳点 与 InMemoryDexClassLoader 类加载器的脱壳点重合 ;
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(base)),
string_ids_(reinterpret_cast(base + header_->string_ids_off_)),
type_ids_(reinterpret_cast(base + header_->type_ids_off_)),
field_ids_(reinterpret_cast(base + header_->field_ids_off_)),
method_ids_(reinterpret_cast(base + header_->method_ids_off_)),
proto_ids_(reinterpret_cast(base + header_->proto_ids_off_)),
class_defs_(reinterpret_cast(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)
关注
打赏
- 【Android Gradle 插件】Gradle 自定义 Plugin 插件 ③ ( 自定义插件作用 | Android Gradle 插件的扩展 | 自定义 Extension 扩展 )
- 【Android Gradle 插件】Gradle 构建生命周期 ③ ( BuildListener 构建监听器 | TaskExecutionGraphListener 任务执行图监听器 )
- 【Android Gradle 插件】Gradle 构建生命周期 ② ( Gradle 类的添加构建生命周期监听器函数 | Gradle#addListener 函数 )
- 【Android Gradle 插件】Gradle 构建生命周期 ① ( 分析构建脚本 | 执行初始化配置 | 执行 Gradle 任务 | Project#beforeEvaluate 函数 )
- 【Android Gradle 插件】自定义 Gradle 任务 ⑨ ( 控制 Gradle 执行任务顺序 | Task#finalizedBy 函数 | 控制 Gradle 执行任务顺序示例分析 )
- 【Android Gradle 插件】自定义 Gradle 任务 ⑧ ( 控制 Gradle 执行任务顺序 | Task#shouldRunAfter 函数 | 三个函数使用场景对比 )
- 【Android Gradle 插件】自定义 Gradle 任务 ⑦ ( 控制 Gradle 执行任务顺序 | Task#dependsOn 函数 | Task#mustRunAfter 函数 )
- 【数学分析】集合 ① ( 集合概念 | 集合表示 | 常用的数集合 | 集合的表示 )
- 【数学分析】学科简介 ( 初等数学缺陷 | 微分与积分 | 学习数学分析的目的 | 数学分析与高等数学对比 )
- 【Android Gradle 插件】自定义 Gradle 任务 ③ ( Gradle 自定义任务创建方法 Project#task 函数 | Task#doFirst 函数用法 )