📕
Radare2手册
  • 简介
  • 历史
  • Radare2框架
  • 下载radare2
  • 编译与可移植性
  • Compilation on Windows
  • Compilation on Android
  • 用户界面
  • 快速上手
    • 命令行选项
    • 命令格式
    • 表达式
    • 基本的debug操作
    • Contributing to radare2
  • 配置
    • Colors
    • 配置项
    • radare2相关文件
  • 基本命令
    • 定位
    • 块大小
    • 节区
    • 映射文件
    • 输出模式
    • 标记符(Flags)
    • 写入数据
    • Zoom模式
    • 复制/粘贴
    • 字节比较
    • SDB
    • Dietline
  • 可视化模式
    • 反汇编界面
    • 汇编界面
    • 变量编辑器界面
    • 可视化面板
  • 搜索字节
    • 基本的搜索用法
    • 配置搜索引擎
    • 搜索重复字节序列
    • 搜索中的自动化
    • 回溯搜索
    • 搜索汇编指令
    • Searching for AES Keys
  • 反汇编
    • 为反汇编添加元数据
    • ESIL
  • 分析
    • 代码分析
    • 变量
    • 类型
    • 调用约定
    • 虚函数表
    • 系统调用
    • 模拟执行
    • Symbols 信息
    • 函数签名
    • 图形化命令
  • 脚本化
    • 循环(Loops)
    • 宏(Macros)
    • R2pipe
  • 调试器
    • 入门
    • 从ida, GDB 或 WinDBG迁移到radare2
    • 寄存器(Registers)
    • 内存映射(Memory Maps)
    • 堆(Heap)
    • 文件(Files)
    • 反向调试
    • Windows消息(Messages)
  • 远程访问
    • 远程GDB调试
    • 远程WinDbg
  • 命令行工具
    • Rax2
    • Rafind2
    • Rarun2
    • Rabin2
      • 文件信息识别
      • 入口点(EP)
      • 导入(Imports)
      • 导出(Exports)
      • Symbols (exports)
      • 库文件
      • 字符串(String)
      • 节区(Sections)
    • Radiff2
      • 二进制文件比较
    • Rasm2
      • 汇编
      • 反汇编
      • 配置项
    • Ragg2
      • Language
    • Rahash2
      • Rahash Tool
  • 插件
    • IO 插件
    • Asm 插件
    • Analysis 插件
    • Bin 插件
    • 其它插件
    • Python插件
    • 对插件进行调试
    • 测试
    • 打包
  • Crackmes
    • IOLI
      • IOLI 0x00
      • IOLI 0x01
    • Avatao R3v3rs3 4
      • .radare2
      • .first_steps
      • .main
      • .vmloop
      • .instructionset
      • .bytecode
      • .outro
  • Reference Card
  • Acknowledgments
由 GitBook 提供支持
在本页

这有帮助吗?

  1. 插件

Python插件

在用python写r2插件之前,需要安装一个r2lang的插件 r2pm -i lang-python 。 注意 - 为了可读性,下面的例子中省略了实际的解码函数。

编写python插件的流程如下: 1. import r2lang and from r2lang import R (for constants) 2. 创建一个函数,其包含两个子函数 - assemble 和 disassemble 并返回一个一个plugin struct - 用于RAsm插件

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. 该结构体需要包含指向这两个函数的指针 - assemble和disassemble

    return {
            "name" : "mycpu",
            "arch" : "mycpu",
            "bits" : 32,
            "endian" : R.R_SYS_ENDIAN_LITTLE,
            "license" : "GPL",
            "desc" : "MYCPU disasm",
            "assemble" : assemble,
            "disassemble" : disassemble,
    }
  1. 创建一个函数,其包含两个子函数 - set_reg_profile 和 op, 并返回一个plugin struct - 用于RAnal插件

    ```python def mycpu_anal(a): def set_reg_profile(): profile = "=PC pc\n" + "=SP sp\n" + "gpr r0 .32 0 0\n" + "gpr r1 .32 4 0\n" + "gpr r2 .32 8 0\n" + "gpr r3 .32 12 0\n" + "gpr r4 .32 16 0\n" + "gpr r5 .32 20 0\n" + "gpr sp .32 24 0\n" + "gpr pc .32 28 0\n" return profile

     except:
         result = analop
     # Don't forget to return proper instruction size!
     return [4, result]
5. 该结构体需要包含指向这两个函数的指针 - `set_reg_profile`和`op`

```python
    return {
            "name" : "mycpu",
            "arch" : "mycpu",
            "bits" : 32,
            "license" : "GPL",
            "desc" : "MYCPU anal",
            "esil" : 1,
            "set_reg_profile" : set_reg_profile,
            "op" : op,
    }
  1. 分别用r2lang.plugin("asm") and r2lang.plugin("anal")进行注册:

print("Registering MYCPU disasm plugin...")
print(r2lang.plugin("asm", mycpu))
print("Registering MYCPU analysis plugin...")
print(r2lang.plugin("anal", mycpu_anal))

可以把上面这些写在一个文件里,然后用-i选项加载就行了:

r2 -I mycpu.py some_file.bin

或者可以在r2 shell里进行加载: #!python mycpu.py

参见:

用python实现一个文件格式插件

注意 - 为了可读性,下面的例子中省略了实际的解码函数。

步骤: 1. import r2lang 2. 创建一个函数,其包含下面这些子函数:

  • load

  • load_bytes

  • destroy

  • check_bytes

  • baddr

  • entries

  • sections

  • imports

  • relocs

  • binsym

  • info

    并返回一个plugin struct - 用于 RAsm plugin:

    def le_format(a):
    def load(binf):
       return [0]
    
    def check_bytes(buf):
       try:
           if buf[0] == 77 and buf[1] == 90:
               lx_off, = struct.unpack("<I", buf[0x3c:0x40])
               if buf[lx_off] == 76 and buf[lx_off+1] == 88:
                   return [1]
           return [0]
       except:
           return [0]

    诸如此类。 注意检查每个函数的参数和返回值的格式,entries,sections,imports,relocs都返回一个包含字典的列表,但不同列表中包含的字典类型是不同的。 其余函数的返回值只是一个包含数值的列表,甚至可能只包含一个元素。 下面是一个特殊的函数,其返回文件信息结构 - info:

    def info(binf):
       return [{
               "type" : "le",
               "bclass" : "le",
               "rclass" : "le",
               "os" : "OS/2",
               "subsystem" : "CLI",
               "machine" : "IBM",
               "arch" : "x86",
               "has_va" : 0,
               "bits" : 32,
               "big_endian" : 0,
               "dbg_info" : 0,
               }]
  1. 返回的plugin struct需要包含大部分的核心函数指针,比如check_bytes, load和load-bytes, entries, relocs, imports。

    return {
            "name" : "le",
            "desc" : "OS/2 LE/LX format",
            "license" : "GPL",
            "load" : load,
            "load_bytes" : load_bytes,
            "destroy" : destroy,
            "check_bytes" : check_bytes,
            "baddr" : baddr,
            "entries" : entries,
            "sections" : sections,
            "imports" : imports,
            "symbols" : symbols,
            "relocs" : relocs,
            "binsym" : binsym,
            "info" : info,
    }
  1. 作为为文件格式插件进行注册:

print("Registering OS/2 LE/LX plugin...")
print(r2lang.plugin("bin", le_format))
上一页其它插件下一页对插件进行调试

最后更新于4年前

这有帮助吗?

def op(memview, pc): analop = { "type" : R.R_ANAL_OP_TYPE_NULL, "cycles" : 0, "stackop" : 0, "stackptr" : 0, "ptr" : -1, "jump" : -1, "addr" : 0, "eob" : False, "esil" : "", } try: opcode = get_opcode(memview) # esilstr = optbl[opcode][2] if optbl[opcode][0] == "J": # it's jump analop["type"] = R.R_ANAL_OP_TYPE_JMP analop["jump"] = decode_jump(opcode, j_mask) esilstr = jump_esil(esilstr, opcode, j_mask)

https://docs.python.org/3/library/stdtypes.html#memoryview
Python
Javascript