【Android 逆向】ART 脱壳 ( InMemoryDexClassLoader 脱壳 | DexFile.java 对应的 dalvik_system_DexFile.cc 本地函数分析 )
文章目錄
- 一、DexFile 對應的 dalvik_system_DexFile.cc 中的 Native 方法
- 1、dalvik_system_DexFile.cc 的 DexFile_createCookieWithDirectBuffer 函數
- 2、dalvik_system_DexFile.cc 的 DexFile_createCookieWithArray 函數
- 二、dalvik_system_DexFile.cc 的 CreateSingleDexFileCookie 函數
一、DexFile 對應的 dalvik_system_DexFile.cc 中的 Native 方法
在上一篇博客 【Android 逆向】ART 脫殼 ( InMemoryDexClassLoader 脫殼 | DexFile 構造函數及相關調用函數 | Android 源碼中查找 native 函數 ) 中 , 分析了 DexFile 構造函數 , 以及 makeInMemoryDexElements 函數 ; 并查找了 DexFile 中的 native 函數 createCookieWithDirectBuffer 和 createCookieWithArray 函數定義在 /art/runtime/native/dalvik_system_DexFile.cc 中 ;
1、dalvik_system_DexFile.cc 的 DexFile_createCookieWithDirectBuffer 函數
在 DexFile_createCookieWithDirectBuffer 函數中 , 調用的 memcpy 方法中的 dex_mem_map->Begin() 就是 dex 文件的起始地址 , length 是 dex 文件的長度 , 這里可以將內存中的 dex 數據導出 ;
// ★ 此處的 dex_mem_map->Begin() 就是 dex 文件的起始地址 , length 是 dex 文件的長度memcpy(dex_mem_map->Begin(), base_address, length);// DexFile 中 createCookieWithDirectBuffer 函數對應的 C++ 函數 static jobject DexFile_createCookieWithDirectBuffer(JNIEnv* env,jclass,jobject buffer,jint start,jint end) {uint8_t* base_address = reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(buffer));if (base_address == nullptr) {ScopedObjectAccess soa(env);ThrowWrappedIOException("dexFileBuffer not direct");return 0;}std::unique_ptr<MemMap> dex_mem_map(AllocateDexMemoryMap(env, start, end));if (dex_mem_map == nullptr) {DCHECK(Thread::Current()->IsExceptionPending());return 0;}size_t length = static_cast<size_t>(end - start);// ★ 此處的 dex_mem_map->Begin() 就是 dex 文件的起始地址 , length 是 dex 文件的長度memcpy(dex_mem_map->Begin(), base_address, length);// ★ 核心跳轉return CreateSingleDexFileCookie(env, std::move(dex_mem_map)); }
源碼路徑 : /art/runtime/native/dalvik_system_DexFile.cc
2、dalvik_system_DexFile.cc 的 DexFile_createCookieWithArray 函數
DexFile_createCookieWithDirectBuffer 和 DexFile_createCookieWithArray 222 個 native 方法 , 在最后返回時都調用了 CreateSingleDexFileCookie 方法 ;
// DexFile 中 createCookieWithArray 函數對應的 C++ 函數 static jobject DexFile_createCookieWithArray(JNIEnv* env,jclass,jbyteArray buffer,jint start,jint end) {std::unique_ptr<MemMap> dex_mem_map(AllocateDexMemoryMap(env, start, end));if (dex_mem_map == nullptr) {DCHECK(Thread::Current()->IsExceptionPending());return 0;}auto destination = reinterpret_cast<jbyte*>(dex_mem_map.get()->Begin());env->GetByteArrayRegion(buffer, start, end - start, destination);// ★ 核心跳轉return CreateSingleDexFileCookie(env, std::move(dex_mem_map)); }
源碼路徑 : /art/runtime/native/dalvik_system_DexFile.cc
二、dalvik_system_DexFile.cc 的 CreateSingleDexFileCookie 函數
在該方法中 , 主要創建 dex_file 實例 , 然后將該實例對象返回到 Java 層 ; 傳入的參數是 CreateDexFile(env, std::move(data)) , 下面開始分析該函數 ;
dalvik_system_DexFile.cc 的 CreateSingleDexFileCookie 函數源碼 :
static jobject CreateSingleDexFileCookie(JNIEnv* env, std::unique_ptr<MemMap> data) {// ★ 創建 dex_file std::unique_ptr<const DexFile> dex_file(CreateDexFile(env, std::move(data)));if (dex_file.get() == nullptr) {DCHECK(env->ExceptionCheck());return nullptr;}std::vector<std::unique_ptr<const DexFile>> dex_files;dex_files.push_back(std::move(dex_file));return ConvertDexFilesToJavaArray(env, nullptr, dex_files); }源碼路徑 : /art/runtime/native/dalvik_system_DexFile.cc#CreateSingleDexFileCookie
總結
以上是生活随笔為你收集整理的【Android 逆向】ART 脱壳 ( InMemoryDexClassLoader 脱壳 | DexFile.java 对应的 dalvik_system_DexFile.cc 本地函数分析 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Android 逆向】ART 脱壳 (
- 下一篇: 【Android 逆向】ART 脱壳 (