Code Monkey home page Code Monkey logo

bnil-graph's Introduction

BNIL Instruction Graph

A BinaryNinja plugin to graph a BNIL instruction tree and meta-program python instruction matchers.

Installation

Installation is supported two ways, the first using the new plugin manager and the second being a manual install.

Plugin Manager

Use the new plugin manager by selecting "Manage Plugins" from the "Edit" menu. Search the plugin list for "BNIL Instruction Graph", right click on it and click "Install" then right click again and select "Enable".

Manual Installation

  1. Clone the repository to your prefered location: $ git clone https://github.com/withzombies/bnil-graph.git
  2. Change to the Binary Ninja plugins directory: $ cd ~/Library/Application\ Support/Binary\ Ninja/plugins
  3. Create a symlink to the folder: $ ln -s ~/git/bnil-graph .
  4. Restart Binary Ninja

Usage

To use bnil-graph, right click on an instruction and select "BNIL Instruction Graph". This graphs the BNIL instructions assocaited with that address and displays them as an HTML form.

Binary Ninja adds operand accessors dynamically, due to this the convenient accesors do not show up in dir() calls or in the api documentation. bnil-graph shows the structure of the IL instruction including its nice accessor names (such as insn.src for the source register or memory)

Menu Example

Example graph:

Example Graph

Matchers

In addition to the graph plugin, bnil-graph also will generate a matcher function that will match the selected instructions exactly. This feature will allow new plugin developers to quickly match instructions. The intended use is to find an instruction similar to the one you want to match, generate a matcher function, then modify the generated function to better support your needs.

An example would be trying to find all MediumLevelILSSA MLIL_CALL_SSA instructions that take 3 parameters. I generated a matcher against an unrelated function with 0 parameters:

def match_MediumLevelILSSA_140001194_0(insn):
    # mem#1 = 0x14000d49c() @ mem#0
    if insn.operation != MediumLevelILOperation.MLIL_CALL_SSA:
        return False

    # invalid
    if insn.output.operation != MediumLevelILOperation.MLIL_CALL_OUTPUT_SSA:
        return False

    if insn.output.dest_memory != 0x1:
        return False

    if len(insn.output.dest) != 0:
        return False

    # 0x14000d49c
    if insn.dest.operation != MediumLevelILOperation.MLIL_CONST_PTR:
        return False

    if insn.dest.constant != 0x14000d49c:
        return False

    if len(insn.params) != 0:
        return False

    if insn.src_memory != 0x0:
        return False

    return True

We can modify this to remove some specific constraints:

def match_MediumLevelILSSA_140001194_0(insn):
    # mem#1 = 0x14000d49c() @ mem#0
    if insn.operation != MediumLevelILOperation.MLIL_CALL_SSA:
        return False

    # invalid
    if insn.output.operation != MediumLevelILOperation.MLIL_CALL_OUTPUT_SSA:
        return False

    # 0x14000d49c
    if insn.dest.operation != MediumLevelILOperation.MLIL_CONST_PTR:
        return False

    if len(insn.params) != 0:
        return False

    return True

We removed the call destination and the memory versioning constraints. Next, update the params check to check for 3 parameters:

def match_3_param_MLIL_CALL_SSA(insn):
    if insn.operation != MediumLevelILOperation.MLIL_CALL_SSA:
        return False

    if insn.output.operation != MediumLevelILOperation.MLIL_CALL_OUTPUT_SSA:
        return False

    if insn.dest.operation != MediumLevelILOperation.MLIL_CONST_PTR:
        return False

    if len(insn.params) != 3:
        return False

    return True

Now, we have a matcher which will identify MLIL_CALL_SSA instructions with 3 parameters! Now iterate over MLIL SSA instructions and call the matcher and we're done:

if __name__ == '__main__':
    bv = binaryninja.BinaryViewType.get_view_of_file(sys.argv[1])
    bv.update_analysis_and_wait()

    for func in bv.functions:
        mlil = func.medium_level_il

        for block in mlil.ssa_form:
            for insn in block:
                if match_3_param_MLIL_CALL_SSA(insn):
                    print "Match: {}".format(insn)

Example matcher:

Example Matcher

License

This project copyright Ryan Stortz (@withzombies) and is available under the Apache 2.0 LICENSE.

bnil-graph's People

Contributors

psifertex avatar stong avatar vasco-jofra avatar withzombies avatar xusheng6 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bnil-graph's Issues

Exception thrown on generating graph

I'm getting this error when generating the IL tree in certain functions, not sure why.

Traceback (most recent call last):
  File "C:\binja\python\binaryninja\plugin.py", line 150, in _address_action
    action(view_obj, addr)
  File "C:\Users\User\AppData\Roaming\Binary Ninja\plugins\bnil-graph\__init__.py", line 183, in graph_bnil
    graph_ils(bv, g, head, function, addr)
  File "C:\Users\User\AppData\Roaming\Binary Ninja\plugins\bnil-graph\__init__.py", line 128, in graph_ils
    for il in sorted(ils):
TypeError: '<' not supported between instances of 'LowLevelILInstruction' and 'LowLevelILInstruction'

It seems to happen to instructions in _start in an ELF (the function that calls __libc_start_main)

Request to increase the function: display the code of the matching assembly and the modified assembly

In reverse engineering, dirty asm are very common, and manually matching a dirty assembly instruction is very cumbersome. Similarly, modifying many dirty assembly instruction is also very cumbersome.
If your plug-in can provide sample codes for matching assembly and modifying assembly, it will very helpful!

And as a beginner of python api, to be honest, it is difficult for me to figure out how to quickly get information about an assembly instruction. The only similar operation is to get the token of instructions, and in idapython, I can use idc.print_operand( ), GetDisam, print_insn_mnem and other functions directly obtain the operand or operand.

ValueError: invalid literal for int() with base 10: '2170 Personal'

Hey, after opening Instruction graph from right click on tree view, I get this exception:

Traceback (most recent call last):
  File "/home/enedil/data/code/binja/binaryninja/plugins/../python/binaryninja/plugin.py", line 153, in _address_action
    action(view_obj, addr)
  File "/home/enedil/.binaryninja/repositories/community/plugins/withzombies_bnilgraph/__init__.py", line 271, in graph_bnil
    show_graph_report(bv, g, "Instruction Graph ({:#x})".format(addr))
  File "/home/enedil/.binaryninja/repositories/community/plugins/withzombies_bnilgraph/__init__.py", line 58, in show_graph_report
    patch = int(patch.split('-')[0])
ValueError: invalid literal for int() with base 10: '2170 Personal'

I'm don't know where this 2170 Personal comes from, but might it be a type of licence? I'm using latest Binary Ninja on Linux.

Failing to Generate Graphs

I've been hitting this crash a lot recently:

Traceback (most recent call last):
  File "/Applications/Binary Ninja.app/Contents/MacOS/plugins/../../Resources/python/binaryninja/plugin.py", line 129, in _address_action
    action(view_obj, addr)
  File "/Users/kyle/Library/Application Support/Binary Ninja/repositories/community/plugins/withzombies_bnilgraph/__init__.py", line 302, in graph_bnil
    graph_ils(bv, g, head, function, addr)
  File "/Users/kyle/Library/Application Support/Binary Ninja/repositories/community/plugins/withzombies_bnilgraph/__init__.py", line 224, in graph_ils
    graph_il(g, head, il_type, il)
  File "/Users/kyle/Library/Application Support/Binary Ninja/repositories/community/plugins/withzombies_bnilgraph/__init__.py", line 212, in graph_il
    graph_il_insn(g, il_desc, il, "operation")
  File "/Users/kyle/Library/Application Support/Binary Ninja/repositories/community/plugins/withzombies_bnilgraph/__init__.py", line 105, in graph_il_insn
    edge_label, ty = il.ILOperations[il.operation][op_index]
IndexError: list index out of range

Seems to fail on any hlil that doesn't have operands ... noreturns, function calls with no parameters, etc

HighLevelILInstruction Missing '<' operator causes exception

On certain instructions the follow error is triggered when generating instruction graph:

  File "/home/user/binaryninja/plugins/../python/binaryninja/plugin.py", line 153, in _address_action
    action(view_obj, addr)
  File "/home/user/.binaryninja/repositories/community/plugins/withzombies_bnilgraph/__init__.py", line 269, in graph_bnil
    graph_ils(bv, g, head, function, addr)
  File "/home/user/.binaryninja/repositories/community/plugins/withzombies_bnilgraph/__init__.py", line 202, in graph_ils
    for il in sorted(ils):
TypeError: '<' not supported between instances of 'HighLevelILInstruction' and 'HighLevelILInstruction'

I was about to replicate it on a x86-64 'call' instruction:

00008928  ff1572b60900       call    qword [rel __libc_start_main@GOT]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.