JNI搜索dex中的method

Dex文件格式


dex的定義在DexFile.h里。

typedef struct DexFile {
    /* directly-mapped "opt" header */
    const DexOptHeader* pOptHeader;

    /* pointers to directly-mapped structs and arrays in base DEX */
    const DexHeader*    pHeader;
    const DexStringId*  pStringIds;
    const DexTypeId*    pTypeIds;
    const DexFieldId*   pFieldIds;
    const DexMethodId*  pMethodIds;
    const DexProtoId*   pProtoIds;
    const DexClassDef*  pClassDefs;
    const DexLink*      pLinkData;
 
    /* mapped in "auxillary" section */
    const DexClassLookup* pClassLookup;
 
    /* points to start of DEX file data */
    const u1*           baseAddr;
 
    /* track memory overhead for auxillary structures */
    int                 overhead;
 
    /* additional app-specific data structures associated with the DEX */
    void*               auxData;
} DexFile;

查找dex里class method舉例


1、把a(bǔ)pk讀進(jìn)內(nèi)存,釋放dex。

deflateDexToBuffer(apkName, &dexBuf, &length)

2、內(nèi)存里的dex來(lái)構(gòu)造dexfile

DexFile *pDexFile = dexFileParse(dexBuf, length, flags); 

3、在dexfile的頭文件里查找method個(gè)數(shù)

int count = (int) pDexFile->pHeader->methodIdsSize;

4、在dexfile里定義了所有method的id

const DexMethodId*  pMethodIds;
循環(huán)讀取里面包含的所有ids
```c
const DexMethodId* dexGetMethodId(const DexFile* pDexFile, u4 idx) {
    return &pDexFile->pMethodIds[idx];  

5、DexMethodId包含了類(lèi)id和名字id的信息

typedef struct DexMethodId {
    u2  classIdx;           /* index into typeIds list for defining class */
    u2  protoIdx;           /* index into protoIds for method prototype */
    u4  nameIdx;            /* index into stringIds for method name */
} DexMethodId;

6、根據(jù)nameIdx獲取字符串
步驟int nameidx->DexStringId* stringidx->****u4 **stringDataOff->char * **

const char* strmethod = dexStringById(pDexFile, mid->nameIdx); 

nameidx->stringidx

const DexStringId* pStringId = dexGetStringId(pDexFile, idx);

即&pDexFile->pStringIds[idx];
隱含stringidx->stringDataOff

typedef struct DexStringId {
    u4  stringDataOff;      /* file offset to string_data_item */
} DexStringId;  

**stringDataOff->char ***
調(diào)用dexGetStringData(pDexFile, pStringId);

/* return the const char* string data referred to by the given string_id */
const char* dexGetStringData(const DexFile* pDexFile,
        const DexStringId* pStringId) {
    const u1* ptr = pDexFile->baseAddr + pStringId->stringDataOff;

    // Skip the uleb128 length.
    while (*(ptr++) > 0x7f) /* empty */ ;

    return (const char*) ptr;
}

ps:dex里面用了unsigned leb128,因此要轉(zhuǎn)換
http://fgsink.blog.163.com/blog/static/16716997020124179455097/

7、獲取method所在class name類(lèi)似,不過(guò)多了一個(gè)步驟。
**步驟classidx->typdidx->****stringidx->stringDataOff->char ***

const char* strclass = dexStringByTypeIdx(pDexFile, mid->classIdx);
/*
 * Direct-mapped "type_id_item".
 */
typedef struct DexTypeId {
    u4  descriptorIdx;      /* index into stringIds list for type descriptor */
} DexTypeId;

8、保存method的name和所在的class

strcpy(pList->methodInfo[i].name, strmethod);
strcpy(pList->methodInfo[i].classtype, strclass);  

如何查找


java層調(diào)用獲取命中包的列表,參數(shù)為包的路徑。

Paste_Image.png

checkRiskInfo檢查字符串的函數(shù),調(diào)用了findtargetstring來(lái)查找。

Paste_Image.png
Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 2,041評(píng)論 0 9
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評(píng)論 19 139
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,881評(píng)論 25 709
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類(lèi)相關(guān)的語(yǔ)法,內(nèi)部類(lèi)的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚(yú)_t_閱讀 34,637評(píng)論 18 399
  • 我們常常會(huì)聽(tīng)說(shuō) Objective-C 是一門(mén)動(dòng)態(tài)語(yǔ)言,那么這個(gè)「動(dòng)態(tài)」表現(xiàn)在哪呢?我想最主要的表現(xiàn)就是 Obje...
    Ethan_Struggle閱讀 2,326評(píng)論 0 7

友情鏈接更多精彩內(nèi)容