Python插件

At first, to be able to write a plugins in Python for radare2 you need to install r2lang plugin. If you're going to use Python 2, then use r2pm -i lang-python2, otherwise (and recommended) - install the Python 3 version: r2pm -i lang-python3. Note - in the following examples there are missing functions of the actual decoding for the sake of readability!

For this you need to do this: 1. import r2lang and from r2lang import R (for constants) 2. Make a function with 2 subfunctions - assemble and disassemble and returning plugin structure - for RAsm plugin

def mycpu(a):
    def assemble(s):
        return [1, 2, 3, 4]

    def disassemble(memview, addr):
        try:
            opcode = get_opcode(memview) # https://docs.python.org/3/library/stdtypes.html#memoryview
            opstr = optbl[opcode][1]
            return [4, opstr]
        except:
            return [4, "unknown"]
  1. This structure should contain a pointers to these 2 functions - assemble and disassemble

    return {
            "name" : "mycpu",
            "arch" : "mycpu",
            "bits" : 32,
            "endian" : "little",
            "license" : "GPL",
            "desc" : "MYCPU disasm",
            "assemble" : assemble,
            "disassemble" : disassemble,
    }
  1. Make a function with 2 subfunctions - set_reg_profile and op and returning plugin structure - for RAnal plugin

  1. This structure should contain a pointers to these 2 functions - set_reg_profile and op

  1. Then register those using r2lang.plugin("asm") and r2lang.plugin("anal") respectively

You can combine everything in one file and load it using -i option:

Or you can load it from the r2 shell: #!python mycpu.py

See also:

Implementing new format plugin in Python

Note - in the following examples there are missing functions of the actual decoding for the sake of readability!

For this you need to do this: 1. import r2lang 2. Make a function with subfunctions:

  • load

  • load_bytes

  • destroy

  • check_bytes

  • baddr

  • entries

  • sections

  • imports

  • relocs

  • binsym

  • info

    and returning plugin structure - for RAsm plugin

    and so on. Please be sure of the parameters for each function and format of returns. Note, that functions entries, sections, imports, relocs returns a list of special formed dictionaries - each with a different type. Other functions return just a list of numerical values, even if single element one. There is a special function, which returns information about the file - info:

  1. This structure should contain a pointers to the most important functions like

    check_bytes, load and load_bytes, entries, relocs, imports.

  1. Then you need to register it as a file format plugin:

最后更新于

这有帮助吗?