python - 如何使用 r2pipe 转储函数的反汇编
问题描述
我即将编写一个软件,将二进制文件放入radare2,然后将包括指令、地址和指令的二进制表示在内的子程序转储到文本文件中。
我让它与 IDA Pro 和 IDAPython 一起工作,但我也想为radare2重新创建它。文本文件最终应如下所示:
0x0804ba0a 55 push ebp
0x0804ba0b 89e5 mov ebp, esp
0x0804ba0d 83ec18 sub esp, 0x18
0x0804ba10 83e4f0 and esp, 0xfffffff0
0x0804ba13 b800000000 mov eax, 0
0x0804ba18 29c4 sub esp, eax
不幸的是,网络上的资源很少,文档也不长。我很想给你更多的工作,但我不知何故被困在这里。我想出了如何使用 pdf 命令反汇编一个函数,我可能可以在 python 中像这样使用它,但是我这样做的方式,main、入口点和 sym.main 将丢失。我想反汇编整个 .text 部分或 .text 部分中的所有函数:
import r2pipe
file = 'path_to_file'
r = r2pipe.open()
with open (file, 'w') as f:
r.cmd('aaa')
# disassemble all functions starting with fcn and write them
# to the file
f.write(r.cmd('pdf @@ fcn*'))
解决方案
环境
- 雷达2:雷达2 4.2.0-git 23519@linux-x86-64 git.4.1.1-84-g0c46c3e1e 提交:0c46c3e1e30bb272a5a05fc367d874af32b41fe4 构建:2020-01-08__09:49:0
- 系统: Ubuntu 18.04.3 LTS
解决方案
- 这可以使用来自命令提示符或支持 r2pipe 的语言的两个radare2 命令来实现。
- 命令一:aaaa #分析文件
- 命令二:pdf @@f > out
- pdf # 打印函数的反汇编
- @@f # 对每个函数重复该命令
- > out # 将输出重定向到名为out的文件
例子
使用radare2 shell的示例
user@host:~$ r2 /bin/ls
[0x00005850]> aaaa
...
[0x00005850]> pdf @@f > out
[0x00005850]> q
user@host:~$ cat out
...
┌ 38: fcn.00014840 ();
│ ; var int64_t var_38h @ rsp+0xffffffd0
│ 0x00014840 53 push rbx
│ 0x00014841 31f6 xor esi, esi
│ 0x00014843 31ff xor edi, edi
│ 0x00014845 e846f2feff call sym.imp.getcwd
│ 0x0001484a 4885c0 test rax, rax
│ 0x0001484d 4889c3 mov rbx, rax
│ ┌─< 0x00014850 740e je 0x14860
│ │ ; CODE XREF from fcn.00014840 @ 0x14868
│ ┌──> 0x00014852 4889d8 mov rax, rbx
│ ╎│ 0x00014855 5b pop rbx
│ ╎│ 0x00014856 c3 ret
..
│ ╎│ ; CODE XREF from fcn.00014840 @ 0x14850
│ ╎└─> 0x00014860 e88beffeff call sym.imp.__errno_location
│ ╎ 0x00014865 83380c cmp dword [rax], 0xc
│ └──< 0x00014868 75e8 jne 0x14852
└ 0x0001486a e861feffff call fcn.000146d0
; CALL XREFS from fcn.00013d00 @ 0x13d9d, 0x13da8
...
使用带有 r2pipe 的 Python 示例
import r2pipe
R2 = r2pipe.open('/bin/ls') # Open r2 with file
R2.cmd('aaaa') # Analyze file
R2.cmd('pdf @@f > out') # Write disassembly for each function to out file
R2.quit() # Quit r2
推荐阅读
- java - 树形图是如何工作的?
- javascript - 从 AntDesign 中选择时停止传播
- python - 烧瓶框架中的 sqlalchemy.exc.OperationalError (No such coumn)
- oracle - Oracle REGEXP_SUBSTR 在有多个空格时按空格提取字符串
- visual-studio-extensions - 无法在 Visual Studio 2019 中安装扩展并且失败并出现错误 Invalid TimeStamp 或 Digital Signature as none
- javascript - 我可以在单击单选输入字段时更改布尔值吗?
- dexie - DexieJS 在嵌套查询中返回 NotFoundError
- python - 执行 .py 文件和使用 spyder 时,Tkinter 窗口格式不同
- javascript - TypeScript 在 curry 函数中使用泛型函数参数
- google-coral - 如何在没有 USB 连接的情况下连接到 Coral 开发板