首页
社区
课程
招聘
[原创]JEB脚本(二)(交叉引用 调用图)
发表于: 2020-10-27 15:21 8715

[原创]JEB脚本(二)(交叉引用 调用图)

2020-10-27 15:21
8715

https://github.com/acbocai/jeb_script
.

.

查询DEX中方法的交叉引用信息,
使用ActionXrefsDataActionContext

.
.

查询一个函数被谁调用了,或查询它内部调用了谁,
关注:
INativeCodeAnalyzer
INativeCodeModel
ICallGraphManager
ICallGraph


.

查询一个Function的交叉引用情况
或查询一个Block入口指令的引用交叉引用情况
关注:
INativeCodeAnalyzer
INativeCodeModel
IReferenceManager
IReference

.
.

获取一条指令的地址,所属Function或Block.

image.png

 
# -*- coding: utf-8 -*-
from com.pnfsoftware.jeb.client.api import IClientContext
from com.pnfsoftware.jeb.core import IRuntimeProject
from com.pnfsoftware.jeb.core.actions import ActionXrefsData, Actions, ActionContext
from com.pnfsoftware.jeb.core.units import IUnit
from com.pnfsoftware.jeb.core.units.code.android import IDexUnit
from com.pnfsoftware.jeb.core.units.code.android.dex import IDexMethod, IDexClass
 
def Test(ctx):
    input_path = r"D:\tmp\2\project\about_dex_diff\code\jsq\jsq.dex"
    class_sign  = "Lcom/BestCalculatorCN/MyCalculator;"
    method_sign = "Lcom/BestCalculatorCN/MyCalculator;->b(Lcom/BestCalculatorCN/MyCalculator;Ljava/lang/String;)V"
    unit = ctx.open(input_path);                                   
    prj = ctx.getMainProject();                                    
    dexUnit = prj.findUnit(IDexUnit);                              
    clz = dexUnit.getClass(class_sign);                            
    method = dexUnit.getMethod(method_sign);                       
 
    # 1 查询某method交叉引用列表
    # 使用(unit,操作,地址,itemid)来创建一个context对象,提供给JEB引擎,用于后续执行
    print "------------------------------------------------"
    actionXrefsData = ActionXrefsData()
    actionContext = ActionContext(dexUnit, Actions.QUERY_XREFS, method.getItemId(), None)
    if unit.prepareExecution(actionContext,actionXrefsData):
        for xref_addr in actionXrefsData.getAddresses():
            print xref_addr
 
    # 2 查询整个class的交叉引用列表
    print "------------------------------------------------"
    actionXrefsData = ActionXrefsData()
    actionContext = ActionContext(dexUnit, Actions.QUERY_XREFS, clz.getItemId(), None)
    if unit.prepareExecution(actionContext,actionXrefsData):
        for idx,xref_addr in enumerate(actionXrefsData.getAddresses()):
            print idx,xref_addr
# -*- coding: utf-8 -*-
from com.pnfsoftware.jeb.client.api import IClientContext
from com.pnfsoftware.jeb.core import IRuntimeProject
from com.pnfsoftware.jeb.core.actions import ActionXrefsData, Actions, ActionContext
from com.pnfsoftware.jeb.core.units import IUnit
from com.pnfsoftware.jeb.core.units.code.android import IDexUnit
from com.pnfsoftware.jeb.core.units.code.android.dex import IDexMethod, IDexClass
 
def Test(ctx):
    input_path = r"D:\tmp\2\project\about_dex_diff\code\jsq\jsq.dex"
    class_sign  = "Lcom/BestCalculatorCN/MyCalculator;"
    method_sign = "Lcom/BestCalculatorCN/MyCalculator;->b(Lcom/BestCalculatorCN/MyCalculator;Ljava/lang/String;)V"
    unit = ctx.open(input_path);                                   
    prj = ctx.getMainProject();                                    
    dexUnit = prj.findUnit(IDexUnit);                              
    clz = dexUnit.getClass(class_sign);                            
    method = dexUnit.getMethod(method_sign);                       
 
    # 1 查询某method交叉引用列表
    # 使用(unit,操作,地址,itemid)来创建一个context对象,提供给JEB引擎,用于后续执行
    print "------------------------------------------------"
    actionXrefsData = ActionXrefsData()
    actionContext = ActionContext(dexUnit, Actions.QUERY_XREFS, method.getItemId(), None)
    if unit.prepareExecution(actionContext,actionXrefsData):
        for xref_addr in actionXrefsData.getAddresses():
            print xref_addr
 
    # 2 查询整个class的交叉引用列表
    print "------------------------------------------------"
    actionXrefsData = ActionXrefsData()
    actionContext = ActionContext(dexUnit, Actions.QUERY_XREFS, clz.getItemId(), None)
    if unit.prepareExecution(actionContext,actionXrefsData):
        for idx,xref_addr in enumerate(actionXrefsData.getAddresses()):
            print idx,xref_addr
# -*- coding: utf-8 -*-
from com.pnfsoftware.jeb.client.api import IClientContext
from com.pnfsoftware.jeb.core import IRuntimeProject
from com.pnfsoftware.jeb.core.units import IUnit, INativeCodeUnit
from com.pnfsoftware.jeb.core.units.code.asm.analyzer import INativeCodeAnalyzer, INativeCodeModel, IReferenceManager, ICallGraphManager, ICallGraph, CallGraphVertex
from com.pnfsoftware.jeb.core.units.code.asm.items import INativeMethodItem
 
 
# callees/callers 调用与被调用信息
def Test(ctx):
    assert isinstance(ctx,IClientContext)
    input_path = r"D:\tmp\2\project\about_dex_diff\code\xmly\libFace3D.so"
    unit = ctx.open(input_path)
    prj = ctx.getMainProject()
 
    # 获取INativeCodeUnit并执行解析
    nativeCodeUnit = prj.findUnit(INativeCodeUnit)
    bool = nativeCodeUnit.process()
 
    # 获取INativeCodeAnalyzer,获取INativeCodeModel
    nativeCodeAnalyzer = nativeCodeUnit.getCodeAnalyzer()
    nativeCodeAnalyzer.analyze()
    nativeCodeModel = nativeCodeAnalyzer.getModel()
 
    # 获取ICallGraph
    callGraph = nativeCodeModel.getCallGraphManager().getGlobalCallGraph()
 
    # 函数
    funcName = "libunwind::LocalAddressSpace::findFunctionName"
    nativeMethodItem = nativeCodeUnit.getMethod(funcName)
    print ">>> funcAddr:",hex(nativeMethodItem.getRoutineAddress())
 
    # callees 目标函数调用了谁
    callGraphVertexList = callGraph.getCallees(nativeMethodItem,False)
    for callGraphVertex in callGraphVertexList:
        print ">>> Callee:",hex(callGraphVertex.getInternalAddress().getAddress())
 
    # callers 目标函数被谁调用
    callerList = callGraph.getCallers(nativeMethodItem,False)
    for caller in callerList:
        print ">>> Callers:",hex(caller)
 
# >>> funcAddr: 0x19a1cL
# >>> Callee: 0xabfcL
# >>> Callee: 0xac08L
# >>> Callee: 0x9cc0L
# >>> Callers: 0x196b2L
# -*- coding: utf-8 -*-
from com.pnfsoftware.jeb.client.api import IClientContext
from com.pnfsoftware.jeb.core import IRuntimeProject
from com.pnfsoftware.jeb.core.units import IUnit, INativeCodeUnit
from com.pnfsoftware.jeb.core.units.code.asm.analyzer import INativeCodeAnalyzer, INativeCodeModel, IReferenceManager, ICallGraphManager, ICallGraph, CallGraphVertex
from com.pnfsoftware.jeb.core.units.code.asm.items import INativeMethodItem
 
 
# callees/callers 调用与被调用信息
def Test(ctx):
    assert isinstance(ctx,IClientContext)
    input_path = r"D:\tmp\2\project\about_dex_diff\code\xmly\libFace3D.so"
    unit = ctx.open(input_path)
    prj = ctx.getMainProject()
 
    # 获取INativeCodeUnit并执行解析
    nativeCodeUnit = prj.findUnit(INativeCodeUnit)
    bool = nativeCodeUnit.process()
 
    # 获取INativeCodeAnalyzer,获取INativeCodeModel
    nativeCodeAnalyzer = nativeCodeUnit.getCodeAnalyzer()
    nativeCodeAnalyzer.analyze()
    nativeCodeModel = nativeCodeAnalyzer.getModel()
 
    # 获取ICallGraph
    callGraph = nativeCodeModel.getCallGraphManager().getGlobalCallGraph()
 
    # 函数
    funcName = "libunwind::LocalAddressSpace::findFunctionName"
    nativeMethodItem = nativeCodeUnit.getMethod(funcName)
    print ">>> funcAddr:",hex(nativeMethodItem.getRoutineAddress())
 
    # callees 目标函数调用了谁
    callGraphVertexList = callGraph.getCallees(nativeMethodItem,False)
    for callGraphVertex in callGraphVertexList:
        print ">>> Callee:",hex(callGraphVertex.getInternalAddress().getAddress())
 
    # callers 目标函数被谁调用
    callerList = callGraph.getCallers(nativeMethodItem,False)
    for caller in callerList:
        print ">>> Callers:",hex(caller)
 
# >>> funcAddr: 0x19a1cL
# >>> Callee: 0xabfcL
# >>> Callee: 0xac08L
# >>> Callee: 0x9cc0L
# >>> Callers: 0x196b2L
 

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

最后于 2020-10-28 14:32 被爱吃菠菜编辑 ,原因:
收藏
免费 1
支持
分享
最新回复 (4)
雪    币: 4034
活跃值: (2223)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
2
再膜再看
2020-10-27 15:39
0
雪    币: 6573
活跃值: (3933)
能力值: (RANK:200 )
在线值:
发帖
回帖
粉丝
3
图片重新贴一下吧
2020-10-27 16:54
0
雪    币: 1730
活跃值: (2332)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
膜拜大神
2020-10-28 09:10
0
雪    币:
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
5
好人一生平安
2020-10-28 09:23
0
游客
登录 | 注册 方可回帖
返回
//