CTF-All-In-One/doc/5.3.1_angr.md

885 lines
36 KiB
Markdown
Raw Normal View History

2018-02-06 13:30:35 +07:00
# 5.3.1 angr
2017-09-12 18:57:05 +07:00
2018-05-29 20:51:00 +07:00
- [简介](#简介)
2017-12-02 22:38:19 +07:00
- [安装](#安装)
2018-05-29 20:51:00 +07:00
- [使用方法](#使用方法)
- [快速入门](#快速入门)
- [二进制文件加载器](#二进制文件加载器)
- [求解器引擎](#求解器引擎)
2018-06-01 19:10:55 +07:00
- [程序状态](#程序状态)
- [模拟管理器](#模拟管理器)
2018-06-17 16:46:53 +07:00
- [VEX IR 翻译器](#vex-ir-翻译器)
2018-06-02 13:52:30 +07:00
- [扩展工具](#扩展工具)
2018-05-29 20:51:00 +07:00
- [CTF 实例](#ctf-实例)
2017-12-02 22:38:19 +07:00
- [参考资料](#参考资料)
2017-09-12 18:57:05 +07:00
2018-05-29 20:51:00 +07:00
## 简介
2017-12-02 22:38:19 +07:00
2018-08-05 16:43:10 +07:00
[angr](https://github.com/angr/angr) 是一个多架构的二进制分析平台,具备对二进制文件的动态符号执行能力和多种静态分析能力。在近几年的 CTF 中也大有用途。
2017-09-12 18:57:05 +07:00
2017-12-02 22:38:19 +07:00
## 安装
2018-08-05 16:43:10 +07:00
2017-12-02 22:38:19 +07:00
在 Ubuntu 上,首先我们应该安装所有的编译所需要的依赖环境:
2018-08-05 16:43:10 +07:00
```text
2017-12-02 22:38:19 +07:00
$ sudo apt install python-dev libffi-dev build-essential virtualenvwrapper
2017-09-12 18:57:05 +07:00
```
2018-06-01 19:10:55 +07:00
强烈建议在虚拟环境中安装 angr因为有几个 angr 的依赖比如z3是从他们的原始库中 fork 而来,如果你已经安装了 z3,那么肯定不希望 angr 的依赖覆盖掉官方的共享库,开一个隔离的环境就好了:
2018-08-05 16:43:10 +07:00
```text
2018-06-01 19:10:55 +07:00
$ mkvirtualenv angr
$ sudo pip install angr
```
2017-09-12 18:57:05 +07:00
2017-12-08 11:06:55 +07:00
如果这样安装失败的话,那么你可以按照下面的顺序从 angr 的官方仓库安装:
2018-08-05 16:43:10 +07:00
2017-09-12 18:57:05 +07:00
```text
1. claripy
2. archinfo
3. pyvex
4. cle
5. angr
```
2018-08-05 16:43:10 +07:00
2018-05-29 20:51:00 +07:00
例如下面这样:
2018-08-05 16:43:10 +07:00
```text
2017-12-02 22:38:19 +07:00
$ git clone https://github.com/angr/claripy
$ cd claripy
$ sudo pip install -r requirements.txt
$ sudo python setup.py build
$ sudo python setup.py install
2017-09-12 18:57:05 +07:00
```
2018-08-05 16:43:10 +07:00
2017-12-08 11:06:55 +07:00
安装过程中可能会有一些奇怪的错误,可以到官方文档中查看。
2017-09-12 18:57:05 +07:00
2018-06-02 13:52:30 +07:00
另外 angr 还有一个 GUI 可以用,查看 [angr Management](https://github.com/angr/angr-management)。
2018-05-29 20:51:00 +07:00
## 使用方法
2018-08-05 16:43:10 +07:00
### 快速入门
2017-12-08 11:06:55 +07:00
使用 angr 的第一步是新建一个工程,几乎所有的操作都是围绕这个工程展开的:
2018-08-05 16:43:10 +07:00
2017-09-12 18:57:05 +07:00
```python
>>> import angr
2017-12-08 11:06:55 +07:00
>>> proj = angr.Project('/bin/true')
WARNING | 2017-12-08 10:46:58,836 | cle.loader | The main binary is a position-independent executable. It is being loaded with a base address of 0x400000.
```
2018-08-05 16:43:10 +07:00
2017-12-08 11:06:55 +07:00
这样就得到了二进制文件的各种信息,如:
2018-08-05 16:43:10 +07:00
2017-12-08 11:06:55 +07:00
```python
2018-06-01 19:10:55 +07:00
>>> proj.filename # 文件名
2017-12-08 11:06:55 +07:00
'/bin/true'
2018-06-01 19:10:55 +07:00
>>> proj.arch # 一个 archinfo.Arch 对象
2017-12-08 11:06:55 +07:00
<Arch AMD64 (LE)>
2018-06-01 19:10:55 +07:00
>>> hex(proj.entry) # 入口点
'0x401370'
2017-09-12 18:57:05 +07:00
```
2017-12-08 11:06:55 +07:00
程序加载时会将二进制文件和共享库映射到虚拟地址中CLE 模块就是用来处理这些东西的。
2018-08-05 16:43:10 +07:00
2017-12-08 11:06:55 +07:00
```python
>>> proj.loader
<Loaded true, maps [0x400000:0x5008000]>
2017-09-12 18:57:05 +07:00
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
所有对象文件如下,其中二进制文件本身是 main_object然后还可以查看对象文件的相关信息
2018-08-05 16:43:10 +07:00
```text
2018-06-01 19:10:55 +07:00
>>> for obj in proj.loader.all_objects:
... print obj
2018-08-05 16:43:10 +07:00
...
2018-06-01 19:10:55 +07:00
<ELF Object true, maps [0x400000:0x60721f]>
<ELF Object libc-2.27.so, maps [0x1000000:0x13bb98f]>
<ELF Object ld-2.27.so, maps [0x2000000:0x22260f7]>
<ELFTLSObject Object cle##tls, maps [0x3000000:0x300d010]>
<ExternObject Object cle##externs, maps [0x4000000:0x4008000]>
<KernelObject Object cle##kernel, maps [0x5000000:0x5008000]>
2017-12-08 11:06:55 +07:00
>>> proj.loader.main_object
<ELF Object true, maps [0x400000:0x60721f]>
2018-06-01 19:10:55 +07:00
>>> hex(proj.loader.main_object.min_addr)
'0x400000'
>>> hex(proj.loader.main_object.max_addr)
'0x60721f'
>>> proj.loader.main_object.execstack
False
2017-12-08 11:06:55 +07:00
```
2018-08-05 16:43:10 +07:00
2017-12-08 11:06:55 +07:00
通常我们在创建工程时选择关闭 `auto_load_libs` 以避免 angr 加载共享库:
2018-08-05 16:43:10 +07:00
```text
2017-12-08 11:06:55 +07:00
>>> p = angr.Project('/bin/true', auto_load_libs=False)
WARNING | 2017-12-08 11:09:28,629 | cle.loader | The main binary is a position-independent executable. It is being loaded with a base address of 0x400000.
>>> p.loader.all_objects
[<ELF Object true, maps [0x400000:0x60721f]>, <ExternObject Object cle##externs, maps [0x1000000:0x1008000]>, <KernelObject Object cle##kernel, maps [0x2000000:0x2008000]>, <ELFTLSObject Object cle##tls, maps [0x3000000:0x300d010]>]
2017-09-12 18:57:05 +07:00
```
2017-12-08 11:06:55 +07:00
`project.factory` 提供了很多类对二进制文件进行分析,它提供了几个方便的构造函数。
2017-09-12 18:57:05 +07:00
2018-06-01 19:10:55 +07:00
`project.factory.block()` 用于从给定地址解析一个 basic block对象类型为 Block
2018-08-05 16:43:10 +07:00
2017-12-08 11:06:55 +07:00
```python
>>> block = proj.factory.block(proj.entry) # 从程序头开始解析一个 basic block
>>> block
2018-06-01 19:10:55 +07:00
<Block for 0x401370, 42 bytes>
>>> block.pp() # 打印
0x401370: xor ebp, ebp
0x401372: mov r9, rdx
0x401375: pop rsi
0x401376: mov rdx, rsp
0x401379: and rsp, 0xfffffffffffffff0
0x40137d: push rax
0x40137e: push rsp
0x40137f: lea r8, qword ptr [rip + 0x32da]
0x401386: lea rcx, qword ptr [rip + 0x3263]
0x40138d: lea rdi, qword ptr [rip - 0xe4]
0x401394: call qword ptr [rip + 0x205b76]
2017-12-08 11:06:55 +07:00
>>> block.instructions # 指令数量
11
>>> block.instruction_addrs # 指令地址
2018-06-01 19:10:55 +07:00
[4199280L, 4199282L, 4199285L, 4199286L, 4199289L, 4199293L, 4199294L, 4199295L, 4199302L, 4199309L, 4199316L]
2017-12-08 11:06:55 +07:00
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
另外,还可以将 Block 对象转换成其他形式:
2018-08-05 16:43:10 +07:00
2017-12-08 11:06:55 +07:00
```python
>>> block.capstone
2018-06-01 19:10:55 +07:00
<CapstoneBlock for 0x401370>
2017-12-08 11:06:55 +07:00
>>> block.capstone.pp()
2018-05-29 20:51:00 +07:00
2017-12-08 11:06:55 +07:00
>>> block.vex
2018-06-01 19:10:55 +07:00
IRSB <0x2a bytes, 11 ins., <Arch AMD64 (LE)>> at 0x401370
2017-12-08 11:06:55 +07:00
>>> block.vex.pp()
```
2018-06-01 19:10:55 +07:00
程序的执行需要初始化一个模拟程序状态的 `SimState` 对象:
2018-08-05 16:43:10 +07:00
2017-12-08 11:06:55 +07:00
```python
>>> state = proj.factory.entry_state()
>>> state
2018-06-01 19:10:55 +07:00
<SimState @ 0x401370>
2017-12-08 11:06:55 +07:00
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
该对象包含了程序的内存、寄存器、文件系统数据等等模拟运行时动态变化的数据,例如:
2018-08-05 16:43:10 +07:00
2017-12-08 11:06:55 +07:00
```python
2018-06-01 19:10:55 +07:00
>>> state.regs # 寄存器名对象
<angr.state_plugins.view.SimRegNameView object at 0x7f126fdfe810>
>>> state.regs.rip # BV64 对象
<BV64 0x401370>
2017-12-08 11:06:55 +07:00
>>> state.regs.rsp
<BV64 0x7fffffffffeff98>
2018-06-01 19:10:55 +07:00
>>> state.regs.rsp.length # BV 对象都有 .length 属性
64
2017-12-08 11:06:55 +07:00
>>> state.regs.rdi
2018-06-01 19:10:55 +07:00
<BV64 reg_48_0_64{UNINITIALIZED}> # BV64 对象,符号变量
>>> state.mem[proj.entry].int.resolved # 将入口点的内存解释为 C 语言的 int 类型
2017-12-08 11:06:55 +07:00
<BV32 0x8949ed31>
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
这里的 BV即 bitvectors可以理解为一个比特串用于在 angr 里表示 CPU 数据。看到在这里 rdi 有点特殊,它没有具体的数值,而是在符号执行中所使用的符号变量,我们会在稍后再做讲解。
下面是 Python int 和 bitvectors 之间的转换:
2018-08-05 16:43:10 +07:00
2017-12-08 11:06:55 +07:00
```python
2018-06-01 19:10:55 +07:00
>>> bv = state.solver.BVV(0x1234, 32) # 创建值 0x1234 的 BV32 对象
2017-12-08 11:06:55 +07:00
>>> bv
<BV32 0x1234>
2018-06-01 19:10:55 +07:00
>>> hex(state.solver.eval(bv)) # 将 BV32 对象转换为 Python int
2017-12-08 11:06:55 +07:00
'0x1234'
>>> bv = state.solver.BVV(0x1234, 64)
>>> bv
<BV64 0x1234>
>>> hex(state.solver.eval(bv))
'0x1234L'
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
于是 bitvectors 可以进行数学运算:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> one = state.solver.BVV(1, 64)
>>> one_hundred = state.solver.BVV(100, 64)
>>> one_hundred + one # 位数相同时可以直接运算
<BV64 0x65>
>>> one_hundred + one + 0x100
<BV64 0x165>
>>> state.solver.BVV(-1, 64) # 默认为无符号数
<BV64 0xffffffffffffffff>
>>> five = state.solver.BVV(5, 27)
>>> five
<BV27 0x5>
>>> one + five.zero_extend(64 - 27) # 位数不同时需要进行扩展
<BV64 0x6>
>>> one + five.sign_extend(64 - 27) # 或者有符号扩展
<BV64 0x6>
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
使用 bitvectors 可以直接来设置寄存器和内存的值,当传入的是 Python int 时angr 会自动将其转换成 bitvectors
2018-08-05 16:43:10 +07:00
2017-12-08 11:06:55 +07:00
```python
>>> state.regs.rsi = state.solver.BVV(3, 64)
>>> state.regs.rsi
<BV64 0x3>
2018-06-01 19:10:55 +07:00
>>> state.mem[0x1000].long = 4 # 在地址 0x1000 存放一个 long 类型的值 4
2017-12-08 11:06:55 +07:00
>>> state.mem[0x1000].long.resolved # .resolved 获取 bitvectors
<BV64 0x4>
2018-06-01 19:10:55 +07:00
>>> state.mem[0x1000].long.concrete # .concrete 获得 Python int
2017-12-08 11:06:55 +07:00
4L
```
2018-06-01 19:10:55 +07:00
初始化的 state 可以经过模拟执行得到一系列的 states模拟管理器Simulation Managers的作用就是对这些 states 进行管理:
2018-08-05 16:43:10 +07:00
2017-12-08 11:06:55 +07:00
```python
>>> simgr = proj.factory.simulation_manager(state)
>>> simgr
<SimulationManager with 1 active>
2018-06-01 19:10:55 +07:00
>>> simgr.active # 当前 state
[<SimState @ 0x401370>]
>>> simgr.step() # 模拟执行一个 basic block
2017-12-08 11:06:55 +07:00
<SimulationManager with 1 active>
2018-06-01 19:10:55 +07:00
>>> simgr.active # 当前 state 被更新
[<SimState @ 0x1022f80>]
2017-12-08 11:06:55 +07:00
>>> simgr.active[0].regs.rip # active[0] 是当前 state
2018-06-01 19:10:55 +07:00
<BV64 0x1022f80>
>>> state.regs.rip # 但原始的 state 并没有改变
<BV64 0x401370>
2017-12-08 11:06:55 +07:00
```
2018-06-01 19:10:55 +07:00
angr 提供了大量函数用于程序分析,在这些函数在 `Project.analyses.`,例如:
2018-08-05 16:43:10 +07:00
2017-12-08 11:06:55 +07:00
```python
>>> cfg = p.analyses.CFGFast() # 得到 control-flow graph
>>> cfg
2018-06-01 19:10:55 +07:00
<CFGFast Analysis Result at 0x7f1265b62650>
2017-12-08 11:06:55 +07:00
>>> cfg.graph
2018-06-01 19:10:55 +07:00
<networkx.classes.digraph.DiGraph object at 0x7f1265e77310> # 详情请查看 networkx
2017-12-08 11:06:55 +07:00
>>> len(cfg.graph.nodes())
2018-06-01 19:10:55 +07:00
934
>>> entry_node = cfg.get_any_node(proj.entry) # 得到给定地址的 CFGNode
2017-12-08 11:06:55 +07:00
>>> entry_node
2018-06-01 19:10:55 +07:00
<CFGNode 0x401370[42]>
2017-12-08 11:06:55 +07:00
>>> len(list(cfg.graph.successors(entry_node)))
2
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
如果要想画出图来,还需要安装 matplotlib。
2018-08-05 16:43:10 +07:00
2017-12-08 11:06:55 +07:00
```python
>>> import networkx as nx
2018-06-01 19:10:55 +07:00
>>> import matplotlib
>>> matplotlib.use('Agg')
2017-12-08 11:06:55 +07:00
>>> import matplotlib.pyplot as plt
>>> nx.draw(cfg.graph) # 画图
>>> plt.savefig('temp.png') # 保存
```
2017-09-12 18:57:05 +07:00
2018-08-05 16:43:10 +07:00
### 二进制文件加载器
2018-05-29 20:51:00 +07:00
我们知道 angr 是高度模块化的,接下来我们就分别来看看这些组成模块,其中用于二进制加载模块称为 CLE。主类为 `cle.loader.Loader`,它导入所有的对象文件并导出一个进程内存的抽象。类 `cle.backends` 是加载器的后端,根据二进制文件类型区分为 `cle.backends.elf`、`cle.backends.pe`、`cle.backends.macho` 等。
2017-12-09 09:35:26 +07:00
2018-06-01 19:10:55 +07:00
首先我们来看加载器的一些常用参数:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
- `auto_load_libs`:是否自动加载主对象文件所依赖的共享库
- `except_missing_libs`:当有共享库没有找到时抛出异常
- `force_load_libs`:强制加载列表指定的共享库,不论其是否被依赖
- `skip_libs`:不加载列表指定的共享库,即使其被依赖
- `custom_ld_path`:可以到列表指定的路径查找共享库
如果希望对某个对象文件单独指定加载参数,可以使用 `main_ops``lib_opts` 以字典的形式指定参数。一些通用的参数如下:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
- `backend`:使用的加载器后端,如:"elf", "pe", "mach-o", "ida", "blob" 等
- `custom_arch`:使用的 archinfo.Arch 对象
- `custom_base_addr`:指定对象文件的基址
- `custom_entry_point`:指定对象文件的入口点
举个例子:
2018-08-05 16:43:10 +07:00
2017-12-09 09:35:26 +07:00
```python
2018-06-01 19:10:55 +07:00
angr.Project(main_opts={'backend': 'ida', 'custom_arch': 'i386'}, lib_opts={'libc.so.6': {'backend': 'elf'}})
```
加载对象文件和细分类型如下:
2018-08-05 16:43:10 +07:00
```text
2018-06-01 19:10:55 +07:00
>>> for obj in proj.loader.all_objects:
... print obj
2018-08-05 16:43:10 +07:00
...
2018-06-01 19:10:55 +07:00
<ELF Object true, maps [0x400000:0x60721f]>
<ELF Object libc-2.27.so, maps [0x1000000:0x13bb98f]>
<ELF Object ld-2.27.so, maps [0x2000000:0x22260f7]>
<ELFTLSObject Object cle##tls, maps [0x3000000:0x300d010]>
<ExternObject Object cle##externs, maps [0x4000000:0x4008000]>
<KernelObject Object cle##kernel, maps [0x5000000:0x5008000]>
2017-12-09 09:35:26 +07:00
```
2018-08-05 16:43:10 +07:00
2017-12-09 09:35:26 +07:00
- `proj.loader.main_object`:主对象文件
- `proj.loader.shared_objects`:共享对象文件
- `proj.loader.extern_object`:外部对象文件
- `proj.loader.all_elf_object`:所有 elf 对象文件
- `proj.loader.kernel_object`:内核对象文件
通过对这些对象文件进行操作,可以解析出相关信息:
2018-08-05 16:43:10 +07:00
2017-12-09 09:35:26 +07:00
```python
>>> obj = proj.loader.main_object
2018-06-01 19:10:55 +07:00
>>> obj
<ELF Object true, maps [0x400000:0x60721f]>
>>> hex(obj.entry) # 入口地址
'0x401370'
2017-12-09 09:35:26 +07:00
>>> hex(obj.min_addr), hex(obj.max_addr) # 起始地址和结束地址
('0x400000', '0x60721f')
2018-06-01 19:10:55 +07:00
>>> for seg in obj.segments: # segments
... print seg
2018-08-05 16:43:10 +07:00
...
2018-06-01 19:10:55 +07:00
<ELFSegment offset=0x0, flags=0x5, filesize=0x5f48, vaddr=0x400000, memsize=0x5f48>
<ELFSegment offset=0x6c30, flags=0x6, filesize=0x450, vaddr=0x606c30, memsize=0x5f0>
>>> for sec in obj.sections: # sections
... print sec
2018-08-05 16:43:10 +07:00
...
2018-06-01 19:10:55 +07:00
<Unnamed | offset 0x0, vaddr 0x400000, size 0x0>
<.interp | offset 0x238, vaddr 0x400238, size 0x1c>
<.note.ABI-tag | offset 0x254, vaddr 0x400254, size 0x20>
<.note.gnu.build-id | offset 0x274, vaddr 0x400274, size 0x24>
...etc
2017-12-09 09:35:26 +07:00
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
根据地址查找我们需要的东西:
2018-08-05 16:43:10 +07:00
2017-12-09 09:35:26 +07:00
```python
2018-06-01 19:10:55 +07:00
>>> proj.loader.find_object_containing(0x400000) # 包含指定地址的 object
<ELF Object true, maps [0x400000:0x60721f]>
>>> free = proj.loader.find_symbol('free') # 根据名字或地址在 project 中查找 symbol
>>> free
<Symbol "free" in libc.so.6 at 0x1083ab0>
>>> free.name # 符号名
u'free'
>>> free.owner_obj # 所属 object
<ELF Object libc-2.27.so, maps [0x1000000:0x13bb98f]>
>>> hex(free.rebased_addr) # 全局地址空间中的地址
'0x1083ab0'
>>> hex(free.linked_addr) # 相对于预链接基址的地址
'0x83ab0'
>>> hex(free.relative_addr) # 相对于对象基址的地址
'0x83ab0'
>>> free.is_export # 是否为导出符号
True
>>> free.is_import # 是否为导入符号
False
>>> obj.find_segment_containing(obj.entry) # 包含指定地址的 segment
<ELFSegment offset=0x0, flags=0x5, filesize=0x5f48, vaddr=0x400000, memsize=0x5f48>
>>> obj.find_section_containing(obj.entry) # 包含指定地址的 section
<.text | offset 0x12b0, vaddr 0x4012b0, size 0x33d9>
>>> main_free = obj.get_symbol('free') # 根据名字在当前 object 中查找 symbol
>>> main_free
<Symbol "free" in true (import)>
>>> main_free.is_export
False
>>> main_free.is_import
True
>>> main_free.resolvedby # 从哪个 object 获得解析
<Symbol "free" in libc.so.6 at 0x1083ab0>
>>> hex(obj.linked_base) # 预链接的基址
'0x0'
>>> hex(obj.mapped_base) # 实际映射的基址
'0x400000'
```
通过 `obj.relocs` 可以查看所有的重定位符号信息,或者通过 `obj.imports` 可以得到一个符号信息的字典:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> for imp in obj.imports:
... print imp, obj.imports[imp]
2018-08-05 16:43:10 +07:00
...
2018-06-01 19:10:55 +07:00
strncmp <cle.backends.elf.relocation.amd64.R_X86_64_GLOB_DAT object at 0x7faf8301b110>
lseek <cle.backends.elf.relocation.amd64.R_X86_64_GLOB_DAT object at 0x7faf8301b7d0>
malloc <cle.backends.elf.relocation.amd64.R_X86_64_GLOB_DAT object at 0x7faf8301be10>
>>> obj.imports['free'].symbol # 从重定向信息得到导入符号
<Symbol "free" in true (import)>
>>> obj.imports['free'].owner_obj # 从重定向信息得到所属的 object
<ELF Object true, maps [0x400000:0x60721f]>
```
这一部分还有个 hooking 机制,用于将共享库中的代码替换为其他的操作。使用函数 `proj.hook(addr, hook)``proj.hook_symbol(name, hook)` 来做到这一点,其中 `hook` 是一个 SimProcedure 的实例。通过 `.is_hooked`、`.unhook` 和 `.hooked_by` 来进行管理:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> stub_func = angr.SIM_PROCEDURES['stubs']['ReturnUnconstrained'] # 获得一个类
>>> stub_func
<class 'angr.procedures.stubs.ReturnUnconstrained.ReturnUnconstrained'>
>>> proj.hook(0x10000, stub_func()) # 使用类的一个实例来 hook
>>> proj.is_hooked(0x10000)
True
>>> proj.hooked_by(0x10000)
<SimProcedure ReturnUnconstrained>
>>> proj.hook_symbol('free', stub_func())
17316528
>>> proj.is_symbol_hooked('free')
True
>>> proj.is_hooked(17316528)
True
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
当然也可以利用装饰器编写自己的 hook 函数:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> @proj.hook(0x20000, length=5) # length 参数可选,表示程序执行完 hook 后跳过几个字节
... def my_hook(state):
... state.regs.rax = 1
2018-08-05 16:43:10 +07:00
...
2018-06-01 19:10:55 +07:00
>>> proj.is_hooked(0x20000)
True
2017-12-09 09:35:26 +07:00
```
2018-08-05 16:43:10 +07:00
### 求解器引擎
2018-06-01 19:10:55 +07:00
angr 是一个符号执行工具,它通过符号表达式来模拟程序的执行,将程序的输出表示成包含这些符号的逻辑或数学表达式,然后利用约束求解器进行求解。
从前面的内容中我们已经知道 bitvectors 是一个比特串,并且看到了 bitvectors 做的一些具体的数学运算。其实 bitvectors 不仅可以表示具体的数值,还可以表示虚拟的数值,即符号变量。
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> x = state.solver.BVS("x", 64)
>>> x
<BV64 x_0_64>
>>> y = state.solver.BVS("y", 64)
>>> y
<BV64 y_1_64>
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
而符号变量之间的运算同样不会时具体的数值,而是一个 AST所以我们接下来同样使用 bitvector 来指代 AST
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> x + 0x10
<BV64 x_0_64 + 0x10>
>>> (x + 0x10) / 2
<BV64 (x_0_64 + 0x10) / 0x2>
>>> x - y
<BV64 x_0_64 - y_1_64>
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
每个 AST 都有一个 `.op` 和一个 `.args` 属性:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> tree = (x + 1) / (y + 2)
>>> tree
<BV64 (x_0_64 + 0x1) / (y_1_64 + 0x2)>
>>> tree.op # op 是表示操作符的字符串
'__floordiv__'
>>> tree.args # args 是操作数
(<BV64 x_0_64 + 0x1>, <BV64 y_1_64 + 0x2>)
>>> tree.args[0].op
'__add__'
>>> tree.args[0].args
(<BV64 x_0_64>, <BV64 0x1>)
>>> tree.args[0].args[1].op
'BVV'
>>> tree.args[0].args[1].args
(1L, 64)
```
知道了符号变量的表示,接下来看符号约束:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> x == 1 # AST 比较会得到一个符号化的布尔值
<Bool x_0_64 == 0x1>
>>> x + y > 100
<Bool (x_0_64 + y_1_64) > 0x64>
>>> state.solver.BVV(1, 64) > 0 # 无符号数 1
<Bool True>
>>> state.solver.BVV(-1, 64) > 0 # 无符号数 0xffffffffffffffff
<Bool True>
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
正因为布尔值是符号化的,所以在需要做 if 或者 while 判断的时候,不要直接使用比较作为条件,而应该使用 `.is_true``.is_false` 来进行判断:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> yes = state.solver.BVV(1, 64) > 0
>>> yes
<Bool True>
>>> state.solver.is_true(yes)
True
>>> state.solver.is_false(yes)
False
>>> maybe = x == y
>>> maybe
<Bool x_0_64 == y_1_64>
>>> state.solver.is_true(maybe)
False
>>> state.solver.is_false(maybe)
False
```
为了进行符号求解,首先要将符号化布尔值作为符号变量有效值的断言加入到 state 中,作为限制条件,当然如果添加了无法满足的限制条件,将无法求解:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> state.solver.add(x > y) # 添加限制条件
[<Bool x_0_64 > y_1_64>]
>>> state.solver.add(y > 2)
[<Bool y_1_64 > 0x2>]
>>> state.solver.add(10 > x)
[<Bool x_0_64 < 0xa>]
>>> state.satisfiable() # 可以求解
True
>>> state.solver.eval(x + y) # eval 求解得到任意一个符合条件的值
15L
>> state.solver.eval_one(x + y) # 求解得到结果,如果有不止一个结果则抛出异常
>>> state.solver.eval_upto(x + y, 5) # 给出最多 5 个结果
[16L, 13L, 8L, 9L, 17L]
>>> state.solver.eval_atleast(x + y, 5) # 给出至少 5 个结果,否则抛出异常
[16L, 13L, 8L, 9L, 17L]
>>> state.solver.eval_exact(x + y, 5) # 有正好 5 个结果,否则抛出异常
>>> state.solver.min(x + y) # 给出最小的结果
7L
>>> state.solver.max(x + y) # 给出最大的结果
17L
>>> state.solver.eval(x + y, extra_constraints=[x + y < 10, x + y > 5]) # 额外添加临时限制条件
8L
>>> state.solver.eval(x + y, cast_to=str) # 指定输出格式
'\x00\x00\x00\x00\x00\x00\x00\x08'
>>> state.solver.add(x - y > 10) # 添加不可满足的限制条件
[<Bool (x_0_64 - y_1_64) > 0xa>]
>>> state.satisfiable() # 无法求解
False
```
angr 使用 z3 作为约束求解器,而 z3 支持 IEEE754 浮点数的理论,所以我们也可以使用浮点数。使用 `FPV``FPS` 即可创建浮点数值和浮点符号:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> state = proj.factory.entry_state() # 刷新状态
>>> a = state.solver.FPV(3.2, state.solver.fp.FSORT_DOUBLE) # 浮点数值
>>> a
<FP64 FPV(3.2, DOUBLE)>
>>> b = state.solver.FPS('b', state.solver.fp.FSORT_DOUBLE) # 浮点符号
>>> b
<FP64 FPS('FP_b_2_64', DOUBLE)>
>>> a + b
<FP64 fpAdd('RNE', FPV(3.2, DOUBLE), FPS('FP_b_2_64', DOUBLE))>
>>> a + 1.1
<FP64 FPV(4.300000000000001, DOUBLE)>
>>> a + 1.1 > 0
<Bool True>
>>> b + 1.1 > 0
<Bool fpGT(fpAdd('RNE', FPS('FP_b_2_64', DOUBLE), FPV(1.1, DOUBLE)), FPV(0.0, DOUBLE))>
>>> state.solver.add(b + 2 < 0)
[<Bool fpLT(fpAdd('RNE', FPS('FP_b_2_64', DOUBLE), FPV(2.0, DOUBLE)), FPV(0.0, DOUBLE))>]
>>> state.solver.add(b + 2 > -1)
[<Bool fpGT(fpAdd('RNE', FPS('FP_b_2_64', DOUBLE), FPV(2.0, DOUBLE)), FPV(-1.0, DOUBLE))>]
>>> state.solver.eval(b)
-2.4999999999999996
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
bitvectors 和浮点数的转换使用 `raw_to_bv``raw_to_fp`
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> a.raw_to_bv()
<BV64 0x400999999999999a>
>>> b.raw_to_bv()
<BV64 fpToIEEEBV(FPS('FP_b_2_64', DOUBLE))>
>>> state.solver.BVV(0, 64).raw_to_fp()
<FP64 FPV(0.0, DOUBLE)>
>>> state.solver.BVS('x', 64).raw_to_fp()
<FP64 fpToFP(x_3_64, DOUBLE)>
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
或者如果我们需要指定宽度的 bitvectors可以使用 `val_to_bv``val_to_fp`
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> a
<FP64 FPV(3.2, DOUBLE)>
>>> a.val_to_bv(12)
<BV12 0x3>
>>> a.val_to_bv(12).val_to_fp(state.solver.fp.FSORT_FLOAT)
<FP32 FPV(3.0, FLOAT)>
```
2018-08-05 16:43:10 +07:00
### 程序状态
2018-06-01 19:10:55 +07:00
`state.step()` 用于模拟执行的一个 basic block 并返回一个 SimSuccessors 类型的对象,由于符号执行可能产生多个 state所以该对象的 `.successors` 属性是一个列表,包含了所有可能的 state。
程序状态 state 是一个 SimState 类型的对象,`angr.factory.AngrObjectFactory` 类提供了创建 state 对象的方法:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
- `.blank_state()`:返回一个几乎没有初始化的 state 对象,当访问未初始化的数据时,将返回一个没有约束条件的符号值。
- `.entry_state()`:从主对象文件的入口点创建一个 state。
- `.full_init_state()`:与 entry_state() 类似,但执行不是从入口点开始,而是从一个特殊的 SimProcedure 开始,在执行到入口点之前调用必要的初始化函数。
- `.call_state()`:创建一个准备执行给定函数的 state。
下面对这些方法的参数做一些说明:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
- 所有方法都可以传入参数 `addr` 来指定开始地址
- 可以通过 `args` 传入参数列表,`env` 传入环境变量。类型可以是字符串,也可以是 bitvectors
- 通过传入一个符号 bitvector 作为 `argc`,可以将 `argc` 符号化
- 对于 `.call_state(addr, arg1, arg2, ...)``addr` 是希望调用的函数地址,`argN` 是传递给函数的 N 个参数,如果希望分配一个内存空间并传递指针,则需要使用 `angr.PointerWrapper()`;如果需要指定调用约定,可以传递一个 SimCC 对象作为 `cc` 参数
创建的 state 可以很方便地复制和合并:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> s = proj.factory.blank_state()
>>> s1 = s.copy() # 复制 state
>>> s2 = s.copy()
>>> s1.mem[0x1000].uint32_t = 0x41414141
>>> s2.mem[0x1000].uint32_t = 0x42424242
>>> (s_merged, m, anything_merged) = s1.merge(s2) # 合并将返回一个元组
>>> s_merged # 表示合并后的 state
<SimState @ 0x405000>
>>> m # 描述 state flag 的符号变量
[<Bool state_merge_1_14_16 == 0x0>, <Bool state_merge_1_14_16 == 0x1>]
>>> anything_merged # 描述是否全部合并的布尔值
True
>>> aaaa_or_bbbb = s_merged.mem[0x1000].uint32_t # 此时的值需要根据 state flag 来判断
>>> aaaa_or_bbbb
<uint32_t <BV32 Reverse((if (state_merge_1_14_16 == 0x1) then 0x42424242 else (if (state_merge_1_14_16 == 0x0) then 0x41414141 else 0x0)))> at 0x1000>
```
我们已经知道使用 `state.mem` 可以很方便的操作内存,但如果你想要对内存进行原始的操作时,可以使用 `state.memory``.load(addr, size)``.store(addr, val)`
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> s = proj.factory.blank_state()
>>> s.memory.store(0x4000, s.solver.BVV(0x0123456789abcdef, 128)) # 默认大端序
>>> s.memory.load(0x4008, 8) # 默认大端序
<BV64 0x123456789abcdef>
>>> s.memory.load(0x4008, 8, endness=angr.archinfo.Endness.LE) # 小端序
<BV64 0xefcdab8967452301>
>>> s.mem[0x4008].uint64_t.resolved # 与 mem 对比
<BV64 0xefcdab8967452301>
>>> s.memory.store(0x4000, s.solver.BVV(0x0123456789abcdef, 128), endness=angr.archinfo.Endness.LE) # 小端序
>>> s.memory.load(0x4000, 8) # 默认大端序
<BV64 0xefcdab8967452301>
>>> s.memory.load(0x4000, 8, endness=angr.archinfo.Endness.LE) # 小端序
<BV64 0x123456789abcdef>
>>> s.mem[0x4000].uint64_t.resolved # 与 mem 对比
<BV64 0x123456789abcdef>
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
可以看到默认情况下 store 和 load 都使用大端序的方式,但可以通过指定参数 `endness` 来使用小端序。
通过 `state.options` 可以对 angr 的行为做特定的优化。我们既可以在创建 state 时将 option 作为参数传递进去,也可以对已经存在的 state 进行修改。例如:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> s = proj.factory.blank_state(add_options={angr.options.LAZY_SOLVES}) # 启用 options
>>> s = proj.factory.blank_state(remove_options={angr.options.LAZY_SOLVES}) # 禁用 options
>>> s.options.add(angr.options.LAZY_SOLVES) # 启用 option
>>> s.options.remove(angr.options.LAZY_SOLVES) # 禁用 option
```
SimState 对象的所有内容(包括`memory`、`registers`、`mem`等)都是以插件的形式存储的,这样做的好处是将代码模块化,如果我们想要在 state 中存储其他的数据,那么直接实现一个插件就可以了。
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
- `state.globals`:实现了一个标准的 Python dict 的接口,通过它可以在一个 state 上存储任意的数据。
- `state.history`:存储了一个 state 在执行过程中的路径历史数据,它是一个链表,每个节点表示一个执行,通过像 `history.parent.parent` 这样的方式进行遍历。为了得到 history 中某个具体的值,可以使用迭代器 `history.NAME`,这样的值保存在 `history.recent_NAME`。如果想要快速得到这些值的一个列表,可以查看 `.hardcopy`
- `history.descriptions`:对 state 每次执行的描述的列表。
- `history.bbl_addrs`state 每次执行的 basic block 的地址的列表,每次执行可能多于一个地址,也可能是被 hook 的 SimProcedures 的地址。
- `history.jumpkinds`state 每次执行时改变控制流的操作的列表。
- `history.guards`state 执行中遇到的每个分支的条件的列表。
- `history.events`state 执行中遇到的可能有用的事件的列表。
- `history.actions`:通常是空的,但如果启用了 `options.refs`,则会记录程序执行时访问的所有内存、寄存器和临时变量。
- `state.callstack`:用于记录函数调用堆栈,它是一个链表,可以直接遍历 `state.callstack` 获得每个调用的 frame。
- `callstack.func_addr`:当前正在执行的函数的地址。
- `callstack.call_site_addr`:调用当前函数的 basic block 的地址。
- `callstack.stack_ptr`:从当前函数开头开始计算的堆栈指针的值。
- `callstack.ret_addr`:当前函数的返回地址。
2018-08-05 16:43:10 +07:00
### 模拟管理器
2018-06-01 19:10:55 +07:00
模拟管理器Simulation Managers是 angr 最重要的控制接口它允许同时对各组状态的符号执行进行控制同时应用搜索策略来探索程序的状态空间。states 会被整理到 stashes 里,从而进行各种操作。
我们用一个小程序来作例子,它有 3 种可能性,也就是 3 条路径:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
int num = 0;
scanf("%d", &num);
if (num > 50) {
if (num <= 100) {
printf("50 < num <= 100\n");
} else {
printf("100 < num\n");
exit(1);
}
} else {
printf("num <= 50\n");
}
}
// gcc example.c
```
模拟管理器最基本的功能是将一个 stash 里所有的 states 向前推进一个 basic block利用 `.step()` 来实现,而 `.run()` 方法可以直接执行到程序结束:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> proj = angr.Project('a.out', auto_load_libs=False)
>>> state = proj.factory.entry_state()
>>> simgr = proj.factory.simgr(state) # 创建 SimulationManager
>>> simgr
<SimulationManager with 1 active>
>>> simgr.active # active stash
[<SimState @ 0x400640>]
>>> while len(simgr.active) == 1: # 一直执行到 active stash 中有不止一个 state
... simgr.step()
2018-08-05 16:43:10 +07:00
...
2018-06-01 19:10:55 +07:00
<SimulationManager with 1 active>
...
<SimulationManager with 1 active>
<SimulationManager with 2 active>
>>> simgr.active # 有 2 个 active state
[<SimState @ 0x40078f>, <SimState @ 0x400763>]
>>> simgr.step() # 同时推进 2 个 state
<SimulationManager with 3 active>
>>> simgr.active # 得到 3 个 state
[<SimState @ 0x400600>, <SimState @ 0x40076b>, <SimState @ 0x400779>]
>>> simgr.run() # 一直执行到程序结束
<SimulationManager with 3 deadended>
>>> simgr.deadended # deadended stash
[<SimState @ 0x1000068>, <SimState @ 0x1000020>, <SimState @ 0x1000068>]
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
于是我们得到了 3 个 deadended 状态的 state。这一状态表示一个 state 一直执行到没有后继者了,那么就将它从 active stash 中移除,放到 deadended stash 中。
stash 默认的类型有下面几种,当然你也可以定义自己的 stash
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
- `active`:默认情况下存储可以执行的 state。
- `deadended`:当 state 无法继续执行时会被放到这里,包括没有更多的有效指令,没有可满足的后继状态,或者指令指针无效等。
- `pruned`:当启用 `LAZY_SOLVES` 时,除非绝对必要,否则是不会在执行中检查 state 的可满足性的。当某个 state 被发现是不可满足的,则 state 会被回溯上去,以确定最早是哪个 state 不可满足。然后这之后所有的 state 都会被放到 `pruned` stash 中。
- `unconstrained`:如果在 SimulationManager 创建时启用了 `save_unconstrained`,则那些没有约束条件的 state 会被放到 `unconstrained` stash 中。
- `unsat`:如果在 SimulationManager 创建时启用了 `save_unsat`,则那些被认为不可满足的 state 会被放到 `unsat` stash 中。
另外还有一个叫做 `errored` 的列表,它不是一个 stash。如果 state 在执行过程中发生错误,则该 state 会被包装在一个 ErrorRecord 对象中,该对象包含 state 和引发的错误,然后这个对象被插入到 `errored` 中。
可以使用 `.move()`,将 `filter_func` 筛选出来的 state 从 `from_stash` 移动到 `to_stash`
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> simgr.move(from_stash='deadended', to_stash='more_then_50', filter_func=lambda s: '100' in s.posix.dumps(1))
<SimulationManager with 1 deadended, 2 more_then_50>
```
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
每个 stash 都是一个列表,可以用列表的操作来遍历它,同时 angr 也提供了一些高级的方法,例如在 stash 名称前面加上 `one_`,表示该 stash 的第一个 state在名称前加上 `mp_`,将得到一个 [mulpyplexed](https://github.com/zardus/mulpyplexer) 版本的 stash
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
```python
>>> for s in simgr.deadended + simgr.more_then_50:
... print hex(s.addr)
2018-08-05 16:43:10 +07:00
...
2018-06-01 19:10:55 +07:00
0x1000068L
0x1000020L
0x1000068L
>>> simgr.one_more_then_50
<SimState @ 0x1000020>
>>> simgr.mp_more_then_50
MP([<SimState @ 0x1000020>, <SimState @ 0x1000068>])
>>> simgr.mp_more_then_50.posix.dumps(0)
MP(['-2424202024@', '+0000000060\x00'])
```
最后再介绍一下模拟管理器所使用的探索技术exploration techniques。默认策略是广度优先搜索但根据目标程序或者需要达到的目的不同我们可能需要使用不同的探索技术通过调用 `simgr.use_technique(tech)` 来实现,其中 tech 是一个 ExplorationTechnique 子类的实例。angr 内置的探索技术在 `angr.exploration_techniques` 下:
2018-08-05 16:43:10 +07:00
2018-06-01 19:10:55 +07:00
- `Explorer`:该技术实现了 `.explore()` 功能,允许在探索时查找或避免某些地址。
- `DFS`:深度优先搜索,每次只探索一条路径,其它路径会放到 `deferred` stash 中。直到当前路径探索结束,再从 `deferred` 中取出最长的一条继续探索。
- `LoopLimiter`:限制路径的循环次数,超出限制的路径将被放到 `discard` stash 中。
- `LengthLimiter`:限制路径的最大长度
- `ManualMergepoint`:将程序中的某个地址标记为合并点,将在一定时间范围内到达的所有 state 合并在一起。
- `Veritesting`:是[这篇论文](https://users.ece.cmu.edu/~aavgerin/papers/veritesting-icse-2014.pdf)的实现,试图识别出有用的合并点来解决路径爆炸问题。在创建 SimulationManager 时通过 `veritesting=True` 来开启。
- `Tracer`:记录在某个具体输入下的执行路径,结果是执行完最后一个 basic block 的 state存放在 `traced` stash 中。
- `Oppologist`:当遇到某个不支持的指令时,它将具体化该指令的所有输入并使用 unicorn engine 继续执行。
- `Threading`:将线程级并行添加到探索过程中。
- `Spiller`:当处于 active 的 state 过多时,将其中一些转存到磁盘上以保持较低的内存消耗。
2017-09-12 18:57:05 +07:00
2018-08-05 16:43:10 +07:00
### VEX IR 翻译器
2018-05-29 20:51:00 +07:00
angr 使用了 VEX 作为二进制分析的中间表示。VEX IR 是由 Valgrind 项目开发和使用的中间表示,后来这一部分被分离出去作为 libVEXlibVEX 用于将机器码转换成 VEX IR更多内容参考章节5.2.3)。在 angr 项目中,开发了模块 [PyVEX](https://github.com/angr/pyvex) 作为 libVEX 的 Python 包装。当然也对 libVEX 做了一些修改,使其更加适用于程序分析。
2017-12-02 22:38:19 +07:00
2018-05-29 20:51:00 +07:00
一些用法如下:
2018-08-05 16:43:10 +07:00
2018-05-29 20:51:00 +07:00
```python
>>> import pyvex, archinfo
>>> bb = pyvex.IRSB('\xc3', 0x400400, archinfo.ArchAMD64()) # 将一个位于 0x400400 的 AMD64 基本块(\xc3即ret转成 VEX
2018-06-01 19:10:55 +07:00
>>> bb.pp() # 打印 IRSBIntermediate Representation Super Block
2018-05-29 20:51:00 +07:00
IRSB {
t0:Ity_I64 t1:Ity_I64 t2:Ity_I64 t3:Ity_I64
2017-09-12 18:57:05 +07:00
2018-05-29 20:51:00 +07:00
00 | ------ IMark(0x400400, 1, 0) ------
01 | t0 = GET:I64(rsp)
02 | t1 = LDle:I64(t0)
03 | t2 = Add64(t0,0x0000000000000008)
04 | PUT(rsp) = t2
05 | t3 = Sub64(t2,0x0000000000000080)
06 | ====== AbiHint(0xt3, 128, t1) ======
NEXT: PUT(rip) = t1; Ijk_Ret
2017-12-02 22:38:19 +07:00
}
2017-09-12 18:57:05 +07:00
2018-05-29 20:51:00 +07:00
>>> bb.statements[3] # 表达式
<pyvex.stmt.WrTmp object at 0x7f38f1ef84b0>
>>> bb.statements[3].pp()
t2 = Add64(t0,0x0000000000000008)
2017-09-12 18:57:05 +07:00
2018-05-29 20:51:00 +07:00
>>> bb.statements[3].data # 数据
<pyvex.expr.Binop object at 0x7f38f1ef8460>
>>> bb.statements[3].data.pp()
Add64(t0,0x0000000000000008)
2017-09-12 18:57:05 +07:00
2018-05-29 20:51:00 +07:00
>>> bb.statements[3].data.op # 操作符
'Iop_Add64'
2017-09-12 18:57:05 +07:00
2018-05-29 20:51:00 +07:00
>>> bb.statements[3].data.args # 参数
[<pyvex.expr.RdTmp object at 0x7f38f1f77cb0>, <pyvex.expr.Const object at 0x7f38f1f77098>]
>>> bb.statements[3].data.args[0]
<pyvex.expr.RdTmp object at 0x7f38f1f77cb0>
>>> bb.statements[3].data.args[0].pp()
t0
2017-12-02 22:38:19 +07:00
2018-05-29 20:51:00 +07:00
>>> bb.next # 基本块末尾无条件跳转的目标
<pyvex.expr.RdTmp object at 0x7f38f3cb6f38>
>>> bb.next.pp()
t1
>>> bb.jumpkind # 无条件跳转的类型
'Ijk_Ret'
2017-12-02 22:38:19 +07:00
```
2018-06-17 16:46:53 +07:00
到这里 angr 的核心概念就介绍得差不多了,更多更详细的内容还是推荐查看官方教程和 API 文档。另外在我的博客里有 angr 源码分析的笔记。
2018-05-29 20:51:00 +07:00
2018-06-02 13:52:30 +07:00
## 扩展工具
2018-08-05 16:43:10 +07:00
2018-06-02 13:52:30 +07:00
由于 angr 强大的静态分析和符号执行能力,我们可以在 angr 之上开发其他的一些工:
2018-08-05 16:43:10 +07:00
2018-06-02 13:52:30 +07:00
- [angrop](https://github.com/salls/angrop)rop 链自动化生成器
- [Patcherex](https://github.com/shellphish/patcherex):二进制文件自动化 patch 引擎
- [Driller](https://github.com/shellphish/driller):用符号执行增强 AFL 的下一代 fuzzer
- [Rex](https://github.com/shellphish/rex):自动化漏洞利用引擎
2018-05-29 20:51:00 +07:00
## CTF 实例
2018-08-05 16:43:10 +07:00
查看章节 6.2.3、6.2.8。
2017-12-02 22:38:19 +07:00
## 参考资料
2018-08-05 16:43:10 +07:00
2017-12-04 15:13:39 +07:00
- [angr.io](http://angr.io/)
2017-12-02 22:38:19 +07:00
- [docs.angr.io](https://docs.angr.io/)
- [angr API documentation](http://angr.io/api-doc/)
- [The Art of War:Offensive Techniques in Binary Analysis](https://www.cs.ucsb.edu/~vigna/publications/2016_SP_angrSoK.pdf)