python binding can't add label in mlil

オープン
#7,708 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python

調査の方向性

Start by locating the existing Python binding for llil.add_label_for_address and the MLIL label entry points used in the example, including MediumLevelILLabel and MediumLevelILFunction. Determine where an equivalent MLIL API belongs and verify that the shown workflow can create labels and replace the indirect jump with if_expr without failing.

索引モデルが issue の本文から書いたものです。

説明

What is the feature you'd like to have?
add python workflow api to add label in mlil like llil.add_label_for_address

Is your feature request related to a problem?
when solve indirect branch jump, its suppose to use mlil data flow analyze to calculate possible dest and replace 'jump' MLIL to 'if' MLIL , so a easy way to create label in mlil is necessary

Are any alternative solutions acceptable?
i have not found yet

Additional Information:
my code here ,can't work and i can't find a solution

import json
from binaryninja import(
    Workflow,
    AnalysisContext,
    Variable,
    Activity,
    MediumLevelILFunction,
    MediumLevelILConstPtr,
    MediumLevelILConstData,
    MediumLevelILConst,
    MediumLevelILLoad,
    MediumLevelILVar,
    MediumLevelILAdd,
    MediumLevelILSub,
    MediumLevelILXor,
    MediumLevelILMul,
    MediumLevelILJump,
    PossibleValueSet,
    RegisterValueType,
    MediumLevelILLabel,
    LowLevelILLabel,
)
mlil_const=MediumLevelILConstPtr | MediumLevelILConstData | MediumLevelILConst
def search_var_def(var:Variable,mlil:MediumLevelILFunction,cond):
    var_def=mlil.get_var_definitions(var)
    # print(hex(var_def[0].address))
    if len(var_def)==1 and isinstance(var_def[0].src,mlil_const):
        print("def==1",var_def)
        return
    if (len(var_def)==2):
        # print(var_def)
        if isinstance(var_def[0].src,mlil_const) and isinstance(var_def[1].src,mlil_const):
            if cond["value"]==None:
                for key in var_def[0].branch_dependence.keys():
                    cond["value"]=mlil[key].condition.expr_index
                    break
            return {var:{"t":var_def[0].src.value.value,"f":var_def[1].src.value.value}}
        return
    if len(var_def)>2:
        print("def>2",var_def)
        return None
    var_def=var_def[0]
    var_def_src=var_def.src
    # print(var_def_src)
    if isinstance(var_def_src,mlil_const):
        print("const:",var_def_src)
        return
    if isinstance(var_def_src,MediumLevelILLoad):
        # print("load:",var_def_src.operands)
        table=var_def.get_possible_reg_values_after(var_def.dest.storage)
        # print(table.mapping)
        
        var_def_token=list(var_def_src.traverse(lambda x:  x))
        for token in var_def_token:
            if isinstance(token,MediumLevelILVar):
                mp=search_var_def(token.var,mlil,cond)
                if mp==None:
                    return
                # print(table,var_def,var_def.dest.storage)
                if (table.type==RegisterValueType.UndeterminedValue):
                    print("undetermined table,check data section")
                    return
                # print(mp,table.mapping)
                try:
                    return {var:{"t":table.mapping[mp[token.var]["t"]],"f":table.mapping[mp[token.var]["f"]]}}
                except KeyError:
                    return {var:{"t":table.mapping[mp[token.var]["t"]//8],"f":table.mapping[mp[token.var]["f"]//8]}}
        return

    Lvalue=search_var_def(var_def_src.left.var,mlil,cond)
    Rvalue=search_var_def(var_def_src.right.var,mlil,cond)
    tmp=Lvalue|Rvalue
    if isinstance(var_def_src,MediumLevelILAdd):
        Tvalue=tmp[var_def_src.left.var]["t"]+tmp[var_def_src.right.var]["t"]
        Fvalue=tmp[var_def_src.left.var]["f"]+tmp[var_def_src.right.var]["f"]
        return {var:{"t":Tvalue,"f":Fvalue}}
    elif isinstance(var_def_src,MediumLevelILSub):
        Tvalue=tmp[var_def_src.left.var]["t"]-tmp[var_def_src.right.var]["t"]
        Fvalue=tmp[var_def_src.left.var]["f"]-tmp[var_def_src.right.var]["f"]
        return {var:{"t":Tvalue,"f":Fvalue}}
    elif isinstance(var_def_src,MediumLevelILXor):
        Tvalue=tmp[var_def_src.left.var]["t"]^tmp[var_def_src.right.var]["t"]
        Fvalue=tmp[var_def_src.left.var]["f"]^tmp[var_def_src.right.var]["f"]
        return {var:{"t":Tvalue,"f":Fvalue}}
    elif isinstance(var_def_src,MediumLevelILMul):
        Tvalue=tmp[var_def_src.left.var]["t"]*tmp[var_def_src.right.var]["t"]
        Fvalue=tmp[var_def_src.left.var]["f"]*tmp[var_def_src.right.var]["f"]
        return {var:{"t":Tvalue,"f":Fvalue}}
    else:
        print("Unsupported operation:",var_def_src)
        return None
def handle_two_direct_indirect_jump(ctx:AnalysisContext):
    bv=ctx.function.view
    mlil=ctx.function.mlil
    llil=ctx.function.llil
    update=False
    for instr in mlil.instructions:
        if isinstance(instr,MediumLevelILJump) and isinstance(instr.dest,MediumLevelILVar):
            # print(instr.get_possible_reg_values(instr.dest.var.storage))
            if instr.get_possible_reg_values(instr.dest.var.storage).type==RegisterValueType.UndeterminedValue:
                # print(instr.dest.var)
                cond={"value":None}
                res=search_var_def(instr.dest.var, mlil,cond)
                # print(cond)
                
                if res==None or cond["value"]==None:
                    raise RuntimeError("calc fail,check {}".format(hex(instr.address)))
                ctx.function.set_user_indirect_branches(instr.address, [(ctx.function.arch,res[instr.dest.var]["t"]),(ctx.function.arch,res[instr.dest.var]["f"])],ctx.function.arch)
                print(hex(res[instr.dest.var]["t"]),llil.get_instruction_start(res[instr.dest.var]["t"]))
                print(hex(res[instr.dest.var]["f"]),llil.get_instruction_start(res[instr.dest.var]["f"]))
                t_instr=llil[llil.get_instruction_start(res[instr.dest.var]["t"])].mapped_medium_level_il
                f_instr=llil[llil.get_instruction_start(res[instr.dest.var]["f"])].mapped_medium_level_il
                print(hex(res[instr.dest.var]["t"]),t_instr)
                print(hex(res[instr.dest.var]["f"]),f_instr)
                t_label=mlil.get_label_for_source_instruction(t_instr.instr_index)
                f_label=mlil.get_label_for_source_instruction(f_instr.instr_index)
                print("set indirect jump at {} to {}:{},{}:{}".format(hex(instr.address),hex(res[instr.dest.var]["t"]),t_label,hex(res[instr.dest.var]["f"]),f_label))

                if_instr=mlil.if_expr(cond["value"],t_label,f_label,instr.source_location)
                mlil.replace_expr(instr.expr_index,if_instr)
                mlil.append(if_instr)
                update=True
    if update:
        mlil.finalize()
        mlil.generate_ssa_form()

                




wf=Workflow("").clone("satori.function.deobf")
wf.register_activity(Activity(configuration=json.dumps({
    "name":"satori.function.handle_two_direct_indirect_jump.activity",
    "title":"handle_two_direct_indirect_jump",
    "description":"handle_two_direct_indirect_jump",
    "eligibility":{
        "auto":{
            "default":True
        }
    }

}),action=lambda context: handle_two_direct_indirect_jump(context)))
wf.insert("core.function.generateHighLevelIL",["satori.function.handle_two_direct_indirect_jump.activity"])

wf.register()
主要言語
C++
スター
1.3k
フォーク
298
平均マージ
5日 5時間
マージ済み PR(30日)
19

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Vector35/binaryninja-api のほかの issue

Vector35/binaryninja-api の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。