[原创]查找内存中dex数量和位置的方法
发表于:
2016-1-16 11:13
7817
本人菜鸟一枚,大神飘过。
昨天看到有人问这个问题,这个东西放在硬盘上已经半年了,为了防止发霉,分享一下。
下面这个是去年阿里举办比赛时候有人给出的思路,但是没有给出完整的解决方案。
ibdvm->gdvm-> userDexFiles 可以找到加载的JarFile结构,进而找到dex文件地址
下面是我自己研究源码和汇编得到的解决方案(android 4.x版本):
gdvm是一个导出的结构,userDexFiles在其中的偏移是可以查找的。 在源码中通过对 gDvm.userDexFiles的搜索,锁定了一个函数有对这个的调用,dvmInternalNativeStartup(),在InternalNative.cpp。
导出设备中的libdvm.so文件,在导出表的位置查找dvmInternalNativeStartup()(_Z24dvmInternalNativeStartupv)函数。会发现:
STR.W R0, [R3,#0x314],我这个版本是+0x314。
这样就可以通过gdvm查找到userDexFiles结构的位置
//DvmGlobals在Globals.h中
gDvm是一个DvmGlobals结构的全局变量。 HashTable* userDexFiles;
下面是userDexFiles的结构:
typedef struct HashTable {
int tableSize; /* must be power of 2 */
int numEntries; /* current #of "live" entries */
int numDeadEntries; /* current #of tombstone entries */
HashEntry* pEntries; /* array on heap */ +0x0c
HashFreeFunc freeFunc;
pthread_mutex_t lock;
} HashTable;
HashEntry* pEntries存储的是本模块有几个dex模块,这指向一个HashEntry的数组
struct HashEntry {
u4 hashValue;
void* data; //DexOrJar *pDexOrJar
};
下面是DexOrJar 的结构:
typedef struct DexOrJar {
char* fileName;
bool isDex;
bool okayToFree;
RawDexFile* pRawDexFile;
JarFile* pJarFile; //+0x0c
} DexOrJar;
#这个也是可以查找到pDvmDex
struct RawDexFile {
char* cacheFileName;
DvmDex* pDvmDex;
}; typedef struct JarFile {
ZipArchive archive;
//MemMapping map;
char* cacheFileName; //+0x24
DvmDex* pDvmDex; //+0x28 pDvmDex偏移查找方法,dvmJarFileOpen()中可以找到
};
#找到了这个就找了了全部
typedef struct DvmDex {
/* pointer to the DexFile we're associated with */
DexFile* pDexFile;
/* clone of pDexFile->pHeader (it's used frequently enough) */
const DexHeader* pHeader;
/* interned strings; parallel to "stringIds" */
struct StringObject** pResStrings;
/* resolved classes; parallel to "typeIds" */
struct ClassObject** pResClasses;
/* resolved methods; parallel to "methodIds" */
struct Method** pResMethods;
/* resolved instance fields; parallel to "fieldIds" */
/* (this holds both InstField and StaticField) */
struct Field** pResFields;
/* interface method lookup cache */
struct AtomicCache* pInterfaceCache;
/* shared memory region with file contents */
MemMapping memMap;
/* lock ensuring mutual exclusion during updates */
pthread_mutex_t modLock;
} DvmDex; struct DexFile {
/* directly-mapped "opt" header */
+0x00 const DexOptHeader* pOptHeader;
/* pointers to directly-mapped structs and arrays in base DEX */
+0x04 const DexHeader* pHeader;
+0x08 const DexStringId* pStringIds;
+0x0c const DexTypeId* pTypeIds;
+0x10 const DexFieldId* pFieldIds;
+0x14 const DexMethodId* pMethodIds;
+0x18 const DexProtoId* pProtoIds;
+0x1c const DexClassDef* pClassDefs;
+0x20 const DexLink* pLinkData;
/*
* These are mapped out of the "auxillary" section, and may not be
* included in the file.
*/
+0x24 const DexClassLookup* pClassLookup;
+0x28 const void* pRegisterMapPool; // RegisterMapClassPool
/* points to start of DEX file data */
+0x2c const u1* baseAddr;
/* track memory overhead for auxillary structures */
+0x30 int overhead;
/* additional app-specific data structures associated with the DEX */
//void* auxData;
};
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)